Python Classroom Series – 04/Jun/2021

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() ) Preview
  • 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=[]):

  • If you want to fix this
def add_something(item, items=None):
    if items is None:
        items = []
  • 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

  • Explode/Gather Positional Arguments with *
def sum(*args):
    This function will calculate sum of arguments
    sum = 0
    for arg in args:
        sum += arg
    return sum

  • Explode/Gather KeyWord arguments with **
def print_args(*args):
    print(f"Type is {type(args)} and Value is {args}")
    for arg in args:

def print_kwargs(**kwargs):
    print(f"Type is {type(kwargs)}")
    for argument,value in kwargs:
        print(f"{argument}  ==> {value}")

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):

def print_data_again(data,*args,**kwargs):

print_data_again([1,2,3],start=0, end=1)



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
    print(operation(number_1, number_2))

  • Refer Here for the functions created in the class

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin