
Python Variable
Python Variable
Indentation
Indentation
f-string
f-string
Everything you build in Python is just a combination of describing data (variables) and controlling what happens next (loops & conditions). Here are the foundational blocks.
Indentation is law. Python uses whitespace (spaces/tabs) to define code blocks instead of {}. Get your indentation right, or nothing works.
| Concept | Syntax | Example | Common Mistake |
|---|---|---|---|
| Variables | name = value | age = 25 | name = "5" + 5 (TypeError) |
| Type Conversion | int(), float(), str() | int(input()) | forgetting to convert input() |
| Comparison | ==, !=, <, > | if score == 90: | if score = 90: (assignment!) |
You don't declare types, but you must respect them. Mixing types blindly (like "5" + 5) is the #1 beginner mistake.
int (whole numbers), float (decimals)str (text strings), bool (True/False)What happens when you run `'5' + 5` in Python?
input() always returns a string. If you need a number, you must convert it immediately using int() or float().
age = int(input("Age: "))
print(f"You will be {age + 1} next year.")
What does `input()` return in Python?
Make decisions using comparisons (==, !=, <, >). Always use == for checking equality!
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
Repeat actions without going insane.
for when you know exactly how many times to loop (e.g., iterating a list).while when looping until a condition changes. Watch out for infinite loops!When should you use a `while` loop instead of a `for` loop?
| Collection | Syntax | Key Feature | Use When |
|---|---|---|---|
| List | [1, 2, 3] | Ordered, mutable, allows duplicates | Sequence of items |
| Tuple | (1, 2, 3) | Ordered, immutable | Fixed collection, dictionary keys |
| Set | {1, 2, 3} | Unordered, unique, O(1) lookup | Membership testing |
| Dict | {'a': 1} | Key-value pairs, O(1) lookup | Mapping keys to values |
Lists ([]) are the easiest way to store multiple items. Remember that indexing starts at 0.
nums = [10, 20, 30]
print(nums[0]) # 10
Stop rewriting the same code! Use def to create reusable blocks of logic.
def greet(name):
return f"Hi, {name}!"
Python will yell at you with SyntaxError or ValueError. This is normal. Learning to read the Traceback is the real secret to mastering Python.
Which collection should you use for fast O(1) membership testing?
dir([]) and dir({}) to see built-in methods.SyntaxError or ValueError, read the traceback carefully - it tells you exactly what went wrong and where.Ready for the next step? Check out Setting Up Your Python Environment for virtual environments and package management, or jump to Python Tricks & Patterns for practical patterns that save time.
Clean AI CLI logs, build cron expressions, decode JWTs, and calculate chmod permissions offline.

Python uses underscores as naming conventions, not enforced access modifiers - understanding the difference prevents confusing code and helps you write better APIs.
Read more
A practical collection of Python techniques that reduce boilerplate, prevent bugs, and make everyday code cleaner - selected from real codebases, not tutorial fantasy.
Read more
Master pytest for production Python: fixtures, parameterization, monkeypatch mocking, custom markers, and clean test suite architecture.
Read more