Python Programming Manual Printout Copy
Python Programming Manual Printout Copy
Python Programming Manual Printout Copy
import math
print("Armstrong number between 100 to 2000:")
for num in range(100, 2000):
order = len(str(num))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
if num == sum:
print(num)
Output:
Armstrong number between 100 to 2000:
153
370
371
407
1634
char_count=0
vow_count=0
space_count=0
for i in str:
if i in vow:
vow_count=vow_count+1
if i in char:
char_count=char_count+1
elif i==' ':
space_count=space_count+1
print(vow_count)
print(char_count)
print(space_count)
Output:
enter the string: i am a superhero
7
13
3
Ex 4(a) Function to display numbers divided by 3 and not divided by 5 in a range
def display_numbers():
min=int(input(“Enter the min value:”))
max=int(input(“Enter the max value:”))
print(“Numbers divided by 3 and not divisible by 5 between min and max are:”)
for i in range(min,max):
if (i % 3==0) and (i%5!=0):
print(i)
display_numbers()
Output:
Enter the min value: 1
Enter the max value: 20
Numbers divided by 3 and not divisible by 5 between min and max are:
3
6
9
12
18
Ex 4(b) Fibonacci Series
def fib(n):
if n<=1:
return n
else:
return(fib(n-1)+fib(n-2))
nterms=int(input("Enter the number of terms you want in Fibonacci series:"))
4
for i in range(nterms):
print(fib(i))
Output:
Enter the number of terms you want in Fibonacci series: 5
0
1
1
2
3
Ex 5 String Suffix
string = input("Enter the word to add ing/ly:")
if len(string) < 3:
print(string)
elif string[-3:] == 'ing':
print(string + 'ly')
else:
print(string + 'ing')
Output:
Enter the word to add ing/ly: working
Workingly
Enter the word to add ing/ly: do
do
Enter the word to add ing/ly: work
Working
Enter Element:73
minimum value: 32
maximum value: 73
Ex 7 Reverse of a list
n = int(input("Enter the size of list:"))
print("Enter list elements one by one")
L=[ ]
for i in range(n):
x=int(input(“Enter Element:”))
L.append(x)
print("The list is")
print(L)
print("Reserve of list :")
L.reverse()
print(L)
Output:
Enter the size of list:5 Enter list elements one by one Enter Element:2
Enter Element:3 Enter Element:5 Enter Element:4 Enter Element:1 The list is
[2, 3, 5, 4, 1]
Reserve of list :
[1, 4, 5, 3, 2]
T=(1,2,3,4,5,6,7,8,9,10)
print("tuple is")
tuple_length=len(T)
print(T[ :tuple_length //2])
print(T[tuple_length //2: ])
Output:
tuple is
(1, 2, 3, 4, 5)
(6, 7, 8, 9, 10)
Output:
Enter the numbers of words:5
Enter word:H
Enter word:HELLO
6
Enter word:HI
Enter word:HOW ARE YOU
Enter word:THANK YOU
The longest Word is HOW ARE YOU
Ex 10 Linear Search
n=int(input(“Enter the no. of elements:”))
S=[ ]
for i in range(n):
a=int(input(“Enter Element:”))
S.append(a)
x=int(input("enter element to search:"))
for i in range(n):
if x==S[i]:
print("element found at position"+str(i+1))
break
if i==n-1:
print("Not found")
Output:
Enter the no. of elements:5
Enter Element:3
Enter Element:2
Enter Element:5
Enter Element:3
Enter Element:1
enter element to search:3
element found at position1
Ex 11 Selection Sort
Output:
7
Ex 12 Matrix Multiplication
A = [[12, 7, 3],
[4, 5, 6],
[7, 8, 9]]
B = [[5, 8,1,2],
[6,7,3,0],
[4,5,9,1]]
Output:
Resultant Matrix is:
[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]
Ex 13 Tuple Operations
Tup1=(1,3,4,2,'hi',5,'hello')
Tup2=(1,3,4)
print("Concatenation")
Tup3=Tup1+Tup2
print(Tup3)
print("Repetition")
print(Tup2*3)
print("Length")
print(len(Tup3))
Output:
Concatenation
(1, 3, 4, 2, 'hi', 5, 'hello', 1, 3, 4)
8
Repetition
(1, 3, 4, 1, 3, 4, 1, 3, 4)
Length
10
Ex 14 Dictionary Functions
dic={101:'Ramu',102:'Babu',103:'kumar',104:'Somu'}
print("length:")
print(len(dic))
print(dic[101])
print(dic.keys())
print(dic.values())
print(dic.items())
print(dic.get(102))
dic2=dic.copy()
print(dic2)
dic.popitem()
print(dic)
dic.clear()
print(dic)
Output:
length:
4
Ramu
dict_keys([101, 102, 103, 104]) dict_values(['Ramu', 'Babu', 'kumar', 'Somu'])
dict_items([(101, 'Ramu'), (102, 'Babu'), (103, 'kumar'), (104, 'Somu')]) Babu
{101: 'Ramu', 102: 'Babu', 103: 'kumar', 104: 'Somu'}
{101: 'Ramu', 102: 'Babu', 103: 'kumar'}
{}
src = r'C:\Users\DCSE4\Desktop\first.txt'
trg=r'C:\Users\DCSE4\Desktop\second.txt'
wordcount=0
with open(src,'r') as firstfile, open(trg,'a') as secondfile:
for line in firstfile:
secondfile.write(line)
wordcount=wordcount+len(line.split())
print(“Total no. of words copied from file1 are:”)
print(wordcount)
Output:
first.txt second.txt
9
hi hi
hello hello
how are you how are you