An Introduction to Programming

Python for All

Chapter Seven — Exercises

Thanasis Troboukis  ·  All Chapters

Chapter Seven

Exercises

Ten short exercises to strengthen what you learned in Chapter Five and Chapter Six.

01

For each value below, print the value and its type.

Hint: loop over the list and use type().

Python · Your solution

      
02

Convert the tuple below to a list, append one more city, and print both the list and its type.

Hint: use list() and .append().

Python · Your solution

      
03

Remove duplicates from this list of tags and print the unique tags.

Hint: convert list to set, then back to list if you want.

Python · Your solution

      
04

Create a dictionary from these key/value pairs and print the dictionary.

Hint: use dict() on the list of tuples.

Python · Your solution

      
05

Loop through the values and print whether each value is an int, float, string, or other.

Hint: use isinstance() with if/elif.

Python · Your solution

      
06

Compute the sum of numeric values only from the list below.

Hint: keep only values that are int or float using isinstance(v, (int, float)).

Python · Your solution

      
07

Update this dictionary by changing age, adding university, then print keys, values, and all key/value pairs.

Hint: use assignment or .update(), then .keys(), .values(), and .items().

Python · Your solution

      
08

Practice set methods: add one item, remove one item, safely discard one missing item, then print the set.

Hint: use .add(), .remove(), and .discard().

Python · Your solution

      
09

Create an integer array from the numbers below, convert it to a list with .tolist(), and print both types.

Hint: use from array import array and array("i", ...).

Python · Your solution

      
10

Mini challenge: from the list below, build a dictionary counting how many values belong to each type (int, float, str, other).

Hint: use a dictionary as counters and isinstance() inside a loop.

Python · Your solution

      

Chapter Navigation

Move between chapters.

Loading Python environment — this may take a moment…