Python basics

Download exercises zip

Browse online files

PREREQUISITES:

Jupyter

Jupyter is an editor that allows working on so called notebooks, which are files ending with the extension .ipynb. They are documents divided in cells where for each cell you can insert commands and immediately see the respective output. Let’s try to open this.

  1. Unzip exercises zip in a folder, you should obtain something like this:

basics
    basics1-ints.ipynb
    basics1-ints-sol.ipynb
    basics2-bools.ipynb
    basics2-bools-sol.ipynb
    basics3-floats.ipynb
    basics3-floats-sol.ipynb
    basics4-chal.ipynb
    jupman.py

WARNING: to correctly visualize the notebook, it MUST be in an unzipped folder !

  1. open Jupyter Notebook. Two things should appear, first a console and then a browser. In the browser navigate the files to reach the unzipped folder, and open the notebook basics1-ints.ipynb

WARNING: open the notebook WITHOUT the -sol at the end!

Seeing now the solutions is too easy ;-)

  1. Go on reading the exercises file, sometimes you will find paragraphs marked Exercises which will ask to write Python commands in the following cells.

WARNING: In this book we use ONLY PYTHON 3

If you obtain weird behaviours, check you are using Python 3 and not 2. If by typing python your operating system runs python 2, try executing python3

Shortcut keys:

  • to execute Python code inside a Jupyter cell, press Control + Enter

  • to execute Python code inside a Jupyter cell AND select next cell, press Shift + Enter

  • to execute Python code inside a Jupyter cell AND a create a new cell aftwerwards, press Alt + Enter

  • If the notebooks look stuck, try to select Kernel -> Restart

Objects

In Python everything is an object. Objects have properties (fields where to save values) and methods (things they can do). For example, an object car has the properties model, brand, color, numer of doors, etc … and the methods turn right, turn left, accelerate, brake, shift gear …

According to Python official documentation:

"Objects are Python’s abstraction for data. All data in a Python program is represented by objects or by relations between objects."

For now it’s enough to know that Python objects have an identifier (like, their name), a type (numbers, text, collections, …) and a value (the actual value represented by objects). Once the object has been created the identifier and the type never change, while the value may change (mutable objects) or remain constant (immutable objects).

Python provides these predefined types ( built-in ):

Type

Meaning

Domain

Mutable?

bool

Condition

True, False

no

int

Integer

\(\mathbb{Z}\)

no

long

Integer

\(\mathbb{Z}\)

no

float

Rational

\(\mathbb{Q}\) (more or less)

no

str

Text

Text

no

list

Sequence

Collezione di oggetti

yes

tuple

Sequence

Collezione di oggetti

no

set

Set

Collezione di oggetti

yes

dict

Mapping

Mapping between objects

yes

For now we will consider only the simplest ones, later in the book we will deep dive in each of them.

Variables

Variables are associations among names and objects (we can call them values).

Variables can be associated, or in a more technical term, assigned to objects by using the assignment operator =.

The instruction

[2]:
diamonds = 4

may represent how many precious stones we keed in the safe. What happens when we execute it in Python?

  • an object is created

  • its type is set to int (an integer number)

  • its value is set to 4

  • a name diamonds is create in the environment and assigned to that object

Detect the type of a variable

When you see a variable or costant and you wonder what type it could have, you can use the predefined function type:

[3]:
type(diamonds)
[3]:
int
[4]:
type(4)
[4]:
int
[5]:
type(4.0)
[5]:
float
[6]:
type("Hello")
[6]:
str

Reassign a variable

Consider now the following code:

[7]:
diamonds = 4

print(diamonds)
4
[8]:
diamonds = 5

print(diamonds)
5

The value of diamonds variable has been changed from 4 to 5, but as reported in the previous table, the int type is immutable. Luckily, this didn’t prevent us from changing the value diamonds from 4 to 5. What happend behind the scenes? When we executed the instructions diamonds = 5, a new object of type int was created (the integer 5) and made available with the same name diamonds

Reusing a variable

When you reassign a variable to another value, to calculate the new value you can freely reuse the old value of the variable you want to change. For example, suppose to have the variable

[9]:
flowers = 4

and you want to augment the number of flowers by one. You can write like this:

[10]:
flowers = flowers + 1

What happened? When Python encounters a command with =, FIRST it calculates the value of the expression it finds to the right of the =, and THEN assigns that value to the variable to the left of the =.

