Solutions
Solutions
# Calculating Total
total = num1 + num2
# Printing Total
print(f"Total is {total}")
2. Write a Program that takes Length and Breadth as input from user and
print the Area of Rectangle.
length = float(input("Enter length of rectangle = "))
breadth = float(input("Enter breadth of rectangle = "))
# Calculating Area
area = length * breadth
# Printing Area
print(f"Area of rectangle is {area}")
# Calculating Average
avg = (num1 + num2 + num3) / 3
# Calculating Percentage
percentage = total / 500 * 100
5. Same as above
6. Ask value in Rupees and Convert into Paise.
rupees = float(input("Enter rupees = "))
9. Take two numbers as input from User and print which one is greater or
are they equal.
num1 = int(input("Enter number 1 = "))
num2 = int(input("Enter number 2 = "))
10. Take three numbers as input from User and print which one is greater
or are they equal.
num1 = int(input("Enter number 1 = "))
num2 = int(input("Enter number 2 = "))
num3 = int(input("Enter number 3 = "))
11. Take Salary as input from User and Update the salary of an employee
a. salary less than 10,000, 5 % increment
b. salary between 10,000 and 20, 000, 10 % increment
c. salary between 20,000 and 50,000, 15 % increment
d. salary more than 50,000, 20 % increment
salary = float(input("Enter your salary = "))
12. Ask the number from User and print whether the number is Odd or
Even.
number = int(input("Enter any number = "))
if number % 2 == 0:
print(f"The number {number} is even")
else:
print(f"The number {number} is odd")
# ------------ OR ----------
mark = int(input("Enter your marks = "))
14. A student will not be allowed to sit in exam if his/her attendance is less
than 75%.
a. Take following input from user
i. Number of classes held
ii. Number of classes attended.
b. Print percentage of class attended
c. Print Is student is allowed to sit in exam or not.
totalClasses = int(input("Total classes conducted = "))
classesAttend = int(input("Total classes attended = "))
15. Calculate the purchase amount after deduction criteria on printed price
a. printed price between 500 and 1000, allow 5% discount
b. printed price between 1000 and 5000, allow 10% discount
c. printed price between 5000 and 10000, allow 15% discount
d. printed price more than 10000, allow 20% discount
billAmount = float(input("Enter bill amount = "))
while i <= n:
total = total + i
i += 1
print(f"Total is {total}")
# Calculating factors
while i <= n:
if n % i == 0:
totalFactors = totalFactors + 1
i += 1
while m > 0:
digit = m % 10
total = total + (digit ** 3)
m = m // 10
if n == total:
print("It is an armstrong number")
else:
print("It is not an armstrong number")
while n > 0:
total = total * n
n = n - 1
print(f"Factorial = {total}")
while m > 0:
digit = m % 10
totalDigits = totalDigits + 1
m = m // 10
while m > 0:
digit = m % 10
print(digit, end="")
m = m // 10
print(f"Total is {total}")
if totalFactors == 2:
print(f"{number} is a prime number")
else:
print(f"{number} is not a prime number")
1
12
123
1234
12345
for i in range(1, 6):
for j in range(1, i + 1):
print(f"{j} ", end=" ")
print()
5
54
543
5432
54321
for i in range(5, 0, -1):
for j in range(5, i - 1, -1):
print(f"{j} ", end=" ")
print()
55555
4444
333
22
1
for i in range(5, 0, -1):
for j in range(1, i + 1):
print(f"{i} ", end=" ")
print()
1
21
321
4321
54321
for i in range(1, 6):
# To print space on left side
for j in range(5, i, -1):
print(f" ", end=" ")
# To print numbers
for k in range(i, 0, -1):
print(f"{k} ", end="")
print()
5
45
345
2345
12345
for i in range(1, 6):
# To print space on left side
for j in range(5, i, -1):
print(f" ", end=" ")
# To print numbers
for k in range(6 - i, 6):
print(f"{k} ", end="")
print()
b. 2 4 6 8 10 12 14 16 18 20
for i in range(1, 21):
if i % 2 == 0:
print(i, end=" ")
c. 3 6 9 12 15 18 21 24 27 30
for i in range(3, 31, 3):
print(i, end=" ")
d. 4 8 12 16 20 24 28 32 36 40
for i in range(4, 41, 4):
print(i, end=" ")
e. 5 10 15 20 25 30 35 40 45 50
for i in range(5, 51, 5):
print(i, end=" ")
# Solution 1
total = sum(myList)
print(f"Total of {myList} is {total}")
# Solution 2
total = 0
for i in myList:
total = total + i
print(f"Total of {myList} is {total}")
total = 1
for i in myList:
total = total * i
print(f"Total multiplication of {myList} is {total}")
36. Take 10 integer inputs from user and store them in a list and print them
on screen.
myList = []
print(f"Answer = {myList}")
37. Take 20 integer inputs from user and print the following:
a. number of positive numbers
b. number of negative numbers
c. number of odd numbers
d. number of even numbers
e. number of 0s.
myList = []
positive = 0
negative = 0
odd = 0
even = 0
zero = 0
for i in myList:
if i > 0:
positive += 1
elif i < 0:
negative += 1
else:
zero += 1
if i % 2 == 0:
even += 1
else:
odd += 1
print(f"Positive = {positive}")
print(f"Negative = {negative}")
print(f"Zero = {zero}")
print(f"Even = {even}")
print(f"Odd = {odd}")
38. Take 10 integer inputs from user and store them in a list. Again, ask user
to give a number. Now, tell user whether that number is present in list
or not.
myList = []
if number in myList:
print("Number is present in the list")
else:
print("Number is not present in the list")
39. Make a list by taking 10 inputs from user. Now delete all repeated
elements of the list.
myList = []
newList = []
for i in myList:
if i not in newList: # Check if number is present
newList.append(i)
40. Ask user to give integer inputs to make a list. Store only even values
given and print the list.
myList = []
print(myList)
42. Create a tuple with some numbers. Ask a random number from user
and add that number to the end of the tuple.
myTuple = (54, 34, 12, 33, 55, 65, 44)
43. Write a Python program to get the 4th element and 4th element from
last of a tuple.
myTuple = (56, 89, 77, 65, 32, 12, 45, 33)
for i in myTuple:
# Checking if number comes more than 1 time
if myTuple.count(i) > 1:
# Checking if number already in repeatedElements
if i not in repeatedElements:
repeatedElements.append(i)
if myTuple.count(number) > 0:
myList = list(myTuple)
myList.remove(number)
myTuple = tuple(myList)
print(myTuple)
else:
print("Number does not exist in tuple")
46. Write a Python program calculate the product, multiplying all the
numbers of a given tuple.
myTuple = (4, 3, 2, 2, -1, 18)
total = 1
for i in myTuple:
total = total * i
dict1.update(dict2)
print(dict1)
48. Write a python program to find the sum of all items in a dictionary.
myDictionary = {"maths": 77, "science": 90, "english": 34,
"sst": 24, "comp": 99}
total = 0
for i in myDictionary.values():
total = total + i
49. Write a python program to check whether a given key already exists in a
Dictionary.
myDictionary = {"name": "Akshay", "gender": "Male", "age": 66,
"phone": 45454}
50. Write a Python program to iterate over dictionaries using for loops.
myDictionary = {"name": "Akshay", "gender": "Male", "age": 66,
"phone": 45454}
for k, v in myDictionary.items():
print(f"Key -> {k} and value -> {v}")
51. Write a Python program to generate and print a dictionary that contains
a number (between 1 and n) in the form (x, x*x).
myDictionary = {}
n = int(input("Enter any number = "))
print("Final answer")
print(myDictionary)
print(myDictionary)
53. Write a Python program to get the maximum and minimum value in a
dictionary.
myDictionary = {"maths": 19, "english": 87, "science": 43,
"sst": 88, "comp": 99}
allMarksList = []
for i in myDictionary.values():
allMarksList.append(i)
print(myDictionary)
for k, v in myDictionary.items():
print("{:10s} {}".format(k, v))
print(myDictionary)
for i in myDictionary.keys():
myDictionary[i] = []
print(myDictionary)
57. Write a Python program to convert a given dictionary into a list of lists.
myDictionary = {1: 'red', 2: 'green', 3: 'black', 4: 'white',
5: 'black'}
myList = []
for k, v in myDictionary.items():
myList.append([k, v])
print(myList)
myList = n.split()
for i in myList:
if len(i) % 2 == 0:
print(i)
# ---------Solution 1---------
print(n[-1::-1])
# ---------Solution 2---------
myList = []
for i in n:
myList.append(i)
myList.reverse()
for i in myList:
print(i, end='')
n = n.lower()
total = n.count('a') + n.count('e') + n.count('i') +
n.count('o') + n.count('u')
62. Remove the even index characters from the string entered by User.
n = input("Enter any string = ")
ans = ''
for i in range(0, len(n)):
if i % 2 == 0:
ans += n[i]
print(ans)
if len(n) % 4 == 0:
print(n[-1::-1])
else:
print(n)
for i in n:
if i.isdigit():
total += 1
if n == n[-1::-1]:
print("It is a palindrome")
else:
print("It is no a palindrome")
for i in n:
if n.count(i) > 1:
if i not in myList:
myList.append(i)
myList = list(n)
myList.reverse()
for i in myList:
print(i, end="")
reverseString()
reverseString()
71. Write a Python function that takes a number as a parameter and check
the number is prime or not.
def checkPrime(n):
totalFactors = 0
for i in range(1, n + 1):
if n % i == 0:
totalFactors += 1
if totalFactors == 2:
print(f"{n} is a prime number")
else:
print(f"{n} is not a prime number")
checkPrime(55)
checkPrime(7)
checkPrime(70)
72. Write a Python program to print the even numbers from a given list.
def evenList(x):
myList = []
for i in x:
if i % 2 == 0:
myList.append(i)
print(myList)
checkPalindrome("Noon")
checkPalindrome("moM")
checkPalindrome("abc")
75. Write a Python program to read a file line by line and store it into a list.
f = open("hello.txt", "r")
myList = f.readlines()
print(myList)
f.close()
longestWord = ''
for i in allWords:
if len(i) > len(longestWord):
longestWord = i
f.close()
77. Write a Python program to count the number of lines in a text file.
f = open("hello.txt", "r")
f.close()
for i in allWords:
if i not in myWords:
myWords.append(i)
for i in myWords:
print(f"{i} has repeated {allWords.count(i)} times")
f.close()
79. Write a Python program to copy the contents of a file to another file.
f = open("hello.txt", "r")
content = f.read()
f.close()
f = open("hello1.txt", "w")
f.write(content)
f.close()
f = open("hello.txt", "r")
allLines = f.readlines()
f.close()