0% found this document useful (0 votes)
7 views24 pages

Python Syntax and Semantics

The document provides an overview of Python's syntax, semantics, and design philosophy, emphasizing its readability and simplicity. It details Python's keywords, function annotations, module organization, and data structures, highlighting the language's dynamic typing and collection types. Additionally, it discusses the object system, string literals, and the importance of indentation in Python code structure.

Uploaded by

sushantkr743125
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views24 pages

Python Syntax and Semantics

The document provides an overview of Python's syntax, semantics, and design philosophy, emphasizing its readability and simplicity. It details Python's keywords, function annotations, module organization, and data structures, highlighting the language's dynamic typing and collection types. Additionally, it discusses the object system, string literals, and the importance of indentation in Python code structure.

Uploaded by

sushantkr743125
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python syntax and semantics

The syntax of the Python programming language is the set of


rules that defines how a Python program will be written and
interpreted (by both the runtime system and by human
readers). The Python language has many similarities to Perl,
C, and Java. However, there are some definite differences
between the languages. It supports multiple programming
paradigms, including structured, object-oriented
programming, and functional programming, and boasts a
dynamic type system and automatic memory management.

Python's syntax is simple and consistent, adhering to the


principle that "There should be one—and preferably only one
—obvious way to do it." The language incorporates built-in
data types and structures, control flow mechanisms, first-class A snippet of Python code demonstrating
functions, and modules for better code reusability and binary search
organization. Python also uses English keywords where other
languages use punctuation, contributing to its uncluttered
visual layout.

The language provides robust error handling through exceptions, and includes a debugger in the standard
library for efficient problem-solving. Python's syntax, designed for readability and ease of use, makes it a
popular choice among beginners and professionals alike.

Design philosophy
Python was designed to be a highly readable language.[1] It has a relatively uncluttered visual layout and
uses English keywords frequently where other languages use punctuation. Python aims to be simple and
consistent in the design of its syntax, encapsulated in the mantra "There should be one— and preferably
only one —obvious way to do it", from the Zen of Python.[2]

This mantra is deliberately opposed to the Perl and Ruby mantra, "there's more than one way to do it".

Keywords
Python 3 has 35 keywords or reserved words; they cannot be used as identifiers.[3][4]

and await[note 1] def


as break del
assert class elif
async[note 1] continue else
except in raise
False[note 2] is return
finally lambda True[note 2]
for None try
from nonlocal[note 3] while
global not with
if or yield
import pass
In addition, Python 3 also has 4 soft keywords, including type added in Python 3.12. Unlike regular hard
keywords, soft keywords are reserved words only in the limited contexts where interpreting them as
keywords would make syntactic sense. These words can be used as identifiers elsewhere, in other words,
match and case are valid names for functions and variables.[6][7]

_[note 4]
case[note 4]
match[note 4]
type

Function annotations
Function annotations (type hints) are defined in PEP 3107.[8] They allow attaching data to the arguments
and return of a function. The behaviour of annotations is not defined by the language, and is left to third
party frameworks. For example, a library could be written to handle static typing:[8]

def haul(item: Haulable, *vargs: PackAnimal) -> Distance:


# implementation here

While annotations are optional in Python, the rest of this article will use annotations to provide clarity.

Modules and import statements


In Python, code is organized into files called modules, and namespaces are defined by the individual
modules. Since modules can be contained in hierarchical packages, then namespaces are hierarchical
too.[9][10] In general when a module is imported then the names defined in the module are defined via that
module's namespace, and are accessed in from the calling modules by using the fully qualified name.

# assume ModuleA defines two functions : func1() and func2() and one class : Class1
import ModuleA

ModuleA.func1()
ModuleA.func2()
a: ModuleA.Class1 = Modulea.Class1()
The from ... import ... statement can be used to insert the relevant names directly into the
calling module's namespace, and those names can be accessed from the calling module without the
qualified name:

# assume ModuleA defines two functions : func1() and func2() and one class : Class1
from ModuleA import func1

func1()
func2() # this will fail as an undefined name, as will the full name ModuleA.func2()
a: Class1 = Class1() # this will fail as an undefined name, as will the full name ModuleA.Class1()

Since this directly imports names (without qualification) it can overwrite existing names with no
warnings.

A special form of the statement is from ... import * which imports all names defined in the
named package directly in the calling module's namespace. Use of this form of import, although
supported within the language, is generally discouraged as it pollutes the namespace of the calling
module and will cause already defined names to be overwritten in the case of name clashes.[11] However,
this page will present code as if the line "from typing import *" were included, for referring to
collection types.

The different import statements are demonstrated here:

# imports the argument parsing module


import argparse
# imports the Pattern class from the regular expressions module
from re import Pattern
# imports all symbols inside the typing module
from typing import *

Using from import statements in Python can simplify verbose namespaces, such as nested
namespaces.

from [Link] import Firefox


from [Link].action_chains import ActionChains
from [Link] import By
from [Link] import Keys
from [Link] import WebElement

if __name__ == "__main__":
driver: Firefox = Firefox()
element: WebElement = driver.find_element([Link], "myInputField")
element.send_keys(f"Hello World{[Link]}")
action: ActionChains = ActionChains(driver)
action.key_down([Link]).send_keys("a").key_up([Link]).perform()

Python also supports import x as y as a way of providing an alias or alternative name for use by the
calling module:

import numpy as np
from [Link] import NDArray, float32

a: NDArray[float32] = [Link](1000)
When a module is imported, the Python interpreter first checks if it exists in the [Link] cache,
and reuses it if it had been imported previously, otherwise it loads it. When loading, it searches it in
[Link], and compiles it to bytecode or interprets its contents. All code in the global scope of the
module is executed. However, this can be mitigated using an explicit main function, which behaves
similarly to an entry point in most compiled languages, using the entry point idiom described as follows.

