@@ -58,39 +58,11 @@ We didn't get an error... but `(3, 14)` is not at all what we expected!
5858So from now on, let's use a dot with decimal numbers, because ` 3.14 `
5959worked just fine. Later we'll learn what ` (3, 14) ` is.
6060
61- What if we type a ` # ` ?
62-
63- ``` python
64- >> > #
65- >> >
66- ```
67-
68- Nothing happened at all. Maybe we can type a ` # ` and then some text
69- after it?
70-
71- ``` python
72- >> > # hello there
73- >> >
74- ```
75-
76- Again, nothing happened.
77-
78- If you're not using IDLE, the prompt will change from ` >>> ` to
79- ` ... ` . Just press Enter again to get it back to ` >>> ` .
80-
81- ``` python
82- >> > # hello again
83- ...
84- >> >
85- ```
86-
87- In Python, these pieces of text starting with a ` # ` are known as
88- ** comments** . They don't change how the code works in any way, but
89- we can use them to explain what our code does.
90-
9161## Using Python as a calculator
9262
93- Maybe we could type mathematical statements?
63+ ** WARNING:** This part contains boring math. Be careful!
64+
65+ Let's type some math stuff into Python and see what it does.
9466
9567``` python
9668>> > 17 + 3
@@ -106,19 +78,22 @@ Maybe we could type mathematical statements?
10678
10779It's working, Python just calculates the result and echoes it back.
10880
109- The spaces between numbers and operators don't affect anything, they
110- just make the code easier to read when they are used correctly.
81+ I added a space on both sides of ` + ` , ` - ` , ` * ` and ` / ` . Everything would
82+ work without those spaces too:
11183
11284``` python
113- >> > 14 + 2 + 1
114- 17
115- >> > 14 + 2 + 1
116- 17
85+ >> > 4 + 2 + 1
86+ 7
87+ >> > 4 + 2 + 1
88+ 7
11789>> >
11890```
11991
120- The evaluation order is similar to math. The parentheses ` ( ` and ` ) `
121- also work the same way.
92+ However, I recommend always adding the spaces because they make the code
93+ easier to read.
94+
95+ Things are calculated in the same order as in math. The parentheses ` ( `
96+ and ` ) ` also work the same way.
12297
12398``` python
12499>> > 1 + 2 * 3 # 2 * 3 is calculated first
@@ -128,78 +103,33 @@ also work the same way.
128103>> >
129104```
130105
131- Square brackets ` [] ` and curly brackets ` {} ` cannot be used to change
132- the evaluation order. We'll learn more about what they do later.
106+ Python also supports many other kinds of calculations, but most of the
107+ time you don't need them. Actually you don't need even these
108+ calculations most of the time, but these calculations are probably
109+ enough when you need to calculate something.
133110
134- ``` python
135- >> > [1 + 2 ] * 3
136- [3 , 3 , 3 ]
137- >> > {1 + 2 } * 3
138- Traceback (most recent call last):
139- File " <stdin>" , line 1 , in < module>
140- TypeError : unsupported operand type (s) for * : ' set' and ' int'
141- >> >
142- ```
143-
144- # # More advanced math
145-
146- I decided to include this in my tutorial because some people might be
147- interested in this. Feel free to [skip this](# summary) if you're not
148- interested.
111+ ## Comments
149112
150- The `// ` operator will divide and then throw away the dot and everything
151- after it. For example, `17 / 3 ` is `5.666666666666667 ` , and so `17 // 3 `
152- is `5 ` because we throw away the `.666666666666667 ` part.
113+ We can also type a ` # ` and then whatever we want after that. These bits
114+ of text are known as ** comments** , and we'll find uses for them later.
153115
154116``` python
155- >> > 17 / 3
156- 5.666666666666667
157- >> > 17 // 3
158- 5
117+ >> > 1 + 2 # can you guess what the result is?
118+ 3
159119>> >
160120```
161121
162- The `% ` operator gets the division remainder.
122+ Again, I put a space after the ` # ` and multiple spaces before it just to
123+ make things easier to read.
163124
164- ```python
165- >> > 17 % 3
166- 2
167- >> >
168- ```
169-
170- For example, if there were 17 apples that should be given evenly to 3
171- people, everyone would get 5 apples and there would be 2 apples left
172- over.
125+ If we write comment on a line with no code on it, the prompt changes
126+ from ` >>> ` to ` ... ` . To be honest, I have no idea why it does that and I
127+ think it would be better if it would just stay as ` >>> ` . The prompt goes
128+ back to ` >>> ` when we press Enter again.
173129
174130``` python
175- >> > 17 // 3
176- 5
177- >> > 17 % 3
178- 2
179- >> >
180- ```
181-
182- This is also useful for converting time from minutes to seconds. 500
183- seconds is 8 minutes and 20 seconds.
184-
185- ```python
186- >> > 500 // 60
187- 8
188- >> > 500 % 60
189- 20
190- >> >
191- ```
192-
193- `** ` can be used to raise to a power, so 3 ² in math is `3 ** 2 ` in Python.
194- Powers are calculated before `* ` and `/ ` , but after `()` .
195-
196- ```python
197- >> > 2 ** 3
198- 8
199- >> > 2 * 3 ** 2 # 3 ** 2 is calculated first
200- 18
201- >> > (2 * 3 ) ** 2 # 2 * 3 is calculated first
202- 36
131+ >> > # hello there
132+ ...
203133>> >
204134```
205135
@@ -208,8 +138,8 @@ Powers are calculated before `*` and `/`, but after `()`.
208138- Errors don't matter.
209139- We can enter any Python commands to the interactive ` >>> ` prompt, and
210140 it will echo back the result.
211- - Pieces of text starting with a `# ` are comments.
212141- ` + ` , ` - ` , ` * ` and ` / ` work in Python just like in math.
142+ - Pieces of text starting with a ` # ` are comments.
213143
214144***
215145
0 commit comments