- I learnt how to create/ define custom functions in Python, and how to retrieve the values from within the lists into the custom function using arguments in the function.
- We can set default values in custom functions, using = within the function.
We can create arguments within a custom function, and to assign them a certain value before letting them run inside a loop to achieve an output.
What I learnt:
1. Defining custom functions
- def function_name( ) with lists
Example 1: if condition met, print somethingdef calculator(operation, number1, number2): if operation=='sum': print(number1+number2) elif operation=='minus': print(number1-number2) elif operation=='times': print(number1*number2) else: print('Try again') operations=['sum', 'minus', 'times'] number1=10 number2=20 for operation in operations: calculator(operation, number1, number2)
- 1st line: I created/ defined a custom function called calculator( ) using def
- Last 2 lines: For the element (operation) in operations which is assigned to the list in line 10, going from left to right in the list one element by one element,
the program runs the code where this function is called, where calculator( ) is,
and retrieves the values assigned to the variables (operation, number1, number2) as indicated within the calculator( ) function - the values respectively consist of: the element in the list (operation) and the 2 integers 10 & 20
- Lines 2 to 8: The custom function then uses the values assigned to the variables indicated within the function to check if the criterias specified by if and elif are met. Depending on the fulfilment of the condition, different outputs are printed using the expressions / values stated inside the print( ) function.
Example 2: for number in list, print number times 5 for all numbers
list1=[3,6,4,7]
def multiply(list_of_numbers):
for number in list_of_numbers:
print(number*5)
multiply(list1)
- 1st line: I have a variable *list1* on the left side assigned to a list of numbers on the right.
- 2nd line: I created/ defined a custom function called multiply( )
- Last line: The multiply( ) function is called and the list assigned to the variable indicated within, *list1*, is assigned to variable *list_of_numbers* by the custom function in line 2
- 3rd line: for the element (named *number*) in the list *list_of_numbers* (from left to right element by element)
- 4th line: print the result of the expression within the print( ) function, that is the element in the list\*5.
Repeat for all the next elements in the list one by one.
Example 3: for number in list print number if even number
list1=[3,6,4,7]
def print_even(list_of_numbers):
for number in list_of_numbers:
if number%2==0:
print(number)
print_even(list1)
- 1st line: I have a variable *list1* on the left side assigned to a list of numbers on the right.
- 2nd line: I created/ defined a custom function called print_even( )
- Last line: The print_even( ) function is called and the list assigned to the variable indicated within, *list1*, is assigned to variable *list_of_numbers* by the custom function in line 2
- 3rd line: for the element (named *number*) in the list *list_of_numbers* (from left to right element by element)
- 4th line: if the result of the remainder of the element after dividing by 2 is 0,
- 5th line: print the value within the print( ) function, that is the element in the list (*number*).
Repeat for all the next elements in the list one by one.
Example 4: for name in list of names print hello, person’s name
list_of_names=['Soham', 'Jack', 'Michael', 'Maria']
def hello(namelist):
for name in namelist:
print("Hello,", name)
hello(list_of_names)
- def function_name( ) with lists and variables in loops
Example 1: sum all numbers in a listlist1=[3,6,4,7] def sum_all(numberlist): sum_of_numbers=0 for number in numberlist: sum_of_numbers=sum_of_numbers+number print(sum_of_numbers) sum_all(list1)
- 1st line: I have a variable list1 on the left side assigned to a list of numbers on the right.
- 2nd line: I created/ defined a custom function called sum_all( )
- Last line: The sum_all( ) function is called and the list assigned to the variable indicated within, list1, is assigned to variable numberlist by the custom function in line 2
- 4rd line: for the element (named number) in the list numberlist (from left to right element by element)
- 3rd & 5th line: as we want to get the sum of all numbers inside the list, in the 5th line, we assign a variable, sum_of_numbers, with a value of sum_of_numbers + number, & in the 3rd line, we first assign a variable, sum_of_numbers, with a value of 0
- This way, the first run of the line gives sum_of_numbers = number (the first number in list) + 0 , and the next run of the line due to the loop will add this sum to the next number in the list and so on.
- 6th line: print the value within the print( ) function, that is the final value of sum_of_numbers after the loops end.
Example 2: multiply all numbers in a list
list1=[3,6,4,7]
def multiply_all(numberlist):
multiple_of_numbers=1
for number in numberlist:
multiple_of_numbers=multiple_of_numbers*number
print(multiple_of_numbers)
return multiple_of_numbers
multiple=multiply_all(list1)
print(multiple)
- similar to above, but multiply instead of add for all iterations
- I used return to return the value from the custom function so that we can call the returned values elsewhere in the code
2. Default arguments in custom functions
- def function_name( ) with argument with default values set
def func1(name, country='Singapore'): print('I am in func1. My name is', name) print('I am from ', country) name='Beile' func1(name,country)
Output:
I am in func1. My name is Beile I am from Singapore
Thoughts
I’m glad that I learnt how to create and use custom functions as they look like something that I will be using frequently for programming!