Entry point
A pseudo-entry point can be created by the following idiom, which relies on the internal variable
__name__ being set to __main__ when a program is executed, but not when it is imported as a
module (in which case it is instead set to the module name); there are many variants of this
structure:[12][13][14]

import sys

def main(argv: list[str]) -> int:


argc: int = len(argv) # get length of argv
n: int = int(argv[1])
print(n + 1)
return 0

if __name__ == "__main__":
[Link](main([Link]))

In this idiom, the call to the named entry point main is explicit, and the interaction with the operating
system (receiving the arguments, calling system exit) are done explicitly by library calls, which are
ultimately handled by the Python runtime. This contrasts with C, where these are done implicitly by the
runtime, based on convention.

Indentation
Python uses whitespace to delimit control flow blocks (following the off-side rule). Python borrows this
feature from its predecessor ABC: instead of punctuation or keywords, it uses indentation to indicate the
run of a block.

In so-called "free-format" languages – that use the block structure derived from ALGOL – blocks of code
are set off with braces ({ }) or keywords. In most coding conventions for these languages, programmers
conventionally indent the code within a block, to visually set it apart from the surrounding code.

A recursive function named foo, which is passed a single parameter, x, and if the parameter is 0 will call
a different function named bar and otherwise will call baz, passing x, and also call itself recursively,
passing x-1 as the parameter, could be implemented like this in Python:

def foo(x: int) -> None:


if x == 0:
bar()
else:
baz(x)
foo(x - 1)

and could be written like this in C:


void foo(int x) {
if (x == 0) {
bar();
} else {
baz(x);
foo(x - 1);
}
}

Incorrectly indented code could be misread by a human reader differently than it would be interpreted by
a compiler or interpreter. For example, if the function call foo(x - 1) on the last line in the example
above was erroneously indented to be outside the if/else block:

def foo(x: int) -> None:


if x == 0:
bar()
else:
baz(x)
foo(x - 1)

it would cause the last line to always be executed, even when x is 0, resulting in an endless recursion.

While both space and tab characters are accepted as forms of indentation and any multiple of spaces can
be used, spaces are recommended[15] and four spaces (as in the above examples) are recommended and
are by far the most commonly used.[16][17] Mixing spaces and tabs on consecutive lines is not allowed
starting with Python 3[18] because that can create bugs which are difficult to see, since many text editors
do not visually distinguish spaces and tabs.

Data structures
Since Python is a dynamically-typed language, Python values, not variables, carry type information. All
variables in Python hold references to objects, and these references are passed to functions. Some people
(including Python creator Guido van Rossum himself) have called this parameter-passing scheme "call by
object reference". An object reference means a name, and the passed reference is an "alias", i.e. a copy of
the reference to the same object, just as in C/C++. The object's value may be changed in the called
function with the "alias", for example:

my_list: list[str] = ["a", "b", "c"]


def my_func(l: list[str]) -> None:
[Link]("x")
print(l)

print(my_func(my_list))
# prints ['a', 'b', 'c', 'x']
print(my_list)
# prints ['a', 'b', 'c', 'x']

Function my_func changes the value of my_list with the formal argument l, which is an alias of
my_list. However, any attempt to operate (assign a new object reference to) on the alias itself will
have no effect on the original object.

my_list: list[str] = ["a", "b", "c"]


def my_func(l: list[str]) -> None:
# [Link]("x")
l = l + ["x"] # a new list created and assigned to l means l is no more alias for my_list
print(l)

print(my_func(my_list))
# prints ['a', 'b', 'c', 'x']
print(my_list)
# prints ['a', 'b', 'c']

In Python, non-innermost-local and not-declared-global accessible names are all aliases.

Among dynamically-typed languages, Python is moderately type-checked. Implicit conversion is defined


for numeric types (as well as Booleans), so one may validly multiply a complex number by an integer
(for instance) without explicit casting. However, there is no implicit conversion between, for example,
numbers and strings; a string is an invalid argument to a mathematical function expecting a number.

Base types
Python has a broad range of basic data types. Alongside conventional integer and floating-point
arithmetic, it transparently supports arbitrary-precision arithmetic, complex numbers, and decimal
numbers.

Python supports a wide variety of string operations. Strings in Python are immutable, meaning that string
operations, such as replacement of characters, return a new string; in other programming languages the
string might be altered in place. Performance considerations sometimes push for using special techniques
in programs that modify strings intensively, such as joining character arrays into strings only as needed.

Collection types
One of the very useful aspects of Python is the concept of collection (or container) types. In general a
collection is an object that contains other objects in a way that is easily referenced or indexed. Collections
come in two basic forms: sequences and mappings.

The ordered sequential types are lists (dynamic arrays), tuples, and strings. All sequences are indexed
positionally (0 through length - 1) and all but strings can contain any type of object, including multiple
types in the same sequence. Both strings and tuples are immutable, making them perfect candidates for
dictionary keys (see below). Lists, on the other hand, are mutable; elements can be inserted, deleted,
modified, appended, or sorted in-place.

Mappings, on the other hand, are (often unordered) types implemented in the form of dictionaries which
"map" a set of immutable keys to corresponding elements (much like a mathematical function). For
example, one could define a dictionary having a string "toast" mapped to the integer 42 or vice versa.
The keys in a dictionary must be of an immutable Python type, such as an integer or a string, because
they are implemented via a hash function. This makes for much faster lookup times, but requires keys to
remain unchanged.

Dictionaries are central to the internals of Python as they reside at the core of all objects and classes: the
mappings between variable names (strings) and the values which the names reference are stored as
dictionaries (see Object system). Since these dictionaries are directly accessible (via an object's
__dict__ attribute), metaprogramming is a straightforward and natural process in Python.
A set collection type is an unindexed, unordered collection that contains no duplicates, and implements
set theoretic operations such as union, intersection, difference, symmetric difference, and subset testing.
There are two types of sets: set and frozenset, the only difference being that set is mutable and
frozenset is immutable. Elements in a set must be hashable. Thus, for example, a frozenset can
be an element of a regular set whereas the opposite is not true.

