Python 3.6 (The best is to use virtuall environment)
- 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
- type ( check, compare, cast )
- def
- pass
- return
def function_name(Arguments):
# code
- An Empty Function
- Use print
def add(a, b):
print (a + b)
add(5,6)
def add(a, b):
return a + b
sum = add(5,6)
print(sum)
# sum = add(5+6+7) [error]
def add(a, b):
return a + b
sum = add(a=5,b=6)
print(sum)
sum = add(b=5,a=6)
print(sum)
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)
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')
def function_a():
a = 1
b = 2
return a+b
def function_b():
c = 3
return a+c
print( function_a() )
print( function_b() )
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() )
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)
- 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'))
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))
add = lambda x, y : x + y
print( add(3,4) )
def fun():
fun()
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
- 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.
- 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}
- Sharif Chowdhury - techsharif