PYTHON PROGRAMMING PRACTICE WORKSHEET
Topics: Theory Questions & Programming
SECTION A: SHORT ANSWER QUESTIONS (2 Marks Each) - 10 Questions
1. What is the difference between a list and a string in Python? Give one example of each.
2. Explain the difference between // (floor division) and / (division) operators in Python with suitable
examples.
3. What is the difference between the break statement and the continue statement? Provide one example for
each.
4. Predict the output of the following code:
python
message = "Python is easy"
print([Link]("is"))
print([Link]("e"))
5. Write any two arithmetic operators and any two logical operators in Python with examples for each.
6. Identify the correct output(s) of the following code. Also write the minimum and maximum possible values
of the variable x .
python
import random
word = "CODING"
x = [Link](2, 5)
for i in range(1, x, 2):
print(word[i], end='@')
(A) O@ (B) O@D@ (C) O@D@N@ (D) C@D@
7. What are the two types of type conversion in Python? Explain with examples.
8. Observe the following code and rewrite it after removing all syntax and logical errors. Underline all
corrections made.
python
number = input("Enter a number: ")
if number % 3 = 0:
print("Divisible by 3")
else:
print("Not divisible by 3")
9. Give two examples each of:
(i) Comparison operators
(ii) Logical operators
10. Predict the output:
python
num = 5
while num > 0:
num = num - 2
if num == 1:
break
print(num, end=" ")
SECTION B: PROGRAMMING QUESTIONS (3 Marks Each) - 5 Questions
11. Write a Python program to check whether a given string is a palindrome or not.
12. Write a Python program to display the following pattern for n rows:
1
12
123
1234
12345
13. Write a Python program to find the factorial of a given number using a while loop.
14. Write a Python program to accept a number from the user and print all even numbers from 1 to that number.
15. Write a Python program to find the sum of digits of a given number.
SECTION C: PROGRAMMING QUESTIONS (4 Marks Each) - 5 Questions
16. Write a Python program to find the sum of all odd numbers from 1 to n (where n is input by the user).
17. Write a Python program to check whether a given number is a prime number or not.
18. Write a Python program to display the multiplication table of a number entered by the user.
19. Write a Python program to find the largest of three numbers entered by the user.
20. Write a Python program to reverse a number using a while loop (e.g., input: 1234, output: 4321).
SECTION D: PROGRAMMING QUESTIONS (5 Marks Each) - 5 Questions
21. Write a Python program to:
Accept a string from the user
Count and display:
Number of digits
Number of alphabets
Number of spaces
Number of special characters
22. Write a Python program to display the following number pattern:
1
23
456
7 8 9 10
11 12 13 14 15
(Accept number of rows from user)
23. Write a Python program to check whether a given number is a Perfect number or not. (A perfect number is
a positive integer that is equal to the sum of its proper divisors. Example: 6 = 1 + 2 + 3)
24. Write a Python program to:
Accept a sentence from the user
Display the sentence with:
First letter of each word capitalized
Count of words starting with a vowel
Count of words starting with a consonant
25. Write a Python program to generate and display the Fibonacci series up to n terms (where n is input by the
user). Example: 0, 1, 1, 2, 3, 5, 8, 13, 21...
ANSWER GUIDELINES
1:
String: Immutable (cannot be changed), stores sequence of characters, uses quotes " " or ' '
Example: my_string = "Hello"
List: Mutable (can be changed), stores collection of items, uses square brackets []
Example: my_list = [1, 2, 3, "Hello"]
2:
/ gives floating-point result: 7 / 2 = 3.5
// gives integer result (floor division): 7 // 2 = 3
3:
break: Exits the loop completely
python
for i in range(5):
if i == 3:
break
print(i) # Output: 0 1 2
continue: Skips current iteration, continues with next
python
for i in range(5):
if i == 3:
continue
print(i) # Output: 0 1 2 4
4:
('Python ', 'is', ' easy')
2
5:
Arithmetic Operators:
Addition (+): x = 5 + 3 (Result: 8)
Multiplication (*): x = 4 * 2 (Result: 8)
Logical Operators:
and: x = (5 > 3) and (10 > 7) (Result: True)
or: x = (5 > 10) or (8 > 3) (Result: True)
6:
Correct outputs: (A) and (B)
Minimum value of x: 2
Maximum value of x: 5
7:
Implicit Type Conversion: Automatically done by Python
Example: x = 5 + 2.5 (int + float = float)
Explicit Type Conversion: Done by programmer using functions
Example: x = int("10") (string to integer)
8:
python
number = int(input("Enter a number: "))
if number % 3 == 0:
print("Divisible by 3")
else:
print("Not divisible by 3")
Corrections: Convert input to int, use == instead of = , add indentation
9:
Comparison operators: > , < , >= , <= , == , !=
Logical operators: and , or , not
10:
3
PROGRAMMING SOLUTIONS (Sample Code)
11: Palindrome Check
python
string = input("Enter a string: ")
if string == string[::-1]:
print("Palindrome")
else:
print("Not a Palindrome")
12: Number Pattern
python
n = int(input("Enter number of rows: "))
for i in range(1, n+1):
for j in range(1, i+1):
print(j, end="")
print()
13: Factorial
python
num = int(input("Enter a number: "))
factorial = 1
i=1
while i <= num:
factorial = factorial * i
i=i+1
print("Factorial:", factorial)
14: Even Numbers
python
n = int(input("Enter a number: "))
for i in range(2, n+1, 2):
print(i, end=" ")
15: Sum of Digits
python
num = int(input("Enter a number: "))
sum_digits = 0
while num > 0:
digit = num % 10
sum_digits = sum_digits + digit
num = num // 10
print("Sum of digits:", sum_digits)
16: Sum of Odd Numbers
python
n = int(input("Enter a number: "))
sum_odd = 0
for i in range(1, n+1, 2):
sum_odd = sum_odd + i
print("Sum of odd numbers:", sum_odd)
17: Prime Number Check
python
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not a Prime number")
break
else:
print("Prime number")
else:
print("Not a Prime number")
18: Multiplication Table
python
num = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
19: Largest of Three Numbers
python
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a > b and a > c:
print("Largest:", a)
elif b > c:
print("Largest:", b)
else:
print("Largest:", c)
20: Reverse a Number
python
num = int(input("Enter a number: "))
reverse = 0
while num > 0:
digit = num % 10
reverse = reverse * 10 + digit
num = num // 10
print("Reversed number:", reverse)
21: Character Analysis
python
string = input("Enter a string: ")
digits = alphabets = spaces = special = 0
for char in string:
if [Link]():
digits += 1
elif [Link]():
alphabets += 1
elif [Link]():
spaces += 1
else:
special += 1
print("Digits:", digits)
print("Alphabets:", alphabets)
print("Spaces:", spaces)
print("Special characters:", special)
22: Continuous Number Pattern
python
n = int(input("Enter number of rows: "))
num = 1
for i in range(1, n + 1):
for j in range(1, i + 1):
print(num, end=" ")
num = num + 1
print()
23: Perfect Number
python
num = int(input("Enter a number: "))
sum_divisors = 0
for i in range(1, num):
if num % i == 0:
sum_divisors += i
if sum_divisors == num:
print("Perfect number")
else:
print("Not a perfect number")
24: Sentence Analysis
python
sentence = input("Enter a sentence: ")
words = [Link]()
vowel_count = 0
consonant_count = 0
vowels = "aeiouAEIOU"
# Capitalize first letter of each word using title()
print("Capitalized:", [Link]())
# Count words starting with vowel or consonant
for word in words:
if word[0] in vowels:
vowel_count += 1
elif word[0].isalpha():
consonant_count += 1
print("Words starting with vowel:", vowel_count)
print("Words starting with consonant:", consonant_count)
25: Fibonacci Series
python
n = int(input("Enter number of terms: "))
a, b = 0, 1
count = 0
while count < n:
print(a, end=" ")
a, b = b, a + b
count += 1
Practice regularly to master these concepts!