GeezerVison is showing github.com — Split lists into new chapter · python043/full-speed-python@2f4b378
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: chapters/basic-datatypes.tex +1-48Lines changed: 1 addition & 48 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | \chapter{Basic datatypes}\label{basic-datatypes} | 2 | 2 |
| 3 | | -In this chapter we will work with the most basic datatypes, numbers, strings and lists. Start your Python REPL and write the following on it: | | 3 | +In this chapter we will work with the most basic datatypes, numbers and strings. Start your Python REPL and write the following: | 4 | 4 |
| 5 | 5 | \begin{lstlisting} | 6 | 6 | >>> a = 2 | @@ -51,37 +51,6 @@ \chapter{Basic datatypes}\label{basic-datatypes} | 51 | 51 | 'HelloHelloHello' | 52 | 52 | \end{lstlisting} | 53 | 53 |
| 54 | | -Finally, Python also supports the list datatype. Lists are data structures that allows us to group some values. Lists can have values of several types and you can also mix different types within the same list although all values are usually of the same datatype. | 55 | | - | 56 | | -Lists are created by starting and ending with square brackets and separated by commas. The values in a list can be accessed by its position where 0 is the index of the first value: | 57 | | - | 58 | | -\begin{lstlisting} | 59 | | ->>> l = [1, 2, 3, 4, 5] | 60 | | ->>> l[0] | 61 | | -1 | 62 | | ->>> l[1] | 63 | | -2 | 64 | | -\end{lstlisting} | 65 | | - | 66 | | -Can you access the number 4 in the previous list? | 67 | | - | 68 | | -Sometimes you want just a small portion of a list, a sublist. Sublists can be retrieved using a technique called \textit{slicing}, which consists on using the start and end indexes on the sublist: | 69 | | - | 70 | | -\begin{lstlisting} | 71 | | ->>> l = ['a', 'b', 'c', 'd', 'e'] | 72 | | ->>> l[1:3] | 73 | | -['b', 'c'] | 74 | | -\end{lstlisting} | 75 | | - | 76 | | -Finally, there's also some arithmetic that you can do on lists, like adding two lists together or repeating the contents of a list. | 77 | | - | 78 | | -\begin{lstlisting} | 79 | | ->>> [1,2] + [3,4] | 80 | | -[1, 2, 3, 4] | 81 | | ->>> [1,2] * 2 | 82 | | -[1, 2, 1, 2] | 83 | | -\end{lstlisting} | 84 | | - | 85 | 54 |
| 86 | 55 | \section{Exercises with numbers} | 87 | 56 |
| @@ -128,19 +97,3 @@ \section{Exercises with strings} | 128 | 97 | \end{enumerate} | 129 | 98 |
| 130 | 99 | \end{enumerate} | 131 | | - | 132 | | -\section{Exercises with lists} | 133 | | - | 134 | | -Create a list named "l" with the following values ([1, 4, 9, 10, 23]). Using the Python documentation about lists (\url{https://docs.python.org/3.5/tutorial/introduction.html#lists}) solve the following exercises: | 135 | | - | 136 | | -\begin{enumerate} | 137 | | - | 138 | | -\item Using list slicing get the sublists [4, 9] and [10, 23]. | 139 | | - | 140 | | -\item Append the value 90 to the end of the list "l". Check the difference between list concatenation and the "append" method. | 141 | | - | 142 | | -\item Calculate the average value of all values on the list. You can use the "sum" and "len" functions. | 143 | | - | 144 | | -\item Remove the sublist [4, 9]. | 145 | | - | 146 | | -\end{enumerate} |
Collapse file Copy file name to clipboard +49Lines changed: 49 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +\chapter{Lists}\label{lists} | | 2 | + | | 3 | +Python lists are data structures that group sequences of elements. Lists can have elements of several types and you can also mix different types within the same list although all elements are usually of the same datatype. | | 4 | + | | 5 | +Lists are created using square brackets and the elements separated by commas. The elements in a list can be accessed by their positions where 0 is the index of the first element: | | 6 | + | | 7 | +\begin{lstlisting} | | 8 | +>>> l = [1, 2, 3, 4, 5] | | 9 | +>>> l[0] | | 10 | +1 | | 11 | +>>> l[1] | | 12 | +2 | | 13 | +\end{lstlisting} | | 14 | + | | 15 | +Can you access the number 4 in the previous list? | | 16 | + | | 17 | +Sometimes you want just a small portion of a list, a sublist. Sublists can be retrieved using a technique called \textit{slicing}, which consists on defining the start and end indexes: | | 18 | + | | 19 | +\begin{lstlisting} | | 20 | +>>> l = ['a', 'b', 'c', 'd', 'e'] | | 21 | +>>> l[1:3] | | 22 | +['b', 'c'] | | 23 | +\end{lstlisting} | | 24 | + | | 25 | +Finally, arithmetic with lists is also possible, like adding two lists together or repeating the contents of a list. | | 26 | + | | 27 | +\begin{lstlisting} | | 28 | +>>> [1,2] + [3,4] | | 29 | +[1, 2, 3, 4] | | 30 | +>>> [1,2] * 2 | | 31 | +[1, 2, 1, 2] | | 32 | +\end{lstlisting} | | 33 | + | | 34 | + | | 35 | +\section{Exercises with lists} | | 36 | + | | 37 | +Create a list named "l" with the following values ([1, 4, 9, 10, 23]). Using the Python documentation about lists (\url{https://docs.python.org/3.5/tutorial/introduction.html#lists}) solve the following exercises: | | 38 | + | | 39 | +\begin{enumerate} | | 40 | + | | 41 | +\item Using list slicing get the sublists [4, 9] and [10, 23]. | | 42 | + | | 43 | +\item Append the value 90 to the end of the list "l". Check the difference between list concatenation and the "append" method. | | 44 | + | | 45 | +\item Calculate the average value of all values on the list. You can use the "sum" and "len" functions. | | 46 | + | | 47 | +\item Remove the sublist [4, 9]. | | 48 | + | | 49 | +\end{enumerate} |
Collapse file Copy file name to clipboardExpand all lines: full-speed-python.tex +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 55 | 55 | \include{chapters/introduction} | 56 | 56 | \include{chapters/installation} | 57 | 57 | \include{chapters/basic-datatypes} | | 58 | +\include{chapters/lists} | 58 | 59 | \include{chapters/functions} | 59 | 60 | \include{chapters/loops} | 60 | 61 | \include{chapters/dictionaries} | |
|