PRACTICE QUESTIONS
A. Introduction, Variables & Data Types
Q1. Write a program that takes a user's name, department, and year of study, and prints a
structured introduction message.
Q2. Create a program that calculates the area and perimeter of a rectangle using variables.
Input length and breadth from the user.
Q3. Given:
a = 10, b = 3.14, c = "25", d = True
Print the data type of each and convert each to at least one other type where possible.
B. Implicit & Explicit Type Conversions
Q4. Demonstrate implicit type conversion using a combination of int, float, and boolean values
in an arithmetic expression.
Q5. Write a program to accept a price (string) and quantity (string), convert them to numbers,
and compute total amount with 18% tax.
C. Operators & Expressions
Q6. Write a program to compute:
• Simple Interest
• Compound Interest
Take P, R, T as inputs.
Q7. Using logical operators, check if a student is eligible for a scholarship:
Eligible if:
• attendance ≥ 85% and
• CGPA ≥ 8.0
• OR they have won a national-level competition.
D. Decision Constructs
Q8. Create a program that checks if a given year is:
• a leap year,
• a century year,
• or a normal year.
Q9. Write a menu-driven program for a calculator supporting +, -, *, /, %, ** (power).
Take operator as input from user.
E. Iteration (for, while) + break/continue/pass
Q10. Print all numbers from 1 to 200 that are divisible by 7 but not divisible by 5.
Q11. Write a program to check if a number is prime using a loop and break when a factor is
found.
Q12. Display the first 10 numbers of the Fibonacci series using a while loop.
Q13. Using continue, skip all vowels and print only consonants from a given string.
Q14. Use pass inside a loop for all characters except digits; for digits, print “Digit found: X”.
F. Strings & String Functions
Q15. Given a string, print:
• number of uppercase letters
• number of lowercase letters
• number of digits
• number of special characters
Q16. Implement basic encryption: shift each character by +2 (Caesar Cipher).
SOLUTIONS
A. Solutions: Introduction, Variables & Data Types
Solution Q1
name = input("Enter your name: ")
dept = input("Enter your department: ")
year = input("Enter your year of study: ")
print(f"Hello {name}, you are studying in {dept} department, Year {year}.")
Solution Q2
length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))
area = length * breadth
perimeter = 2 * (length + breadth)
print("Area:", area)
print("Perimeter:", perimeter)
Solution Q3
a = 10
b = 3.14
c = "25"
d = True
print(type(a), type(b), type(c), type(d))
print(float(a))
print(int(b))
print(int(c))
print(int(d)) # True = 1, False = 0
B. Solutions: Type Conversions
Solution Q4
result = 10 + 2.5 + True # True becomes 1
print(result, type(result)) # float
Solution Q5
price = float(input("Enter price: "))
qty = int(input("Enter quantity: "))
total = price * qty
bill = total + (total * 0.18)
print("Final bill amount:", bill)
C. Solutions: Operators & Expressions
Solution Q6
P = float(input("Enter principal: "))
R = float(input("Enter rate: "))
T = float(input("Enter time: "))
si = (P * R * T) / 100
ci = P * ((1 + R/100)**T) - P
print("Simple Interest =", si)
print("Compound Interest =", ci)
Solution Q7
attendance = float(input("Attendance %: "))
cgpa = float(input("CGPA: "))
national = input("Won national-level competition? (yes/no): ")
eligible = (attendance >= 85 and cgpa >= 8.0) or ([Link]() == "yes")
print("Eligible for scholarship:", eligible)
D. Solutions: Decision Constructs
Solution Q8
year = int(input("Enter year: "))
if year % 400 == 0:
print("Century Leap Year")
elif year % 100 == 0:
print("Century Year (not leap year)")
elif year % 4 == 0:
print("Leap Year")
else:
print("Normal Year")
Solution Q9
a = float(input("Enter number 1: "))
b = float(input("Enter number 2: "))
op = input("Enter operator (+,-,*,/,%,**): ")
if op == '+':
print(a + b)
elif op == '-':
print(a - b)
elif op == '*':
print(a * b)
elif op == '/':
print(a / b)
elif op == '%':
print(a % b)
elif op == '**':
print(a ** b)
else:
print("Invalid operator")
E. Solutions: Loops + break/continue/pass
Solution Q10
for i in range(1, 201):
if i % 7 == 0 and i % 5 != 0:
print(i)
Solution Q11
num = int(input("Enter number: "))
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
print("Prime" if is_prime else "Not Prime")
Solution Q12
a, b = 0, 1
count = 0
while count < 10:
print(a)
a, b = b, a + b
count += 1
Solution Q13
s = input("Enter string: ")
for ch in s:
if [Link]() in "aeiou":
continue
print(ch, end="")
Solution Q14
s = input("Enter string: ")
for ch in s:
if [Link]():
print("Digit found:", ch)
else:
pass
F. Solutions: Strings
Solution Q15
s = input("Enter string: ")
upper = lower = digit = special = 0
for ch in s:
if [Link]():
upper += 1
elif [Link]():
lower += 1
elif [Link]():
digit += 1
else:
special += 1
print("Upper:", upper)
print("Lower:", lower)
print("Digits:", digit)
print("Special:", special)
Solution Q16: Caesar Cipher (+2)
text = input("Enter message: ")
encrypted = ""
for ch in text:
encrypted += chr(ord(ch) + 2)
print("Encrypted:", encrypted)
MODULE 2 – PRACTICE PROGRAMS (QUESTIONS ONLY)
1. Variables & Data Types
1. Write a program to input your name, age, and height, and print them with their data
types.
2. Swap the values of two variables without using a third variable.
3. Input a number and print whether it is int, float, or string type (use type checking).
4. Calculate the area of a circle when the radius is given by the user.
5. Convert temperature from Celsius to Fahrenheit. f = (c * 9/5) + 1
2. Implicit & Explicit Type Conversion
6. Input two integers and print their sum, product, float conversion, and string
conversion.
7. Convert a float number to integer and observe the data loss (print both before & after).
8. Add an integer and a string value after converting them appropriately.
9. Input a number as a string, convert it to float, and print the square root.
3. Operators & Expressions
10. Write a program to check whether a number is even or odd using modulo operator.
11. Compute simple interest and compound interest using formulas.
12. Input three numbers and find the maximum using only logical operators (no max()).
13. Compute the grade of a student based on average marks (use comparison operators).
14. Write a program to check if a number is divisible by both 3 and 5.
4. Decision Control (if-elif-else)
15. Check if a number is positive, negative, or zero.
16. Input marks and print "Pass" if marks ≥ 33, else "Fail".
17. Check whether a character is a vowel or consonant.
18. Check if a student’s attendance percentage is eligible for exam (≥ 75%).
19. Write a program to classify a number as small (<10), medium (10–99), or large (≥100).
20. Menu-driven program:
o 1 → Add two numbers
o 2 → Subtract
o 3 → Multiply
o 4 → Divide
5. Loops (for, while, break, continue, pass)
21. Print numbers from 1 to 50 using a for loop.
22. Print the sum of first 100 natural numbers.
23. Print multiplication table of a number (user input).
24. Input 10 numbers and count how many are positive.
25. Print all even numbers between 1 and 100.
26. Using while loop, keep taking input until the user enters 0 → then stop.
27. Print the first 10 multiples of 7, skip the multiples of 3 (use continue).
28. Print numbers 1–20 but stop when number reaches 13 (use break).
29. Use pass inside a loop.
30. Check if a number is prime (basic version).
6. Strings
31. Input a string and print its length.
32. Count how many vowels are in a given string.
33. Check if two strings are equal or not.
34. Print each character of a string on a new line.
35. Reverse a string without using slicing.
36. Check if a string is a palindrome.
37. Count uppercase and lowercase letters in a string.
38. Replace all spaces in a string with -.
39. Find the frequency of a particular character in a string.
40. Remove all vowels from a string.
7. String Functions
41. Input a name and print it in upper(), lower(), title(), swapcase().
42. Input a sentence and count the number of words (use split()).
43. Check whether a string starts with and ends with a specific substring.
44. Remove leading and trailing spaces from a string (strip()).
45. Find the index of a character in a string (index() or find()).
46. Replace a substring inside a sentence.
47. Join a list of words into a single string using join().
48. Check if a string contains only digits (isdigit()).
49. Check if a string contains only alphabets (isalpha()).
50. Count how many times "the" appears in a sentence (case-insensitive).
SOLUTIONS
1. Variables & Data Types
1. Print name, age & height with data types
name = input("Enter name: ")
age = int(input("Enter age: "))
height = float(input("Enter height: "))
print("Name:", name, type(name))
print("Age:", age, type(age))
print("Height:", height, type(height))
2. Swap two variables without using third variable
a = int(input("Enter a: "))
b = int(input("Enter b: "))
a, b = b, a
print("After swap:", a, b)
3. Check type of input
value = input("Enter something: ")
if [Link]():
print("Integer")
elif [Link]('.', '', 1).isdigit():
print("Float")
else:
print("String")
4. Area of circle
r = float(input("Enter radius: "))
area = 3.14159 * r * r
print("Area =", area)
5. Celsius → Fahrenheit
c = float(input("Enter Celsius: "))
f = (c * 9/5) + 32
print("Fahrenheit =", f)
2. Type Conversion
6. Sum, product & conversions
a = int(input("Enter a: "))
b = int(input("Enter b: "))
print("Sum =", a + b)
print("Product =", a * b)
print("Float a =", float(a))
print("String b =", str(b))
7. Float → int (data loss observation)
x = float(input("Enter float: "))
print("Before:", x)
print("After:", int(x))
8. Add integer + string after conversion
num = 10
text = "20"
result = num + int(text)
print("Sum =", result)
9. String → float → sqrt
import math
x = input("Enter number: ")
x = float(x)
print("Square root =", [Link](x))
3. Operators & Expressions
10. Even or odd
n = int(input("Enter number: "))
print("Even" if n % 2 == 0 else "Odd")
11. Simple & compound interest
p = float(input("Principal: "))
r = float(input("Rate: "))
t = float(input("Time: "))
si = (p * r * t) / 100
ci = p * ((1 + r/100)**t) - p
print("Simple Interest =", si)
print("Compound Interest =", ci)
12. Max of 3 numbers (without max())
a, b, c = map(int, input("Enter 3 numbers: ").split())
if a >= b and a >= c:
print("Max =", a)
elif b >= a and b >= c:
print("Max =", b)
else:
print("Max =", c)
13. Grade calculation
m1 = int(input("Marks1: "))
m2 = int(input("Marks2: "))
m3 = int(input("Marks3: "))
avg = (m1 + m2 + m3) / 3
if avg >= 90: grade = "A"
elif avg >= 75: grade = "B"
elif avg >= 60: grade = "C"
elif avg >= 33: grade = "D"
else: grade = "F"
print("Grade =", grade)
14. Divisible by 3 and 5
n = int(input("Enter number: "))
if n % 3 == 0 and n % 5 == 0:
print("Divisible by both")
else:
print("Not divisible")
4. Decision Control
15. Positive, negative or zero
n = int(input("Enter number: "))
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")
16. Pass/Fail
marks = int(input("Enter marks: "))
print("Pass" if marks >= 33 else "Fail")
17. Vowel or consonant
ch = input("Enter letter: ").lower()
if ch in "aeiou":
print("Vowel")
else:
print("Consonant")
18. Attendance eligibility
attendance = float(input("Enter attendance %: "))
if attendance >= 75:
print("Eligible")
else:
print("Not Eligible")
19. Number classification
n = int(input("Enter number: "))
if n < 10:
print("Small")
elif n < 100:
print("Medium")
else:
print("Large")
20. Menu-driven calculator
print("[Link] [Link] [Link] [Link]")
choice = int(input("Choose: "))
a = int(input("a = "))
b = int(input("b = "))
if choice == 1:
print(a + b)
elif choice == 2:
print(a - b)
elif choice == 3:
print(a * b)
elif choice == 4:
print(a / b)
else:
print("Invalid choice")
5. Loops
21. Print 1 to 50
for i in range(1, 51):
print(i)
22. Sum of first 100 natural numbers
s=0
for i in range(1, 101):
s += i
print("Sum =", s)
23. Multiplication table
n = int(input("Enter number: "))
for i in range(1, 11):
print(n, "*", i, "=", n*i)
24. Count positive numbers among 10 inputs
count = 0
for i in range(10):
n = int(input("Enter number: "))
if n > 0:
count += 1
print("Positive numbers =", count)
25. Even numbers 1–100
for i in range(2, 101, 2):
print(i)
26. Input until user enters 0
while True:
n = int(input("Enter number: "))
if n == 0:
break
27. First 10 multiples of 7, skip multiples of 3
count = 0
num = 1
while count < 10:
val = 7 * num
num += 1
if val % 3 == 0:
continue
print(val)
count += 1
28. Print 1–20 but stop at 13
for i in range(1, 21):
if i == 13:
break
print(i)
29. Program using pass
for i in range(5):
if i == 2:
pass
print(i)
30. Prime number check
n = int(input("Enter number: "))
flag = True
if n <= 1:
flag = False
else:
for i in range(2, n):
if n % i == 0:
flag = False
break
print("Prime" if flag else "Not prime")
6. Strings
31. Length of string
s = input("Enter string: ")
print("Length =", len(s))
32. Count vowels
s = input("Enter string: ").lower()
count = 0
for ch in s:
if ch in "aeiou":
count += 1
print("Vowels =", count)
33. Strings equal or not
a = input("Enter first: ")
b = input("Enter second: ")
print("Equal" if a == b else "Not equal")
34. Print each character on new line
s = input("Enter string: ")
for ch in s:
print(ch)
35. Reverse string (without slicing)
s = input("Enter string: ")
rev = ""
for ch in s:
rev = ch + rev
print("Reversed:", rev)
36. Palindrome check
s = input("Enter string: ")
if s == s[::-1]:
print("Palindrome")
else:
print("Not palindrome")
37. Count uppercase & lowercase
s = input("Enter string: ")
u=l=0
for ch in s:
if [Link]():
u += 1
elif [Link]():
l += 1
print("Uppercase:", u)
print("Lowercase:", l)
38. Replace spaces with -
s = input("Enter string: ")
print([Link](" ", "-"))
39. Frequency of a character
s = input("Enter string: ")
ch = input("Character to count: ")
print("Frequency =", [Link](ch))
40. Remove vowels
s = input("Enter string: ")
out = ""
for ch in s:
if [Link]() not in "aeiou":
out += ch
print("Without vowels:", out)
7. String Functions
41. upper(), lower(), title(), swapcase()
name = input("Enter name: ")
print([Link]())
print([Link]())
print([Link]())
print([Link]())
42. Count words
s = input("Enter sentence: ")
words = [Link]()
print("Word count =", len(words))
43. startswith() and endswith()
s = input("Enter text: ")
print([Link]("Hello"))
print([Link]("bye"))
44. strip()
s = input("Enter text with spaces: ")
print("After strip:", [Link]())
45. Find index of character
s = input("Enter string: ")
ch = input("Enter character: ")
print("Index =", [Link](ch))
46. Replace substring
s = input("Enter sentence: ")
old = input("Replace what? ")
new = input("Replace with? ")
print([Link](old, new))
47. Join list of words
words = ["Python", "is", "fun"]
print(" ".join(words))
48. Check if digits
s = input("Enter string: ")
print([Link]())
49. Check if alphabets
s = input("Enter string: ")
print([Link]())
50. Count "the" (case-insensitive)
s = input("Enter sentence: ").lower()
print("Count =", [Link]("the"))