Python Programs Exp 3 to Exp 19
3. Write a Python program to demonstrate operator precedence concept by evaluating
the following expressions.
3 + 4 * 2 / (1 - 5) ** 2 ** 3
(3 + 4) * 2 / (1 - 5) ** 2 ** 3
100 % 3 + 5 ** (2 + 1) - 10 / 2
(100 % 3) + 5 ** (2 + 1) – (10 / 2)
((4 + 5 * 2) / 3) ** 2 – 7
(4 + 5) * 2 / 3) ** 2 – 7
# Example to demonstrate operator precedence
result = 10 + 5 * 2 # Multiplication (*) has higher precedence than addition (+)
print("Result of 10 + 5 * 2:", result) # Output: 20 #
Using parentheses to change precedence
result_with_parentheses = (10 + 5) * 2
print("Result of (10 + 5) * 2:", result_with_parentheses) # Output: 30
print("Expression 1: 3 + 4 * 2 / (1 - 5) ** 2 ** 3") Expression 1: 3 + 4 * 2 / (1 - 5) ** 2
step1 = 2 ** 3 ** 3
print("Step 1: 2 ** 3 =", step1)
Step 1: 2 ** 3 = 8
step2 = 1 - 5
print("Step 2: (1 - 5) =", step2) Step 2: (1 - 5) = -4
step3 = step2 ** step1 Step 3: (-4) ** 8 = 65536
print(f"Step 3: ({step2}) ** {step1} =", step3)
Step 4: 4 * 2 = 8
step4 = 4 * 2
print("Step 4: 4 * 2 =", step4) Step 5: 8 / 65536 =
0.0001220703125
step5 = step4 / step3
print(f"Step 5: {step4} / {step3} =", step5) Step 6: 3 + 0.0001220703125 =
3.0001220703125
expr1 = 3 + step5
print("Step 6: 3 +", step5, "=", expr1) Final Result of Expression 1:
3.0001220703125
print("Final Result of Expression 1:", expr1)
print()
4. Write a program to check if the person is eligible to vote using if- else construct.
age = int(input("Enter your age: ")) Enter your age: 34
if age >= 18: You are eligible to vote.
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
1
5. Write a program to check if the person is a senior citizen or not using if- else construct.
age = int(input("Enter your age: ")) Enter your age: 12
You are not a senior citizen.
if age >= 60:
print("You are a senior citizen.")
else:
print("You are not a senior citizen.")
6. Write a Python program that asks the user to enter their age. Based on the age, the
program should print different messages:
● If the age is less than 13, print: "You are a child."
● If the age is between 13 and 19 (inclusive), print: "You are a teenager."
● If the age is between 20 and 59 (inclusive), print: "You are an adult."
● If the age is 60 or above, print: "You are a senior citizen."
age = int(input("Enter your age: ")) Enter your age: 45
You are an adult.
if age < 13:
print("You are a child.")
elif age >= 13 and age <= 19:
print("You are a teenager.")
elif age >= 20 and age <= 59:
print("You are an adult.")
elif age >= 60:
print("You are a senior citizen.")
7. Write a Python program that classifies a user-entered integer into various categories
using if-elif-else statements.
A. Prompt the user to enter an integer number.
B. The program should classify the number according to the following rules:
a. If the number is positive and even, print: "The number is positive and
even."
b. If the number is positive and odd, print: "The number is positive and odd."
c. If the number is zero, print: "The number is zero."
d. If the number is negative and even, print: "The number is negative and
even."
2
e. If the number is negative and odd, print: "The number is negative and
odd."
C. Handle invalid input gracefully by printing: "Invalid input! Please enter a valid
integer."
num = int(input("Enter an integer: ")) Enter an integer: 12
The number is positive and even.
if num > 0 and num % 2 == 0:
print("The number is positive and even.")
elif num > 0 and num % 2 != 0:
print("The number is positive and odd.")
elif num == 0:
print("The number is zero.")
elif num < 0 and num % 2 == 0:
print("The number is negative and even.")
elif num < 0 and num % 2 != 0:
print("The number is negative and odd.")
else:
print("Invalid input! Please enter a valid integer.")
While loop
8. Write a Python program that asks the user to enter positive numbers one by one and
keeps adding them until the user enters 0. When 0 is entered, the program should print
the total sum using while loop.
total_sum = 0 Enter a positive number (or 0 to
stop): 10
while True: Enter a positive number (or 0 to
num = int(input("Enter a positive number (or 0 to stop): 20
stop): ")) Enter a positive number (or 0 to
stop): 15
if num == 0: Enter a positive number (or 0 to
break stop): 1
elif num > 0: Enter a positive number (or 0 to
total_sum += num stop): 234
else: Enter a positive number (or 0 to
print("Please enter a positive number.") stop): 21
Enter a positive number (or 0 to
print("The total sum is:", total_sum) stop): 0
The total sum is: 301
9. Write a Python program that prints the multiplication table of ‘n’ from 1 to 10 using a
3
while loop.
n = int(input("Enter a number to print its Enter a number to print its
multiplication table: ")) multiplication table: 5
i=1 5X1=5
5 X 2 = 10
while i <= 10: 5 X 3 = 15
print(n, "X", i, "=", n * i) 5 X 4 = 20
i += 1 5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
10. Write a Python program that calculates the sum of the first N natural numbers using a
while loop.
N = int(input("Enter a number N: ")) Enter a number N: 20
The sum of the first 20 natural
sum_of_numbers = 0 numbers is: 210
i=1
while i <= N:
sum_of_numbers += i
i += 1
print("The sum of the first",N,"natural numbers is:",
sum_of_numbers)
For loop
11. Write a Python program that takes a string input from the user and counts the number
of vowels using a for loop.
user_input = input("Enter a string: ") Enter a string: BS Abdur Rahman
vowel_count = 0 Crescent College
The number of vowels in the string
for char in user_input: is: 9
if char in "aeiouAEIOU":
vowel_count += 1
print("The number of vowels in the string is:",
vowel_count)
12. Write a Python program that calculates the sum of all numbers in the given list using
a for loop.
4
numbers = [11, 2, 23, 14, 50] The sum of all numbers in the list is:
100
sum = 0
for num in numbers:
sum += num
print("The sum of all numbers in the list is:",
13. Write a Python program that prints the multiplication table of ‘n’ from 11 to 20 using a
for loop.
n = int(input("Enter a number to print its Enter a number to print its
multiplication table from 11 to 20: ")) multiplication table from 11 to 20: 14
14 X 11 = 154
for i in range(11, 21): 14 X 12 = 168
print(n, "X", i, "=", n * i) 14 X 13 = 182
14 X 14 = 196
14 X 15 = 210
14 X 16 = 224
14 X 17 = 238
14 X 18 = 252
14 X 19 = 266
14 X 20 = 280
14. Write a Python program that asks the user to enter numbers continuously:
If the number is divisible by 5, skip it (use continue).
If the number is negative, stop asking for inputs (use break).
At the end, print the sum of all entered positive numbers that were not divisible by 5.
sum = 0 Enter a number: 1
Enter a number: 2
while True: Enter a number: 3
num = int(input("Enter a number: ")) Enter a number: 5
Enter a number: 10
if num % 5 == 0: Enter a number: 2
continue Enter a number: -1
if num < 0: The sum of all valid numbers is: 8
break
sum += num
print("The sum of all valid numbers is:", sum)
Function
15. Write a Python program to check whether a number is perfect or not, using function.
6 (1 + 2 + 3 = 6)
5
28 (1 + 2 + 4 + 7 + 14 = 28)
def perfect(n): Enter a number: 34
sum_of_divisors = 0 34 not a perfect number.
for i in range(1, n):
if n % i == 0:
sum_of_divisors += i Enter a number: 28
return (sum_of_divisors == n) 28 perfect number.
num = int(input("Enter a number: "))
if perfect(num):
print(num,"perfect number.")
else:
print(num, "not a perfect number.")
16. Write a Python program to calculate the factorial of a number, using function.
def factorial(n): Enter a number: 5
if n == 0: 120
return 1
fact = 1
for i in range(1, n + 1):
fact = fact * i
return fact
num = int(input("Enter a number: "))
print(factorial(num))
17. Write a Python program to solve the Fibonacci sequence using recursion.
def fibonacci(n): How many Fibonacci numbers to
if n <= 0: print? 7
return "Input must be a positive integer." 0
elif n == 1: 1
return 0 1
elif n == 2: 2
return 1 3
else: 5
return fibonacci(n - 1) + fibonacci(n - 2) 8
n = int(input("How many Fibonacci numbers to
print? "))
6
for i in range(1,n+1):
print(fibonacci(i), end=" ")
18. Write a Python program to create a dictionary to store the names and ages of five
people. Print the entire dictionary.
people = {} Person 1
Enter name: Raja
count = 1 Enter age of Raja: 34
for i in range(5): Person 2
print("Person", count) Enter name: Ramu
name = input("Enter name: ") Enter age of Ramu: 32
age = int(input("Enter age of " + name + ": ")) Person 3
people[name] = age Enter name: Ravi
count = count + 1 Enter age of Ravi: 23
Person 4
print("\nDictionary of names and ages:") Enter name: Rajesh
print(people) Enter age of Rajesh: 41
Person 5
Enter name: Rakki
Enter age of Rakki: 21
Dictionary of names and ages:
{'Raja': 34, 'Ramu': 32, 'Ravi': 23,
'Rajesh': 41, 'Rakki': 21}
19. Write a program that takes a string input from the user and uses a dictionary to count
and print the frequency of each character.
text = input("Enter a string: ") Enter a string: welcome
frequency = {} Character frequencies:
w:1
for char in text: e:2
if char in frequency: l:1
frequency[char] += 1 c:1
else: o:1
frequency[char] = 1 m:1
print("Character frequencies:")
for key in frequency:
print(key,":" ,frequency[key])