17385
17385
#Program 8:
# Read a four-digit number and find the sum of first two and last two digits.
l = []
n = num
while n != 0:
l.insert(0, n % 10)
n = n // 10
Output:
Enter first number: 1234
Sum of first two digits: 3
Sum of last two digits: 7
#Program 9:
# Read a number from the user and convert to its equivalent binary number.
Output:
Enter a number: 121
Binary equivalent of 121 is 1111001
#Program 10:
# Read a number from the user and check if it is a prime number or not.
Output1:
Enter number: 21
21 is not prime.
Output2:
Enter number: 5
5 is prime.
#Program 11:
# Read a number from the user and check if it is a perfect number or not.
if sum == num:
print(num, 'is a perfect number.')
else:
print(num, 'is not a perfect number.')
Output 1:
Enter number: 6
6 is a perfect number.
Output 2:
Enter number: 36
36 is not a perfect number.
#Program 12:
# Read two numbers from the user and compute their GCD and LCM.
Output:
Enter first number: 45
Enter second number: 5
GCD of 45 and 5 is 5
LCM of 45 and 5 is 45
#Program 13:
#Program to find the sum of the series:
# x + x2/ 2! - x3/3! + x4/ 4! - x5/ 5! +............+ xn/ n!
#Program 14:
'''Program to display the pattern
12345
1234
123
12
1
'''
n=int(input("enter the number of lines:"))
for i in range(n):
for j in range(1,n-i+1):
print(j,end=" ")
print()
Output:
1 2345
1 234
1 23
1 2
1
#Program 15:
#Program to read a string and display count of:
#1. Number of vowels
#2. Number of consonants
#3. Number of uppercase letters
#4. Number of lowercase letters
Output:
Output:
#Program 17:
#Program to input a list of numbers and swap elements at the even location with the
#elements at the odd location.
i=0
while i < len(l) - 1:
l[i], l[i+1] = l[i+1], l[i]
i += 2
Output:
if n in l:
print(n,'is at position', l.index(n) + 1)
else:
print(n, 'does not exist in the collection.')
Output:
#Program 19:
#Program that inputs two tuples and prints True if element in first tuple is also
#an element of the second tuple, else prints False.
isSubset = False
if isSubset:
print('First sequence is a subset of second sequence')
else:
print('First sequence is not a subset of second sequence')
Output:
#Program 20:
#Program that creates the following tuple using a for loop ('a', 'b', 'c', 'd' . . .), and then
#modify it as ('a', 'bb', 'ccc', 'dddd', . . . .) that ends with 26 copies of the letter z.
t = ()
for i in range(97, 123):
t = t + tuple(chr(i))
print('Tuple before:', t)
l = list(t)
i=1
while i < len(l):
l[i] = l[i] * i
i += 1
t = tuple(l)
print('Tuple after:', t)
Output:
Tuple before: ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z')
Tuple after: ('a', 'b', 'cc', 'ddd', 'eeee', 'fffff', 'gggggg', 'hhhhhhh', 'iiiiiiii', 'jjjjjjjjj', 'kkkkkkkkkk',
'lllllllllll', 'mmmmmmmmmmmm', 'nnnnnnnnnnnnn', 'oooooooooooooo',
'ppppppppppppppp', 'qqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrr', 'ssssssssssssssssss',
'ttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuu', 'vvvvvvvvvvvvvvvvvvvvv',
'wwwwwwwwwwwwwwwwwwwwww', 'xxxxxxxxxxxxxxxxxxxxxxx',
'yyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzz')
#Program 21:
#Program that creates a dictionary with the roll number, name and marks of n students
#in a class and display the names of students who have marks above 75.
Output:
Enter no of students: 3
Enter the details for student 1 :
Enter the roll no: 100
Enter the name: Aryan
Enter the marks: 91
Enter the details for student 2 :
Enter the roll no: 101
Enter the name: Varun
Enter the marks: 67
Enter the details for student 3 :
Enter the roll no: 102
Enter the name: Dhruv
Enter the marks: 75
Students whose marks are above 75 are:
{'rollno': '100', 'name': 'Aryan', 'marks': 91}
#Program 22:
#Program that has a dictionary D1 which has values in the form of a list of numbers.
#Write a program to create a new dictionary D2 having same keys as D1 but values as
#the sum of the list elements.
print(D1)
print(D2)
Output: