🎯 Learning Objectives
- Create variables and assign values using
= - Understand Python's core data types:
int,float,str,bool,None - Use
type()to inspect any value - Follow Python's variable naming rules and conventions
- Convert between types with
int(),float(),str(),bool()
Variables
A variable is a name that refers to a value stored in memory.
You create one with the assignment operator =:
name = "Alice"
age = 30
pi = 3.14159
is_student = True
variables.py
The left side is the name; the right side is the value. Python figures out the type automatically — you never need to declare it.
Multiple assignment
# Assign several variables at once
x, y, z = 1, 2, 3
# Same value to multiple names
a = b = c = 0
# Swap two variables (no temp needed!)
x, y = y, x
multi_assign.py
Naming Rules & Conventions
| Rule | Example | Invalid |
|---|---|---|
| Must start with a letter or underscore | score, _private |
2fast ❌ |
| Can contain letters, digits, underscores | player_1 |
my-var ❌ |
| Case-sensitive | Name ≠ name ≠ NAME |
— |
| Cannot be a reserved keyword | my_class |
class ❌ |
PEP 8 conventions
- Variables & functions:
snake_case— all lowercase, words separated by underscores. - Constants:
UPPER_SNAKE_CASE— e.g.MAX_RETRIES = 5 - Classes:
PascalCase— e.g.HttpClient(covered in Lesson 20) - Avoid single-letter names except for simple loop counters (
i,j).
user_age over a; prefer is_valid over flag.
Core Data Types
Every value in Python has a type. Use the built-in type()
function to check:
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>
print(type("hello")) # <class 'str'>
print(type(True)) # <class 'bool'>
print(type(None)) # <class 'NoneType'>
check_types.py
int — Integers
Whole numbers with no decimal point. Python integers have unlimited precision — they can be as large as your memory allows.
count = 10
negative = -7
big = 1_000_000_000 # underscores for readability
hex_val = 0xFF # 255 in hexadecimal
bin_val = 0b1010 # 10 in binary
integers.py
float — Floating-Point Numbers
Numbers with a decimal point. Internally stored as 64-bit IEEE 754 doubles.
price = 9.99
temperature = -40.0
scientific = 6.022e23 # 6.022 × 10²³
floats.py
0.1 + 0.2 returns 0.30000000000000004, not 0.3.
This is a property of how all computers store decimals — not a Python bug.
Use the decimal module when precision matters (e.g. money).
str — Strings
Text data enclosed in single quotes, double quotes, or triple quotes:
greeting = "Hello, World!"
letter = 'A'
multiline = """This string
spans multiple
lines."""
# Strings are immutable — you can't change a character in place
# greeting[0] = 'h' ← TypeError!
strings_intro.py
We'll explore strings in full depth in Lesson 03.
bool — Booleans
Two possible values: True or False (capitalised). Used for logic and conditions.
is_active = True
has_permission = False
# Booleans are actually integers under the hood
print(True + True) # 2
print(False * 100) # 0
booleans.py
None — The Absence of a Value
None is Python's null value. It represents "nothing" or "not yet assigned".
result = None
if result is None:
print("No result yet")
none_example.py
is None (not == None) to check for None.
The is keyword checks identity, which is faster and more correct for singletons.
Type Conversion (Casting)
Python provides built-in functions to convert between types:
# String → Integer
age = int("25") # 25
# String → Float
price = float("9.99") # 9.99
# Number → String
label = str(100) # "100"
# Truthy / Falsy → Boolean
print(bool(0)) # False
print(bool("")) # False
print(bool(42)) # True
print(bool("hello")) # True
casting.py
Truthy and falsy values
When converted to bool, these values are falsy (evaluate to False):
| Value | Type |
|---|---|
0, 0.0 | int / float |
"" (empty string) | str |
[], (), {} | list / tuple / dict |
None | NoneType |
False | bool |
Everything else is truthy.
int("hello") → ValueError.
Always validate input before casting when the source is untrusted (e.g. user input).
Dynamic vs Static Typing
Python is dynamically typed — a variable's type is determined at runtime by whatever value is assigned to it. Compare this with statically typed languages (Java, C, Rust) where you must declare types up front.
x = 10 # x is an int
x = "ten" # now x is a str — no error, but be careful!
# Python 3.6+ supports TYPE HINTS (optional annotations)
age: int = 30
name: str = "Alice"
# These don't enforce anything at runtime — they're for
# documentation and tools like mypy (Lesson 30).
dynamic_typing.py
Primary source: Python Docs — Built-in Types
Ask your AI tutor! Confused about when to use int vs
float? Unsure why bool([]) is False?
Ask — these type fundamentals will come up in every single lesson going forward.
💻 Exercises
Create a file types.py. Define five variables — one of each core type
(int, float, str, bool,
None). For each, print both the variable and its type using
type().
Show solution
count = 42
rate = 3.14
name = "Python"
active = True
data = None
print(count, type(count)) # 42 <class 'int'>
print(rate, type(rate)) # 3.14 <class 'float'>
print(name, type(name)) # Python <class 'str'>
print(active, type(active)) # True <class 'bool'>
print(data, type(data)) # None <class 'NoneType'>
Given a = "left" and b = "right", swap their values
so that a becomes "right" and b becomes
"left". Do it in one line, without a temporary variable.
Show solution
a = "left"
b = "right"
a, b = b, a
print(a) # right
print(b) # left
Write a script that asks the user for their birth year using input(),
converts it to an integer, calculates their approximate age, and prints:
"You are about X years old."
Show solution
birth_year = input("Enter your birth year: ")
birth_year = int(birth_year) # cast string → int
age = 2024 - birth_year
print("You are about " + str(age) + " years old.")