How to write Python Functions
Functions
Functions are useful for breaking up a large program to make it easier to read and maintain. (Decrease Complexity)
They are also useful if find ourselves writing the same code at several different points in our program(Reusability of the code).
We can put that code in a function and call the function whenever we want to execute that code. (Remove Redundancy)
There are two types of functions Built in Functions and User defined functions. Functions those are provided by the language are called Built in Functions.
- Functions help us to develop our own library. (User defined functions)
- Functions are defined in Python with def and end with colon.
- The code of the function are indented below def.
Some examples of User Defined Functions.
Example 1:
Python function to print a box
#define square def my_square(): print('|'*25) print('|',' '*21,'|') print('|',' '*21,'|') print('|',' '*21,'|') print('|'*25) #call function my_square()
Example 2:
Python function to print a box where width of the box is passed as parameter.
#define square def my_square(n): print('|'*n) print('|',' '*(n-4),'|') print('|',' '*(n-4),'|') print('|',' '*(n-4),'|') print('|'*n) #call function by giving the value to argument my_square(35) my_square(55)
Example 3:
Python Function returns area of the circle where radius is passed as parameter .
#define a function to calculate area of circle #radious is passed into function by a parameter def circle_area(r): p=22/7 return p*r*r #the function circle_area() is called with print print(circle_area(7)) print(circle_area(14))
Example 4:
Function with default arguments. Default arguments need to come at the end of the function definition, after all of the non-default arguments.
def multiple_print(string, n=1): print(string * n) print() #call function with replace the default value 1 with 5 multiple_print('Hello', 5) #call function with default value 1 multiple_print('Hello')
Example 5:
Function to convert the Celcious to Fahrenheit. Celcious is passed as parameter and the function return the Fahrenheit value.
#define function in Python def ctof(c): f=(c*9)/5+32; return f #calling function by giving Celcious value which is input by user cc=int(input('Enter Celcious value ?')) print("Celcious " ,cc," =Fahrenheite",ctof(cc))
Example 6:
Function with list as parameter
def mylist(list): return list l=[] for i in range(0,10): l.append(i) print(mylist(l))
Example 7:
Function to count each characters of a given string where string value is passed as parameter.
def char_search(n): string = n string=string.upper() for c in ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'): char = c val = string.count(char) print(c,'Total = ',val) yourname=input('Enter you Name?') char_search(yourname)
Example 8:
Function to find whether a given number is odd number.
def find_odd(x): if x%2==1: return True else: return False #call the function print (find_odd(5)) print (find_odd(6))
Example 9:
Function to return a random number between 1 and 10.
#Python function to find a random number between 1 and 10. def find_rand(): from random import randint x = randint(1,10) return x #call the function for i in range(10): print(find_rand())
Example 10:
Function to return numbers of vowels in a string where string is passed as parameter.
def count_vowels(s): a=s.count('a') e=s.count('e') i=s.count('i') o=s.count('o') u=s.count('u') return a+e+i+o+u print (count_vowels('orange'))
It is very useful for me to learn and understand easily.
ReplyDeletePython Online Training
Python Online Training Hyderabad