Skip to content

Commit e1bfbab

Browse files
authored
Merge pull request #34 from mciwing/christina-feedback-2
[Python Crash Course] Incorporate remaining suggestions from Christina
2 parents 503c358 + 8985904 commit e1bfbab

File tree

13 files changed

+157
-64
lines changed

13 files changed

+157
-64
lines changed
76.8 KB
Loading

docs/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
- :fontawesome-brands-python: __Python__
2828

2929
---
30-
![header](assets/python/python-header.gif)
30+
31+
[![header](assets/python/python-header.gif)](python/index.md)
3132
Learn the programming language **:fontawesome-brands-python: Python**.
3233
We will cover the basics from types, control structures and functions to
3334
packages.

docs/python/containers/dict.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ print(experiment["sample_weight_in_grams"])
5555
Manage the cost of raw materials in a dictionary. The dictionary should
5656
contain the following key-value pairs:
5757

58-
- `#!python "Steel"`: `#!python 100`
59-
- `#!python "Aluminum"`: `#!python 150`
60-
- `#!python "Copper"`: `#!python 200`
61-
- `#!python "Plastic"`: `#!python 50`
58+
- `#!python "steel"`: `#!python 100`
59+
- `#!python "aluminium"`: `#!python 150`
60+
- `#!python "copper"`: `#!python 200`
61+
- `#!python "plastic"`: `#!python 50`
6262

6363
Create the dictionary and print the price of copper.
6464

@@ -122,9 +122,9 @@ print(experiment)
122122

123123
```py
124124
production = {
125-
"singapore": {"steel": 100, "aluminum": 150},
126-
"taipeh": {"steel": 200, "aluminum": 250},
127-
"linz": {"steel": 300, "aluminum": 350, "copper": 100},
125+
"singapore": {"steel": 100, "aluminium": 150},
126+
"taipeh": {"steel": 200, "aluminium": 250},
127+
"linz": {"steel": 300, "aluminium": 350, "copper": 100},
128128
}
129129
```
130130

@@ -133,7 +133,7 @@ print(experiment)
133133

134134
- Remove `linz` from the dictionary.
135135
- Add a new location `vienna` with the production of 200 steel
136-
and 250 aluminum.
136+
and 250 aluminium.
137137
- Print the `aluminium` value of `taipeh` (try accessing it step
138138
by step and use variables for each step).
139139

@@ -151,7 +151,7 @@ print(experiment)
151151

152152
We have covered following topics in this section:
153153

154-
- Dictionaries store key-value pairs.
154+
- Dictionaries store key-value pairs
155155
- How to get and modify values
156156
- Adding key-value pairs
157157
- Removing key-value pairs with `#!python del`

docs/python/containers/list.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ manipulation. `Python` provides several built-in container types, each with its
99
own characteristics and use cases. In this first section, we cover
1010
`#!python list` objects, followed by dictionaries and tuples.
1111

12-
## What Is a `#!python list`?
12+
## What is a `#!python list`?
1313

1414
A `#!python list` is a collection of items. You can make a `#!python list`
1515
including the letters of the alphabet or the digits from `#!python 0` to
@@ -63,7 +63,7 @@ John
6363

6464
???+ info
6565

66-
In Python, index positions **start at 0**, not 1. This is true of most
66+
In Python, index positions **start at 0**, not 1. This is true for most
6767
programming languages. If you’re receiving unexpected results, determine
6868
whether you are making a simple off-by-one error.
6969

@@ -148,11 +148,11 @@ print(numbers)
148148
[1.0, 2.0, 0.5, 4.0, 3.0, 'one hundred']
149149
```
150150

151-
Up until now, our lists contained only one type of elements - strings. However,
152-
as in the example above, you can store multiple different types of data in a
153-
`#!python list`. Moreover, you can do nesting (for example, you can store a
154-
`#!python list` within a `#!python list` - more on that later). Hence,
155-
lists can represent complex data structures.
151+
Up until now, our lists contained only one type of elements -
152+
strings or integers. However, as in the example above, you can store
153+
multiple different types of data in a `#!python list`. Moreover, you can do
154+
nesting (for example, you can store a `#!python list` within a `#!python list`
155+
- more on that later). Hence, lists can represent complex data structures.
156156
Nevertheless, don't mix and match every imaginable data type within a single
157157
`#!python list` (just because you can) as it makes the handling of your
158158
`#!python list` quite difficult.
@@ -182,7 +182,7 @@ print(pokemon)
182182

183183
To remove an item from a `#!python list`, you can use the `#!python remove()`
184184
method. You need to specify the value which you want to remove. However,
185-
this it will only remove the first occurrence of the item.
185+
this will only remove the first occurrence of the item.
186186

187187
```py hl_lines="2"
188188
pokemon = ["Charmander", "Squirtle", "Charmeleon", "Charizard", "Squirtle"]
@@ -227,8 +227,8 @@ Bulbasaur
227227
## Organizing a `#!python list`
228228

