Python - Functions

Functions are the block of statements that can be reuse as many times as required. In Python, Functions is defined using def keyword.

Example 1: Display message 'Hello World' using function.

def message():
    print("Hello World")

message()

Example 2: Write a Python function to add 2 numbers.

def add(x,y): #formal arguments
    z=x+y
    return z

s=add(4,5) #actual arguments
print("Sum:",s)

Example 3: Write a Python function to find Square and Cube of a Number.

def power(a):
    s=a*a
    c=a*a*a
    return s, c
s,c=power(5)
print("Square = ",s)
print("Cube = ",c)

Python actual arguments types:

  • Position argument: In position arguments the sequence of arguments must be in same order, in which they are called and declared. The above examples are the position argument.
  • Keyword argument: You can specify variable value using its name, In this case, position or order of variable does not matter.
  • Default argument: In default argument you can specify the default value of one or all arguments, if you omit the argument value then the default value(mentioned value) is assigned.
  • variable length: Any number of arguments can be passed to the function, it can be zero or more.

Example 4: Python function to define a keyword argument.

def person(name,age): #
    print(name)
    print(age)

person(age=40,name='python') #keyword argument

Example 5: Python function to define a default argument.

def person(name,age=40): #
    print(name)
    print(age)

person('python') #default

Example 6: Python function to define a variable length argument.

#def add(*b): #
def add(a, *b): #
    c=a
    for i in b:
        c=c+i
    print(c)

add(5,6,7,8) #variable length

Example 7: Python function to define a keyword variable length argument.

def person(name, **data): #keyword variable length arguments
    print(name)
    #print(data)
    for i,j in data.items():
        print(i,j)

person('username', age=20, city='Delhi', ph=12345)
#Global variable
a=10
def fun1():
    #a=15
    global a
    a=20
    print("fun1:",a)

def fun2():
    #x=globals()['a']
    globals()['a']=30
    print("fun2:",a)

def fun3():
    print("fun3:",a)

fun1()
fun2()
fun3()

Example 8: Python function to count number of odd and even numbers seperately.

#count no of odd and even
def count(mylist):
    even=odd=0
    for i in mylist:
        if i%2==0:
            even+=1
        else:
            odd+=1
    return even,odd

mylist = [20,30,33,44,55]
even, odd = count(mylist)
#print(even)
print("Even = {} and Odd = {}".format(even,odd))

Example 9: Function to calculate square of a number.

def square(a):
    return a*a

result = square(5)
print(result)

Lambda Fuction is defined without name, and it should have only 1 expression but can take multiple arguments

Example 10: Lambda function to calculate square of a number.

square=lambda a: a*a #Lambda function
result=square(5)
print(result)

Example 11: Filter function to calculate even numbers.

def is_even(n):
    return n%2==0

nums={2,3,16,7,9,12}
evens=list(filter(is_even,nums)) #filter returns the list (fun, list)
print(evens)

Example 12: Filter & lambda function to calculate even numbers.

nums={2,3,16,7,9,12}
evens=list(filter(lambda n: n%2==0,nums))
print(evens)

Example 13: Map function to find square of every number in a list.

#map - if you want to change value
nums=(2,3,16,7,9,12)
squares=list(map(lambda n:n*2,nums))
print(squares)

Example 14: Reduce function to find sum of all numbers in a list.

#reduce - if you want one value out of list
from functools import reduce
nums=(2,3,16,7,9,12)
sum=reduce(lambda a,b:a+b,nums)
print(sum)

Exercise: Python Functions