Python syntax and semantics

Uses your system voice; voice and rate in settings.

Snapshot

≈ 23 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

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 programming, 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 functions, and modules for better code reusability and 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.

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 programming, 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 functions, and modules for better code reusability and 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. 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. 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. 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 selenium.webdriver import Firefox from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.remote.webelement import WebElement if __name__ == "__main__": driver: Firefox = Firefox() element: WebElement = driver.find_element(By.ID, "myInputField") element.send_keys(f"Hello World{Keys.ENTER}") action: ActionChains = ActionChains(driver) action.key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL).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 numpy.typing import NDArray, float32 a: NDArray[float32] = np.arange(1000) When a module is imported, the Python interpreter first checks if it exists in the sys.modules cache, and reuses it if it had been imported previously, otherwise it loads it. When loading, it searches it in sys.path , 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. 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__": sys.exit(main(sys.argv)) 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. 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 and four spaces (as in the above examples) are recommended and are by far the most commonly used. Mixing spaces and tabs on consecutive lines is not allowed starting with Python 3 because that can create bugs which are difficult to see, since many text editors do not visually distinguish spaces and tabs. my_list: list[str] = ["a", "b", "c"] def my_func(l: list[str]) -> None: l.append("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: # l.append("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. 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 . print(Dear {recipient}, I wish you to leave Sunnydale and never return. Not Quite Love, {sender} .format(sender="Buffy the Vampire Slayer", recipient="Spike")) A Windows path, even raw strings cannot end in a backslash win_path: str = r"C:\Foo\Bar\Baz\" Error: File " ", 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(re.match(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(re.sub(r'\(([^,]*?),([^ ,]*?)\)', r'(\2, \1)', code)) prints 'foo(2, bar)' Note that this won't work if either argument has parens or commas in it. 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" s: str = "Γειά" # Hello in Greek print(s) 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 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() . 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. The comparison operators, i.e. == , != , , > , , >= , is , is not , in and not in 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 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 is false, c is never evaluated as the expression cannot possibly be true anymore. For expressions without side effects, a is equivalent to a . However, there is a substantial difference when the expressions have side effects. a will evaluate f(x) exactly once, whereas a will evaluate it twice if the value of a is less than f(x) and once otherwise. 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 . 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])) Python 2.7+ also supports set comprehensions and dictionary comprehensions. 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, and more idiomatically with conditional expressions. def derivative(f: Callable[[float], float], dx: float) -> Callable[[float], 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 A closure can be defined by referencing a variable from an outer scope; in the example above, the use of f in the inner scope of function implicitly references the f defined in the scope of derivative . However, such an implicit reference cannot reassign to a variable from an other scope. The following example prints False since incorrect_tracker creates a new called_yet in its inner scope rather than mutating the called_yet from the outer scope: called_yet = False def incorrect_tracker() -> None: called_yet = True # Creates a new variable instead of updating the other called_yet incorrect_tracker() print(called_yet) # False Declaring a variable global or nonlocal will override this behavior, allowing assignment to outer variables. called_yet = False def tracker() -> None: global called_yet called_yet = True # Reassigns called_yet from the global scope tracker() print(called_yet) # True import itertools def generate_primes(stop_at: Optional[int] = None) -> Iterator[int]: primes: list[int] = [] for n in itertools.count(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: primes.append(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. import itertools primes_under_million: Iterator[int] = (i for i in generate_primes() if i 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 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. 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} 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 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 spam.eggs = toast might really invoke spam.set_eggs(toast) . This nullifies the practical advantage of accessor functions, and it remains 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 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. class MyClass: def __init__(self) -> None: self._a: int = 0 @property def a(self) -> int: return self._a @a.setter # makes the property writable def a(self, value: int) -> None: self._a = value if hasattr(spam, "eggs"): ham = spam.eggs else: handle_missing_attr() This second sample follows the EAFP paradigm: try: ham = spam.eggs 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 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, 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. import sys def getline() -> str: return sys.stdin.readline() # 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 sys.stdin.readline() 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. Single-line docstring: def getline() -> str: """Get one line from stdin and return it.""" return sys.stdin.readline() Multi-line docstring: def getline() -> str: """Get one line from stdin and return it. """ return sys.stdin.readline() 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 module uses them to define command-line options. @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: Unpack[dict[str, Any]]) -> None: 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, but can be used for far more, including tail recursion elimination, memoization and even improving the writing of other decorators. 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.

Text derived from https://en.wikipedia.org/w/index.php?oldid=1376706894 under CC BY-SA 4.0; modifications have been made.