📘 Lesson 04 · Beginner

Python Data Types

Explore Python's built-in data types: int, float, string, boolean with type() and conversion examples.

Why Data Types Matter

Every value in Python has a type that determines what operations are valid on it. You can multiply two integers, but you can't divide a string by a number. Understanding types helps you write correct code and debug errors when you accidentally mix incompatible values. Use type() to inspect any value's type at runtime.

int and float

Integers (int) are whole numbers with no size limit in Python. Floats (float) are decimal numbers. When you divide two integers with /, Python always returns a float — even if the result is a whole number. Use // if you need an integer result. You can use underscores in large numbers to improve readability (e.g. 1_000_000).

numbers.py
age = 25            # int
price = 9.99        # float
big = 1_000_000     # underscores for readability

print(type(age))
print(type(price))
print(7 / 2)          # always returns float
print(7 // 2)         # floor division → int
print(age + price)    # int + float = float
▶ Output


3.5
3
34.99

Strings

A string is a sequence of characters enclosed in single or double quotes — both work identically. Triple quotes allow multi-line strings. Strings are immutable: once created, individual characters cannot be changed. You can combine strings with + (concatenation) and repeat them with *.

strings.py
s1 = "Hello"             # double quotes
s2 = 'World'             # single quotes — same thing
s3 = """Spans
multiple lines"""

print(s1 + " " + s2)   # concatenation
print(s1 * 3)          # repeat
print(len(s1))          # character count
print(s1[0])           # first character (index 0)
▶ Output
Hello World
HelloHelloHello
5
H

Booleans

A boolean is either True or False — always capitalised in Python. Booleans are the result of comparisons and control how your program makes decisions. Under the hood, True equals 1 and False equals 0, which is why arithmetic on booleans works.

bools.py
is_active = True
print(type(is_active))
print(10 > 5)        # comparison → bool
print(3 == 4)        # False — == checks equality
print(True + True)   # True=1, so 1+1=2
▶ Output

True
False
2

Type Conversion

Convert between types using built-in functions: int(), float(), str(), bool(). This is essential because input() always returns a string — you must convert it to a number before doing arithmetic. If conversion is impossible (like int("hello")), Python raises a ValueError.

convert.py
print(int("42"))        # string → integer
print(float("3.14"))    # string → float
print(str(100))         # integer → string "100"
print(bool(0))          # 0 → False
print(bool(""))         # empty string → False
print(bool("hi"))       # non-empty → True
▶ Output
42
3.14
100
False
False
True
Empty values — 0, "", [], {{}}, None — are all considered False in a boolean context. Everything else is True.

🧠 Quick Check

What does type(3.14) return?

Tags

data typesintfloatstringbooltype()conversion