Syntax — Python's Whitespace Grammar
$ on variables, no semicolons, no curly braces. Indentation defines blocks. Variables spring into existence on assignment. print() is your echo. That's most of it.The file is just code
Coming from PHP, the first relief is that there's no <?php tag. A .py file is code from the first byte. You run it with python script.py, or you type lines into a notebook cell. No "enter code mode" ceremony.
🐘 PHP: PHP needs the opening tag because it was born to sprinkle code into HTML. Python was born as a general scripting language, so the whole file is the program. No tag to forget at 1am.
Indentation IS the block
This is the headline. Where PHP and JavaScript use { } to group statements, Python uses indentation. The colon opens a block; the indented lines underneath are the block; dedenting closes it.
if revenue > target:
print("Above target")
bonus = revenue * 0.1
print("Always runs") # back at the left margin = outside the if
Pick four spaces per level and never look back — it's the universal convention, and notebooks/editors insert it for you when you press Tab. The one hard rule: don't mix tabs and spaces in the same file. Python will throw a TabError and you'll lose ten minutes to something invisible. Modern editors prevent this if you let them insert spaces for Tab.
🐘 PHP: In PHP, indentation is purely cosmetic — the braces do the real work, and you could write a whole function on one line. In Python the indentation is the braces. It feels strict for a day, then you realise every Python file on earth is formatted consistently because the language forces it. That's a gift when you read other people's code.
Variables — no dollar sign, no declaration
count = 0
name = "Eric"
is_enrolled = True
tags = ["python", "data", "ba"]
A variable exists the moment you assign to it. No $, no var/let, no type keyword. The naming convention is snake_case for variables and functions, PascalCase for classes, SCREAMING_SNAKE for constants — identical to the PHP conventions you already use, just without the prefix.
🐘 PHP: $name = "Eric"; becomes name = "Eric". Drop the dollar, drop the semicolon. The dollar sign's one perk — variables standing out visually — is gone, so lean on good names instead.
No semicolons (and no, you don't need them)
A newline ends a statement. You can put a semicolon between two statements on one line, but it's considered ugly and you'll almost never see it. One statement, one line.
x = 5
print(x)
If a single expression genuinely needs to span multiple lines, wrap it in parentheses and Python reads across the line breaks happily — no backslash gymnastics required:
total = (
base_price
+ tax
+ shipping
)
Printing and the f-string
print() is your output workhorse — think echo, but it's a function so it takes parentheses. The modern way to build strings with values baked in is the f-string: put f before the quotes and drop variables inside { }.
name = "Eric"
revenue = 1800
print(f"Hello, {name}. Revenue is ${revenue:,}.")
# Hello, Eric. Revenue is $1,800.
That :, inside the braces is a format spec — here it adds thousands separators, which you'll use constantly in financial reporting. F-strings are clean, fast, and the default choice; reach for them every time.
🐘 PHP: An f-string is exactly PHP's double-quoted interpolation ("Hello, $name") but more powerful, because the { } can hold any expression and a format spec. Plain string literals without the f are like PHP single quotes — literal, no substitution.
Comments and the docstring
# single-line comment, the only everyday kind
"""
Triple-quoted string. Used as a multi-line note, and
as a function's official documentation (a 'docstring').
"""
Python has just one comment marker: #. The triple-quoted string pulls double duty as multi-line notes and as documentation that tools can read. As in PHP, comment the why, not the obvious what.
Case sensitivity — consistent, unlike PHP
Good news: Python is case-sensitive everywhere, with no exceptions. Revenue, revenue, and REVENUE are three different names, full stop — for variables, functions, and classes alike. No "functions are case-insensitive but variables aren't" surprises. One rule, applied uniformly.
🐘 PHP: Remember PHP's quirk where strlen() and STRLEN() both work but $Name ≠ $name? Python doesn't have that split personality. Everything is case-sensitive, so just stay in lowercase snake_case and you'll never trip.
Your First Script
Goal: write a tiny standalone script that computes a simple business metric, so the syntax leaves the notebook and becomes a real file.
- In your
ba-labfolder, createmargin.py - Inside it:
revenue = 1800 cost = 1100 profit = revenue - cost margin = profit / revenue print(f"Profit: ${profit:,}") print(f"Margin: {margin:.1%}") - Run it from the terminal (venv active):
python margin.py - You should see
Profit: $700andMargin: 38.9%
Notice {margin:.1%} — the format spec multiplied by 100, added the percent sign, and rounded to one decimal. Format specs are a quiet superpower for clean reports.
if block and run it. Read the IndentationError — Python tells you the exact line and what it expected. Learning to read these calmly now saves you frustration later, because indentation slips are the most common beginner error.