An Introduction to Programming

Python for All

Chapter Two — Exercises

Thanasis Troboukis  ·  All Chapters

Chapter Two

Exercises

Write the code yourself. Each cell below is empty — type your solution and press Run to check it.

01

Print the text Hello, World! to the screen.

Hint: use the print() function with the text inside quotation marks.

Python · Your solution

      
02

Print your name, your age, and where you are from — each on its own line.

Hint: store each piece of information in a variable, then use f-strings to print them.

Python · Your solution

      
03

Your teacher Thanasis was born in 1980. If you are younger than him, print I am younger than Thanasis by X years. If you are older, print I am older than Thanasis by X years.

Hint: define your own birth year as a variable, then compare it to 1980 using an if/else statement.

Python · Your solution

      
04

Print the sentence: When I was born, Thanasis was X years old. Calculate X from the birth years.

Hint: subtract Thanasis's birth year (1980) from your own birth year to find the age difference.

Python · Your solution

      
05

Create a list containing the names of five of your best friends, then print the whole list.

Hint: use square brackets and separate each name with a comma.

Python · Your solution

      
06

Using the list from Exercise 5, loop over it and print each friend's name on its own line, like: My friend: Maria.

Hint: use a for loop and an f-string inside it.

Python · Your solution

      
07

Create a dictionary of Greek Prime Ministers and the year each first took office, using the data below. Then loop through it and print only the ones who took office after the year you were born.

The data to enter into your dictionary:

Prime Minister Year took office
Konstantinos Karamanlis1974
Georgios Rallis1980
Andreas Papandreou1981
Konstantinos Mitsotakis1990
Kostas Simitis1996
Kostas Karamanlis2004
Giorgos Papandreou2009
Loukas Papadimos2011
Antonis Samaras2012
Alexis Tsipras2015
Kyriakos Mitsotakis2019
Python · Your solution

      

Chapter Navigation

Move between chapters.

Loading Python environment — this may take a moment…