Variables, Data Types & Literals¶
A variable is a name that points to a value. Python figures out the type automatically — no need to declare it.
Creating a variable¶
Use = to assign. Python infers the type from what you put on the right.
The main data types¶
| Type | What it is | Example |
|---|---|---|
int |
Whole number | 42, -7, 0 |
float |
Decimal number | 3.14, -0.5 |
str |
Text | "hello", 'world' |
bool |
True or False | True, False |
list |
Ordered collection | [1, 2, 3] |
tuple |
Like a list but unchangeable | (1, 2, 3) |
dict |
Key-value pairs | {"name": "Alice"} |
set |
Unique items | {1, 2, 3} |
NoneType |
"Nothing" | None |
Check a value's type with type():
print(type(42))
print(type(3.14))
print(type("hello"))
print(type(True))
print(type([1, 2, 3]))
print(type(None))
Multiple assignment¶
Assign multiple variables in one line:
Swap two variables (Pythonic)¶
In most languages this needs a temp variable. In Python:
Naming rules¶
✅ OK: name, age2, _private, user_name, MAX_SIZE
❌ Not OK: 2name (starts with digit), user-name (hyphen), class (reserved keyword)
Convention: use snake_case for variables and functions, UPPER_CASE for constants.
Reserved keywords¶
You can't use these as variable names — Python uses them for syntax:
False None True and as assert async await break class
continue def del elif else except finally for from
global if import in is lambda nonlocal not or pass
raise return try while with yield
Try this — it fails because class is reserved:
Rename it to class_name and it works.
Literals — the values themselves¶
A literal is a value written directly in the code (42, "hello", True).
Number literals:
decimal = 100
binary = 0b1010 # binary — 10 in decimal
octal = 0o12 # octal — 10 in decimal
hexadecimal = 0xFF # hex — 255 in decimal
print(decimal, binary, octal, hexadecimal)
String literals — single or double quotes both work:
a = 'hello'
b = "world"
c = """A multi-line
string that spans
several lines."""
print(a, b)
print(c)
Practice¶
Make this assign 1, 2, 3 to a, b, c
Expected: 1 2 3
Quiz — Quick check¶
What you remember
Q1. Which variable name is not valid in Python?
-
_private -
user_name -
2nd_user -
MAX_SIZE
Why: Variable names can't start with a digit. They must start with a letter or underscore.
Q2. What does type(3.14) return?
-
<class 'int'> -
<class 'float'> -
<class 'decimal'> -
<class 'number'>
Why: Any literal with a decimal point is a
float.intis for whole numbers only.
Q3. What's the convention for variable names in Python?
- camelCase
- PascalCase
- snake_case
- kebab-case (hyphens aren't even allowed)
Why: PEP 8 (the style guide) recommends
snake_casefor variables and functions, andUPPER_SNAKE_CASEfor constants.PascalCaseis reserved for class names.
Common doubts¶
Do I need to declare the type, like int age = 25?
No. Python is dynamically typed — the type is inferred from the value. age = 25 is enough. You can add optional type hints (age: int = 25) for readability and tooling, but they don't change how the code runs.
What's the difference between =, ==, and is?
= assigns a value (x = 5). == compares for equality (x == 5). is checks if two names point to the same object in memory (x is None). You almost always want == for comparisons, except when checking against None/True/False — there, is is the convention.
Can a variable change type?
Yes — Python doesn't lock the type. x = 5 then x = "hello" is perfectly legal. It's flexible but can lead to bugs; in real codebases people usually keep the type stable per variable.