Python_imp_questions
Q1. Write a program to count occurrences of all characters
within a string.
char_count = {}
for ch in text:
if ch in char_count:
char_count[ch] += 1
else:
char_count[ch] = 1
print(char_count)
Q2. Write a program to check whether a number is prime or not.
num = int(input("Enter a number: "))
if num <= 1:
print("Not a Prime Number")
else:
for i in range(2, num):
if num % i == 0:
print("Not a Prime Number")
break
else:
print("Prime Number")
[Link] and print how many times 'football' appears in list.
Sports = ['cricket', 'football', 'tennis', 'football', 'hockey']
count = 0
for game in Sports:
if game == 'football':
count += 1
print("Football appears", count, "times")
Q4. Find and print the largest and smallest number in a list.
[8, 2, 15, 1, 9] without using max() and min().
numbers = [8, 2, 15, 1, 9]
largest = numbers[0]
smallest = numbers[0]
for num in numbers:
if num > largest:
largest = num
if num < smallest:
smallest = num
print("Largest number:", largest)
print("Smallest number:", smallest)
Q5. Write a code to print the key of a minimum value from the
following dictionary.
Sample :
Input:{‘math’:89,’phy’:80,’chem’:67,’eng’:75}
Output:chem
marks = {'math': 89, 'phy': 80, 'chem': 67, 'eng': 75}
min_key = None
min_value = None
for key, value in [Link]():
if min_value is None or value < min_value:
min_value = value
min_key = key
print(min_key)
Q6. Find Common Elements in Two Lists.
Sample :
List1:[10,20,30,40]
List2:[30,40,50,60]
Output:30,40
List1 = [10, 20, 30, 40]
List2 = [30, 40, 50, 60]
for item in List1:
if item in List2:
print(item)
Q7. Write a Python program to count the total number of digits
in a number.
num = int(input("Enter any value: "))
count = 0
while num > 0:
num = num // 10
count += 1
print("Total digits:", count)
Q 8. WAP to give list of all factors for any number.
n=int(input("Enter any no:"))
factors = []
for i in range(1,n+1):
if n%i==0:
[Link](i)
print("Factors are :",factors)
Q 9. WAP to arrenge all items from list in assending order.
l = [64, 34, 25, 12, 22, 11, 90, 5]
n = len(l)
for i in range(n-1):
for j in range(n-i-1):
if l[j] > l[j+1]:
l[j], l[j+1] = l[j+1], l[j]
print(l)
Q 10. WAP to arrenge all items from list in desending order.
l = [64, 34, 25, 12, 22, 11, 90, 5]
n = len(l)
for i in range(n-1):
for j in range(n-i-1):
if l[j] < l[j+1]:
l[j], l[j+1] = l[j+1], l[j]
print(l)
Q 11. WAP to find maximum digit in given list.
l = [2,4,9,3,5]
max = l[0]
for i in range(len(l)-1):
if l[i]>l[i+1]:
max = l[i]
l[i],l[i+1]=l[i+1],l[i]
else:
max=l[i+1]
print(max)
Q 12. WAP to find minimum digit in given list.
l = [2,4,9,3,5]
min = l[0]
for i in range(len(l)-1):
if l[i]<l[i+1]:
min = l[i]
l[i],l[i+1]=l[i+1],l[i]
else:
min=l[i+1]
print(min)
Q 13. WAP to check given number is either perfect number or
not.
n=int(input("Enter any no:"))
sum = 0
for i in range(1,n):
if n%i==0:
sum=sum+i
if n==sum:
print(f'given number {n} is perfect number')
else:
print(f'given number {n} is not a perfect number')
Q 14. WAP to arrenge all even and odd no at a place.
# i/p :- [2,1,3,8,4,8,5]
# o/p :- [2,8,4,8,1,3,5]
l = [2,1,3,8,4,8,5]
l1=[]
for i in l:
if i %2==0:
[Link](i)
for i in l:
if i%2 !=0:
[Link](i)
print(l1)
Q 15. WAP to arrenge all zeroes at the end.
# i/p :- [2,0,3,0,1,0,5]
# o/p :- [2,3,1,5,0,0,0]
l = [2,0,3,0,1,0,5] # [2,3,1,5,0,0,0]
l1=[]
for i in l:
if i !=0:
[Link](i)
n = len(l)-len(l1)
for i in range(n):
[Link](0)
print(l1)
Q 16. WAP to arrenge below menrtion format.
# i/p :- l = [1, 1, 1, 2, 3, 4, 3, 4, 2, 5, 6, 5, 7]
# o/p :- {1: 3, 2: 2, 3: 2, 4: 2, 5: 2, 6: 1, 7: 1}
l = [1, 1, 1, 2, 3, 4, 3, 4, 2, 5, 6, 5, 7]
output_dict = {}
for item in l:
if item in output_dict:
output_dict[item] += 1
else:
output_dict[item] = 1
print(output_dict)
Q 17. WAP to calculate LCM of two numbers.
x = int(input("Enter any number: "))
y = int(input("Enter any number: "))
if x > y:
greater = x
else:
greater = y
while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1
print("LCM of {} and {} is {}".format(x,y,lcm))
Q 18. WAP to calculate HCF of two numbers.
x=int(input("Enter any no:"))
y= int(input("Enter any no:"))
if x>y:
smaller = y
else:
smaller = x
for i in range(1,smaller+1):
if (x%i==0 and y%i==0):
hcf = i
print("HCF of {} and {} is {}".format(x,y,hcf))
Q 19. WAP to print Factors of any numbers.
n=int(input("Enter any no:"))
factors = []
for i in range(1,n+1):
if n%i==0:
[Link](i)
print("Factors are :",factors)