Exercises (raw input)¶
Using
raw_input()
, ask the user for his/her favourite food, store the result in a variablefood
, then print to screen:I also like
and the favourite food.Ask the user for two integers, let’s say
a
andb
, then a third integer, let’s sayresult
.Check whether the sum of
a
andb
is equal toresult
: if so, print to screenTrue
, otherwiseFalse
.Ask the user for a key and a value, and build a dictionary including (only) that key-value pair. Print to screen the result.
Ask the user for his/her name, store it in the variable
name
, then print to screen the name, making sure that all the words contained inname
are in lower case, except for the first characters, that must be in upper case.
Exercises (Filesystem)¶
Warning
If open()
raises an error, maybe you are in the wrong directory: fix the path in order to be in the right directory.
Use
open()
to open the file"data/aatable"
in read-only mode, storing the result ofopen()
in the variablef
.Next, use
readlines()
to read the content of the file, storing the result inrows
.What is the type of
rows
? What is the type of the first element ofrows
? How many rows are there in the file?Use
open()
to open the file"data/aatable"
in read-only mode, storing the result ofopen()
in the variablef
.Next, use
readline()
(notreadlines()
!) to read the first row of the file, storing the result infirst_row
.How many rows are left to read? Check using
readlines()
.At this point, how many other rows are left to read?
Use
open()
to read the file"output.txt"
in write-only mode"w"
, storing the result ofopen()
in the variablef
.Next, write the string
"check one two three check"
in the file.Close the file using the method
close()
.Now open the file
"output.txtx"
in read-only mode and print to screen its content.use
open()
to open the file"poetry.txt"
in write-only memory. Next, write in the file the strings of the following list, one per line:verses = [ "S'i fosse fuoco, arderei 'l mondo" "s'i fosse vento, lo tempestarei" ]
Next, do the same opening twice the file
"poetry2.txt"
in write-only mode (appending), and each time write one of the two verses.What does it happen when we re-open
"poetry2.txt"
in mode"w"
and we close it immediately?Write a module
trick.py
that prints to screen its code.Curiosity: we just wrote (cheating!) a Quine program.