On this page
  1. Syntax and Variables
  2. Contents
  3. Indentation & Statements
  4. Dynamic Typing & Name Binding
  5. Naming Conventions & Reserved Keywords
  6. Basic Input & Output

Syntax and Variables

Prev: Core Fundamentals | Up: Core Fundamentals | Next: Primitive Data Types
Rel: python fundamentals, print, input, id, type, keyword, sys
Ref: docs.python.org — Informal Introduction to Python | PEP 8 — Style Guide
Tags: #public #python3 #tutorials #fundamentals #syntax


Contents


Indentation & Statements

Python uses indentation (4 spaces per level by PEP 8) to delimit code blocks instead of curly braces. Statements normally end at a newline; expressions inside parentheses (), brackets [], or braces {} continue across lines implicitly.

total = (
    10
    + 20
    + 30
)
if total > 50:
    print("Above threshold:", total)  # Inline comment

Dynamic Typing & Name Binding

Variables are references (names) bound to objects in memory. Types belong to objects, not to variable names, so a name can be rebound to an object of a different type at any time.

value = 42
print(type(value), id(value))   # <class 'int'> ...

value = "forty-two"
print(type(value), id(value))   # <class 'str'> ...

# Multiple assignment and tuple swapping
x, y = 1, 2
x, y = y, x
assert (x, y) == (2, 1)

Naming Conventions & Reserved Keywords

Use keyword to inspect reserved identifiers programmatically:

import keyword

print(keyword.iskeyword("match"))     # False (soft keyword)
print(keyword.issoftkeyword("match")) # True
print(keyword.iskeyword("return"))    # True
print("valid_name".isidentifier())    # True

Basic Input & Output

import io
import sys

# Formatted print options
print("alpha", "beta", "gamma", sep=" | ", end="!\n")

# Simulating stdin with io.StringIO
buf = io.StringIO("Ada Lovelace\n")
name = buf.readline().strip()
print(f"Hello, {name}!", file=sys.stdout)

Linked from

Related notes