Given this order, FIRST in the expression on the right the old value is used (in this case 4) and 1 is summed so to obtain 5 which is THEN assigned to flowers.

[11]:
flowers
[11]:
5

In a completely equivalent manner, we could rewrite the code like this, using a helper variable x. Let’s try it in Python Tutor:

[12]:
# WARNING: to use the following jupman.pytut() function,
# it is necessary first execute  this cell with Shift+Enter

# it's enough to execute once, you can also find in all notebooks in the first cell.

import jupman
[13]:

flowers = 4

x = flowers + 1

flowers = x

jupman.pytut()
[13]:

You can execute a sum and do an assignment at the same time with the += notation

[14]:
flowers = 4
flowers += 1
print(flowers)
5

This notation is also valid for other arithetic operators:

[15]:
flowers = 5
flowers -= 1     # subtraction
print(flowers)
4
[16]:
flowers *= 3     # multiplication
print(flowers)
12
[17]:
flowers /= 2     # division
print(flowers)
6.0

Assignments - questions

QUESTION: Look at the following questions, and for each try to guess the result it produces (or if it gives an error). Try to verify your guess both in Jupyter and in another editor of .py files like Spyder:

  1. x = 1
    x
    x
    
  2. x = 1
    x = 2
    print(x)
    
  3. x = 1
    x = 2
    x
    
  4. x = 1
    print(x)
    x = 2
    print(x)
    
  5. print(zam)
    print(zam)
    zam = 1
    zam = 2
    
  6. x = 5
    print(x,x)
    
  7. x = 5
    print(x)
    print(x)
    
  8. x = 3
    print(x,x*x,x**x)
    
  9. 3 + 5 = x
    print(x)
    
  10. 3 + x = 1
    print(x)
    
  11. x + 3 = 2
    print(x)
    
  12. x = 2
    x =+ 1
    print(x)
    
  13. x = 2
    x = +1
    print(x)
    
  14. x = 2
    x += 1
    print(x)
    
  15. x = 3
    x *= 2
    print(x)
    

Exercise - exchange

✪ Given two variables a and b:

a = 5
b = 3

write some code that exchanges the two values, so that after your code it must result

>>> print(a)
3
>>> print(b)
5
  • are two variables enough? If they aren’t, try introducing a third one.

Show solution
[18]:

a = 5
b = 3

# write here


Exercise - cycling

✪ Write a program that given three variables with numebers a,b,c, cycles the values, that is, puts the value of a in b, the value of b in c, and the value of c in a .

So if you begin like this:

a = 4
b = 7
c = 9

After the code that you will write, by running this:

print(a)
print(b)
print(c)

You should see:

9
4
7

There are various ways to do it, try to use only one temporary variable and be careful not to lose values !

HINT: to help yourself, try to write down in comments the state of the memory, and think which command to do

# a b c t    which command do I need?
# 4 7 9
# 4 7 9 7    t = b
#
#
#
[19]:
a = 4
b = 7
c = 9

# write code here

print(a)
print(b)
print(c)
4
7
9
Show solution
[20]:

9
4
7

Changing type during execution

You can also change the type of a variable duting the program execution but normally it is a bad habit because it makes harder to understand the code, and increases the probability to commit errors. Let’s make an example:

[21]:
diamonds = 4          # integer
[22]:
diamonds + 2
[22]:
6
[23]:
diamonds = "four"  # text

Now that diamonds became text, if by mistake we try to treat it as if it were a number we will get an error !!

diamonds + 2

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-6124a47997d7> in <module>
----> 1 diamonds + 2

TypeError: can only concatenate str (not "int") to str

Multiple commands on the same line

It is possible to put many commands on the same line (non only assignments) by separating them with a semi-colon ;

[24]:
a = 10; print('So many!'); b = a + 1;
So many!
[25]:
print(a,b)
10 11

NOTE: multiple commands on the same line are ‘not much pythonic’

Even if sometimes they may be useful and less verbose of explicit definitions, they are a style frowned upon by true Python ninjas.

Multiple initializations

Another thing are multiple initializations, separated by a comma , like:

[26]:
x,y = 5,7
[27]:
print(x)
5
[28]:
print(y)
7

Differently from multiple commands, multiple assignments are a more acceptable style.

Exercise - exchange like a ninja

✪ Try now to exchange the value of the two variables a and b in one row with multiple initialization

a,b = 5,3

After your code, it must result

