Lesson · Python for Beginners
Numbers, strings, and booleans
The kind of a value affects which operations make sense and what their results mean.
What you will learn
- Distinguish integers, floats, strings, and booleans
- Convert compatible values
- Avoid mixing text and numbers accidentally
Types describe values
An integer represents a whole number, while a float can represent a number with a fractional part. A string is text surrounded by quotes, and a boolean is either True or False. These are values, not labels: the characters '8' form text, while 8 is a number, even though they look similar.
Operations depend on type
Adding two numbers calculates a sum, while adding two strings joins their text. Python does not guess when you try to combine text with a number in an unsupported way; it raises a TypeError. Inspecting a value with type can help while learning, but clear variable names and deliberate conversions are better long-term habits.
Convert at the boundary
The input function returns text, even when a person types digits. Convert that text with int for a whole-number calculation or float when a decimal is needed. Conversion can fail for unsuitable text, so later lessons will show how to report that problem clearly instead of assuming every input is valid.
Booleans represent decisions
Comparisons such as greater-than produce a boolean. That result can be stored, printed, or used to choose a branch. Do not confuse the text 'True' with the boolean True: the first is a string and the second is a value used in logical conditions.
Choose conversions deliberately
Conversion should happen at the boundary where text becomes data for a calculation. Decide whether a decimal should be rounded, rejected, or preserved instead of silently truncating it. For example, converting a float to int drops its fractional part. Test a valid value, an empty string, and unexpected punctuation so the program's assumptions are visible.
price = 42
quantity_text = '3'
quantity = int(quantity_text)
total = price * quantity
print(type(price).__name__)
print(total)
Expected output
int
126
Practice exercise
Convert the text '18' to an integer, add 2, and print the result.
Show a hint
Call int with the text before using the plus operator.
Show one possible solution
age_text = '18'; age = int(age_text); print(age + 2) prints 20.
Quick check
Input arrives as text. A program must convert it before using it as a number.