An Introduction to Programming

Python for All

Chapter Four — Exercises

Thanasis Troboukis  ·  All Chapters

Chapter Four

Exercises

These exercises combine what you learned in Chapter One and Chapter Three. Read each prompt carefully before you start typing.

01

You are given a messy list of names. Clean each one up — remove surrounding spaces and make it title case (first letter uppercase, rest lowercase) — then print the cleaned list.

Hint: use .strip() to remove spaces and .title() to fix capitalisation. Build a new list with a for loop and .append().

Python · Your solution

      
02

Given the list of names below, find and print the longest name and how many characters it has.

Hint: loop over the list, compare len() of each name to a running maximum, and keep track of which name is longest.

Python · Your solution

      
03

Count how many names in the list below start with the letter A, and print the result.

Hint: use a counter variable, a for loop, and .startswith() inside an if statement.

Python · Your solution

      
04

The list below contains numbers stored as text strings. Convert each one to an integer, then print their total and their average rounded to one decimal place.

Hint: use int() to convert each string inside a for loop, append the results to a new list, then use sum() and len() to calculate total and average.

Python · Your solution

      
05

Using the Greek Prime Ministers dictionary from Chapter Two (Exercise 7), loop through it and print only the Prime Ministers whose name starts with the letter K, along with the year they took office.

Hint: combine .items() to loop the dictionary, .startswith("K") to filter, and an f-string to format the output.

Python · Your solution

      

Chapter Navigation

Move between chapters.

Loading Python environment — this may take a moment…