Variables and Data Types
⏱ 15 min read
A variable is a name that points to a value. Python is dynamically typed — you don't declare the type, Python figures it out automatically.
The 4 Core Types:
int — stores whole numbers (unlimited size in Python!)
Example: age = 25
float — stores decimal numbers
Example: price = 9.99
bool — stores only True or False (capital T and F!)
Example: is_student = True
str — stores text (single or double quotes — same thing)
Example: name = "Alice"
Other Types:
None — represents no value (like null in Java)
complex — complex numbers: 3+4j
String Operations:
name = "Alice"
name.upper() — "ALICE"
name.lower() — "alice"
len(name) — 5
name[0] — "A" (first character)
name[-1] — "e" (last character)
name[1:4] — "lic" (slicing)
name.contains("li") → name — "li" in name → True
f-Strings — The Best Way to Format:
f"Hello, {name}!" — embed variable
f"Price: ${price:.2f}" — 2 decimal places
f"Next year: {age + 1}" — expressions work too!
Constants:
Python has no final keyword. Use ALL_CAPS names by convention to signal a constant.
Example: PI = 3.14159 / MAX_STUDENTS = 30
Naming Rules:
Use snake_case: first_name, total_score, is_active
Constants are ALL_CAPS: MAX_SIZE, PI
Python is case-sensitive: name, Name, and NAME are different.
The 4 Core Types:
int — stores whole numbers (unlimited size in Python!)
Example: age = 25
float — stores decimal numbers
Example: price = 9.99
bool — stores only True or False (capital T and F!)
Example: is_student = True
str — stores text (single or double quotes — same thing)
Example: name = "Alice"
Other Types:
None — represents no value (like null in Java)
complex — complex numbers: 3+4j
String Operations:
name = "Alice"
name.upper() — "ALICE"
name.lower() — "alice"
len(name) — 5
name[0] — "A" (first character)
name[-1] — "e" (last character)
name[1:4] — "lic" (slicing)
name.contains("li") → name — "li" in name → True
f-Strings — The Best Way to Format:
f"Hello, {name}!" — embed variable
f"Price: ${price:.2f}" — 2 decimal places
f"Next year: {age + 1}" — expressions work too!
Constants:
Python has no final keyword. Use ALL_CAPS names by convention to signal a constant.
Example: PI = 3.14159 / MAX_STUDENTS = 30
Naming Rules:
Use snake_case: first_name, total_score, is_active
Constants are ALL_CAPS: MAX_SIZE, PI
Python is case-sensitive: name, Name, and NAME are different.
Log in to track your progress and earn badges as you complete lessons.
Log In to Track Progress