Data Types & Casting
type(42), type(3.14), type("42"), type(True)Python has different kinds of values: whole numbers, decimals, text and True/False.
1Level 1 · BasicsBasics
Every value in Python has a type. The type decides what you can do with the value: you can divide numbers, but not words. Knowing the type of your data prevents a lot of confusing errors.
The four basic types
| Type | What it holds | Examples |
|---|---|---|
int | whole numbers | 7, -3, 2026 |
float | numbers with a decimal point | 3.14, -0.5, 2.0 |
str | text (a string of characters) | "hello", '42' |
bool | yes/no values | True, False |
There's also None, which means "no value yet". Use type() to ask Python what type something is.
The same symbol can mean different things
+ adds numbers but joins strings. "5" + "5" is "55", while 5 + 5 is 10. Quotes change everything!
Casting: converting between types
To change a value's type, call the type like a function:
int("42") # 42 text to whole number
float("2.5") # 2.5 text to decimal
str(99) # "99" number to text
int(7.9) # 7 drops the decimal part (no rounding)
bool(0) # False 0, "" and empty things are False
Casting matters most when numbers arrive as text. "20" + "5" gives "205", but int("20") + int("5") gives 25. You will use this all the time in the Input lesson, because whatever the user types arrives as a string.
When conversion fails
int("hello") and int("3.7") both raise a ValueError because that text isn't a whole number. Convert "3.7" with float() instead.
Key points: int, float, str and bool are the basic types;type()tells you the type;int(),float(),str()convert; numbers written in quotes are text until you convert them.
See it in actionConverting between types
Notice the quotes: "20" is text until int() turns it into a number.
Try it yourselfExample 1: Inspect types
type() shows the type of any value. Notice that "10" in quotes is a str, not an int.
Output appears hereWhat type is it?
Fill the blank so the program prints the type of 3.5: <class 'float'>.
x = 3.5 print((x))
Example 2: Text plus text, number plus number
The same + does two different jobs depending on the types.
Output appears hereText plus a number
This should print I am 12 years old, but it crashes. Fix it.
Example 3: int() cuts, round() rounds
int() simply drops the decimal part. Use round() when you want the nearest whole number.
Output appears hereCut or round?
Fill the blanks so the output is 7 8: first cut off the decimals of 7.6, then round it.
n = 7.6 print((n), (n))
Price from text
The price comes in as text: price = "250". Turn it into a number and print the price for 3 items: Total: 750.
Example 4: Truthy and falsy values
bool() turns any value into True or False. Zero and empty values are False; everything else is True.
Output appears hereEmpty or not?
Print the result of bool() for these three values, one per line, in this order: "", "hi", 0. The output should be False, True, False.
2Level 2 · IntermediateNumbers in depth
Python has more than one kind of number, and each behaves a little differently.
int has no size limit
12345678901234567890 * 98765432109876543210 just works. Python integers grow as large as memory allows. You can write long numbers with underscores: 1_000_000.
float is approximate
Decimal fractions are stored in binary, so 0.1 + 0.2 gives 0.30000000000000004. Tidy a float result with round(x, 2) before you show it.
Other bases and None
bin(10), hex(255) and int("ff", 16) convert between bases. None is the type for "no value yet".
See it in actionConverting step by step
Watch the type of each variable as text becomes numbers and back.
Example 5: Big integers and float surprises
The huge product is exact, but 0.1 + 0.2 is a tiny bit off. round() tidies it.
Output appears hereExample 6: Binary, hex and back
int() takes a second argument, the base of the text.
Output appears here3Level 3 · AdvancedExact numbers and conversion rules
For money, approximate floats are not good enough. There is a simple fix that uses only whole numbers.
Count money in the smallest unit
Store 19.99 Tk as 1999 paisa. Whole numbers are always exact, so totals never drift. Divide by 100 only when you show the result. (Later, in the Modules lesson, you will meet Decimal, which does exact decimal maths for you.)
Numeric tower
When types mix, Python widens: bool → int → float → complex. True + True is 2, and 3 + 2j is a complex number.
Rounding rules
round(2.5) gives 2 and round(3.5) gives 4: Python rounds halves to the nearest even number (banker's rounding). When you need the school rule "round half up" for a positive number, use int(x + 0.5).
See it in actionBooleans are numbers too
Watch how True counts as 1 when Python adds things up.
Example 7: Money that adds up exactly
Compare the float total with the paisa total. Then see the two rounding rules side by side.
Output appears hereExample 8: Mixing types
Each result's type is the "widest" type in the expression.
Output appears hereint("3.7") fails. Convert with float() first, then int() if you need a whole number: int(float("3.7")) gives 3.What does this print?
print("5" * 3)50 graded problems are waiting in Topic Practice.