Python File
Python File
1
Programs on Loops
1) Wap to find the sum of a number.
num = int(input("Enter a number: "))
if num < 0:
else:
sum = 0
sum += num
num -= 1
2
for i in range(1, 10+1):
print(j, end="\t")
print()
3
4) Wap that return true or false depending on whether the given no.
is palindrome. A palindromic is a no. (such as 16461) that remains the
same when its digits are reversed.
n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
digit=n%10
rev=rev*10+digit
n=n//10
if(temp==rev):
print("The number is a palindrome!")
Programs on Strings
4
1 )Write a Python program to calculate the length of a
string.
for s in str:
counter = counter+1
if len(a)%4==0:
print(‘’.join(reversed(a)))
else:
reverse=’’.join(line.split()[::-1])
print(reverse)
vowels="aeiouAEIOU"
Programs on lists
6
1)Write a python program to get the largest and second largest
number from a list.
y=max(x)
x.remove(y)
z=max(x)
diff12=list(set(lst1)-set(lst2))
diff21=list(set(lst2)-set(lst1))
print(diff12+diff21)
print(x)
lst.index(‘35’)
append=lst1+lst2
print(append)
my_newlist=list(my_set)
print(my_newlist)
res={}
for I in mylist:
if (I in res):
res[i]+=1
else:
res[i]=1
print(key,”:“,value)
8) Write a python program to find common items from the two lists.
y=””.join(mylist)
print(y)
y=lst1.remove(lst1[-1])
new_list=lst1+lst2
print(new_list)
11)Write a python program to find the list in a list whose sum of the
elements is the highest.
10
num=[[1, 2, 3], [4, 5, 6], [10, 11, 12], [7, 8, 9]]
print(max(num, key=sum))
new_num=[]
new_num.append(elem)
num=new_num
print(new_num)
lis=[‘0’, ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘4’, ‘5’, ‘6’, ‘6’, ‘7’, ‘8’, ‘9’, ‘4’, ‘4’]
11
i=0
while i<len(lis)-1:
if lis[i]==lis[i+1]:
del lis[i]
else:
i+=1
print(lis)
k=4
result=lis[:k-1] + lis[k:]
print(result)
Programs on Sets
1) Write a python program to add members in a set and iterate over
it.
12
num_set=set(['0', '1', '2', '3', '4'])
for n in num_set:
vowels.remove('a')
print(vowels)
my_set.clear()
print(my_set)
13
m=set(input("enter the second set").split())
if n.isdisjoint(m):
else:
n.difference_update(m)
Programs on Dictionary
1) Write a python script to sort (ascending and descending) a
dictionary by value.
14
import operator
d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
s= sorted(d.items(), key=operator.itemgetter(1))
print('ascending order : ',s)
s1= dict( sorted(d.items(),
key=operator.itemgetter(1),reverse=True))
print('descending order : ',s1)
my_dict={0:10, 1:20}
my_dict[2]=30
print(my_dict)
if x in my_dict:
15
print("key is present in the dictionary")
else:
d=dict()
d[x]=x*x
print(d)
d1={1:'Jan', 2:'Feb'}
d2={3:'Mar', 4:'Apr'}
d1.update(d2)
print(d1)
16
6) Write a python program to sum and multiply all the items in a
dictionary.
print(sum(my_dict.values()))
result=1
result=result*my_dict[key]
print(result)
print(sorted(my_dict.keys()))
17
8) Write a python program to remove the duplicate values from
dictionary.
uniq=dict()
uniq[key]=value
print(uniq)
Programs on Functions
18
def list_sum(lst):
add_items=sum(lst)
return add_items
def reverse(str1):
return str1[::-1]
def factorial(x):
if x==1:
return 1
19
else:
fact=x*factorial(x-1)
return(fact)
import string
def ispangram(str1):
alpha=set(string.ascii_lowercase);
return alpha<=set(x.lower());
print(ispangram(x))
lst=[2, 4, 6, 8, 9]
20
z=map(lambda x:x**3, lst)
print(z(str1, c))
21
print("the no. of even numbers in the list is ", even_ctr)
4) Write a python program to add two given lists using map and
lambda.
lst2=[3, 6, 7, 8, 3, 2, 1]
a=[16,19,11,15,10,12,14]
22
for j in range(len(a)):
swapped=False
i=0
while i<len(a)-1:
if a[i]>a[i+1]:
a[i],a[i+1]=a[i+1],a[i]
swapped=True
i=i+1
if swapped==False:
break
a=[18,10,11,15,60,56,14]
i=0
while i<len(a):
23
smallest=min(a[i:])
index_of_smallest=a.index(smallest)
a[i],a[index_of_smallest]=a[index_of_smallest],a[i]
i=i+1
lst = []
for n in range(num):
lst.append(numbers)
24
x = int(input("Enter number to search in list :- "))
i=0
flag = False
for i in range(len(lst)):
if lst[i] == x:
flag = True
break;
if flag == 1:
else:
25