Python Syntax

Python · Getting Started

Theory

Python uses indentation, the spaces at the start of a line, to decide which statements belong together. A line that opens a block (if, for, while, def, class) ends with a colon, and every line inside that block must be indented by the same amount, conventionally 4 spaces. The block ends when the indentation goes back to the previous level. Other languages use curly braces for this; in Python the layout itself is the structure.

Because indentation has meaning, getting it wrong is an error, not a style issue. An unexpected indent or a missing indent raises IndentationError, and mixing tabs and spaces inconsistently raises TabError. Each statement normally sits on its own line with no semicolon, and an expression inside (), [] or {} can safely continue across several lines.

Why we use it

Forcing indentation makes every Python program look structured the same way, so code written by someone else is easy to read. There are no braces to mismatch, and what you see on screen is exactly how the code is grouped.

When to use it

Always indent the body of if, for, while, def, class, with and try blocks. Configure your editor to insert 4 spaces when you press Tab. To split a long line, wrap it in parentheses rather than ending lines with a backslash.

How it works

  1. Python reads a line ending with a colon, e.g. if 5 > 2:
  2. Expect the next line to be indented further
  3. Is the next line indented? Yes: It joins the if block. No: IndentationError: expected an indented block.
  4. Keep reading lines at the same indentation as part of the block
  5. A line back at the outer level closes the block
  6. Execution continues after the block

Syntax

if condition:
    do_something()

Example

if 5 > 2:
    print("Five is greater than two")

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic