Exercises
Below is a collection of exercises for the first chapter of the Python Fundamentals course.
Exercise 1: Arithmetic operations
What will this evaluate to?
2 + 5 - 3 * 4-5Exercise 2: Input function
Is the following statement true or false?
The
inputfunction reads a string, a number and a boolean from the user.
False, the input function reads a string from the user.
Exercise 3: Boolean conversion
What will this evaluate to?
bool("False")True, since "False" is a non-empty string.
Exercise 4: Variable type and value
What is the type and value of each variable?
a = str("Hello World")b = int(1)c = int(2.8)d = float(2)e = float()f = float("4.1")g = str(1)h = str(2.8)i = int("Hello World")j = complex(b, c)a: str = "Hello World"b: int = 1c: int = 2d: float = 2.0e: float = 0.0f: float = 4.1g: str = "1"h: str = "2.8"i: ValueError: invalid literal for int() with base 10: 'Hello World'j: complex = 1+2jExercise 5: Swap two numbers
Write a program which reads two numbers from the user and swaps them.
a = int(input("Enter the first number: "))b = int(input("Enter the second number: "))print("a =", a, "b =", b)t = a a = b b = tprint("a =", a, "b =", b)Exercise 6: Category
Write a program that reads a number n from the user then prints its category. Category A: if 0 <= n < 30 Category B: Otherwise!
n = int(input("Enter a number: "))if 0 <= n < 30: print("Category A")else: print("Category B")Exercise 7: Lucky number
Write a program which reads a four digit number from the user and checks if it is lucky, if so it prints L otherwise it prints U. A lucky number ABCD is such that A + B = C + D.
n = int(input("Enter a four digit number: "))D = n % 10;C = (n // 10) % 10;B = (n // 100) % 10;A = n // 1000;if A + B == C + D: print("L")else: print("U")Exercise 8: Maximum between three numbers
Write a program which reads three numbers from the user and prints the maximum between them.
a = int(input("Enter the first number: "))b = int(input("Enter the second number: "))c = int(input("Enter the third number: "))if a > b and a > c: print(a)elif b > a and b > c: print(b)else: print(c)A better approach would be:
a = int(input("Enter the first number: "))b = int(input("Enter the second number: "))c = int(input("Enter the third number: "))
#Suppose that the maximum is an = aif b > n:n = bif c > n:n = cprint(n)Exercise 9: Maximum between four numbers
Write a program which reads four numbers from the user and finds the maximum between them.
a = int(input("Enter the first number: "))b = int(input("Enter the second number: "))c = int(input("Enter the third number: "))d = int(input("Enter the fourth number: "))
#Suppose that the maximum is an = aif b > n:n = bif c > n:n = cif d > n:n = dprint(n)Exercise 10: Sort three numbers
Write a program that reads three integers a, b, c and then sorts them in ascending order a <= b <= c. Suppose that the user enters a = 5, b = 3 and c = 2. Then the numbers should become a = 2, b = 3, c = 5
a = int(input("Enter the first number: "))b = int(input("Enter the second number: "))c = int(input("Enter the third number: "))if a > b: t = a a = b b = tif a > c: t = a a = c c = tif b > c: t = b b = c c = tprint(a, b, c)Ready for your first quiz? Check out the next section.
