25
loading...
This website collects cookies to deliver better user experience
def functionName( arguments):
block of statements to be executed
return value
def service(studentName):
pass # Remember to implement this!
# Here x and y are the function parameters
def add(x,y):
return x+y
# Here 8 and 3 are arguments
add(8,3)
# Function definition is here
def displayName (name):
"This prints a passed string into this function"
print (name)
return
# To call the displayName function
displayName ("Marty")
displayName ("Stephen")
Marty
Stephen
def displayName (name):
"This prints a passed string into this function"
print (name)
return
# To call the displayName function
displayName ()
def displayName (name):
print (name)
return
displayName('Annabelle') # Gives an error
Annabelle
def roomArea(width, length):
return length * width
# Now you can call roomArea function
Print(roomArea(length = 50, width = 40))
# Function definition is here
def displayInfo(city,address = 40400):
"This prints a passed info into this function"
print ("city: ", city)
print ("Address: ", address)
return
# To call the displayName function
displayInfo('Nairobi', address = 1020)
displayInfo('Paris' )
city: Nairobi
Address: 1020
city: Paris
Address: 40400
def functionname(formal_argument, *variable_tuple ):
"Code statements"
return [expression]
def matchScores(roundNo, *score):
print('Current Round: ', roundNo)
for i in score:
print('Score: ',i)
return
matchScores(5)
matchScores(9,20,40,30)
Current Round: 5
Current Round: 9
Score: 20
Score: 40
Score: 30
def
and return
keywords are not used when declaring this type of function. Small functions may be implicitly created using lambda
keyword. Lambda
forms takes any no of arguments but only returns one type of value and cannot contain multiple expressions or commands. It requires an expression and parameters just like any other function. A lambda function may shorten the normal function to a single line expression as shown:
# To add two variable values
def add(x, y)
return x + y
# Using lambda function
lambda x, y : x + y
# To double the value of a variable
def double(x):
return x * 2
# Using lambda function
lambda x, y : 2 * x
lambda
can also be used with conditional statementsminimum = lambda x, y : x if x < y else y
print(minimum(3,9))
map
function applies a given function to each element of a sequence and returns a modified list.map(function, input_list)
def square(list1):
myList = []
for i in list1:
myList.append(i**2)
return myList
print(square([2,3,4,5,6]))
[4, 9, 16, 25, 36]
myList = [2,3,4,5,6]
print(list(map(lambda x : x**2, myList)))
filter(function, input_list)
def myFunc(list1):
myList = [x for x in list1 if x > 2]
return myList
print(myFunc([0,2,5,3,7,9]))
[5, 3, 7, 9]
myList = [0,2,5,3,7,9]
print( list( filter( lambda x: x > 2, myList)))
def stringFunction():
return "Hello World!"
def worldSplit(stringFunction):
func = stringFunction()
return func.split()
print(worldSplit(stringFunction))
['Hello', 'World!']
worldSplit(stringFunction):
takes in stringFunction():
as an argument and returns a list composed of stringed words from the argument function.def print_function():
def helloWorld():
return "Hello World"
return helloWorld()
output = print_function()
print(output)
Hello World
output = print_function()