Table of Contents
Function Definition and Function calling in Python
Write a python program to define a function to check that given character is vowel or not.
Write a python program to define a function to compute length of given string.
Write a python program to define a procedure histogram() that takes a list of integers and prints a histogram to the screen.
For example, histogram([4, 9, 7]) should print the following:
****
*********
*******
Program to check if character is vowel or not
Practical 2a(TYPE 1) : Write a python program to define a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise.
""" Author : ITVoyagers (https://itvoyagers.in) Date :08th August 2018 Description : Write a python program to define a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise. """ #TYPE 1 #function definition def is_vowel(char): if(char=='a' or char=='A' or char=='e' or char=='E' or char=='i' or char=='I' or char=='o' or char=='O' or char=='u' or char=='U'): print(char, "is a vowel") else: print(char, "is not a vowel") #function call char=input("enter a character") is_vowel(char)
OUTPUT
>>> enter a character p p is not a vowel >>> ================================ RESTART ================================ >>> enter a character A A is a vowel
Practical 2a(TYPE 2) : Write a python program to define a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise.
""" Author : ITVoyagers (https://itvoyagers.in) Date :08th August 2018 Description : Write a python program to define a function that takes a character (i.e. a string of length 1) and returns True if it is a vowel, False otherwise. """ #TYPE 2 #function definition def check_vowel(char): check="aeiou" #already characters will be in lower case so no need of "AEIOU" if char in check: print(char ,"is vowel") else: print(char ,"is not a vowel") #function call char=input("enter a character") check_vowel(char.lower()) #lower() converts character to lower case and passes it to function
OUTPUT
>>> enter a character K k is not a vowel >>> ================================ RESTART ================================ >>> enter a character i i is not a vowel >>>
Practical 2b : Write a python program to define a function that computes the length of a given list or string.
""" Author : ITVoyagers (https://itvoyagers.in) Date :08th August 2018 Description : Write a python program to define a function that computes the length of a given list or string. """ def length_of_string(any_string): count = 0 for i in any_string: count += 1 return count itv=input("enter string") t=length_of_string(itv) print(t)
OUTPUT
>>> enter string Itvoyagers 11
Practical 2c : Write a python program to define a procedure histogram() that takes a list of integers and
prints a histogram to the screen.
For example, histogram([4, 9, 7]) should print the following:
****
*********
*******
""" Author : ITVoyagers (https://itvoyagers.in) Date :08th August 2018 Description : Write a python program to define a procedure histogram() that takes a list of integers and prints a histogram to the screen. For example, histogram([4, 9, 7]) should print the following: **** ********* ******* """ def histogram(list1): for i in range(len(list1)): print (list1[i] * '*') #list = [4,9,7] #we can directly pass the list or we can take values as below histogram([4,9,7]) a = int(input("Enter 1st value for list")) b = int(input("Enter 2nd value for list")) c = int(input("Enter 3rd value for list")) L = [a,b,c] histogram(L)
OUTPUT
>>> **** ********* ******* Enter 1st value for list 10 Enter 2nd value for list 8 Enter 3rd value for list 6 ********** ******** ****** >>>
NOTE :
1. Variables passed in function definition are called as formal parameters or formal arguments and values passed in function call are called as actual arguments.
2.Functions defined with return statement are called as fruitful functions and functions defined without return statement are called as void functions,these functions does not remember the value once the use of variables is served
For other python practicals:
For other python basics related posts:
For other advanced python related posts: