Introduction to programming using Python

Session 9

Matthieu Choplin

Objectives: recap

  • Quiz
  • Exercises


What is a variable in your words?


What are the variables in that script?

print('Hello world')
students = ['Jack', 'Adam', 'Brad']
for s in students:
    print('Hello', s)

Modulo operator %

# first block
if number % 2 == 0:
    # mind the indentation!
    print('The number is even')
# 2nd block
    # an other block (indented!)
    print('The numver is odd')


Is producing a sequence of integers (whole numbers) on which we can iterate

>>> print(range(5))
range(0, 5)
>>> print(list(range(5)))
[0, 1, 2, 3, 4]
>>> for i in range(3):
...     print(i)

EX3: reminder of %, conditional statements and range combined

Given an integer, n, perform the following conditional actions:

  • If n is odd, print Weird
  • If n is even and in the inclusive range of 2 to 5, print Not Weird
  • If n is even and in the inclusive range of 6 to 20, print Weird
  • If n is even and greater than 20, print Not Weird
n = int(input())

Q3: Loops

What are the 2 ways of creating loops in python?


How would you make this code more concise (1)? And more flexible if for example the program can take any number of sales (2)? Think DRY!

sale_1 = int(input('Enter sale price 1: '))
sale_2 = int(input('Enter sale price 2: '))
sale_3 = int(input('sale price 3: '))
print('The average sale was ', (sale_1 + sale_2 + sale_3) / 3)

Q5: functions

Is the program calling the function here?

def hello():

a = hello

Think YAGNI!

EX5: writing functions (1)

You are given the year, and you have to write a function to check if the year is leap or not.

def is_leap(year):
    leap = False
    # Write your logic here: year divisible by 400 or year NOT divisible by 100 and divisible by 4
    return leap

year = int(input())

Can you find on google which python builtin module you could use to say if a year is a leap year?

EX6: writing functions (2)

The program reads a letter of the alphabet from the user. If the user enters a, e, i, o or u then your program should display a message indicating that the entered letter is a vowel.

Define a function and call it when the program is run

letter = input('Enter a letter')
# your code

Q7: libraries and modules

What is a python module?

What do we have to do before being able to use a library or a python module?

EX7: more functions (3)

Ask a user to enter 1 if he wants to flip a coin or 2 if he wants to roll a dice and print the number obtained or 'tail' or 'face'

Create one function for each choice. Use the random library (use the keyword import)

# import libraries at the top of the program
# write your functions here
choice = int(input('Enter 1 to flip a coin or 2 to roll a dice'))
if choice == 1:
    pass  # replace with your code
    pass  #replace with your code

Class and Type

What are the simple builtin class in python?

What are the container types?


What is an object in your own words?

How would you create a string object?

How would you create a list object?

How would you create a custom object?

EX8: creating classroom object

Create 3 classrooms objects that have each a name, a schedule and a building.

Comparing list with dictionaries

How do you add/access element to a list?

How do you add/access element to a dictionary?

EX9: finding the 2nd largest item of a list

Suppose you have the following as input:

4 7 9 1 3 2 5

Write a program that gives the 2nd largest item:

Hint: input is returning a string, to transform a string to a list, use split()

Define and call a function

Ex10: dictionary and json

Create a currency converter using the api

Install the requests library

import json
import requests
r = requests.get('')
d = json.loads(r.content)
# your code here:

Ex11: file writing

Using the api, create the file that was used in the exercise of the session 7 (guess capital)

Use the deserialize data from the api and write them in a file called 'countries'.

Instead of using urllib, can you rewrite the program using the requests library?

import json
from urllib import request

infile = request.urlopen(
content_as_python_obj = json.loads(
# to complete: create the file

Websites and apps to practice