Variables & Data Types
The absolute foundation. Learn how Python stores and works with information — strings, numbers, booleans, and more.
What is a Variable?
Think of a variable as a labelled box. You put something inside, give the box a name, and later you can open it to see what's there — or swap the contents.
In Python, you create a variable the moment you assign a value to it. No var, no let, no type declaration needed.
# Creating variables — just name = value
name = "Alice" # str (text)
age = 25 # int (whole number)
height = 1.68 # float (decimal)
is_student = True # bool (True or False)
print(name) # Alice
print(age) # 25
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
Output
TipPython is dynamically typed — the type is attached to the value, not the variable name. You can reassign a variable to any type at any time.
Python's Core Data Types
Every value in Python belongs to a type. The four you'll use constantly are:
| Type | Example | Description |
|---|---|---|
| str | "Hello" | Text — anything in quotes |
| int | 42 | Whole numbers |
| float | 3.14 | Decimal numbers |
| bool | True | Yes/No, On/Off values |
Working with Strings
Strings are sequences of characters. Python gives you a huge toolkit for manipulating them.
first = "John"
last = "Doe"
# f-strings (the modern way — Python 3.6+)
print(f"Hello, {first} {last}!") # Hello, John Doe!
print(f"Name has {len(first)} letters") # Name has 4 letters
# String methods
text = " python is awesome "
print(text.strip()) # "python is awesome"
print(text.strip().upper()) # "PYTHON IS AWESOME"
print(text.strip().title()) # "Python Is Awesome"
print(text.strip().replace("awesome", "great")) # "python is great"
# Slicing: [start:stop:step]
word = "Python"
print(word[0]) # P (first character)
print(word[-1]) # n (last character)
print(word[0:3]) # Pyt (first 3)
print(word[::-1]) # nohtyP (reversed!)
Output
Numbers and Arithmetic
x, y = 10, 3
print(x + y) # 13 — addition
print(x - y) # 7 — subtraction
print(x * y) # 30 — multiplication
print(x / y) # 3.333... — division (always float)
print(x // y) # 3 — floor division (whole number)
print(x % y) # 1 — modulo (remainder)
print(x ** y) # 1000 — exponentiation (10^3)
# Type conversion
pi = 3.14159
print(int(pi)) # 3 (truncates, doesn't round)
print(round(pi, 2)) # 3.14
print(str(42)) # "42" (number to string)
print(int("99")) # 99 (string to number)
Output
Booleans and Comparison
Booleans are the result of comparisons. They're the backbone of all decision-making in code.
age = 20
# Comparison operators → produce True/False
print(age == 20) # True — equal
print(age != 18) # True — not equal
print(age > 18) # True — greater than
print(age <= 20) # True — less than or equal
# Logical operators
has_id = True
has_ticket = False
print(has_id and has_ticket) # False — both must be True
print(has_id or has_ticket) # True — at least one True
print(not has_id) # False — reverses it
# Truthiness — these are all "falsy":
# False, 0, 0.0, "", [], {}, None
print(bool("")) # False
print(bool("hi")) # True
print(bool(0)) # False
print(bool(42)) # True
Output
Watch outPython is case-sensitive.
True and False must be capitalised. true will cause a NameError.Variable Naming Rules
# ✅ Valid names (use snake_case by convention)
user_name = "Alice"
total_price = 49.99
_private_var = 42
age2 = 25
MAX_RETRIES = 3 # ALL_CAPS for constants
# ❌ Invalid names
# 2fast = True → can't start with a digit
# user-name = "Bob" → hyphens not allowed
# for = 10 → can't use Python keywords
# Python keywords you can't use as names:
import keyword
print(keyword.kwlist)
Output