Layer 0 Session 02 Python Basics

Operators, Strings & Type Conversion

Variables that just sit there are not very useful. This session is about doing things with them: arithmetic, combining text, and deliberately converting between types.

x == y True / False

Combining and comparing values produces a new value.

Estimated time: 30โ€“35 minutes  ยท  Pre-quiz โ†’ Concept โ†’ Lab โ†’ Commit โ†’ Post-quiz

1. Learning Objective

By the end of this session you will be able to:

  • Use the arithmetic operators +, -, *, /, // and % and explain the difference between / and //
  • Combine strings with + and with f-strings
  • Convert between types explicitly with int(), float(), and str()
  • Explain why 1 + "1" raises an error instead of producing 2 or "11"
  • Use comparison operators (==, !=, <, >, <=, >=) to produce a bool

2. Pre-Coding Quiz

Answer these before reading the concept explanation or writing any code. It is fine to get these wrong โ€” that is the point. You need 4/5 to proceed to the lab.

Question 1 of 5

What does 7 // 2 return, as opposed to 7 / 2?

/ always produces a float result. // ("floor division") divides and throws away the remainder, giving you a whole number โ€” useful when you specifically want the whole-number part of a division.
Question 2 of 5

What does "Population: " + str(54) produce?

str() explicitly converts the number to text first, so + is now combining two strings (called "concatenation"), producing one longer string.
Question 3 of 5

What happens if you write "Population: " + 54 WITHOUT calling str() first?

Python does not automatically guess what you meant by mixing a string and a number with + โ€” it raises a TypeError rather than silently doing something that might not be what you intended. This is deliberate: implicit, silent type mixing causes more bugs than it saves keystrokes.
Question 4 of 5

Given population = "54" (a string), how do you get it as an actual number you could do math with?

int() converts a string of digits into an actual integer. Containing only digit CHARACTERS does not make something a number โ€” it is still text until you explicitly convert it.
Question 5 of 5

What does population > 50 evaluate to, given population = 54?

Comparison operators (>, <, ==, etc.) always produce a bool โ€” exactly True or False โ€” which is what makes them usable in the conditionals we cover next session.

3. The Concept โ€” Operators, Strings, and Explicit Conversion

10 / 33.333...10 // 33 (floor)

/ always gives you a precise float; // gives you the floor (whole-number) result of the same division.

Arithmetic operators

Python supports the operators you would expect from a calculator, plus two less obvious ones: // (floor division) and % (modulo, the remainder after division).