229229
For various reasons, often, your lists will be unordered. If you want to
230-
present your `#!python list` in a particular order, you can use the method `#!python
231-
sort()`, or the function `#!python sorted()`.
230+
present your `#!python list` in a particular order, you can use the method
231+
`#!python sort()`, or the function `#!python sorted()`.
232232

233233
### `#!python sort()`
234234

@@ -299,7 +299,8 @@ print(players[0:3])
299299
Define a `#!python list` of your choice with at least `#!python 5`
300300
elements.
301301

302-
- Now, perform a slice from the second to (including) the fourth element.
302+
- Now, perform a slice from the second up to and including the fourth
303+
element.
303304
- Next, omit the first index in the slice (only omit the number!). What
304305
happens?
305306
- Lastly, re-add the first index and omit the second index of your

docs/python/containers/tuple.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ print(type(coordinates))
1818
A `#!python tuple` is created with round brackets (`()`). As with lists and dictionaries,
1919
the elements are separated by commas. Tuples can hold any type of data.
2020

21-
Access the individual elements of a `#!python tuple` using the index.
21+
## Accessing elements
22+
23+
With indexing, the individual elements of a `#!python tuple` can be retrieved.
2224

2325
```py
2426
coordinates = (47.262996862335854, 11.393082185178823)
@@ -31,6 +33,8 @@ print(coordinates[1])
3133
11.39308218517882
3234
```
3335

36+
## Immutability
37+
3438
Let's try to change the value of an element in a `#!python tuple`.
3539

3640
```py
@@ -57,6 +61,8 @@ coordinates = (47.262996862335854, 11.393082185178823)
5761
coordinates = (5.513615392318705, 95.2060492604128)
5862
```
5963

64+
## `#!python tuple` unpacking
65+
6066
Tuples can be unpacked, to use them separately.
6167

6268
```py
@@ -85,7 +91,7 @@ latitude, longitude = coordinates
8591

8692
In this rather short section, we introduced tuples and covered:
8793

88-
- mutable vs. immutable
89-
- how to define a `#!python tuple`
90-
- accessing elements
91-
- and `#!python tuple` unpacking.
94+
- Mutability vs. immutability
95+
- How to define a `#!python tuple`
96+
- Access elements with indexing
97+
- ... and `#!python tuple` unpacking

docs/python/control-structures/for.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ Sum: 22.23
213213

214214
???+ question "Calculate the average"
215215

216-
Calculate the average of following list:
216+
Calculate the average of the following list:
217217
```py
218218
numbers = [4.52, 3.14, 2.71, 1.0, 8.38]
219219
```
@@ -242,8 +242,8 @@ a single line.
242242

243243
## Looping over dictionaries
244244

245-
As previously discussed, you can not only loop over a `#!python list` but
246-
using a variety of different data types such as dictionaries.
245+
As previously discussed, you can not only loop over a `#!python list`, but
246+
also iterate over a variety of different data types, such as dictionaries.
247247
You can loop over a dictionary’s key-value pairs, solely over the keys
248248
or just the values.
249249

docs/python/control-structures/if.md

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ if condition is True:
2020

2121
You can put any condition in the first line and just about any action in the
2222
indented block following the test. If the condition evaluates to `True`,
23-
`Python` executes the code following the `#!python if` statement.
24-
If the test evaluates to `False`, the code following the `#!python if` is ignored.
23+
`Python` executes the indented code following the `#!python if` statement.
24+
If the test evaluates to `False`, the indented code block (following the
25+
`#!python if`) is ignored.
2526

2627
```py
2728
user = "admin"
@@ -88,7 +89,7 @@ Indentation plays the same role in `#!python if` statements as it did in
8889
Now, loop over the passwords and check if each password exceeds the
8990
character limit of 12. If so, print the password.
9091

91-
## `#!python else`
92+
### `#!python else`
9293

9394
Previously, every time the condition in the `#!python if` statement
9495
evaluated to `#!python False`,
@@ -117,7 +118,7 @@ Only admins can enter this area!
117118

118119
*Hint*: Introduce an `else` statement to save the invalid passwords.
119120

120-
## `#!python elif`
121+
### `#!python elif`
121122

