STUDY MATERIAL –PYTHON
Lesson [6-9]
Hands on Practice
12
Prepared By
Mr.M.Dhanapal,MCA.,B.Ed.,
PG-Assist In Computer Science
Literacy Mission MHSS, Samalapuram
12-Hands on Practice Python lesson 6-9
Lesson 6
1. Write a program to check whether the given character is a vowel or not.
ch=input("enter the character:")
if ch in ('a','e','i','o','u','A','E','I','O','U'):
print("Given character is Vowel")
else:
print("Given Character is not Vowel")
Output:
enter the character:c
Given Character is not Vowel
>>>
Output:
enter the character:a
Given character is Vowel
>>>
2. Using if..else..elif statement check smallest of three numbers.
a=input("enter the Number 1:")
b=input("enter the Number 2:")
c=input("enter the Number 3:")
if a<b and a<c :
print("The given number {} is smallest".format(a))
elif b<a and b<c:
print("The given number {} is smallest".format(b))
else:
print("The given number {} is smallest".format(c))
Output:
enter the Number 1:5
enter the Number 2:4
enter the Number 3:2
The given number 2 is smallest
>>>
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 1
12-Hands on Practice Python lesson 6-9
3. Write a program to check if a number is Positive, Negative or zero.
a=int(input("enter the Number 1:"))
if a>0 :
print("The given number {} is Positive".format(a))
elif a<0:
print("The given number {} is Negative".format(a))
else:
print("The given number {} is Zero".format(a))
Output:
enter the Number 1:4
The given number 4 is Positive
>>>
4. Write a program to display Fibonacci series 0 1 1 2 3 4 5…… (upto n terms)
a,b=0,1
n=int(input("Enter the number of terms to display fibbonaci:"))
print(a,end='\n')
print(b,end='\n')
for i in range(2,n,1):
c=a+b
print(c,end='\n')
a=b
b=c
Output:
Enter the number of terms to display fibbonaci:5
0
1
1
2
3
>>>
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 2
12-Hands on Practice Python lesson 6-9
5. Write a program to display sum of natural numbers, up to n.
n=int(input("Enter the number of terms to find sun:"))
sum=0
for i in range(1,n+1):
sum+=i
print("The sum of given number {} is {}".format(n,sum))
Output:
Enter the number of terms to find sun:10
The sum of given number 10 is 55
>>>
6. Write a program to check if the given number is a palindrome or not.
n=int(input("enter a number: "))
temp=n
r=0
while temp!=0:
r=(r*10)+(temp%10)
temp=temp//10
if n==r:
print("number is palindrome")
else:
print("number is not palindrome")
Output:
enter a number: 424
number is palindrome
>>>
7. Write a program to print the following pattern
*****
****
***
**
*
for i in range(6,0,-1):
j=1
while(j<i):
print("*",end=' ')
j+=1
print('\n')
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 3
12-Hands on Practice Python lesson 6-9
Output:
*****
****
***
**
*
8. . Write a program to check if the year is leap year or not.
y=int(input("Enter a year : "))
n=y%4
if (y==0):
print("The year {} is a Leap Year".format(y))
else:
print("The year {} is not a Leap Year".format(y))
Output:
Enter a year : 2001
The year 2001 is not a Leap Year
>>>
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 4
12-Hands on Practice Python lesson 6-9
Lesson 7
1. Try the following code in the program
def printinfo( name, salary = 3500):
print (“Name: “, name)
print (“Salary: “, salary)
return
printinfo(“Mani”)
Slno code Result
1 printinfo(“3500”) Name:3500
Salary:3500
2 printinfo(“3500”,”Sri”) Name:3500
Salary:Sri
3 printinfo(name=”balu”) Name:balu
Salary:3500
4 printinfo(“Jose”,1234) Name:jose
Salary:1234
5 printinfo(“ ”,salary=1234) Name:
Salary:1234
2. Evaluate the following functions and write the output
Slno Function Output
1 eval(‘25*2-5*4') 30
2 math.sqrt(abs(-81)) 9.0
3 math.ceil(3.5+4.6) 9
4 math.floor(3.5+4.6) 8
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 5
12-Hands on Practice Python lesson 6-9
3. Evaluate the following functions and write the output
Slno function Output
1 1) abs(-25+12.0)) 13.0
2) abs(-3.2) 3.2
2 1) ord('2') 50
2) ord('$') 36
3 type('s') <class ‘str’>
4 bin(16) 0b10000
5 1) chr(13) ‘\r’
2) print(chr(13))
6 1) round(18.2,1) 18.2
2) round(18.2,0) 18.0
3) round(0.5100,3) 0.51
4) round(0.5120,3) 0.512
7 1) format(66, 'c') ‘B’
2) format(10, 'x') ‘a’
3) format(10, 'X') ‘A’
4) format(0b110, 'd') ‘6’
5) format(0xa, 'd') ‘10’
8 1) pow(2,-3) 0.125
2) pow(2,3.0) 8.0
3) pow(2,0) 1
4) pow((1+2),2) 9
5) pow(-3,2) 9
6) pow(2*2,2) 16
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 6
12-Hands on Practice Python lesson 6-9
Lesson 8
1. Write a python program to find the length of a string.
a=input("Enter the string:")
x=len(a)
print("The Length of the given string :",x)
Output:
Enter the string:welcome to literacy
The Length of the given string : 19
>>>
2.Write a program to count the occurrence of each word in a given
a=input("Enter the string:")
#b=input("enter the word to count in the string:")
for i in a:
print("the character is ",i,"is ",a.count(i)," times")
Output:
Enter the string:welcome
the character is w is 1 times
the character is e is 2 times
the character is l is 1 times
the character is c is 1 times
the character is o is 1 times
the character is m is 1 times
the character is e is 2 times
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 7
12-Hands on Practice Python lesson 6-9
3.Write a program to add a prefix text to all the line in a string.
b="python is "
a=input("Enter the string:")
b+=a
print(b)
Output:
Enter the string:programming language
python is programming language
>>>
4. Write a program to print integers with ‘*’ on the right of specified width.
y=int(input("enter the number: "))
z=int(input("how many * u need right side-specify the width: "))
x='*'
print(y,x*z)
Output:
enter the number: 123
how many * u need right side-specify the width: 2
123 **
>>>
5. Write a program to create a mirror of the a mirror of the given string
str1 = input("Enter a string: ")
c=""
index = -1
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 8
12-Hands on Practice Python lesson 6-9
for i in str1:
c += str1[index]
index-=1
print("The mirror of the word is : ",c)
OUTPUT:
Enter a string: Well
The mirror of the word is : lleW
>>>
6. Write a to remove all the occurrences of a given character in a string.
str2=input("Enter a string: ")
str1=input("Enter a charactoe to remove: ")
f=""
for i in str2:
if(str1==i):
pass
else:
f+=i
print("The string without {} is {} ".format(str1,f))
OUTPUT:
Enter a string: ate
Enter a charactoe to remove: e
The string without e is at
>>>
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 9
12-Hands on Practice Python lesson 6-9
7. Write a program to append a string to another string without using += operator.
a=input("Enter the string1 :")
b=input("Enter the string2 :")
c=a+b
print("the combined string is :", c)
Output:
Enter the string1 :welcome
Enter the string2 :to python
the combined string is : welcome to python
8.Write a program to Swap a string.
str1 = input("Enter a stering 1")
str2 = input("Enter a stering 2")
cha = str1
str1 = str2
str2=cha
print("The swap of String 1 is: ",str1)
print("The swap of String 2 is: ",str2)
OUTPUT:
Enter a stering 1Literacy
Enter a stering 2School
The swap of String 1 is: School
The swap of String 2 is: Literacy
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 10
12-Hands on Practice Python lesson 6-9
9.Write a program to replace a string with another string without using replace().
s= input("Enter a string: ")
print("String before replaced: ",s)
s= input("Enter a string to replace: ")
print("String after replacement: ",s)
OUTPUT:
Enter a string: How are You ?
String before replaced: How are You ?
Enter a string to replace: Iam Fine.
String after replacement: Iam Fine.
10. Write a program to count the number of characters, words and lines in a given string
s=input("Enter string: ")
char=0
word=1
for i in s:
char=char+1
if(i==' '):
word=word+1
print("Number of words in the string: ",word)
print("Number of characters in the string: ",char)
Output:
Enter string: literacy mission
Number of words in the string: 2
Number of characters in the string: 16
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 11
12-Hands on Practice Python lesson 6-9
Lesson 9
1. Write a program to remove duplicates from a list.
def Remove(d):
final_list = []
for num in d:
if num not in final_list:
final_list.append(num)
return final_list
d = [2, 4, 10, 20, 5, 2, 20, 4]
print("Original List: ",d)
print("After removing duplicate in list ",Remove(d))
Output:
Original List: [2, 4, 10, 20, 5, 2, 20, 4]
After removing duplicate in list [2, 4, 10, 20, 5]
>>>
2. Write a program that prints the maximum value in a Tuple.
tup=(56,34,58,12,35,98)
print("The maximum value in tupil: ",max(tup))
Output:
The maximum value in tupil: 98
>>>
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 12
12-Hands on Practice Python lesson 6-9
3. Write a program that finds the sum of all the numbers in a Tuples using while loop.
tup=(1,6,5,3,4,9)
i=0
sum=0
a=len(tup)
while i<a :
sum=sum+tup[i]
i+=1
print(" the sum of the tupil :",sum)
Output:
the sum of the tupil : 28
4. Write a program that finds sum of all even numbers in a list.
tup=(1,6,5,3,4,9)
i=0
sum=0
a=len(tup)
while i<a :
if tup[i]%2==0:
sum=sum+tup[i]
i+=1
print(" the sum of the even number in this tupil :",sum)
Output:
the sum of the even number in this tupil :10
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 13
12-Hands on Practice Python lesson 6-9
5. Write programs that reverse a list using a loop.
def rev(a):
b=[]
i=-1
while i>=-5:
b.append(a[i])
i=i+-1
print("after reverse: ",b)
a=[1,2,3,4,5]
print("Before reverse: ",a)
rev(a)
Output:
Before reverse: [1, 2, 3, 4, 5]
after reverse: [5, 4, 3, 2, 1]
>>>
6. Write a program to insert a value in a list at the specified location.
a=[1,2,3,4,5]
print(a)
b=int(input("enter the specified location to insert "))
c=int(input("enter the value to insert: "))
a.insert(b,c)
print("The updated list :",a)
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 14
12-Hands on Practice Python lesson 6-9
Output:
[1, 2, 3, 4, 5]
enter the specified location to insert 3
enter the value to insert: 6
The updated list : [1, 2, 3, 6, 4, 5]
>>>
7. Write a program that creates a list of numbers from 1 to 50 that are either divisible
by 3 or divisible by 6.
a=[]
for i in range(1,51):
if i%3==0 or i%6==0:
a.append(i)
print("These are the number divisible by 3 or 6 \n",a)
Output:
These are the number divisible by 3 or 6
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]
>>>
8. Write a program to create a list of numbers in the range 1 to 20. Then delete all the
numbers from the list that are divisible by 3.
a=[]
for i in range(1,21):
a.append(i)
print(a)
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 15
12-Hands on Practice Python lesson 6-9
for i in a:
if i%3==0:
a.remove(i)
print("These are the number not divisible by 3 \n",a)
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
These are the number not divisible by 3
[1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20]
>>>
9. Write a program that counts the number of times a value appears in the list. Use a
loop to do the same.
list=[4,6,8,12]
for i in list:
print(" The number",i," is ",list.count(i)," times")
Output:
The number 4 is 1 times
The number 6 is 1 times
The number 8 is 1 times
The number 12 is 1 times
>>>
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 16
12-Hands on Practice Python lesson 6-9
10.Write a program that prints the maximum and minimum value in a dictionary.
a={1:45,2:58,3:66,4:12,5:4}
print(a)
b=max(a, key = lambda x: a.get(x) )
c=min(a, key = lambda x: a.get(x) )
print("the maximum in the given dictionary ",a[b])
print("the minimum in the given dictionary ",a[c])
Output:
{1: 45, 2: 58, 3: 66, 4: 12, 5: 4}
the maximum in the given dictionary 66
the minimum in the given dictionary 4
>>>
“All the Best”
Prepared By: Mr.M.Dhanapal. MCA.,B.Ed., Literacy Mission MHSS,Samalapuram Page 17