- Function are the block of code that perform the specific task
- redundant coding is bad type of coding
- to create a function
- function takes input and returns output
- input as parameter and returns output(parameters as variables)
- and calling function give the argument to the function (give input as argument which means values
- Function definition , parameters ,function call, argument
<aside>
👉🏻
def (name for the function)
</aside>
Program for the average of 3 numbers
# average of 3 numbers
def sume(var1,var2,var3):
result = (var1 + var2 + var3)/3
return result
print(sume(1,2,3))
Built in and user define function
- built in function are pre defined function like ( print(), len() , type(), range())
- user defined function are defined by the user
Default parameter
- in this case calling function give no argument to the function definition
- Because we assigned default value to the function definition this is know as default parameter
Practice program
1st program
- write a function to print the length of a string (list as a parameter)
cities = ["bellari", "ballari", "ballary", "bellary"]
phonenumbers = ["46484648", "118613128623154", "5444145226", "555452262211"]
def print_len(items):
print(len(items))
print_len(cities)
print_len(phonenumbers)