Lecture 2 2 Functions
Lecture 2 2 Functions
Sukrit Gupta
2 Formal definitions
3 Types of functions
5 Functions in Python
6 Variable Scope
Let go of all your preconceived notions about the topic. Follow me.
What we are trying to do? From the given image, understand how the
two cars look like. Let’s try to summarize the process:
Using visual aids (our eyes), we see the two images. (Input:
Images of vehicles.)
Our brain determines important differentiating factors for the two
vehicles. (Features = ?)
We assign certain value of the features to the two vehicles. For
example: if feature color = red, then vehicle = Verna.
Formal definitions
f (1) = 0.1 × 1 + 5
f (2) = 0.1 × 4 + 5
f (3) = 0.1 × 9 + 5
Types of functions
Functions in Python
def f(x):
return x**2
y = f(3)
z = max_(3, 4)
Variable Scope
def f(x):
y = 1
x = x + y
print ('x =', x) #
print ('y =', y) #
return x
x = 3
y = 2
z = f(x)
print ('z =', z)
print ('x =', x) #
print ('y =', y) #
def f(x):
def h():
z = x
print ('z =', z) #
def g():
x = 'abc'
print ('x =', x) #
x = x + 1
print ('x =', x) #
h()
g()
print ('x =', x) #
return x
x = 3
z = f(x)
print ('x =', x) #
print ('z =', z) #
2 Formal definitions
3 Types of functions
5 Functions in Python
6 Variable Scope