122123
Often, you’ll need to test more than two possible situations, and to evaluate
123124
these, you can use an `if-elif-else` syntax. `Python` executes only one
@@ -169,7 +170,7 @@ current_number = 1
169170
while current_number <= 5:
170171
print(current_number)
171172
# increment the counter value by one
172-
current_number = current_number + 1
173+
current_number += 1
173174
```
174175

175176
```title=">>> Output"
@@ -181,7 +182,42 @@ while current_number <= 5:
181182
```
182183

183184
Note, that the variable, that is checked in the `#!python while`-condition
184-
must be defined prior to the loop, otherwise we will encounter a `#!python NameError`.
185+
must be defined prior to the loop, otherwise we will encounter a
186+
`#!python NameError`.
187+
188+
<div style="text-align: center;">
189+
<img src="../../../assets/python/control-structures/infinite-loop.jpg"
190+
alt="Infinite loops" width="400px" style="border-radius:10px;"
191+
>
192+
<figcaption>Infinite loops</figcaption>
193+
</div>
194+
195+
Moreover, the variable must be updated within the loop
196+
to avoid an infinite loop. For example, if `#!python current_number` is not
197+
incremented by one, the condition `#!python current_number <= 5` will always
198+
evaluate to `#!python True`, leaving us stuck in an infinite loop.
199+
In such cases, simply click the `Stop` button (on the left-hand side of the
200+
respective code cell) to interrupt the execution.
201+
202+
???+ info "Addition assignment"
203+
204+
In the above example, we used the `#!python +=` operator, referred to as
205+
addition assignment. It is a shorthand for incrementing a variable by a
206+
certain value.
207+
208+
```py hl_lines="2"
209+
a = 10
210+
a += 5
211+
print(a)
212+
```
213+
214+
```title=">>> Output"
215+
15
216+
```
217+
218+
The above code is equivalent to `#!python a = a + 5`.
219+
This shorthand assignment can be used with all arithmetic
220+
operators, such as subtraction `#!python -=` or division `#!python /=`.
185221

186222

187223
???+ question "While loop"
@@ -202,12 +238,26 @@ below.
202238
user_name = input("Please enter your username:")
203239
```
204240

241+
However, the `#!python input()` function always returns a string.
242+
243+
```py
244+
age = input("Please enter your age:")
245+
print(type(age))
246+
```
247+
248+
```title=">>> Output"
249+
<class 'str'>
250+
```
251+
252+
... use [casting](../types/bool_and_none.md#casting) to
253+
convert the input to the desired type.
254+
205255

206256
## `#!python break`
207257

208-
To exit any loop immediately without running any remaining code, use the
209-
`#!python break` statement. The `#!python break` statement directs the flow of
210-
your program; you can use it to control which lines of code are executed and
258+
To exit any loop immediately without running any remaining 'loop code', use
259+
the `#!python break` statement. The `#!python break` statement directs the flow
260+
of your program; you can use it to control which lines of code are executed and
211261
which aren’t, so the program only executes code that you want it to, when you
212262
want it to.
213263

@@ -216,12 +266,15 @@ for i in range(5):
216266
if i == 3:
217267
break
218268
print(i)
269+
270+
print("Continue running the program...")
219271
````
220272

221273
```title=">>> Output"
222274
0
223275
1
224276
2
277+
Continue running the program...
225278
```
226279

227280
## `#!python continue`

docs/python/functions.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ Hello, admin!
100100

101101
As you can see in the example above, a docstring can span multiple lines!
102102

103+
Up until now, the functions had no parameters at all or just a single
104+
parameter. However, you can define as many parameters as you like, seperated
105+
by a comma (`#!python ,`):
106+
107+
```py
108+
def greet(first_name, last_name):
109+
print(f"Hello, {first_name} {last_name}!")
110+
```
111+
103112
???+ question "Break-even point"
104113

105114
Remember the task to calculate the break-even point? Now, you'll wrap
@@ -315,10 +324,10 @@ result = square_number(5)
315324
This section introduced the concept of functions to better structure your
316325
code, make it more readable and reusable. We have covered:
317326

318-
- The definition of functions
319-
- Docstrings
320-
- Parameters and arguments
327+
- How to define a function
328+
- Docstrings as a tool to document your functions
329+
- Parameters vs arguments
321330
- Positional and keyword arguments
322-
- Default values
323-
- `#!python return` statement
324-
- Structuring code with functions
331+
- Defining default values for parameters
332+
- The `#!python return` statement
333+
- How to use functions to solve smaller subtasks and structure your code

docs/python/packages.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ solve common problems. Therefore, you won't have to reinvent the wheel.
1212
For example the package `pandas` is the go-to tool for data manipulation and
1313
analysis. With `pandas` you can read text and Excel files
1414
:fontawesome-regular-file-excel: among a lot of other
15-
formats and offers a lot of functionality to manipulate and even plot your
15+
formats and it offers a lot of functionality to manipulate and even plot your
1616
data. Hence, you will rarely see `Python` projects that are not dependent on
1717
`pandas`. Apart from `pandas` there are a wide variety of popular packages:
1818

@@ -36,7 +36,7 @@ on the topic. [Here](https://docs.python.org/3/py-modindex.html) is
3636
an extensive list of all the packages that `Python` ships with.
3737

3838
Let's use the `#!python random` package to generate some random numbers.
39-
First, we have to import the package with following command:
39+
First, we have to import the package with the following command:
4040

4141
```py hl_lines="1"
4242
import random
@@ -386,7 +386,7 @@ In this section, you have learned how to install packages and manage them
386386
within virtual environments. The topics covered:
387387

388388
* `pip`
389-
* install/uninstall packages
390-
* PyPI
391-
* concept and benefits of virtual environments
392-
* creation and basic usage of a virtual environment
389+
* How to install/uninstall packages
390+
* PyPI - the package hub
391+
* Concept and benefits of virtual environments
392+
* Creation and basic usage of a virtual environment

0 commit comments

Comments
 (0)