Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save techsharif/00e389fa3fbe5c37ed790efd286704c5 to your computer and use it in GitHub Desktop.
Save techsharif/00e389fa3fbe5c37ed790efd286704c5 to your computer and use it in GitHub Desktop.
python_training_and_exercise_on_function

Python Training and Exercise on Function

Installing

Python 3.6 (The best is to use virtuall environment)

Topics

  • function intro
  • Built in functions
  • Key Words
  • Basic Custom Function
  • Passing Arguments to a Function
  • Call A Function
  • Return value
  • Keyword Arguments [Call the function by passing the name of the arguments]
  • Default Arguments
  • args and kwargs
  • Passing Collections to a Function
  • Functions - "First Class Citizens"
  • Mapping Functions in a Dictionary
  • Documentation

A function is a block of code that is reusable.

  • print
  • type ( check, compare, cast )

Built in functions

Key Words

  • def
  • pass
  • return

Basic Custom Function

def function_name(Arguments):
	# code
	
  • An Empty Function
  • Use print

Passing Arguments to a Function

def add(a, b):
    print (a + b)

Call A Function

add(5,6)

Return value

def add(a, b):
   return a + b
   
sum = add(5,6)
print(sum)

# sum = add(5+6+7) [error]

Keyword Arguments [Call the function by passing the name of the arguments]

def add(a, b):
   return a + b
   
sum = add(a=5,b=6)
print(sum)

sum = add(b=5,a=6)
print(sum)

Default Arguments

def add(a, b, c=0, d=5):
   return a + b + c + d
   
sum = add(5,6)
print(sum)

sum = add(5,6,7)
print(sum)
   
sum = add(a=5,b=6)
print(sum)

sum = add(b=5,a=6)
print(sum)

sum = add(b=5,a=6, d=7)
print(sum)

*args and **kwargs

You can set up functions to accept infinite arguments using *args and infinite keyword arguments, using *kwargs.

def args_kwargs(*args, **kwargs):
	print(args)
	print(kwargs)

args_kwargs(1,2,3,4,name='sharif', url='techsharif.com')

Scope

def function_a():
	a = 1
	b = 2
	return a+b
	
def function_b():
	c = 3
	return a+c
	
print( function_a() )
print( function_b() )

Global

def function_a():
	global a
	a = 1
	b = 2
	return a+b
	
def function_b():
	c = 3
	return a+c
	
print( function_a() )
print( function_b() )

Passing Collections to a Function

Basic Collection Concept

a = [1, 2, 3]
b = [4, 5, 6]

# statement 01
c = a+b
print(c)

# statement 02
c = a
c += b
print(c)


This is safe

 def no_side_effects(numbers):
     print(numbers)
     numbers = numbers + [11,12]
     print(numbers)
	 
some_numbers = [1,2,3,4,5]
no_side_effects(some_numbers)
print(some_numbers)

This is also safe if you know this

 def with_side_effects(numbers):
     print(numbers)
     numbers += numbers + [11,12]
     print(numbers)
	 
some_numbers = [1,2,3,4,5]
with_side_effects(some_numbers)
print(some_numbers)

Functions - "First Class Citizens"

  • Passing Functions to a Function
def inner_call(x):
    return f'{x}'

def outer_call(inner_call,x):
    return inner_call(x)

print outer_call(inner_call,10)
  • Nested Function
def outer_call(text):
    def inner_call(t):
        return t.lower() + '...'
    return inner_call(text)

print (outer_call('Hello, World'))

Mapping Functions in a Dictionary

def add(a,b):
   retuen a+b
   
def sub(a,b):
   retuen a-b

function_dict = {'add':add, 'sub':sub}
print(add(5,6))
print(function_dict['sub'](5,6))

lambda

add = lambda x, y : x + y
print( add(3,4) )

Recursion

def fun():
	fun()

Documentation

def complex(real=0.0, imag=0.0):
    """Form a complex number.

    Keyword arguments:
    real -- the real part (default 0.0)
    imag -- the imaginary part (default 0.0)
	
	Return:
	Complex number based on real and imag
    """
    if imag == 0.0 and real == 0.0:
        return complex_zero

Exercise

  1. Take a input. If the type of the input value is either int or float, you should print the absolute value of the function input. Otherwise, print "Nope"
  • First, def a function called squere that takes an argument called number.
  • Make that function return the squere of that number (i.e. that number multiplied by itself).
  • Define a second function called by_two that takes an argument called number. if that number is divisible by 2 ,by_two should call squere(number) and return its result. Otherwise, by_two should return False. -Check if it works.
  1. Write a function named word_frequency to find number of occurrences of each word in a dictionary. word_frequency(['a', 'b', 'a']) = {'a': 2, 'b': 1}

Extra Link

Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment