Python Basics — Introduction Practice Exercises

1 minute read

Practice Exercises — Introduction

Test what you’ve learned in this chapter. Try each exercise on your own before checking the answer.


Exercise 1: Hello World

Write a Python program that prints Hello, World! to the screen.

Show Answer
print("Hello, World!")

Output:

Hello, World!

Exercise 2: Multi-Line Output

Write a program that prints your name, your city, and your favorite programming language — each on a separate line.

Show Answer
print("Name: Alice")
print("City: New York")
print("Favorite Language: Python")

Output:

Name: Alice
City: New York
Favorite Language: Python

Exercise 3: Simple Calculation

Write a program that calculates and prints the area of a rectangle with length 15 and width 8.

Show Answer
length = 15
width = 8
area = length * width

print("Length:", length)
print("Width:", width)
print("Area:", area)

Output:

Length: 15
Width: 8
Area: 120

Exercise 4: Python Version Check

Write a single line of Python code in the REPL that tells you what version of Python you’re running. (Hint: use the sys module.)

Show Answer
import sys
print(sys.version)

Output (example):

3.12.1 (main, Dec 7 2024, 00:00:00) [GCC 13.2.0]

Exercise 5: Zen of Python

Run the code that displays the Zen of Python. Which principle resonates with you the most?

Show Answer
import this

Output (first few lines):

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
...

There’s no right or wrong answer for which principle resonates most — this is about thinking like a Pythonista.


Scoring Guide

Score Level
5/5 Excellent — you’re ready for the next chapter!
3-4/5 Good — review the topics you missed
1-2/5 Re-read the chapter and try again

Updated: