python functions continued
-
None: None is a special python value that holds a place when there is nothing to say i.e. None represents No Value
- This seems like a subtle distinction, but its important in Python to distinguish None to empty value (”, [], (,), {}, set() )
- This seems like a subtle distinction, but its important in Python to distinguish None to empty value (”, [], (,), {}, set() )
-
Positional Arguments: These are most familiar types of arguments whose values are copied to the corresponding parameter in order
def menu(starter, main_course, dessert):
print(f"""menu is
starter = {starter}
main course = {main_course}
Dessert = {dessert}""")
menu('spring roll','Biryani', 'gulab jamun')
# output
#menu is
# starter = spring roll
# main course = Biryani
# Dessert = gulab jamun
menu('Biryani', 'gulab jamun','spring roll')
# output
#menu is
# starter = Biryani
# main course = gulab jamun
# Dessert = spring roll
-
problem with positional arguments is that you need to remember the meaning of each position.
-
Keyword Arguments: To avoid positional argument confusion, you can specify the argument by the names of corresponding parameters
menu(dessert='Gulab Jamun', main_course='Biryani', starter='Spring roll' )
- Specify Default Parameter Values: We can specify the default values for parameters. The default is used if the user doesn’t provide a corresponding argument.
def menu(starter, main_course, dessert='Vanila'):
print(f"""menu is
starter = {starter}
main course = {main_course}
Dessert = {dessert}""")
menu(main_course='Biryani', starter='Spring Roll')
menu(dessert='Gulab Jamun', main_course='Biryani', starter='Spring roll' )
- Default parameter values are calculated when the function is defined, not when it is run. Python programmers should know the impact of mutable types as default value
def important_for_understanding(arg, result=[]):
result.append(arg)
print(result)
important_for_understanding(4)
important_for_understanding(5)
important_for_understanding('hello')
- If you want to fix this
def add_something(item, items=None):
if items is None:
items = []
items.append(item)
print(item)
- I want to implement an add function which adds all the numbers, user should be able to pass multiple values as arguments:
def add(numbers):
sum = 0
for number in numbers:
sum += number
return sum
print(add([1,2,3,4,5,6,7,8]))
- Explode/Gather Positional Arguments with *
def sum(*args):
'''
This function will calculate sum of arguments
'''
sum = 0
for arg in args:
sum += arg
print(sum)
return sum
sum(1)
sum(1,2,3,4,5,6,7,8)
- Explode/Gather KeyWord arguments with **
def print_args(*args):
print(f"Type is {type(args)} and Value is {args}")
for arg in args:
print(arg)
def print_kwargs(**kwargs):
print(f"Type is {type(kwargs)}")
print(kwargs)
for argument,value in kwargs:
print(f"{argument} ==> {value}")
print_args(1,2,3)
print_kwargs(name='Python', usage= 'Every where')
- When We are defining arguments then there is order
- Required Positional arguments
- Optional positional arguments
- Optional Keyword arguments
def print_data(data, start=10, end=100):
pass
def print_data_again(data,*args,**kwargs):
pass
print_data_again([1,2,3],start=0, end=1)
print_data(range(1,1000))
print_data_again([1,2,3],1,3)
Functions are First Class Citizens
- Functions are first-class citizens in python.
- We can assign them to variables, use them as arguments to other function and return them from functions.
- This gives us the capability to do some things in Python that are difficult to impossible in many other languages
def mul(*args):
result = 1
for arg in args:
result *= arg
return result
def add(*args):
result = 0
for arg in args:
result += arg
return result
while True:
number_1 = int(input('Enter the number1: '))
number_2 = int(input('Enter the number2: '))
choice = input('Do you want to add or multiply: [press a for add and m for multiply and q for quit]')
operation = None
if choice == 'a':
operation = add
elif choice == 'm':
operation = mul
else:
break
print(operation(number_1, number_2))
- Refer Here for the functions created in the class