>>> print(a)
3
>>> print(b)
5
Show solution
[29]:
a,b = 5,3

# write here


Names of variables

IMPORTANT NOTE:

You can chose the name that you like for your variables (we advise to pick something reminding their meaning), but you need to adhere to some simple rules:

  1. Names can only contain upper/lower case digits (A-Z, a-z), numbers (0-9) or underscores _;

  2. Names cannot start with a number;

  3. Variable names should start with a lowercase letter

  4. Names cannot be equal to reserved keywords:

Reserved words:

and

as

assert

break

class

continue

def

del

elif

else

except

exec

finally

for

from

global

if

import

in

is

lambda

nonlocal

not

or

pass

raise

return

try

while

with

yield

True

False

None

system functions: beyond reserved words (which are impossible to redefine), Python also offers several predefined system function:

  • bool, int,float,tuple,str,list,set,dict

  • max, min, sum

  • next, iter

  • id, dir, vars,help

Sadly, Python allows careless people to redefine them, but we do not:

V COMMANDMENT: You shall never ever redefine system functions

Never declare variables with such names !

Names of variables - questions

For each of the following names, try to guess if it is a valid variable name or not, then try to assign it in following cell

  1. my-variable

  2. my_variable

  3. theCount

  4. the count

  5. some@var

  6. MacDonald

  7. 7channel

  8. channel7

  9. stand.by

  10. channel45

  11. maybe3maybe

  12. "ciao"

  13. 'hello'

  14. as PLEASE: DO UNDERSTAND THE VERY IMPORTANT DIFFERENCE BETWEEN THIS AND FOLLOWING TWOs !!!

  15. asino

  16. As

  17. lista PLEASE: DO UNDERSTAND THE VERY IMPORTANT DIFFERENCE BETWEEN THIS AND FOLLOWING TWOs !!!

  18. list DO NOT EVEN TRY TO ASSIGN THIS ONE IN THE INTERPRETER (like list = 5), IF YOU DO YOU WILL BASICALLY BREAK PYTHON

  19. List

  20. black&decker

  21. black & decker

  22. glab()

  23. caffè (notice the accented è !)

  24. ):-]

  25. €zone (notice the euro sign)

  26. some:pasta

  27. aren'tyouboredyet

  28. <angular>

[30]:
# write the names here

Numerical types

We already mentioned that numbers are immutable objects. Python provides different numerical types:

integers (int), reals (float), booleans, fractions and complex numbers.

It is possible to make arithmetic operations with the following operators, in precedence order:

Operator

Description

**

power

+ -

Unary plus and minus

* / // %

Multiplication, division, integer division, module

+ -

Addition and subtraction

There are also several predefined functions:

Function

Description

min(x,y, ...)

the minimum among given numbers

max(x,y, ...)

the maximum among given numbers

abs(x)

the absolute value

Others are available in the math module (remember that in order to use them you must first import the module math by typing import math):

Function

Description

math.floor(x)

round x to inferior integer

math.ceil(x)

round x to superior integer

math.sqrt(x)

the square root

math.log(x)

the natural logarithm of n

math.log(x,b)

the logarithm of n in base b

… plus many others we don’t report here.

Integer numbers

The range of values that integer can have is only limited by available memory. To work with numbers, Python also provides these operators:

[31]:
7 + 4
[31]:
11
[32]:
7 - 4
[32]:
3
[33]:
7 // 4
[33]:
1

NOTE: the following division among integers produces a float result, which uses a dot as separator for the decimals (we will see more details later):

[34]:
7 / 4
[34]:
1.75
[35]:
type(7 / 4)
[35]:
float
[36]:
7 * 4
[36]:
28

NOTE: in many programming languages the power operation is denoted with the cap ^, but in Python it is denoted with double asterisk **:

[37]:
7 ** 4   # power
[37]:
2401

Exercise - deadline 1

✪ You are given a very important deadline in:

[38]:
days = 4
hours = 13
minutes = 52

Write some code that prints the total minutes. By executing it, it should result:

In total there are 6592 minutes left.
Show solution
[39]:

days = 4
hours = 13
minutes = 52

# write here


Modulo operator

To find the reminder of a division among integers, we can use the modulo operator which is denoted with %:

[40]:
5 % 3  # 5 divided by 3 gives 2 as reminder
[40]:
2
[41]:
5 % 4
[41]:
1
[42]:
5 % 5
[42]:
0
[43]:
5 % 6
[43]:
5
[44]:
5 % 7
[44]:
5