Python also provides extensive collection manipulating abilities such as built in containment checking
and a generic iteration protocol.

Object system
In Python, everything is an object, even classes. Classes, as objects, have a class, which is known as their
metaclass. Python also supports multiple inheritance and mixins.

The language supports extensive introspection of types and classes. Types can be read and compared:
Types are instances of the object type. The attributes of an object can be extracted as a dictionary.

Operators can be overloaded in Python by defining special member functions – for instance, defining a
method named __add__ on a class permits one to use the + operator on objects of that class.

Literals

Strings
Python has various kinds of string literals.

Normal string literals


Either single or double quotes can be used to quote strings. Unlike in Unix shell languages, Perl or Perl-
influenced languages such as Ruby or Groovy, single quotes and double quotes function identically, i.e.
there is no string interpolation of $foo expressions. However, interpolation can be done in various ways:
with "f-strings" (since Python 3.6[19]), using the format method or the old % string-format operator.

For instance, all of these Python statements:

print(f"I just printed {num} pages to the printer {printer}")

print("I just printed {} pages to the printer {}".format(num, printer))


print("I just printed {0} pages to the printer {1}".format(num, printer))
print("I just printed {a} pages to the printer {b}".format(a=num, b=printer))

print("I just printed %s pages to the printer %s" % (num, printer))


print("I just printed %(a)s pages to the printer %(b)s" % {"a": num, "b": printer})

are equivalent to the Perl statement:

print "I just printed $num pages to the printer $printer\n"

They build a string using the variables num and printer.


Multi-line string literals
There are also multi-line strings, which begin and end with a series of three single or double quotes and
function like here documents in Perl and Ruby.

A simple example with variable interpolation (using the format method) is:

print('''Dear {recipient},

I wish you to leave Sunnydale and never return.

Not Quite Love,


{sender}
'''.format(sender="Buffy the Vampire Slayer", recipient="Spike"))

Raw strings
Finally, all of the previously mentioned string types come in "raw" varieties (denoted by placing a literal r
before the opening quote), which do no backslash-interpolation and hence are very useful for regular
expressions; compare "@-quoting" in C#. Raw strings were originally included specifically for regular
expressions. Due to limitations of the tokenizer, raw strings may not have a trailing backslash.[20]
Creating a raw string holding a Windows path ending with a backslash requires some variety of
workaround (commonly, using forward slashes instead of backslashes, since Windows accepts both).

Examples include:

# A Windows path, even raw strings cannot end in a backslash


win_path: str = r"C:\Foo\Bar\Baz\"

# Error:
# File "<stdin>", line 1
# win_path: str = r"C:\Foo\Bar\Baz\"
# ^
# SyntaxError: EOL while scanning string literal

dos_path: str = r"C:\Foo\Bar\Baz\ " # avoids the error by adding


print(dos_path.rstrip()) # and removing trailing space
# prints('C:\\Foo\\Bar\\Baz\\')

quoted_dos_path: str = r'"{}"'.format(dos_path)


print(quoted_dos_path)
# prints '"C:\\Foo\\Bar\\Baz\\ "'

# A regular expression matching a quoted string with possible backslash quoting


print([Link](r'"(([^"\\]|\\.)*)"', quoted_dos_path).group(1).rstrip())
# prints 'C:\\Foo\\Bar\\Baz\\'

code: str = 'foo(2, bar)'


# Reverse the arguments in a two-arg function call
print([Link](r'\(([^,]*?),([^ ,]*?)\)', r'(\2, \1)', code))
# prints 'foo(2, bar)'
# Note that this won't work if either argument has parens or commas in it.

Concatenation of adjacent string literals


String literals appearing contiguously and only separated by whitespace (including new lines using
backslashes), are allowed and are aggregated into a single longer string.[21] Thus

title: str = "One Good Turn: " \


'A Natural History of the Screwdriver and the Screw'

is equivalent to

title: str = "One Good Turn: A Natural History of the Screwdriver and the Screw"

Unicode
Since Python 3.0, the default character set is UTF-8 both for source code and the interpreter. In UTF-8,
unicode strings are handled like traditional byte strings. This example will work:

s: str = "Γειά" # Hello in Greek


print(s)

Numbers
Numeric literals in Python are of the normal sort, e.g. 0, -1, 3.4, 3.5e-8.

Python has arbitrary-length integers and automatically increases their storage size as necessary. Prior to
Python 3, there were two kinds of integral numbers: traditional fixed size integers and "long" integers of
arbitrary size. The conversion to "long" integers was performed automatically when required, and thus
the programmer usually did not have to be aware of the two integral types. In newer language versions
the distinction is completely gone and all integers behave like arbitrary-length integers.

Python supports normal floating point numbers, which are created when a dot is used in a literal (e.g.
1.1), when an integer and a floating point number are used in an expression, or as a result of some
mathematical operations ("true division" via the / operator, or exponentiation with a negative exponent).

Python also supports complex numbers natively. The imaginary component of a complex number is
indicated with the J or j suffix, e.g. 3 + 4j.

Lists, tuples, sets, dictionaries


Python has syntactic support for the creation of container types.

Lists (class list) are mutable sequences of items of arbitrary types, and can be created either with the
special syntax

my_list: list[int | str] = [1, 2, 3, "a dog"]

or using normal object creation

my_second_list: list[int] = []
my_second_list.append(4)
my_second_list.append(5)