print(10 + 3)   # 13
print(10 - 3)   # 7
print(10 * 3)   # 30
print(10 / 3)   # 3.3333333333333335 โ€” always a float
print(10 // 3)  # 3   โ€” floor division, whole number part only
print(10 % 3)   # 1   โ€” modulo, the remainder

Combining strings

Strings can be joined with +, but only with other strings โ€” and Python offers a cleaner way for mixing text and values: f-strings. (Population figures throughout this course are in millions, to keep the numbers easy to read.)

name = "Kenya"
population = 54

# + concatenation โ€” every piece must already be a string
message = "Country: " + name + ", Population: " + str(population)
print(message)

# f-string โ€” cleaner, and handles the conversion for you
message = f"Country: {name}, Population: {population}"
print(message)  # identical result, much easier to read and write

Explicit conversion: int(), float(), str()

Python never silently guesses how to convert between types. You always convert deliberately, using a conversion function โ€” this is a safety feature, not an inconvenience: it forces you to notice when a conversion is happening.

population_text = "54"
population_number = int(population_text)   # str -> int
print(population_number + 1)                # 55 โ€” now real math works

rate_text = "2.3"
rate_number = float(rate_text)               # str -> float

count = 42
count_text = str(count)                      # int -> str, for combining with other text

Why mixing types with + is an error, not a guess

Adding a string and a number has no single obvious meaning โ€” should it produce a number, or glue the number onto the end as text? Rather than silently picking one (and possibly surprising you), Python raises an error and makes you decide explicitly.

# population = "Population: " + 54
# TypeError: can only concatenate str (not "int") to str

# You decide what you meant:
population = "Population: " + str(54)   # -> "Population: 54"

Comparison operators produce a bool

Comparing two values always gives you back exactly True or False โ€” this is the building block the next session's if-statements are built on.

population = 54

print(population > 50)   # True
print(population == 54)  # True
print(population < 50)   # False
print(population != 0)   # True

4. Lab

Lab objective: Practice arithmetic, string combination with f-strings, explicit conversions, and comparisons that produce booleans.

What you will build

A file called operators.py.

Step-by-step instructions

1

Create the file and do basic arithmetic

# operators.py
population = 54
neighbors = 3

print(population + 1)
print(population / neighbors)
print(population // neighbors)
print(population % neighbors)
2

Combine strings with + and then with an f-string

Compare how much easier the f-string version is to read.

name = "Kenya"
message_plus = "Country: " + name + ", population: " + str(population)
message_fstring = f"Country: {name}, population: {population}"
print(message_plus)
print(message_fstring)
3

Convert a text number into a real number and do math with it

population_from_form = "54"   # imagine this came from a text input
population_number = int(population_from_form)
print(population_number + 1)  # only works because we converted first
4

Trigger and read the TypeError from mixing types, then fix it

Comment the broken line out after reading the error, and keep the fixed version.

# broken = "Population: " + population   # uncomment to see the TypeError
fixed = "Population: " + str(population)
print(fixed)
5

Write three comparisons and print their boolean results

print(population > 50)
print(population == 54)
print(neighbors <= 2)

5. Expected Files Changed

FileActionWhy
operators.py Created Demonstrates arithmetic, string combination, explicit conversion, and comparisons.
docs/sessions/session-02/index.html Created This session document.
If you find yourself editing any other file, stop. This session touches exactly 2 files.

6. Commit Checkpoint

Once the lab is complete and you can explain every line, make this exact commit:

git add operators.py docs/sessions/session-02/index.html
git commit -m "session-02: arithmetic, f-strings, explicit type conversion, and comparisons"
Do not commit until you can answer out loud: "Why does Python raise an error instead of guessing what "Population: " + 54 should mean?"

7. Code Review Checklist

Go through your code line by line and check each item:


8. Post-Coding Quiz

Same 5 questions. Take it again now that you have written and run the code. You need 4/5 to mark this session complete.

Question 1 of 5

What does 7 // 2 return, as opposed to 7 / 2?

/ always produces a float result. // ("floor division") divides and throws away the remainder, giving you a whole number โ€” useful when you specifically want the whole-number part of a division.
Question 2 of 5

What does "Population: " + str(54) produce?

str() explicitly converts the number to text first, so + is now combining two strings (called "concatenation"), producing one longer string.
Question 3 of 5

What happens if you write "Population: " + 54 WITHOUT calling str() first?

Python does not automatically guess what you meant by mixing a string and a number with + โ€” it raises a TypeError rather than silently doing something that might not be what you intended. This is deliberate: implicit, silent type mixing causes more bugs than it saves keystrokes.
Question 4 of 5

Given population = "54" (a string), how do you get it as an actual number you could do math with?

int() converts a string of digits into an actual integer. Containing only digit CHARACTERS does not make something a number โ€” it is still text until you explicitly convert it.
Question 5 of 5

What does population > 50 evaluate to, given population = 54?

Comparison operators (>, <, ==, etc.) always produce a bool โ€” exactly True or False โ€” which is what makes them usable in the conditionals we cover next session.

9. Reflection Questions

Think through these after the post-quiz. No right answer โ€” they are for discussion.

  1. Why might a language that silently converted "5" + 5 into either 10 or "55" actually cause MORE bugs than Python's explicit-error approach?
  2. When would floor division (//) actually be the right tool, instead of an inconvenience compared to regular division?
  3. Rewrite one of your f-strings using + concatenation instead. How many extra str() calls did you need?
  4. What real-world value might arrive as text that you would need to convert before doing math with it? (Think about anything typed into a search box or form.)

10. What Breaks If This Knowledge Is Missing?

  • The classic string-vs-number bug: Forgetting to convert a value before doing arithmetic on it (or before combining it with text) is one of the most common bugs at every experience level, not just for beginners โ€” this session is designed to make the error message instantly recognisable.
  • Conditionals (next session): Every if-statement in the next session is built directly on the comparison operators from this session. If population > 50 does not clearly evaluate to a bool in your head, conditionals will feel like unexplained magic.

11. What We Learned

Python concept mastered: Arithmetic and comparison operators, string concatenation and f-strings, and explicit type conversion with int()/float()/str().

Unlocks: You can now compute, combine, and convert values โ€” and every comparison you write produces the exact ingredient conditionals need.

Next session: Session 03 โ€” Conditionals. We use the True/False results from comparisons to make a program actually branch and decide.