Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Functions

Functions in python
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Functions

Functions in python
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Functions

Syntax for function definition


def function_name ( ):
Block of statement
def welcome():
print("Hi")
print("welcome to function lecture")

x=input("enter your name")


print(x)
welcome()
y=input("enter your name")
print(y)
welcome()
def welcome(firstname, lastname):
print(f"Hi {firstname} {lastname}")
print("welcome to function lecture")

welcome('Ashmit', 'Acharya')
welcome('sahana', 'suresh')
Functions
def area():
l=int(input("enter the length"))
b=int(input("enter the breath"))
a=l*b
print(a)

x=int(input("enter the room number"))


print(x)
area()
y=int(input("enter the room number"))
print(y)
area()
def area(x,y):
a=x*y
print(a)

x=int(input("enter the room number"))


print(x)
area(30,40)
y=int(input("enter the room number"))
print(y)
area(60,40)
z=int(input("enter the room number"))
area(10,10)
Function with return type
def increment(number, by):
return(number+by)

x=increment(4,2)

print(x)
def increment(number, by):
return number+by

print(increment(4,2))
Function with return type
def add1(a,b):
return a+b

def sub1(a,b):
return(a-b)

def mul1(a,b):
return(a*b)

a=int(input("enter a value"))
b=int(input("enter b value"))
sum1= add1(a,b)
subtract =sub1(a,b)
multiplication = mul1(a,b)
print(sum1, subtract, multiplication)
def multiply(*number):
print (number)

multiply(3,4,5,7)

Output
(3, 4, 5, 7)
def multiply(*number):
result=1
for i in number:
result=result*i
print (result)

multiply(3,4,5,7)
Output
420
def student_details(**data):
print(data)

student_details(id=2, name='Rohan Rai',age='x', mobile_number=


'xxxx')
Output
{'id': 2, 'name': 'Rohan Rai', 'age': 'x', 'mobile_number': 'xxxx'}
def student_details(**data):
print(data["name"])

student_details(id=2, name='Rohan Rai',age='x', mobile_number=


'xxxx')

Output
Rohan Rai
Types of arguments
Function Arguments
The following are the types of arguments that we can use to call a
function:

Default arguments
Keyword arguments
Required arguments
Variable-length arguments
Default Arguments
def function( n1, n2 = 20 ):
print("number 1 is: ", n1)
print("number 2 is: ", n2)

# Calling the function and passing only one argument


print( "Passing only one argument" )
function(30)

# Now giving two arguments to the function


print( "Passing two arguments" )
function(50,30)
Output
Passing only one argument
number 1 is: 30
number 2 is: 20
Passing two arguments
number 1 is: 50
number 2 is: 30
def function( n1, n2=10, n3):
print("number 1 is: ", n1)
print("number 2 is: ", n2)
print("number 2 is: ", n3)

print( "Passing only one argument" )


function(30,20)
Keyword Arguments

def function( n1, n2 ):


print("number 1 is: ", n1)
print("number 2 is: ", n2)

# Calling function and passing arguments without using keyword


print( "Without using keyword" )
function( 50, 30)

# Calling function and passing arguments using keyword


print( "With using keyword" )
function( n2 = 50, n1 = 30)
Output
Without using keyword
number 1 is: 50
number 2 is: 30
With using keyword
number 1 is: 30
number 2 is: 50
-----
def square( item_list ):
squares = [ ]
for l in item_list:
squares.append( l**2 )
return squares

# calling the defined function


my_list = [17, 52, 8];
my_result = square( my_list )
print( "Squares of the list are: ", my_result )
Output
Squares of the list are: [289, 2704, 64]

You might also like