Commit 32c034ec authored by Raffaele Grosso's avatar Raffaele Grosso
Browse files

Add some Python exercises

parent 399d0f26
......@@ -17,23 +17,20 @@
\newcommand{\Hint}[1]{{\\ \color{Blue} Hint:} #1}
\newcommand{\HintN}[2]{{\\ \color{Blue} Hint #1:} #2}
\newcommand{\Note}[1]{{\\ \color{Blue} Note:} #1}
\newcommand{\Example}[1]{{\\ \color{Blue} Example:} #1}
\newcounter{ChapterCounter}
\newcommand{\Chapter}[1]{\refstepcounter{ChapterCounter}\textbf{\theChapterCounter. #1 \label{#1}}}
\newcommand{\Chapter}[1]{\refstepcounter{ChapterCounter}\textbf{\theChapterCounter. #1} \label{#1}}
\newcommand{\ChapterNoNum}[1]{\begin{center}\textbf{#1}\end{center} \label{#1}}
\renewcommand\labelitemi{-}
\begin{document}
\lstset{language=Python}
\Chapter{Hello World, basic I/O and importing a module}
\begin{enumerate}[label=\textbf{\alph*)}]
\item Write a Hello World program, that is a program that just writes: \texttt{"Hello World!"}
\item Write a program which asks the user its name and birth year and then writes the following text: \texttt{"Hi NAME, you will become X years old this year."}.
\HintN{1}{The \texttt{input} and \texttt{raw\_input} functions take as argument a string to be prompted to the user and return the user's input. These two functions differ between Python version 2 or 3. Mind the type of the returned object, casting it in case.}
\HintN{2}{To get the current year and avoid to hardcode it you should use the appropriate function from the \texttt{datetime} module, therefore \texttt{import} it.}
\end{enumerate}
\ChapterNoNum{Preliminaries}
Composing, formatting, printing strings. Reading values from the prompt into a variable. Importing a module. Checking the type of a variable.
\Chapter{Strings}
\begin{enumerate}[label=\textbf{\alph*)}]
......@@ -44,11 +41,29 @@
\item make a string upper (lower) case
\item find a substring inside a string
\item extract from a string a character at a given position
\item \textit{slice} a string, i.e. extract part of the string, from position \texttt{m} to position \texttt{n}
\item \textit{slice} a string, i.e. extract part of the string, say from position \texttt{m} to position \texttt{n}
\end{itemize}
\end{enumerate}
\textbf{Data structures}\\
\Chapter{Reading values from the prompt - Formatting strings}
\begin{enumerate}[label=\textbf{\alph*)}]
\item Write a Hello World program, that is a program that just writes: \texttt{"Hello World!"}
\item Write a program which asks the user its name and birth year and then writes the following text: \texttt{"Hi NAME, you will become X years old this year."}.
\HintN{1}{The \texttt{input} and \texttt{raw\_input} functions take as argument a string to be prompted to the user and return the user's input. These two functions differ between Python version 2 or 3. Mind the type of the returned object, casting it in case.}
\HintN{2}{To get the current year and avoid to hardcode it, you should use the appropriate function from the \texttt{datetime} module and therefore \texttt{import} it.}
\HintN{3}{To format variables into a string there are various methods. You can try and then choose one of the following:
\begin{itemize}
\item \texttt{f"my text contains the value of \{var1\} and of \{var2\}"}
\item \texttt{"my text contains the value of \{0:\} and of \{1:\}".format(var1, var2)}
\item \texttt{"my text contains the value of \%s and of \%d" \% (var1, var2)}
\end{itemize}
}
\end{enumerate}
\vspace{0.5cm}
\ChapterNoNum{Data structures}
Make sure you know the main characteristics of \texttt{list}s, \texttt{tuple}s, \texttt{set}s, \texttt{dict}ionaries.
\Chapter{Lists}
......@@ -61,7 +76,9 @@ Make sure you know the main characteristics of \texttt{list}s, \texttt{tuple}s,
\item return the last element removing it from the list
\item sort and revert the order of elements in the list
\end{itemize}
By means of the methods above instantiate a list and use it as a stack (LIFO $\Leftrightarrow$ Last In First Out) or as a queue (FIFO $\Leftrightarrow$ Last In First Out)).
\item Which methods of the list above would help you using a list as a \textit{stack} (or \textit{LIFO}: Last In First Out)?
\item Lists are not good to be used as a \textit{queue} (or \textit{FIFO}: First In First Out). Why?
\Note{For that purpose you can use \texttt{collections.deque}, which provides a \texttt{popleft} method.}
%\begin{enumerate}[label=\textbf{\alph*)}]
\item Write a program that fills a list with ten integers taken as input from the terminal and returns only the even ones.
\Hint{Use a \textit{list comprehension}.}
......@@ -76,12 +93,24 @@ Make sure you know the main characteristics of \texttt{list}s, \texttt{tuple}s,
\Chapter{Dictionaries}
\begin{enumerate}[label=\textbf{\alph*)}]
\item Make sure you can:
\begin{itemize}
\item instantiate a dictionary (with \texttt{\{\}} and with \texttt{dict()})
\item add an entry (key-value pair) to a dictionary
\item insert/remove an entry
\item get the value associated to a given key
\item check if the dictionary contains the pair with a given key
\item iterate over the elements of the dictionary
\end{itemize}
\item \label{3} Write a program that fills a dictionary with ten (name - birthday month) key-value pairs requested as input and prints out the month of birth for the names chosen by the user.
\end{enumerate}
\Chapter{Conditions}
\Chapter{Conditions and loops}
\begin{enumerate}[label=\textbf{\alph*)}]
\item Write a ``guess the number'' game: the user guesses an integer between 1 and 10 until he/she enters "exit" or until she gets the right number.
\item Make sure you can correctly use \texttt{if-else}-statements and \texttt{for}- and \texttt{while}-loops, in particular the construct \texttt{for i in} + \texttt{range()} or + iterable.
\item List the arguments passed to your script.
\Hint{Using the \texttt{sys} module, \texttt{sys.argv} is a list containing the arguments.}
\item Write a ``guess the number'' game: the user guesses an integer between 1 and 10 until (s)he enters \texttt{exit} or until (s)he gets the right number.
\end{enumerate}
\vspace{2cm}
......@@ -89,6 +118,7 @@ Make sure you know the main characteristics of \texttt{list}s, \texttt{tuple}s,
\begin{enumerate}[label=\textbf{\alph*)}]
\item Write a program reading a text from an input file and writing it to an output file after replacing newlines with spaces. The two file names are to be given as input.
\item Write a program that fills a dictionary with key-value pairs read from a first json file and writes out the dictionary to a second json file after having added or replaced a few entries according to input given by the user.
\Hint{Use the \texttt{dump} and \texttt{laod} functions provided by the \texttt{json} module.}
\end{enumerate}
\Chapter{Functions}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment