Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Introduction to Data Analysis

with Python and R


(CSEN 3135)
Module 1: Introduction to Python
Lecture 6: 01/11/2021

Dr. Debranjan Sarkar


Scope of names or variables
• Names within a function are local to it i.e. have local scope
• Example:
• def func(x,y,z):
• a=5
• v=x*y*z
• return(v)

• # Names inside a function are separate from names outside


• (a,p,q,r) = (15,2,3,4)
• val = func(p,q,r)
• print(‘a = ‘,a) => a = 15
Passing arguments by names
• # power(x,n) is a function to compute the value of x to the power n
• def power (x,n):
• ans = 1
• for i in range(0,n):
• ans = ans * x
• return(ans)

• print(power(2,10)) => prints 1024


• print(power(10,2)) => prints 100

• print(power(n = 10, x = 2)) # If we forget the sequence of arguments


Default arguments
• Python allows default values of arguments
def func(x, y=10):
…………..
• Here at the definition stage of the function ‘func’, it is stated that the
function has two arguments, out of which the default value for the second
argument is taken as 10.
• At the time of calling the function, if the second argument is not provided,
it is, by default, taken as 10.
• If the second value is provided, that value is taken as the value of the
second argument
• Default value must be provided at the time of function definition
Default arguments
• Default value has to be static and cannot be computed
• def sort(a, p = 0, l = len(a)): is not allowed as len(a) is not static

• The function int has actually two arguments with the second argument as
base of the number, with default value of 10
def int(s,b=10): # convert a string to a decimal number (by default)
………..
• int(“95”) gives the result 95 (as it is a valid decimal number
• int(“B5”) gives rise to error (as it is not a valid decimal number)
• int(“B5”,16) does not give any error as it takes B5 as a number with base 16
• int(“B5”,16) gives the hexadecimal equivalent of B5 = 181
Default arguments
• Default values must come at the end
• Default values are identified by the position
• Order of the arguments is important
• Example:
• def func(w,x,y,z): # okay, no default values
• def func(w=10,x,y,z): # wrong
• def func(w,x,y=10,z=18): # okay
• func(5,8) => w = 5, x = 8, y = 10, z = 18
• func(5,8,15) => w = 5, x = 8, y = 15, z = 18
• func(5,8,15,38) => w = 5, x = 8, y = 15, z = 38
• Not possible to use default value of y and given value of z by using 3
arguments
Defining a function
• def is used to define a function
• A function can be defined conditionally
if condition:
def func(x,y,z):
…………
else:
def func(x,y,z):
…………
• Function can also be redefined
• Function can be assigned a new name
def func1(x,y,z):
…………
func2 = func1
• Now func2 is another name for func1
• This is required to pass a function as argument of another function (just like a variable)
Function as an argument of another function
• Let us define a function ‘apply’ to apply, n number of times, a function
‘func’ with argument x
• def apply(func,x,n):
ret_val = x
for i in range(n):
ret_val = func(ret_val)
return(ret_val)

• def square(x):
return(x * x)

• apply(square, 9, 3) In ‘apply’, func = square; x = 9; n = 3


• result = square(square(square(9))) = (81 X 81) X (81 X 81) = 43046721
Recursion: Recursive function
• A function, which calls itself, is called a recursive function
• It is based on inductive definition
• Multiplication is basically repetitive addition
• mX1=m Base case
• m X n = m + m X (n-1) Inductive step
• As m X n = m + m + m + ….. + m (n times)
• Factorial
• 0! = 1 Base case
• n! = n * (n-1)! Inductive step
• As n! = n * (n-1) * (n-2) * … *3 *2 *1
• Fibonacci series (1,1,2,3,5,8,13,21,……)
• Fib(1) = Fib(2) = 1
• Fib(n) = Fib(n-1)+Fib(n-2)
Thank you

You might also like