Back to Chapters
Chapter Progress: 0 %0 / 5 Lessons Read
β€Ήβ€Ί

2. Data Types and Operators

2.4 Arithmetic and comparison operators

Highlights & notes

Select lesson text to highlight it, or tap Add note.

    Bookmarks

    Bookmark a lesson to see it here.

      πŸ§’ The easy way

      Maths signs do sums. Comparison signs are a weighing scale that only says True or False.

      • // shares sweets equally: how many each child gets.
      • % is the sweets left over.
      • == asks "are these the same?"

      Thumb rule: One = puts a value in a box. Two == asks a question.

      Watch it run Β· 1

      Python
      print(7 // 2, 7 % 2)
      Result
      3 1

      Watch it run Β· 2

      Python
      print(5 > 3)
      Result
      True

      Python does maths with arithmetic operators:

      • + add, - subtract, * multiply
      • / divide, which always gives a float: 10 / 2 is 5.0
      • // whole groups: 23 // 10 is 2
      • % remainder: 23 % 10 is 3
      • power: 2 3 is 8

      Comparison operators ask a yes/no question and give True or False: == equal, != not equal, <, >, <=, >=.

      Combine checks with and (both must be true), or (at least one must be true) and not (flips the answer).

      Batching 23 rows into groups of 10

      • 23 // 10 β†’ 2 full batches
      • 23 % 10 β†’ 3 rows left over
      • 23 % 10 > 0 β†’ True, so you need one more small batch
      Must Know:

      / always returns a float, even for whole results: 10 / 5 is 2.0. Use // when you need a whole number.

      How it fits together

      1. 23 rows
      2. 23 // 10 β†’ 2 batches
      3. 23 % 10 β†’ 3 left

      Step through the code

      Python

      rows, batch_size = 23, 10full_batches = rows // batch_sizespare_rows = rows % batch_sizeprint(full_batches, spare_rows)print(spare_rows > 0)

      Output

      2 3
      True

      Practice in Python

      For 35 rows and batches of 8, print the number of full batches followed by the remaining row count.

      Expected output
      4 3

      Reference solution
      rows = 35
      batch_size = 8
      print(rows // batch_size, rows % batch_size)

      Question 1 Β· Easy

      What does 7 % 3 give?

      Question 2 Β· Medium

      What does print(10 / 4, 10 // 4) show?

      Question 3 Β· Hard

      What does print(5 > 3 and not 2 == 2) show?

      Key takeaways

      Use arithmetic to calculate values and comparisons to test them.

      Lesson 8 of 67

      Save your progress

      Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.

      Start free Log in
      PreviousArithmetic and comparison operators
      Save your progress free for 1 month. Start free Β· Log in