Exercise - deadline 2

✪ For another super important deadline there are left:

tot_minutes = 5000

Write some code that prints:

There are left:
   3 days
   11 hours
   20 minutes
Show solution
[45]:

tot_minutes = 5000

# write here


min and max

The minimum among two numbers can be calculated with the function min:

[46]:
min(7,3)
[46]:
3

and the maximum with the function max:

[47]:
max(2,6)
[47]:
6

To min and max we can pass an arbitrary number of parameters, even negatives:

[48]:
min(2,9,-3,5)
[48]:
-3
[49]:
max(2,9,-3,5)
[49]:
9

V COMMANDMENT: You shall never ever redefine system functions like min and max

If you use min and max like they were variables, the corresponding functions will literally stop to work!

min = 4   # NOOOO !
max = 7   # DON'T DO IT !

QUESTION: given two numbers a and b, which of the following expressions are equivalent?

1. max(a,b)
2. max(min(a,b),b)
3. -min(-a,-b)
4. -max(-a,-b)
Show answer

Exercise - transportation

✪✪ A company has a truck that every day delivers products to its best client. The truck can at most transport 10 tons of material. Unfortunately, the roads it can drive through have bridges that limit the maximum weight a vehicle can have to pass. These limits are provided in 5 variables:

b1,b2,b3,b4,b5 = 7,2,4,3,6

The truck must always go through the bridge b1, then along the journey there are three possible itineraries available:

  • In the first itinerary, the truck also drives through bridge b2

  • In the second itinerary, the truck also drives through bridges b3 and b4

  • In the third itinerary, the truck also drives though bridge b5

The company wants to know which are the maximum tons it can drive to destination in a sngle journey. Write some code to print this number.

NOTE: we do not want to know which is the best itinerary, we only need to find the greatest number of tons to ship.

Example - given:

b1,b2,b3,b4,b5 = 7,2,4,6,3

your code must print:

In a single journey we can transport at most 4 tons.
Show solution
[50]:
b1,b2,b3,b4,b5 = 7,2,4,6,3   # 4
#b1,b2,b3,b4,b5 = 2,6,2,4,5  # 2
#b1,b2,b3,b4,b5 = 8,6,2,9,5  # 6
#b1,b2,b3,b4,b5 = 8,9,9,4,7  # 8


# write here


In a single journey we can transport at most 4 tons

Exercise - armchairs

✪✪ The tycoon De Industrionis owns two factories of armchairs, one in Belluno city and one in Rovigo. To make an armchair three main components are needed: a mattress, a seatback and a cover. Each factory produces all required components, taking a certain time to produce each component:

[51]:
b_mat, b_bac, b_cov, r_mat, r_bac, r_cov = 23,54,12,13,37,24

Belluno takes 23h to produce a mattress, 54h the seatcback and 12h the cover. Rovigo, respectively, takes 13, 37 and 24 hours. When the 3 components are ready, assembling them in the finished armchair requires one hour.

Sometimes peculiar requests are made by filthy rich nobles, that pretends to be shipped in a few hours armchairs with extravagant like seatback in solid platinum and other nonsense.

If the two factories start producting the components at the same time, De Industrionis wants to know in how much time the first armchair will be produced. Write some code to calculate that number.

  • NOTE 1: we are not interested in which factory will produce the armchair, we just want to know the shortest time in which we will get an armchair

  • NOTE 2: suppose both factories don’t have components in store

  • NOTE 3: the two factories do not exchange components

Example 1 - given:

b_mat, b_bac, b_cov, r_mat, r_bac, r_cov = 23,54,12,13,37,24

your code must print:

The first armchair will be produced in 38 hours.

Example 2 - given:

b_mat, b_bac, b_cov, r_mat, r_bac, r_cov = 81,37,32,54,36,91

your code must print:

The first armchair will be produced in 82 hours.
Show solution
[52]:

b_mat, b_bac, b_cov, r_mat, r_bac, r_cov = 23,54,12,13,37,24   # 38
#b_mat, b_bac, b_cov, r_mat, r_bac, r_cov = 81,37,32,54,36,91  # 82
#b_mat, b_bac, b_cov, r_mat, r_bac, r_cov = 21,39,47,54,36,91  # 48

# write here


The first armchair will be produced in 38 hours.

Continue

Go on with Basics 2: Booleans