Lesson 3 of 30
Variables and Data Types
Integers, floats, strings, booleans, and how Python's dynamic typing works.
What is a Variable?
A variable is a named container that stores a value. Unlike many languages, Python does not require you to declare a variable's type — Python infers it automatically.
age = 25 # integer
price = 9.99 # float
name = "Alice" # string
is_student = True # boolean
Core Data Types
| Type | Keyword | Example |
|---|---|---|
| Integer | int | 42 |
| Float | float | 3.14 |
| String | str | "Hello" |
| Boolean | bool | True / False |
| None | NoneType | None |
Checking the Type
Use the built-in type() function to inspect any value:
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("hi")) # <class 'str'>
print(type(True)) # <class 'bool'>
Type Conversion
x = "10"
y = int(x) # "10" → 10
z = float(x) # "10" → 10.0
s = str(99) # 99 → "99"
Naming Rules
- Must start with a letter or underscore (
_). - Can contain letters, digits, and underscores.
- Case-sensitive:
nameandNameare different. - Cannot use Python keywords like
if,for,while.
⚠️ Convention
Python convention (PEP 8) uses snake_case for variable names: student_name, total_price. Avoid single-letter names except in short loops.