Skip to content

Commit b509605

Browse files
committed
Finished Arithmetic section
1 parent d683e5c commit b509605

1 file changed

Lines changed: 243 additions & 6 deletions

File tree

‎docs/python-tutorial/chapters/06-operators-pt01.md‎

Lines changed: 243 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Comparison operators always return a boolean values (`True` or `False`) when use
4545

4646
Checks if the left hand side operand is larger than the right hand side operand.
4747

48-
```py
48+
```python
4949
print(1 > 2)
5050
print(20 > 1)
5151
print(3.2 > 9)
@@ -60,7 +60,7 @@ This reads almost like plain English! 1 is not greater than 2, hence it returns
6060

6161
Checks if the right hand side operand is larger than the left hand side operand.
6262

63-
```py
63+
```python
6464
print(3 < 9)
6565
print(200 < 100)
6666
print(23.12 < 9)
@@ -78,7 +78,7 @@ False
7878

7979
Checks if both left and right hand side operands are exactly equal.
8080

81-
```py
81+
```python
8282
print(123 == 321)
8383
print(909 == 909)
8484
print(812 == 22)
@@ -102,7 +102,7 @@ False
102102

103103
Checks if both the operands are _not_ equal.
104104

105-
```py
105+
```python
106106

107107
print(10 != 10)
108108
print(3 != 4)
@@ -121,7 +121,7 @@ True
121121

122122
Checks if the left hand side operand is either greater than or equal to the right hand side operand.
123123

124-
```py
124+
```python
125125
print(90 >= 18)
126126
print(23 >= 32)
127127
print(1 >= 0)
@@ -142,7 +142,7 @@ to 23 which also gives us `True` as the output.
142142

143143
Checks if the right hand side operand is either greater than or equal to the left hand side operand.
144144

