Python Variables, Data Types and Operators with Examples
Part 2 of the Python for Everyone track. Last updated: September 2026.
Python is dynamically typed: you never declare a variable's type — the interpreter figures it out from the value. This makes code short, but means you should understand exactly what types exist and how they behave.
Variables: the rules
name = "Ada" # no type declaration needed age = 36 pi = 3.14159 is_active = True nothing = None # Python's "null" # Naming rules: letters, digits, underscore; cannot start with a digit # Convention: snake_case for variables and functions user_name = "grace"
Use type() to inspect any value: type(age) returns <class 'int'>.
The core data types
- int — whole numbers, any size: 10, -3, 10**100 (no overflow!)
- float — decimals: 3.14, -0.5, 2.0
- str — text in quotes: "hello", 'hello'
- bool — True / False (capital T/F)
- None — the absence of a value (like null)
Type conversion
int("42") # 42
float("3.14") # 3.14
str(100) # "100"
bool(0) # False — 0, "", [], None are all falsy
bool("hello") # True
Watch out: "5" + 3 raises TypeError. Python will not silently mix strings and numbers — convert explicitly.
Operators
# Arithmetic print(7 / 2) # 3.5 (true division — always float) print(7 // 2) # 3 (floor division) print(7 % 2) # 1 (remainder) print(2 ** 10) # 1024 (power) # Comparison — result is always a bool print(5 == 5) # True print(5 != 3) # True print(4 <= 4) # True # Logical print(True and False) # False print(True or False) # True print(not True) # False # Assignment shortcuts x = 10 x += 5 # x is now 15 (also -=, *=, /=, //=, %=, **=)
f-strings: the modern way to build strings
name = "Ada"
score = 95.5
print(f"{name} scored {score}") # Ada scored 95.5
print(f"{score:.1f}") # 95.5 — one decimal place
print(f"{name!r}") # 'Ada' — with quotes (debug-friendly)
Prefer f-strings over concatenation and .format() — they are faster to write and easier to read.
Key takeaways
- No type declarations — but every value still has a type; check with type().
- / always gives a float; // floors; ** is power.
- Convert explicitly: int(), float(), str(), bool().
- Use f-strings for all string building.
Next in this series: Python Strings: The Complete Guide with Examples.
Comments
Post a Comment