@@ -45,7 +45,7 @@ Comparison operators always return a boolean values (`True` or `False`) when use
4545
4646Checks if the left hand side operand is larger than the right hand side operand.
4747
48- ``` py
48+ ``` python
4949print (1 > 2 )
5050print (20 > 1 )
5151print (3.2 > 9 )
@@ -60,7 +60,7 @@ This reads almost like plain English! 1 is not greater than 2, hence it returns
6060
6161Checks if the right hand side operand is larger than the left hand side operand.
6262
63- ``` py
63+ ``` python
6464print (3 < 9 )
6565print (200 < 100 )
6666print (23.12 < 9 )
7878
7979Checks if both left and right hand side operands are exactly equal.
8080
81- ``` py
81+ ``` python
8282print (123 == 321 )
8383print (909 == 909 )
8484print (812 == 22 )
@@ -102,7 +102,7 @@ False
102102
103103Checks if both the operands are _ not_ equal.
104104
105- ``` py
105+ ``` python
106106
107107print (10 != 10 )
108108print (3 != 4 )
121121
122122Checks if the left hand side operand is either greater than or equal to the right hand side operand.
123123
124- ``` py
124+ ``` python
125125print (90 >= 18 )
126126print (23 >= 32 )
127127print (1 >= 0 )
@@ -142,7 +142,7 @@ to 23 which also gives us `True` as the output.
142142
143143Checks if the right hand side operand is either greater than or equal to the left hand side operand.
144144
145- ``` py
145+ ``` python
146146print (4 <= 18 )
147147print (9 <= 3 )
148148print (10 <= 10 )
@@ -155,3 +155,240 @@ True
155155False
156156True
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