Variables, Data Types & Type System
Duration: 5 min
Visual: Type Hierarchy
┌─────────────────────────────────┐
│ Python Type System │
├─────────────────────────────────┤
│ │
│ Immutable: int, float, str │
│ Mutable: list, dict, set │
│ Special: None, bool │
│ Callable: function, class │
│ │
└─────────────────────────────────┘Key Concepts Table
| Type | Immutable | Example | Use |
|---|---|---|---|
| int | Yes | 42 | Integers |
| float | Yes | 3.14 | Decimals |
| str | Yes | "hello" | Text |
| list | No | [1,2,3] | Ordered, changeable |
| tuple | Yes | (1,2,3) | Ordered, fixed |
| dict | No | {"a":1} | Key-value pairs |
| set | No | {1,2,3} | Unique values |
| bool | Yes | True | True/False |
Installing Python
Download Python 3.9 or later from python.org. We recommend Python 3.10 or 3.11 for the best balance of features and library support.
Installation Steps:
- Go to python.org
- Download the installer for your OS
- Run the installer
- IMPORTANT (Windows): Check 'Add Python to PATH'
- Complete installation
# Verify Python installation
python --version
# or
python3 --version
# Check pip (package manager)
pip --versionPython 3.10.7
pip 21.0.1 from /usr/lib/python3.10/site-packages/pipUnderstanding Virtual Environments
A virtual environment is an isolated Python installation for each project. Think of it as a sandbox where each project has its own set of libraries.
Why is this important?
Imagine you have two projects:
- Project A needs TensorFlow 2.8
- Project B needs TensorFlow 2.10
Without virtual environments, you can only have one version installed globally. Virtual environments solve this by letting each project have its own Python installation with its own libraries.
Benefits:
- No conflicts between projects
- Easy to share projects (requirements.txt)
- Prevents 'works on my machine' problems
- Easy to clean up (just delete the folder)
# Create a virtual environment named 'venv'
python -m venv venv
# Activate it (macOS/Linux)
source venv/bin/activate
# Activate it (Windows)
venv\Scripts\activate
# You should see (venv) at the start of your terminal line
# Now install packages - they go into venv only
pip install numpy pandas
# Deactivate when done
deactivateImportant: Always activate your virtual environment before installing packages. Never use sudo with pip in a venv.
Managing Dependencies
# Save all installed packages to a file
pip freeze > requirements.txt
# This creates a file like:
# numpy==1.21.0
# pandas==1.3.0
# scikit-learn==0.24.2
# Share this file with others
# They can install all packages with:
pip install -r requirements.txt💡 Tip: Always use virtual environments. It's a best practice that will save you hours of debugging.
❓ What is the main purpose of a virtual environment?
Dynamic Typing in Python
Python uses dynamic typing, meaning you don't declare variable types explicitly. The type is inferred from the value assigned. This is different from statically-typed languages like Java where you must declare types upfront.
Learn more: https://docs.python.org/3/tutorial/
# Dynamic typing - type is inferred
x = 42 # x is an int
print(type(x)) # <class 'int'>
x = "Hello" # x is now a string
print(type(x)) # <class 'str'>
x = 3.14 # x is now a float
print(type(x)) # <class 'float'>
# Type checking
if isinstance(x, float):
print("x is a float")
# Type conversion
num_str = "123"
num_int = int(num_str) # Convert string to int
print(f"Converted: {num_int}, type: {type(num_int)}")<class 'int'>
<class 'str'>
<class 'float'>
x is a float
Converted: 123, type: <class 'int'>💡 Tip: Use type() to check a variable's type, or isinstance() for more flexible type checking. Type conversion functions like int(), str(), float() are commonly used.
❓ What is a best practice when working with Variables, Data Types & Type System?
Practice Quizzes
Quiz 1: Which is mutable?
- str
- tuple
- [✓] list
- int
Quiz 2: What is type casting?
- Changing variable name
- [✓] Converting between types
- Deleting variables
- Creating new types
Quiz 3: When would you use a tuple instead of a list?
- Always
- When you need mutability
- [✓] When data shouldn't change
- Never