145-
```py
145+
```python
146146
print(4 <= 18)
147147
print(9 <= 3)
148148
print(10 <= 10)
@@ -155,3 +155,240 @@ True
155155
False
156156
True
157157
```
158+
159+
## Arithmetic Operators
160+
161+
## Arithmetic Operators
162+
163+
Arithmetic operators let us perform mathematical calculations in Python. You've already seen one of
164+
them, the `+` operator, in our calculator example. Python supports seven of them:
165+
166+
| Operator | Name | Example | Result |
167+
| -------- | -------------- | -------- | ------ |
168+
| `+` | Addition | `7 + 2` | `9` |
169+
| `-` | Subtraction | `7 - 2` | `5` |
170+
| `*` | Multiplication | `7 * 2` | `14` |
171+
| `/` | Division | `7 / 2` | `3.5` |
172+
| `//` | Floor Division | `7 // 2` | `3` |
173+
| `%` | Modulus | `7 % 2` | `1` |
174+
| `**` | Exponentiation | `7 ** 2` | `49` |
175+
176+
Let's take a look at each of them.
177+
178+
### Addition, Subtraction & Multiplication
179+
180+
These three work exactly as they do in mathematics:
181+
182+
```python
183+
apples = 10
184+
oranges = 4
185+
186+
print(apples + oranges)
187+
print(apples - oranges)
188+
print(apples * oranges)
189+
```
190+
191+
```text
192+
14
193+
6
194+
40
195+
```
196+
197+
!!! note
198+
199+
Notice that we use `*` for multiplication, not `x`. The same goes for division, where we use `/`
200+
instead of `÷`.
201+
202+
### Division
203+
204+
The `/` operator divides the left value by the right value:
205+
206+
```python
207+
print(10 / 4)
208+
print(10 / 5)
209+
```
210+
211+
```text
212+
2.5
213+
2.0
214+
```
215+
216+
Notice that the second result is `2.0` and not `2`. In Python, the `/` operator **always** returns a
217+
float, even when the result is a whole number.
218+
219+
!!! warning
220+
221+
Dividing by zero is not possible, in Python or in mathematics. If you try, Python will stop your
222+
program with a `ZeroDivisionError`.
223+
224+
### Floor Division
225+
226+
Floor division (`//`) divides the left value by the right value, and then rounds the result **down**
227+
to the nearest whole number. In other words, it throws away the decimal part:
228+
229+
```python
230+
print(7 // 2)
231+
print(10 // 3)
232+
```
233+
234+
```text
235+
3
236+
3
237+
```
238+
239+
This is useful when you only care about how many _complete_ times a number fits into another. For
240+
example, how many full hours are in 150 minutes:
241+
242+
```python
243+
minutes = 150
244+
print(minutes // 60)
245+
```
246+
247+
```text
248+
2
249+
```
250+
251+
### Modulus
252+
253+
The modulus operator (`%`) gives us the **remainder** left over after a division. Remember long
254+
division from school? `7 ÷ 2` is `3` with a remainder of `1`, so `7 % 2` is `1`:
255+
256+
```python
257+
print(7 % 2)
258+
print(10 % 3)
259+
print(10 % 5)
260+
```
261+
262+
```text
263+
1
264+
1
265+
0
266+
```
267+
268+
If we continue our minutes example, `%` tells us how many minutes are _left over_ after taking out
269+
the full hours:
270+
271+
```python
272+
minutes = 150
273+
print(minutes // 60) # full hours
274+
print(minutes % 60) # leftover minutes
275+
```
276+
277+
```text
278+
2
279+
30
280+
```
281+
282+
So 150 minutes is 2 hours and 30 minutes.
283+
284+
??? note "A common use of modulus"
285+
286+
Modulus is the standard way to check if a number is even or odd. Any even number divided by `2`
287+
leaves a remainder of `0`, while any odd number leaves a remainder of `1`:
288+
289+
```python
290+
print(8 % 2) # 0, so 8 is even
291+
print(9 % 2) # 1, so 9 is odd
292+
```
293+
294+
### Exponentiation
295+
296+
The `**` operator raises the left value to the power of the right value:
297+
298+
```python
299+
print(2 ** 3)
300+
print(5 ** 2)
301+
```
302+
303+
```text
304+
8
305+
25
306+
```
307+
308+
`2 ** 3` means `2 * 2 * 2`, which is `8`.
309+
310+
## Order of Operations
311+
312+
When an expression has more than one operator, Python follows the same order of operations you
313+
learned in mathematics (often remembered as PEMDAS or BODMAS). Operators with the same priority are
314+
evaluated from left to right. Let's see it in action:
315+
316+
```python
317+
print(2 + 3 * 4)
318+
print((2 + 3) * 4)
319+
```
320+
321+
```text
322+
14
323+
20
324+
```
325+
326+
In the first line, multiplication happens before addition, so `3 * 4` is calculated first, giving
327+
`2 + 12`. In the second line, the parentheses force `2 + 3` to be calculated first, giving `5 * 4`.
328+
329+
!!! tip
330+
331+
When in doubt, use parentheses. Even if they aren't needed, they make your code easier to read and
332+
make sure the calculation happens in the order you intended.
333+
334+
## Mixing Integers and Floats
335+
336+
When you do arithmetic between an integer and a float, the result will always be a float:
337+
338+
```python
339+
print(5 + 2.0)
340+
print(3 * 1.5)
341+
```
342+
343+
```text
344+
7.0
345+
4.5
346+
```
347+
348+
## In-place Operations
349+
350+
It's very common to update a variable using its own value, such as adding to a score:
351+
352+
```python
353+
score = 10
354+
score = score + 5
355+
print(score)
356+
```
357+
358+
```text
359+
15
360+
```
361+
362+
Python provides a shorter way of writing this. Placing the operator before the `=` does the same
363+
thing:
364+
365+
```python
366+
score = 10
367+
score += 5
368+
print(score)
369+
```
370+
371+
```text
372+
15
373+
```
374+
375+
`score += 5` is exactly the same as `score = score + 5`. This works with all of the arithmetic
376+
operators:
377+
378+
```python
379+
number = 100
380+
381+
number -= 20 # number = number - 20
382+
number *= 2 # number = number * 2
383+
number /= 4 # number = number / 4
384+
number %= 10 # number = number %= 10
385+
number += 1 # number = number + 1
386+
387+
print(number)
388+
```
389+
390+
Output:
391+
392+
```text
393+
1.0
394+
```

0 commit comments

Comments
 (0)