Tuples (class tuple) are immutable sequences of items of arbitrary types. There is also a special syntax
to create tuples
my_tuple: tuple[int | str] = 1, 2, 3, "four"
my_tuple: tuple[int | str] = (1, 2, 3, "four")

Although tuples are created by separating items with commas, the whole construct is usually wrapped in
parentheses to increase readability. An empty tuple is denoted by (), while a tuple with a single value
can be created with (1,).

Sets (class set) are mutable containers of hashable items[22] of arbitrary types, with no duplicates. The
items are not ordered, but sets support iteration over the items. The syntax for set creation uses curly
brackets

my_set: set[Any] = {0, (), False}

Python sets are very much like mathematical sets, and support operations like set intersection and union.
Python also features a frozenset class for immutable sets, see Collection types.

Dictionaries (class dict) are mutable mappings tying keys and corresponding values. Python has special
syntax to create dictionaries ({key: value})

my_dictionary: dict[Any, Any] = {"key 1": "value 1", 2: 3, 4: []}

The dictionary syntax is similar to the set syntax; the difference is the presence of colons. The empty
literal {} results in an empty dictionary rather than an empty set, which is instead created using the non-
literal constructor: set().

Operators

Arithmetic
Python includes the +, -, *, / ("true division"), // (floor division), % (modulus), and **
(exponentiation) operators, with their usual mathematical precedence.

In Python 3, x / y performs "true division", meaning that it always returns a float, even if both x and y
are integers that divide evenly.

print(4 / 2)
# prints 2.0

and // performs integer division or floor division, returning the floor of the quotient as an integer.

In Python 2 (and most other programming languages), unless explicitly requested, x / y performed
integer division, returning a float only if either input was a float. However, because Python is a
dynamically-typed language, it was not always possible to tell which operation was being performed,
which often led to subtle bugs, thus prompting the introduction of the // operator and the change in
semantics of the / operator in Python 3.
Comparison operators
The comparison operators, i.e. ==, !=, <, >, <=, >=, is, is not, in and not in[23] are used on all
manner of values. Numbers, strings, sequences, and mappings can all be compared. In Python 3, disparate
types (such as a str and an int) do not have a consistent relative ordering, and attempts to compare
these types raises a TypeError exception. While it was possible to compare disparate types in Python 2
(for example, whether a string was greater-than or less-than an integer), the ordering was undefined; this
was considered a historical design quirk and was ultimately removed in Python 3.

Chained comparison expressions such as a < b < c have roughly the meaning that they have in
mathematics, rather than the unusual meaning found in C and similar languages. The terms are evaluated
and compared in order. The operation has short-circuit semantics, meaning that evaluation is guaranteed
to stop as soon as a verdict is clear: if a < b is false, c is never evaluated as the expression cannot
possibly be true anymore.

For expressions without side effects, a < b < c is equivalent to a < b and b < c. However,
there is a substantial difference when the expressions have side effects. a < f(x) < b will evaluate
f(x) exactly once, whereas a < f(x) and f(x) < b will evaluate it twice if the value of a is less
than f(x) and once otherwise.

Logical operators
In all versions of Python, Boolean operators treat zero values or empty values such as "", 0, None, 0.0,
[], and {} as false, while in general treating non-empty, non-zero values as true. The Boolean values
True and False were added to the language in Python 2.2.1 as constants (subclassed from 1 and 0) and
were changed to be full blown keywords in Python 3. The binary comparison operators such as == and >
return either True or False.

The Boolean operators and and or use minimal evaluation. For example, y == 0 or x/y > 100
will never raise a divide-by-zero exception. These operators return the value of the last operand
evaluated, rather than True or False. Thus the expression (4 and 5) evaluates to 5, and (4 or
5) evaluates to 4.

Bitwise operators
Python is able to do bitwise operations on integers, or binary numbers written with the 0b prefix. It uses
x << y to shift x left by y places, adding zeros to the right. x >> y does the same but shifts x right,
adding copies of the leftmost bit to the left. The operator x & y performs a bitwise AND, x | y does a
bitwise OR, ~ x returns the bitwise complement/NOT of x, and x ^ y does a bitwise XOR, contrary to
the ^ symbol's usual use as an exponentiation operator.[24][25]

Functional programming
A strength of Python is the availability of a functional programming style, which makes working with
lists and other collections much more straightforward.
Comprehensions
One such construction is the list comprehension, which can be expressed with the following format:

l: list[Any] = [mapping_expression for element in source_list if filter_expression]

Using list comprehension to calculate the first five powers of two:

powers_of_two: list[int] = [2 ** n for n in range(1, 6)]

The Quicksort algorithm can be expressed elegantly (albeit inefficiently) using list comprehensions:

T: TypeVar = TypeVar("T")

def qsort(l: list[T]) -> list[T]:


if l == []:
return []
pivot: T = l[0]
return (qsort([x for x in l[1:] if x < pivot]) +
[pivot] +
qsort([x for x in l[1:] if x >= pivot]))

Python 2.7+[26] also supports set comprehensions[27] and dictionary comprehensions.[28]

First-class functions
In Python, functions are first-class objects that can be created and passed around dynamically.

Python's limited support for anonymous functions is the lambda construct. An example is the
anonymous function which squares its input, called with the argument of 5:

f: Callable[[int], int] = lambda x: x**2


f(5)

Lambdas are limited to containing an expression rather than statements, although control flow can still be
implemented less elegantly within lambda by using short-circuiting,[29] and more idiomatically with
conditional expressions.[30]

Closures
Python has had support for lexical closures since version 2.2. Here's an example function that returns a
function that approximates the derivative of the given function:

def derivative(f: Callable[[float], float], dx: float):


"""Return a function that approximates the derivative of f
using an interval of dx, which should be appropriately small.
"""
def function(x: float) -> float:
return (f(x + dx) - f(x)) / dx
return function
Python's syntax, though, sometimes leads programmers of other languages to think that closures are not
supported. Variable scope in Python is implicitly determined by the scope in which one assigns a value to
the variable, unless scope is explicitly declared with global or nonlocal.[31]

