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

Functions

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

Functions

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

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
------

Error will come


def function( n1, n2=10, n3):
print("number 1 is: ", n1)
print("number 2 is: ", n2)
print("number 2 is: ", n3)

# Calling the function and passing only one argument


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

# Now giving two arguments to the function


print( "Passing two arguments" )
function(50,30,10)
-----
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