Practice Sheet
ECE181 (Introduction to Python Programming)
Variables, Expressions, and Statements
1. Problem: Assign the value 5 to variable a and then print it.
2. Problem: Calculate the sum of two numbers a and b and store it in c.
3. Problem: Increment the variable count by 1.
4. Problem: Multiply two variables x and y and print the result.
5. Problem: Convert Celsius to Fahrenheit.
6. Problem: Assign the value of a to b and print both.
7. Problem: Calculate the area of a rectangle.
8. Problem: Find the remainder when x is divided by y.
9. Problem: Assign the sum of a, b, and c to total.
10. Problem: Swap the values of a and b.
Operator Precedence
11. Problem: Evaluate the expression 3 + 5 * 2.
12. Problem: Calculate the expression (3 + 4) * 5.
13. Problem: Compute the expression 2 + 3 * 4 ** 2.
14. Problem: Calculate 7 + 3 * 2 / 4 - 1.
15. Problem: Evaluate 5 * (2 + 3) ** 2.
16. Problem: Compute 4 + 3 ** 2 * 2.
17. Problem: Find the result of 8 / 2 * 3 + 1.
18. Problem: Evaluate (3 + 4) * (5 - 2).
19. Problem: Calculate 10 - 2 * 3 + 4.
20. Problem: Compute 2 * (3 + 4) / 2.
Conditionals (if-else)
21. Problem: Check if a number is positive.
o Partial Solution:
num = 5
if num > :
print("Positive")
(Missing value in the comparison)
22. Problem: Check if a number is even or odd.
o Partial Solution:
num = 7
if num % 2 == 0:
print("Even")
else:
print()
(Missing string in the else clause)
23. Problem: Check if a number is divisible by 5.
o Partial Solution:
num = 15
if num % 5 == 0:
print("Divisible by 5")
else:
print("Not divisible by 5")
24. Problem: Check if a number is positive, negative, or zero.
o Partial Solution:
num = -3
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
25. Problem: Find the largest of three numbers a, b, and c.
o Partial Solution:
a = 3
b = 7
c = 5
if a > b and a > :
print("a is the largest")
elif b > a and b > c:
print("b is the largest")
else:
print("c is the largest")
(Missing variable in the first condition)
26. Problem: Check if a number is a multiple of both 3 and 5.
o Partial Solution:
num = 15
if num % 3 == 0 and num % == 0:
print("Multiple of both 3 and 5")
else:
print("Not a multiple of both")
(Missing divisor in the second condition)
27. Problem: Determine if a year is a leap year.
o Partial Solution:
year = 2020
if year % 4 == 0 and (year % 100 != 0 or year % 400 == ):
print("Leap year")
else:
print("Not a leap year")
(Missing value in the second part of the or condition)
28. Problem: Check if a character is a vowel.
o Partial Solution:
char = 'a'
if char in ['a', 'e', 'i', 'o', 'u']:
print("Vowel")
else:
print("Not a vowel")
29. Problem: Find if a number is between 10 and 20 (inclusive).
o Partial Solution:
num = 15
if 10 <= num and num <= :
print("Between 10 and 20")
else:
print("Not between 10 and 20")
(Missing upper bound value)
30. Problem: Check if a string is empty.
o Partial Solution:
s = ""
if :
print("Empty string")
else:
print("Non-empty string")
(Missing condition to check emptiness)
Loops (for loop)
31. Problem: Print numbers from 1 to 5.
o Partial Solution:
for i in range(1, 5):
print(i)
(Range should be from 1 to 6 for inclusive printing of 5)
32. Problem: Print all even numbers between 1 and 10.
33. Problem: Calculate the sum of the first 10 natural numbers.
34. Problem: Print the multiplication table of 5 up to 10 times.
35. Problem: Count the number of vowels in a given string.
36. Problem: Find the factorial of a given number.
37. Problem: Print the squares of numbers from 1 to 5.
38. Problem: Print the first 10 numbers of the Fibonacci sequence.
39. Problem: Calculate the product of the first 5 odd numbers.
40. Problem: Print the sum of all elements in a list.
Loops (while loop)
41. Problem: Print numbers from 1 to 5 using a while loop.
42. Problem: Calculate the sum of digits of a given number.
43. Problem: Find the reverse of a given number.
44. Problem: Print the first 5 multiples of a given number.
45. Problem: Count the number of digits in a given number.
46. Problem: Print the factorial of a given number using a while loop.
47. Problem: Check if a number is a palindrome.
48. Problem: Calculate the power of a number without using the ** operator.
49. Problem: Print the sum of even numbers between 1 and 20.
50. Problem: Find the greatest common divisor (GCD) of two numbers using the
Euclidean algorithm.