Why Python?
Python is the language of clarity. It reads like English but executes like a machine. It strips away the syntactic noise of other languages, allowing you to focus purely on logic.
- Simple, readable syntax
- Massive library ecosystem
- Perfect for Data Science & AI
import this
# The Zen of Python:
# Simple is better than complex.
# Readability counts.
# Explicit is better than implicit.
Variables & Data Types
Think of variables as labeled boxes. Python figures out what you put in the box automatically.
Strings & Numbers
Text goes in quotes. Numbers don't. Python handles decimals (floats) and whole numbers (ints) seamlessly.
name = "Alice" # String
age = 25 # Integer
height = 5.6 # Float
print(name, age)
Booleans & Casting
True or False? Convert types using int(), str(), or float() when you need to mix them.
score = "100"
# Convert string to number
total = int(score) + 50
Decisions & Repetition
Code doesn't always run line-by-line. Sometimes it needs to make a choice
(if/else) or repeat a task (loops).
If / Elif / Else
Check conditions. Indentation (spaces) defines what belongs to the condition.
For Loops
Iterate over a sequence (like a list) a specific number of times.
While Loops
Keep running as long as a condition remains True. Beware of infinite loops!
Live Logic Example
if i % 2 == 0:
print(f"{i} is Even")
else:
print(f"{i} is Odd")
Data Structures
Don't just store one thing. Store collections of things efficiently.
Lists
[]Ordered, changeable, allows duplicates. Your go-to for sequences.
fruits.append("cherry")
# Access: fruits[0]
Tuples
()Ordered, unchangeable. Use for fixed data like coordinates.
# point[0] = 15
# Error! Cannot change
Dictionaries
{}Key-Value pairs. Unordered (in older versions). Lightning fast lookups.
"name": "John",
"age": 30
}
# Access: user["name"]
Functions & OOP
Don't Repeat Yourself (DRY). Write code once, reuse it forever. Object-Oriented Programming (OOP) lets you model real-world things as "Objects" with properties and actions.
def __init__(self, brand):
self.brand = brand
def drive(self):
print(f"{self.brand} is driving!")
my_car = Car("Tesla")
Lambda Functions
Small anonymous functions for quick operations.
square = lambda x: x * 2
List Comprehension
Create new lists in a single, readable line.
evens = [x for x in range(10) if x%2==0]
File Handling
Always use the with statement to handle
files safely.
with open('file.txt') as f: data = f.read()