Skip to content

Foundations Exercises (Modules 1-9)

Module 2: First Programs

Exercise 1: Input and output

Write a script that asks for a user's name and favorite number, then prints a sentence combining both values.

Hint

Use input() twice and an f-string for output.

One possible solution
name = input("Name: ")
number = input("Favorite number: ")
print(f"{name} likes {number}.")

Module 4: Control Flow

Exercise 2: Branching

Given an integer, print positive, negative, or zero.

Hint

Use if/elif/else in that order.

One possible solution
n = int(input("n: "))
if n > 0:
    print("positive")
elif n < 0:
    print("negative")
else:
    print("zero")

Module 8: Testing

Exercise 3: Simple pytest test

Write a function is_even(n) and test it with pytest.

Hint

Create two assertions: one true case and one false case.

One possible solution
def is_even(n: int) -> bool:
    return n % 2 == 0


def test_is_even():
    assert is_even(4)
    assert not is_even(5)