Note that the closure's binding of a name to some value is not mutable from within the function. Given:

def foo(a: int, b: int) -> None:


print(f"a: {a}")
print(f"b: {b}")
def bar(c: int) -> None:
b = c
print(f"b*: {b}")
bar(a)
print(f"b: {b}")

print(foo(1, 2))
# prints:
# a: 1
# b: 2
# b*: 1
# b: 2

and you can see that b, as visible from the closure's scope, retains the value it had; the changed binding of
b inside the inner function did not propagate out. The way around this is to use a nonlocal b
statement in bar. In Python 2 (which lacks nonlocal), the usual workaround is to use a mutable value
and change that value, not the binding. E.g., a list with one element.

Generators
Introduced in Python 2.2 as an optional feature and finalized in version 2.3, generators are Python's
mechanism for lazy evaluation of a function that would otherwise return a space-prohibitive or
computationally intensive list.

This is an example to lazily generate the prime numbers:

import itertools

def generate_primes(stop_at: Optional[int] = None) -> Iterator[int]:


primes: list[int] = []
for n in [Link](start = 2):
if stop_at is not None and n > stop_at:
return # raises the StopIteration exception
composite: bool = False
for p in primes:
if not n % p:
composite = True
break
elif p ** 2 > n:
break
if not composite:
[Link](n)
yield n

When calling this function, the returned value can be iterated over much like a list:

for i in generate_primes(100): # iterate over the primes between 0 and 100


print(i)
for i in generate_primes(): # iterate over ALL primes indefinitely
print(i)

The definition of a generator appears identical to that of a function, except the keyword yield is used in
place of return. However, a generator is an object with persistent state, which can repeatedly enter and
leave the same scope. A generator call can then be used in place of a list, or other structure whose
elements will be iterated over. Whenever the for loop in the example requires the next item, the
generator is called, and yields the next item.

Generators do not have to be infinite like the prime-number example above. When a generator terminates,
an internal exception is raised which indicates to any calling context that there are no more values. A for
loop or other iteration will then terminate.

Generator expressions
Introduced in Python 2.4, generator expressions are the lazy evaluation equivalent of list comprehensions.
Using the prime number generator provided in the above section, we might define a lazy, but not quite
infinite collection.

import itertools

primes_under_million: Iterator[int] = (i for i in generate_primes() if i < 1000000)


two_thousandth_prime: Iterator[int] = [Link](primes_under_million, 1999, 2000).next()

Most of the memory and time needed to generate this many primes will not be used until the needed
element is actually accessed. Unfortunately, you cannot perform simple indexing and slicing of
generators, but must use the itertools module or "roll your own" loops. In contrast, a list comprehension
is functionally equivalent, but is greedy in performing all the work:

primes_under_million: list[int] = [i for i in generate_primes(2000000) if i < 1000000]


two_thousandth_prime: int = primes_under_million[1999]

The list comprehension will immediately create a large list (with 78498 items, in the example, but
transiently creating a list of primes under two million), even if most elements are never accessed. The
generator comprehension is more parsimonious.

Dictionary and set comprehensions


While lists and generators had comprehensions/expressions, in Python versions older than 2.7 the other
Python built-in collection types (dicts and sets) had to be kludged in using lists or generators:

squares = dict((n, n * n) for n in range(5))


# in Python 3.5 and later the type of squares is dict[int, int]
print(squares)
# prints {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

Python 2.7 and 3.0 unified all collection types by introducing dictionary and set comprehensions, similar
to list comprehensions:
print([n * n for n in range(5)]) # regular list comprehension
# prints [0, 1, 4, 9, 16]
print({n * n for n in range(5)}) # set comprehension
# prints {0, 1, 4, 9, 16}
print({n: n * n for n in range(5)}) # dict comprehension
# prints {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

Objects
Python supports most object-oriented programming (OOP) techniques. It allows polymorphism, not only
within a class hierarchy but also by duck typing. Any object can be used for any type, and it will work so
long as it has the proper methods and attributes. And everything in Python is an object, including classes,
functions, numbers and modules. Python also has support for metaclasses, an advanced tool for
enhancing classes' functionality. Naturally, inheritance, including multiple inheritance, is supported.
Python has very limited support for private variables using name mangling which is rarely used in
practice as information hiding is seen by some as unpythonic, in that it suggests that the class in question
contains unaesthetic or ill-planned internals. The slogan "we're all responsible users here" is used to
describe this attitude.[32]

As is true for modules, classes in Python do not put an absolute barrier between definition and
user, but rather rely on the politeness of the user not to "break into the definition."

