Python Classroom Series – 03/Jun/2021


  • The first step to code reuse is function:
  • A function can take any number and type of input parameters and return any number or type of output results
  • Defining a function with def
    • To define a function we use def, the function name and parantheses enclosing input parameters
    • Naming rules of functions are snake case
    • Lets create a simple function which does nothing
    def do_nothing():
    • Refer Here for the sample code
    • Functions which returns some value

Arguments and Parameters

  • The values which we pass into the function when you call it are known as arguments
  • When you call a function with arguments, the values of those arguments are copied to their corresponding parameters inside the function
  • Refer Here for some more examples

Exercise: Write a program to print all the prime numbers from 100 to 1000

Understanding the arguments and parameters w.r.t memory

  • In python if the arguments passed are of mutable type changes made to the type will reflect without return statement, whereas if the argument is immutable then to pass the changes made to the variable use and explicit return statment
def increment(number):
    print(f"memory location of number is {id(number)}")
    number += 1
    print(f"memory location of number is {id(number)}")
    return number

def add_number(numbers, number):
    import copy
    copy_numbers = copy.deepcopy(numbers)
    print(f"id of numbers is {id(copy_numbers)}")
    print(f"Copy numbers are {copy_numbers}")
    print(f"id of numbers is {id(copy_numbers)}")

numbers = [1,2,3]
print(f"id of numbers is {id(numbers)}")
add_number(numbers, 100)
print(f"id of numbers is {id(numbers)}")

Leave a Comment

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

About learningthoughtsadmin