Chapter 1 Exercises

Wrap up the first chapter with some exercises.

Ali Berro

By Ali Berro

5 min read Section 6
From: Python Fundamentals: From Zero to Hero

Table of Contents

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
Answer:
-5

Exercise 2: Input function

Is the following statement true or false?

The input function reads a string, a number and a boolean from the user.

Answer:

False, the input function reads a string from the user.

Exercise 3: Boolean conversion

What will this evaluate to?

bool("False")
Answer:

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)
Answer:
a: str = "Hello World"
b: int = 1
c: int = 2
d: float = 2.0
e: float = 0.0
f: float = 4.1
g: str = "1"
h: str = "2.8"
i: ValueError: invalid literal for int() with base 10: 'Hello World'
j: complex = 1+2j

Exercise 5: Swap two numbers

Write a program which reads two numbers from the user and swaps them.

Answer:
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
print("a =", a, "b =", b)
t = a
a = b
b = t
print("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!

Answer:
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.

Answer:
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.

Answer:
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 a
n = a
if b > n:
n = b
if c > n:
n = c
print(n)

Exercise 9: Maximum between four numbers

Write a program which reads four numbers from the user and finds the maximum between them.

Answer:
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 a
n = a
if b > n:
n = b
if c > n:
n = c
if d > n:
n = d
print(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

Answer:
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 = t
if a > c:
t = a
a = c
c = t
if b > c:
t = b
b = c
c = t
print(a, b, c)

Ready for your first quiz? Check out the next section.

Course Progress

Section 8 of 17

Back to Course