— 9. Classes ([Link] The Python 2.6 Tutorial (2013)


OOP doctrines such as the use of accessor methods to read data members are not enforced in Python. Just
as Python offers functional-programming constructs but does not attempt to demand referential
transparency, it offers an object system but does not demand OOP behavior. Moreover, it is always
possible to redefine the class using properties (see Properties) so that when a certain variable is set or
retrieved in calling code, it really invokes a function call, so that [Link] = toast might really
invoke spam.set_eggs(toast). This nullifies the practical advantage of accessor functions, and it
remains OOP because the property eggs becomes a legitimate part of the object's interface: it need not
reflect an implementation detail.

In version 2.2 of Python, "new-style" classes were introduced. With new-style classes, objects and types
were unified, allowing the subclassing of types. Even entirely new types can be defined, complete with
custom behavior for infix operators. This allows for many radical things to be done syntactically within
Python. A new method resolution order ([Link] for
multiple inheritance was also adopted with Python 2.3. It is also possible to run custom code while
accessing or setting attributes, though the details of those techniques have evolved between Python
versions.

With statement
The with statement handles resources, and allows users to work with the Context Manager protocol.[33]
One function (__enter__()) is called when entering scope and another (__exit__()) when
leaving. This prevents forgetting to free the resource and also handles more complicated situations such
as freeing the resource when an exception occurs while it is in use. Context Managers are often used with
files, database connections, test cases, etc.

Properties
Properties allow specially defined methods to be invoked on an object instance by using the same syntax
as used for attribute access. An example of a class defining some properties is:

class MyClass:
def __init__(self) -> None:
self._a: int = 0

@property
def a(self) -> int:
return self._a

@[Link] # makes the property writable


def a(self, value: int) -> None:
self._a = value

Descriptors
A class that defines one or more of the three special methods __get__(self, instance,
owner), __set__(self, instance, value), __delete__(self, instance) can be
used as a descriptor. Creating an instance of a descriptor as a class member of a second class makes the
instance a property of the second class.[34]

Class and static methods


Python allows the creation of class methods and static methods via the use of the @classmethod and
@staticmethod decorators. The first argument to a class method is the class object instead of the self-
reference to the instance. A static method has no special first argument. Neither the instance, nor the class
object is passed to a static method.

Exceptions
Python supports (and extensively uses) exception handling as a means of testing for error conditions and
other "exceptional" events in a program.

Python style calls for the use of exceptions whenever an error condition might arise. Rather than testing
for access to a file or resource before actually using it, it is conventional in Python to just go ahead and
try to use it, catching the exception if access is rejected.

Exceptions can also be used as a more general means of non-local transfer of control, even when an error
is not at issue. For instance, the Mailman mailing list software, written in Python, uses exceptions to jump
out of deeply nested message-handling logic when a decision has been made to reject a message or hold it
for moderator approval.
Exceptions are often used as an alternative to the if-block, especially in threaded situations. A
commonly invoked motto is EAFP, or "It is Easier to Ask for Forgiveness than Permission,"[35] which is
attributed to Grace Hopper.[36][37] The alternative, known as LBYL, or "Look Before You Leap",
explicitly tests for pre-conditions.[38]

In this first code sample, following the LBYL approach, there is an explicit check for the attribute before
access:

if hasattr(spam, "eggs"):
ham = [Link]
else:
handle_missing_attr()

This second sample follows the EAFP paradigm:

try:
ham = [Link]
except AttributeError:
handle_missing_attr()

These two code samples have the same effect, although there will be performance differences. When
spam has the attribute eggs, the EAFP sample will run faster. When spam does not have the attribute
eggs (the "exceptional" case), the EAFP sample will run slower. The Python profiler ([Link]
[Link]/library/[Link]) can be used in specific cases to determine performance characteristics. If
exceptional cases are rare, then the EAFP version will have superior average performance than the
alternative. In addition, it avoids the whole class of time-of-check to time-of-use (TOCTTOU)
vulnerabilities, other race conditions,[37][39] and is compatible with duck typing. A drawback of EAFP is
that it can be used only with statements; an exception cannot be caught in a generator expression, list
comprehension, or lambda function.

Comments and docstrings


There are two ways to annotate Python code. One is by using comments to indicate what some part of the
code does. Single-line comments begin with the hash character (#) and continue until the end of the line.
Comments spanning more than one line are achieved by inserting a multi-line string (with """ or ''' as
the delimiter on each end) that is not used in assignment or otherwise evaluated, but sits in between other
statements.

Commenting a piece of code:

import sys

def getline() -> str:


return [Link]() # Get one line and return it

Commenting a piece of code with multiple lines:

def getline() -> str:


"""This function gets one line and returns it.
As a demonstration, this is a multiline docstring.

This full string can be accessed as getline.__doc__.


"""
return [Link]()

Docstrings (documentation strings), that is, strings that are located alone without assignment as the first
indented line within a module, class, method or function, automatically set their contents as an attribute
named __doc__, which is intended to store a human-readable description of the object's purpose,
behavior, and usage. The built-in help function generates its output based on __doc__ attributes. Such
strings can be delimited with " or ' for single line strings, or may span multiple lines if delimited with
either """ or ''' which is Python's notation for specifying multi-line strings. However, the style guide
for the language specifies that triple double quotes (""") are preferred for both single and multi-line
docstrings.[40]

Single-line docstring:

def getline() -> str:


"""Get one line from stdin and return it."""
return [Link]()

Multi-line docstring:

def getline() -> str:


"""Get one line
from stdin
and return it.
"""
return [Link]()

Docstrings can be as large as the programmer wants and contain line breaks. In contrast with comments,
docstrings are themselves Python objects and are part of the interpreted code that Python runs. That
means that a running program can retrieve its own docstrings and manipulate that information, but the
normal usage is to give other programmers information about how to invoke the object being documented
in the docstring.

There are tools available that can extract the docstrings from Python code and generate documentation.
Docstring documentation can also be accessed from the interpreter with the help() function, or from
the shell with the pydoc command pydoc.

The doctest standard module uses interactions copied from Python shell sessions into docstrings to create
tests, whereas the docopt ([Link] module uses them to define command-line options.

Decorators
A decorator is any callable Python object that is used to modify a function, method or class definition. A
decorator is passed the original object being defined and returns a modified object, which is then bound to
the name in the definition. Python decorators were inspired in part by Java annotations, and have a
similar syntax; the decorator syntax is pure syntactic sugar, using @ as the keyword:
@viking_chorus
def menu_item() -> None:
print("spam")

is equivalent to

def menu_item() -> None:


print("spam")
menu_item = viking_chorus(menu_item)

Decorators are a form of metaprogramming; they enhance the action of the function or method they
decorate. For example, in the sample below, viking_chorus might cause menu_item to be run 8
times (see Spam sketch) for each time it is called:

R: TypeVar = TypeVar("R")

def viking_chorus(myfunc: Callable[..., R]) -> Callable[..., None]:


def inner_func(*args: tuple[Any, ...], **kwargs: dict[str, Any]):
for i in range(8):
myfunc(*args, **kwargs)
return inner_func

Canonical uses of function decorators are for creating class methods or static methods, adding function
attributes, tracing, setting pre- and postconditions, and synchronization,[41] but can be used for far more,
including tail recursion elimination,[42] memoization and even improving the writing of other
decorators.[43]

Decorators can be chained by placing several on adjacent lines:

@invincible
@favourite_colour("Blue")
def black_knight() -> None:
pass

is equivalent to

def black_knight() -> None:


pass
black_knight = invincible(favourite_colour("Blue")(black_knight))

or, using intermediate variables

def black_knight() -> None:


pass
blue_decorator = favourite_colour("Blue")
decorated_by_blue = blue_decorator(black_knight)
black_knight = invincible(decorated_by_blue)

In the example above, the favourite_colour decorator factory takes an argument. Decorator
factories must return a decorator, which is then called with the object to be decorated as its argument:

def favourite_colour(colour: str) -> Callable[[Callable[[], R]], Callable[[], R]]:


def decorator(func: Callable[[], R]) -> Callable[[], R]:
def wrapper() -> R:
print(colour)
func()
return wrapper
return decorator

This would then decorate the black_knight function such that the colour, "Blue", would be printed
prior to the black_knight function running. Closure ensures that the colour argument is accessible to
the innermost wrapper function even when it is returned and goes out of scope, which is what allows
decorators to work.

Despite the name, Python decorators are not an implementation of the decorator pattern. The decorator
pattern is a design pattern used in statically-typed object-oriented programming languages to allow
functionality to be added to objects at run time; Python decorators add functionality to functions and
methods at definition time, and thus are a higher-level construct than decorator-pattern classes. The
decorator pattern itself is trivially implementable in Python, because the language is duck typed, and so is
not usually considered as such.

Easter eggs
Users of curly bracket languages, such as C or Java, sometimes expect or wish Python to follow a block-
delimiter convention. Brace-delimited block syntax has been repeatedly requested, and consistently
rejected by core developers. The Python interpreter contains an easter egg that summarizes its developers'
feelings on this issue. The code from __future__ import braces raises the exception
SyntaxError: not a chance. The __future__ module is normally used to provide features
from future versions of Python.

Another hidden message, the Zen of Python (a summary of Python design philosophy), is displayed when
trying to import this.

The message Hello world! is printed when the import statement import __hello__ is used. In
Python 2.7, instead of Hello world! it prints Hello world....

Importing the antigravity module opens a web browser to xkcd comic 353 ([Link]
that portrays a humorous fictional use for such a module, intended to demonstrate the ease with which
Python modules enable additional functionality.[44] In Python 3, this module also contains an
implementation of the "geohash" algorithm, a reference to xkcd comic 426 ([Link]

Notes
1. async and await were introduced in Python 3.5.[5]
2. True and False became keywords in Python 3.0. Previously they were global variables.
3. nonlocal was introduced in Python 3.0.
4. match, case and _ were introduced as keywords in Python 3.10.
References
1. "Readability counts." - PEP 20 - The Zen of Python ([Link]
020/) Archived ([Link]
eps/pep-0020/) 2014-12-05 at the Wayback Machine
2. "PEP 20 - The Zen of Python" ([Link] Python
Software Foundation. 2004-08-23. Archived ([Link]
[Link] from the original on 2008-12-03. Retrieved
2008-11-24.
3. "2. Lexical analysis" ([Link]
Python 3 documentation. Python Software Foundation. Retrieved 2021-03-11.
4. "2. Lexical analysis" ([Link]
Python v2.7.18 documentation. Python Software Foundation. Retrieved 2021-03-11.
5. "New Keywords" ([Link] Python v3.5
documentation. [Link]. Archived ([Link]
tps://[Link]/3//whatsnew/[Link]#new-keywords) from the original on 2016-06-18.
Retrieved 2016-06-01.
6. "2. Lexical analysis" ([Link]
s). Python 3 documentation. Python Software Foundation. Retrieved 2022-01-22.
7. "PEP 622 -- Structural Pattern Matching" ([Link]
wards-compatibility). 2020-06-23. Retrieved 2022-01-22.
8. "PEP 3107 -- Function Annotations" ([Link] Archived
([Link]
from the original on 2015-01-06. Retrieved 2014-08-15.
9. "6. Modules" ([Link] The Python Tutorial. Python
Software Foundation. Retrieved 25 October 2010.
10. "Python Scopes and Namespaces" ([Link]
pes-and-namespaces). [Link]. Retrieved 2011-07-26.
11. [Link] "in general the practice of importing * from a
module or package is frowned upon"
12. Guido van Rossum (May 15, 2003). "Python main() functions" ([Link]
ogs/[Link]?thread=4829). Archived ([Link]
p://[Link]/weblogs/[Link]?thread=4829) from the original on July 11, 2015.
Retrieved June 29, 2015,comments ([Link]
ead=4829)
13. Code Like a Pythonista: Idiomatic Python ([Link]
iomatic/[Link]#modules-scripts) Archived ([Link]
143/[Link]
s) 2014-05-27 at the Wayback Machine—on Python scripts used as modules
14. Ned Batchelder (6 June 2003). "Python main() functions" ([Link]
306/python_main_functions.html). Archived ([Link]
ttp://[Link]/blog/200306/python_main_functions.html) from the original on 20
September 2015. Retrieved 29 June 2015.
15. "PEP 8 -- Style Guide for Python Code" ([Link]
[Link]. Retrieved 2021-03-17.
16. Hoffa, Felipe (2017-07-26). "400,000 GitHub repositories, 1 billion files, 14 terabytes of
code: Spaces or Tabs?" ([Link]
-14-terabytes-of-code-spaces-or-tabs-7cfe0b5dd7fd). Medium. Retrieved 2021-03-11.
17. "Tabs or Spaces" ([Link] [Link]. Retrieved
2021-03-11.
18. "PEP 8 -- Style Guide for Python Code" ([Link]
[Link]. Retrieved 2021-03-11.
19. "PEP 498 - Literal String Interpolation" ([Link]
ew36-pep498). What’s New In Python 3.6. 2016-12-23. Archived ([Link]
b/20170330002530/[Link] from
the original on 2017-03-30. Retrieved 2017-03-29.
20. "2. Lexical analysis" ([Link]
Python v2.7.5 documentation. [Link]. Archived ([Link]
1023010739/[Link] from the
original on 2012-10-23. Retrieved 2013-08-16.
21. "2. Lexical analysis" ([Link]
ncatenation). Python v2.7.5 documentation. [Link]. Archived ([Link]
org/web/20121023010739/[Link]
al-concatenation) from the original on 2012-10-23. Retrieved 2013-08-16.
22. Hashable items are usually immutable, but not necessarily so by definition. See
[Link]/3/[Link] ([Link]
23. "6. Expressions — Python 3.9.2 documentation" ([Link]
[Link]#comparisons). [Link]. Retrieved 2021-03-17.
24. "BitwiseOperators - Python Wiki" ([Link]
[Link]. Retrieved 2025-12-02.
25. "Python Bitwise Operators" ([Link]
sp). [Link]. Retrieved 2025-12-02.
26. "Python 2.7.0 Release" ([Link] Archived ([Link]
[Link]/web/20160127021350/[Link] from
the original on 2016-01-27. Retrieved 2016-01-19.
27. "5. Data Structures — Python 2.7.18 documentation" ([Link]
[Link]#sets). Archived ([Link]
[Link]/2/tutorial/[Link]#sets) from the original on 2016-01-26. Retrieved
2016-01-19.
28. "5. Data Structures — Python 2.7.18 documentation" ([Link]
[Link]#dictionaries). Archived ([Link]
s://[Link]/2/tutorial/[Link]#dictionaries) from the original on 2016-01-
26. Retrieved 2016-01-19.
29. David Mertz. "Functional Programming in Python" ([Link]
1222/[Link] IBM
developerWorks. Archived from the original ([Link]
_python_13.html) on 2007-02-20. Retrieved 2007-08-27.
30. "PEP 308 -- Conditional Expressions" ([Link]
Archived ([Link]
ep-0308/) from the original on 2016-03-13. Retrieved 2016-04-14.
31. The nonlocal keyword was adopted by PEP 3104 ([Link]
104/) Archived ([Link]
eps/pep-3104) 2014-12-02 at the Wayback Machine
32. "Python Style Guide" ([Link]
sible-users). [Link]. Archived ([Link]
5/[Link] from the
original on 2015-03-09. Retrieved 2015-03-08.
33. "PEP 343 -- The "with" Statement" ([Link] Archived
([Link]
from the original on 2014-12-14. Retrieved 2014-08-15.
34. "Glossary — Python 3.9.2 documentation" ([Link]
criptor). [Link]. Retrieved 2021-03-23.
35. EAFP ([Link] Archived ([Link]
eb/20121026064048/[Link] 2012-10-26 at the
Wayback Machine, Python Glossary
36. Hamblen, Diane. "Only the Limits of Our Imagination: An exclusive interview with RADM
Grace M. Hopper" ([Link]
archives/86_jul/[Link]). Department of the Navy Information Technology Magazine.
Archived from the original ([Link] on
January 14, 2009. Retrieved 2007-01-31.
37. Python in a nutshell, Alex Martelli, p. 134 ([Link]
cC&pg=PA134)
38. LBYL ([Link] Archived ([Link]
eb/20180121071609/[Link] 2018-01-21 at the
Wayback Machine, Python Glossary
39. Alex Martelli (19 May 2003). "EAFP v. LBYL" ([Link]
7643/). python-list mailing list. Archived ([Link]
[Link]/lists/python-list/337643/) from the original on 14 July 2012. Retrieved 18 July
2011.
40. "PEP 8 -- Style Guide for Python Code" ([Link]
[Link]. Retrieved 2021-03-23.
41. "Python 2.4 Decorators: Reducing code duplication and consolidating knowledge" ([Link]
[Link]/184406073#l11). Dr. Dobb's. 2005-05-01. Archived ([Link]
b/20070206063944/[Link] from the original on 2007-02-06.
Retrieved 2007-02-08.
42. "New Tail Recursion Decorator" ([Link]
e/496691). ASPN: Python Cookbook. 2006-11-14. Archived ([Link]
070209010200/[Link] from
the original on 2007-02-09. Retrieved 2007-02-08.
43. "The decorator module" ([Link]
Archived ([Link]
heles/python/[Link]) from the original on 2007-02-10. Retrieved 2007-02-08.
44. cpython: The Python programming language ([Link] Python,
2017-10-15, archived ([Link]
on/cpython) from the original on 2017-09-15, retrieved 2017-10-15
45. "Another hidden treasure. · python/cpython@b1614a7" ([Link]
ommit/b1614a7b6705f939b29df4045e591fcf53a8611b). GitHub. Retrieved 2017-10-15.

External links
"The Python Language Reference" ([Link]
e-index).
Van Rossum, Guido. "The Python Tutorial" ([Link] (written by
the author of Python)
Ramalho, Luciano (April 2022). Fluent Python, 2nd Edition ([Link]
nsights/books/fluent-python-2nd-edition). O'Reilly Media, Inc. ISBN 9781492056355.

Retrieved from "[Link]

You might also like