0% found this document useful (0 votes)
0 views13 pages

Functions

The document provides an overview of Python functions, including types such as global, local, lambda, and recursive functions, along with their syntax and examples. It discusses function arguments, including positional, keyword, default, and variable arguments, and demonstrates the use of lambda functions for concise expressions. Additionally, it includes examples of arithmetic operations and a section on potential viva questions related to Python.

Uploaded by

aakashps214
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views13 pages

Functions

The document provides an overview of Python functions, including types such as global, local, lambda, and recursive functions, along with their syntax and examples. It discusses function arguments, including positional, keyword, default, and variable arguments, and demonstrates the use of lambda functions for concise expressions. Additionally, it includes examples of arithmetic operations and a section on potential viva questions related to Python.

Uploaded by

aakashps214
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

PYTHON

FUNCTIONS
Functions
Function Arguments
Lambda Functions
• Package up and parameterize functionality
• Function Types:
 Global functions # accessible in same module (.py file)
 Local functions # defined inside other functions as helper
 lambda functions # expressions; created at the point of use
 Recursive functions

• Syntax:
def <function-name> ( parameters ):
function-suite comma-separated identifiers
return [expression]
#1
def sum_of_numbers(x,y):
'''This function finds the
sum of 2 numbers''' # function docString
return x+y

a = int(input('Enter first number Enter


: ')) first number : -90
b = int(input('Enter second numberEnter: '))second number :
sum = sum_of_numbers(a,b) 120
-90 + 120 = 30
print(a,' + ',b,' = ',sum)
Enter first number : 10
Enter second number : 20
10 + 20 = 30
import math Enter sides of a triangle :
formal parameters
345
a= 3
def area(a, b, c): b= 4
'''Computes area of a triangle''' c= 5
s = ( a+b+c )/2 Area = 6.0

return math.sqrt (s * (s-a) * (s-b) * (s-c))


return statement
None
• no return statement
a,b,c = input ('Enter sides of a triangle : ' ) • return with no arguments
print('a = ',a,'\nb = ',b,'\nc = ',c) • return <single-value>
• return <comma-separated
print('Area = ', area ( int(a), int(b), int(c) ) )
values>
Too few or too large no. of arguments; • return <collection/sequence
raises TypeError
object>
Types of actual arguments
def maxim ( l ):
return max(l)  Positional or Required
Arguments
l1 = [1,2,3,4,5] Positional  Keyword Arguments
l2 = [-10,-20,-30,-40,-50] Argument  Default Arguments
 Variable Arguments
result = maxim (l1) * maxim (l2)
print('Result 1 : ',result)

Result 1 : -50
result = abs(maxim(l2))
Result 2 : 10
print('Result 2 : ',result)
Result 3 : 0 1 2 3 4
print('Result 3 : ',end=' ')
for i in range( maxim(l1) ):
print(i, end=' ')
def shorten(text, length = 15, indicators = '...' ): # Default Arguments
if len(text) >= length:
text = text[:length - len(indicators)] + indicators
return text
Winnie
# Function calls ---
print(shorten('Winnie the Pooh', 10, '---')) # Positional Arguments Winn...
print(shorten(length = 7, text = 'Winnie the Pooh')) # Keyword Arguments
print(shorten('Winnie the Pooh', indicators='&', length = 10)) Winnie
# Positional and Keyword arguments th&
print(shorten('Winnie the Pooh')) # Positional Argument Winnie the
P...
# Variable no. of Positional Arguments

def listings ( a, b, *l ):
print('List : ', end = ' ')
for i in l:
print( i ,end = ',')
List : 123,1234,12345,

listings (1, 12, 123, 1234, 12345)

List : 3,4,5,
l1 = [1,2,3,4,5]
listings(*l1) #unpacking operator
# Variable no. of Keyword Arguments

def listings ( **l ):


print('List : ', end = ' ')
for i in l:
print( i ,end = ',')
List : a,b,c,d,e,
listings (a=1, b=12, c=123, d=1234, e = 12345)
import sys
diction = {1:'ONE', 2:'TWO', 3:'THREE', 4:'FOUR', 5:'FIVE'}

def print_digits(n):
for d in n:
print( diction[int(d)], end=' ' )

print('Command line arguments : ',sys.argv )

if len(sys.argv)==1 or sys.argv[1] in ('-h','--help'):


print(' Usage : {0} <number> '.format
Command (sys.argv[0])
line) arguments :
sys.exit(0) ['samplex.py', '432']
else: FOUR THREE TWO
print_digits(sys.argv[1])
Lambda Functions:
Syntax:
<function-name> = lambda <positional arg> : <expression>

<expression> - no branches
no loops 5 files processed
no return statement
(3) s = lambda x: ' 'if x==1 else 's'
Eg: (1) square = lambda x : x * x print('{0} file{1} processed'. format(5,s(5)))
print(square(2)) 4
(4) x = lambda a : a + a * 0.1
print(x(10))
(2) sum = lambda x,y : x + y 11.0
30
print(sum(10,20))
Use of lambda functions: (b) old = [2, 31, 42, 11, 6, 5, 23, 44]
new = list( filter( lambda x: x%2!=0, old))
(a) def myfn(n): print(new)
[31, 11, 5, 23]
return lambda a: a*n

double = myfn(2) (c ) old = [2, 31, 42, 11, 6, 5, 23, 44]


22
print(double(11)) new = list( map (lambda x: x+2, old) )
print(new)
triple = myfn(3)
print(triple(11)) 33 [4, 33, 44, 13, 8, 7, 25, 46]
def calculate(a,b): Number 1 : 2
''' Performs Arithmetic Operations Number 2 : 3
''' Sum = 5
sum = a+b Difference = -1
Product = 6
diff = a-b Division =
prod = a*b 0.6666666666666666
div = a/b
return sum,diff,prod,div

n1 = int(input('Number 1 : '))
n2 = int(input('Number 2 : '))
s,d,p,v = calculate(n1,n2)
print('Sum = ',s)
print('Difference = ',d)
print('Product = ',p)
print('Division = ',v)
Viva Questions – 1.12.2021
1. Predict the output:
‘-‘.join(‘12345’)
‘-‘.join([‘984’,’749’,’6143’])
2. What is slicing in Python?
3. Why do we need membership operator?
4. How to remove leading whitespaces from a string in Python?
5. What is negative index in Python?

You might also like