Chapter 2: Variables and Simple Data Types
Data types are the foundation. Master these before moving on.
Variables
Variables are labels pointing to values, not boxes containing them.
message = "Hello, Python"
print(message)
message = "Hello, World" (1)
print(message)
| 1 | Variables can be reassigned |
Naming Rules
| Valid | Invalid |
|---|---|
|
|
|
|
|
|
|
|
Convention: snake_case for variables, UPPER_CASE for constants.
Name Errors
message = "Hello"
print(mesage) # typo
NameError: name 'mesage' is not defined. Did you mean: 'message'?
Python tells you what’s wrong. Read the error.
Strings
Text data. Single or double quotes - pick one style.
name = "ada lovelace"
name = 'ada lovelace' # equivalent
String Methods
Methods are actions called with .method():
name = "ada lovelace"
name.title() # 'Ada Lovelace'
name.upper() # 'ADA LOVELACE'
name.lower() # 'ada lovelace'
Methods don’t modify the original - they return new strings:
name = "ada lovelace"
name.upper()
print(name) # still 'ada lovelace'
name = name.upper() (1)
print(name) # now 'ADA LOVELACE'
| 1 | Reassign to keep the change |
f-Strings
Embed variables in strings with f"":
first = "ada"
last = "lovelace"
full = f"{first} {last}" # 'ada lovelace'
greeting = f"Hello, {full.title()}!" # 'Hello, Ada Lovelace!'
f-strings can contain any expression:
f"2 + 2 = {2 + 2}" # '2 + 2 = 4'
f"Length: {len(name)}" # 'Length: 12'
Whitespace
Special characters for formatting:
print("Line1\nLine2") # newline
print("\tIndented") # tab
Strip unwanted whitespace:
text = " python "
text.strip() # 'python'
text.lstrip() # 'python '
text.rstrip() # ' python'
Prefixes and Suffixes
url = "https://example.com"
url.removeprefix("https://") # 'example.com'
filename = "script.py"
filename.removesuffix(".py") # 'script'
Syntax Errors
Quotes must match:
# Wrong
message = 'Python's great' # apostrophe breaks it
# Right
message = "Python's great" # double quotes outside
message = 'Python\'s great' # escape the apostrophe
Numbers
Integers
2 + 3 # 5 (add)
3 - 2 # 1 (subtract)
2 * 3 # 6 (multiply)
3 / 2 # 1.5 (divide - always returns float)
3 // 2 # 1 (floor division)
3 % 2 # 1 (modulo - remainder)
3 ** 2 # 9 (exponent)
Order of operations applies. Use parentheses for clarity:
2 + 3 * 4 # 14
(2 + 3) * 4 # 20
Floats
Numbers with decimals:
0.1 + 0.2 # 0.30000000000000004
| Floating-point math has precision limits. This is true in all languages. |
Mixing Types
Any operation with a float returns a float:
1 + 2.0 # 3.0
4 / 2 # 2.0 (division always returns float)
Readability
Underscores in large numbers:
universe_age = 14_000_000_000
print(universe_age) # 14000000000
Multiple Assignment
x, y, z = 1, 2, 3
a = b = c = 0
Constants
Python has no true constants. Convention: ALL_CAPS means "don’t change this":
MAX_CONNECTIONS = 5000
PI = 3.14159
Comments
# This is a comment
x = 1 # inline comment
Write comments that explain why, not what:
# Bad: increment x by 1
x += 1
# Good: retry count for transient failures
x += 1
Exercises
2-1. Variables
Assign your name to a variable. Print a greeting using an f-string.
2-2. Cases
Store a name in lowercase. Print it in title(), upper(), and lower().
2-3. Strip
Create a string with leading/trailing whitespace. Print it with each strip method.
2-4. Math
Write four different operations that all result in 8.
2-5. Favorite Number
Store your favorite number in a variable. Print a message using it.
Summary
| Concept | Example |
|---|---|
Variable assignment |
|
f-string |
|
String methods |
|
Integer ops |
|
Comments |
|
Next: Lists - storing collections of data.