On this page
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
- > Dynamic Typing & Name Binding
- > Naming Conventions & Reserved Keywords
- > Basic Input & Output
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
snake_case: variables, functions, modules (user_count,parse_line).UPPER_SNAKE_CASE: module-level constants (MAX_RETRIES = 3).PascalCase: classes and exceptions (HttpClient,ParseError)._leading_underscore: internal/private-by-convention names._: throwaway variable in unpacking or loops (for _ in range(3): ...).
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
[[__builtins__/print|print()]]writes formatted text tosys.stdout(configurable viasep,end,file, andflush).[[__builtins__/input|input()]]reads a line fromsys.stdinand returns astrwithout the trailing newline.- For scriptable or non-interactive streams, read from
[[stdlib_module/sys|sys.stdin]]or[[stdlib_module/io|io.StringIO]].
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
- tuts/python3/tutorials/core_fundamentals Linked here; links to this page; shared tags: #fundamentals, #tutorials
- tuts/python3/tutorials/core_fundamentals/primitive_data_types Linked here; links to this page; shared tags: #fundamentals, #tutorials
- tuts/python3/python fundamentals Linked here; links to this page; shared tags: #fundamentals
- tuts/python3/tutorials links to this page; shared tags: #tutorials
- tuts/python3/__builtins__/id Linked here
- tuts/python3/__builtins__/input Linked here
- tuts/python3/__builtins__/print Linked here
- tuts/python3/__builtins__/type Linked here
- tuts/python3/stdlib_module/keyword Linked here
- tuts/python3/stdlib_module/sys Linked here