Python syntax and semantics

From Wikipedia, the free encyclopedia
Jump to: navigation, search

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). 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 towards simplicity and generality 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".


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

  • and
  • as
  • assert
  • break
  • class
  • continue
  • def
  • del
  • elif
  • else
  • except
  • exec[note 1]
  • False[note 2]
  • finally
  • for
  • from
  • global
  • if
  • import
  • in
  • is
  • lambda
  • None
  • nonlocal[note 2]
  • not
  • or
  • pass
  • print[note 1]
  • raise
  • return
  • True[note 2]
  • try
  • while
  • with
  • yield
  1. ^ a b Starting from Python 3, exec and print are functions, so they are not keywords anymore.
  2. ^ a b c Starting from Python 3, keywords True, False and nonlocal were introduced.


Python uses whitespace to delimit program 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.



foo function in C with K&R indent style:

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

foo function in Python:

def foo(x):
    if x == 0:
        foo(x - 1)


Misleading indentation in C:



Indentation errors in Python:

def foo(x):
    if x == 0:
    foo(x - 1)



Data structures[edit]


All variables in Python hold references to objects, and these references are passed to functions; a function cannot change the value of variable references in its calling function (not entirely true, see below). Some people (including 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 like in C/C++. The object's value may be changed in the called function with the "alias", for example:

>>> alist = ['a', 'b', 'c']
>>> def myfunc(al):
...     al.append('x')
...     print al
>>> myfunc(alist)
>>> alist

Function "myfunc" changed the value of "alist" with the formal argument "al", which is an alias of "alist". However, any attempt to operate on the alias itself will have no effect on the original object. In Python, non-innermost-local and not-declared-global accessible names are all aliases.


Base types[edit]



Collection types[edit]



Mappings, on the other hand, are 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 under the hood they are implemented via a hash function. This makes for much faster lookup times, but requires keys not change (and also results in a dictionary's lack of order).

Dictionaries are also central to the internals of the language as they reside at the core of all Python 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.



Object system[edit]






Python has various kinds of string literals.

Normal string literals[edit]

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 using the % string-format operator, e.g. the Perl statement

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

is equivalent to the Python statement


However, the current standard for this sort of string formatting is to use the format method of strings:


Multi-line string literals[edit]

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 % string-format operator) is:

print("""Dear %(recipient)s,
I wish you to leave Sunnydale and never return.
Not Quite Love,
""" % {'sender': 'Buffy the Vampire Slayer', 'recipient': 'Spike'})

Raw strings[edit]

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.[5] 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
r"C:\Foo\Bar\Baz\"   # raises SyntaxError
r"C:\Foo\Bar\Baz\ ".rstrip()  # avoids the error by adding and removing trailing space
# A regular expression matching a quoted string with possible backslash quoting
# Reverse the arguments in a two-arg function call, e.g. foo(2, bar) -> foo(bar, 2).
re.sub(r'\(([^,]*?),([^,]*?)\)', r'(\2, \1)', code)
# Note that this won't work if either argument has parens or commas in it.

Concatenation of adjacent string literals[edit]

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

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

is equivalent to

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


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 the storage size as necessary. Prior to Python version 3, there were two kinds of integral numbers: traditional fixed size integers and "long" integers of arbitrary range. The conversion to "long" integers was performed automatically when required, and thus the programmer usually didn't have to be aware of the two integral types. In newer language versions the fixed-size integers are completely gone.

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. Complex numbers are indicated with the J or j suffix, e.g. 3 + 4j.

Lists, tuples, sets, dictionaries[edit]

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

a_list = [1, 2, 3, "a dog"]

or using normal object creation

a_second_list = list()

Tuples (class tuple) are immutable sequences of items of arbitrary types. There is also a special syntax to create tuples

a_tuple = 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 ().

Sets (class set) are mutable containers of items of arbitrary types. The items are not ordered, but sets support iteration over the items. A syntax for set creation appeared in Python 2.7/3.0

some_set = {0, (), False}

In earlier Python versions, sets would be created by calling initializing the set class with a list argument. 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.

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

a_dictionary = {"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().




Traditionally, x / y performed integer division if both x and y were integers (returning the floor of the quotient), and returned a float if either 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. For example, with

def mean(seq):
    return sum(seq) / len(seq)


def mean(seq):


Comparison operators[edit]

The basic comparison operators such as ==, <, >=, and so forth are used on all manner of values. Numbers, strings, sequences, and mappings can all be compared. Although disparate types (such as a str and an int) are defined to have a consistent relative ordering, this is considered a historical design quirk and will no longer be allowed in Python 3.0.



Logical operators[edit]



Functional programming[edit]


List comprehensions[edit]

Main article: List comprehension

One such construction is the list comprehension, which can be expressed with the following format:

L = [mapping-expression for element in source-list if filter-expression]

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


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

def qsort(L):
    if L == []:
        return []
    pivot = L[0]
            [pivot] +

First-class functions[edit]


Python's limited support for anonymous functions is the lambda construct. Since the availability of full anonymous functions is non-existent, the main use for lambda functions is named functions. Lambdas are limited to containing expressions rather than statements, although control flow can still be implemented less elegantly within lambda by using short-circuiting.[7]



def derivative(f, dx):
    """Return a function that approximates the derivative of f
    using an interval of dx, which should be appropriately small.
    def function(x):
    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.[8]

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

def foo(a, b):
  print 'a: %r' % a
  print 'b: %r' % b
  def bar(c):
    b = c
    print 'b*: %r' % b
  print 'b: %r' % b
foo(1, 2)

the output is:

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 mutable value and change that value, not the binding. E.g., a list with one element.



This is an example to lazily generate the prime numbers:

from itertools import count
def generate_primes(stop_at=0):
    primes = []
    for n in count(2):
        if 0 < stop_at < n:
            return # raises the StopIteration exception
        composite = False
        for p in primes:
            if not n % p:
                composite = True
            elif p ** 2 > n:
        if not composite:
            yield n

To use this function simply call, e.g.:

for i in generate_primes():  # iterate over ALL primes
    if i > 100:



Generator expressions[edit]

Further information: List comprehension


from itertools import islice
primes_under_million = (i for i in generate_primes() if i < 1000000)
two_thousandth_prime = islice(primes_under_million, 1999, 2000).next()


primes_under_million = [i for i in generate_primes(2000000) if i < 1000000]
two_thousandth_prime = primes_under_million[1999]


Dictionary and set comprehensions[edit]

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:


Python 2.7 and 3.0 unify all collection types by introducing dict and set comprehensions, similar to list comprehensions:

[0, 1, 4, 9, 16]
{0, 1, 4, 16, 9}






With statements[edit]

The "with" statement handles resources. One function is called when entering scope and another when leaving. This prevents forgetting to remove the resource and also handles more complicated situations such as exceptions.



class MyClass(object):
   def get_a(self):
      return self._a
   def set_a(self, value):
      self._a = value - 1
# Python 2.6 style
class MyClass(object):
   def a(self):
      return self._a
   @a.setter # makes the property writable
   def a(self, value):
      self._a = value - 1



Class and static methods[edit]

Python allows the creation of class methods and static method 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 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,"[10] which is attributed to Grace Hopper.[11][12] In this first code sample, there is an explicit check for the attribute (i.e., "asks permission"):

if hasattr(spam, 'eggs'):
    ham = spam.eggs

This second sample follows the EAFP paradigm:

    ham = spam.eggs
except AttributeError:

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,[12][13] 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[edit]

Python has 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 are terminated by the end of line. Comments spanning more than one line are achieved by inserting a multi-line string (with """ 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:

def getline():
    return sys.stdin.readline()       # Get one line and return it

Commenting a piece of code with multiple lines:

def getline():
    return sys.stdin.readline()    """this function
                                      gets one line
                                      and returns it"""

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():
    return sys.stdin.readline()

Multi-line docstring:

def getline():
    """Get one line
       from stdin
       and return it."""
    return sys.stdin.readline()




Function annotations[edit]

Function annotations are defined in PEP 3107. 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:




def menu_item():

is equivalent to

def menu_item():
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 above sample, viking_chorus might cause menu_item to be run 8 times (see Spam sketch) for each time it is called:

def viking_chorus(myfunc):
    def inner_func(*args, **kwargs):
        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 synchronisation,[14] but can be used for far more besides, including tail recursion elimination,[15] memoization and even improving the writing of decorators.[16]

Decorators can be chained by placing several on adjacent lines:

def black_knight():

is equivalent to

def black_knight():

or, using intermediate variables

def black_knight():
blue_decorator = favorite_color("Blue")
decorated_by_blue = blue_decorator(black_knight)
black_knight = invincible(decorated_by_blue)

In the above example, the favorite_color 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 favorite_color(color):
    def decorator(func):
        def wrapper():
        return wrapper
    return decorator

This would then decorate the black_knight function such that the color, "Blue", would be printed prior to the black_knight function running. Closure ensures that the color 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.

In Python prior to version 2.6, decorators apply to functions and methods, but not to classes. Decorating a (dummy) __new__ method can modify a class, however.[17] Class decorators are supported[18] starting with Python 2.6.


Easter eggs[edit]



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....

An antigravity module was added to Python 2.7 and 3.0. Importing it opens a web browser to an xkcd comic that portrays a humorous fictional use for such a module, intended to demonstrate the ease with which Python modules enable additional functionality.[19]


  1. ^ "Readability counts." - PEP 20 - The Zen of Python
  2. ^ "PEP 20 - The Zen of Python". Python Software Foundation. 2004-08-23. Retrieved 2008-11-24. 
  3. ^ "2. Lexical analysis". Python v3.4.0a1 documentation. Retrieved 2013-08-16. 
  4. ^ "2. Lexical analysis". Python v2.7.5 documentation. Retrieved 2013-08-16. 
  7. ^ David Mertz. "Functional Programming in Python". IBM developerWorks. 
  8. ^ The nonlocal keyword was adopted by PEP 3104
  9. ^ "Python Style Guide". Retrieved 2015-03-08. 
  10. ^ EAFP, Python Glossary
  11. ^ Hamblen, Diane. "Only the Limits of Our Imagination: An exclusive interview with RADM Grace M. Hopper". Department of the Navy Information Technology Magazine. Archived from the original on January 14, 2009. Retrieved 2007-01-31. 
  12. ^ a b Python in a nutshell, Alex Martelli, p. 134
  13. ^ Alex Martelli (19 May 2003). "EAFP v. LBYL". python-list mailing list. 
  16. ^ "The decorator module". Retrieved 2007-02-08. 
  18. ^ "PEP 3129 - Class Decorators". What's New in Python 2.6. 2010-08-11. Retrieved 2011-01-23. 
  19. ^ The referenced comic strip is xkcd #353; the module was added to the Python trunk for the 3.0 release. In Python 3.0 there has been added a geohash function, which calculates a hash as described in xkcd #426.

External links[edit]