PYTHON DOCUMENTATION
Introduction

    Python is a high level general-purpose programming language. It uses a multi-paradigm approach, meaning it supports procedural, object-oriented, and some functional programming constructs.

    It was created by Guido van Rossum as a successor to another language (called ABC) between 1985 and 1990, and is currently used on a large array of domains like web development, desktop applications, data science, DevOps, and automation/productivity.

   Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms.

   The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python Web site, https://www.python.org/, and may be freely distributed. The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation.

   The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C). Python is also suitable as an extension language for customizable applications.


Lexical analysis

   A Python program is read by a parser. Input to the parser is a stream of tokens, generated by the lexical analyzer. This chapter describes how the lexical analyzer breaks a file into tokens.

   Python reads program text as Unicode code points; the encoding of a source file can be given by an encoding declaration and defaults to UTF-8, see PEP 3120 for details. If the source file cannot be decoded, a SyntaxError is raised.

   
  • Line structure
  •    A Python program is divided into a number of logical lines.

       
  • Logical lines
  •    The end of a logical line is represented by the token NEWLINE. Statements cannot cross logical line boundaries except where NEWLINE is allowed by the syntax (e.g., between statements in compound statements). A logical line is constructed from one or more physical lines by following the explicit or implicit line joining rules.

       
  • Physical lines
  •    A physical line is a sequence of characters terminated by an end-of-line sequence. In source files and strings, any of the standard platform line termination sequences can be used - the Unix form using ASCII LF (linefeed), the Windows form using the ASCII sequence CR LF (return followed by linefeed), or the old Macintosh form using the ASCII CR (return) character. All of these forms can be used equally, regardless of platform. The end of input also serves as an implicit terminator for the final physical line. When embedding Python, source code strings should be passed to Python APIs using the standard C conventions for newline characters (the \n character, representing ASCII LF, is the line terminator).


    Control Flow Tools

       Besides the while statement just introduced, Python uses the usual flow control statements known from other languages, with some twists.

       
  • if Statements
  •    Perhaps the most well-known statement type is the if statement. For example:

    x = int(input("Please enter an integer: "))
    Please enter an integer: 42
    >>> if x < 0:
    ... x = 0
    ... print('Negative changed to zero')
    ... elif x == 0:
    ... print('Zero')
    ... elif x == 1:
    ... print('Single')
    ... else:
    ... print('More')
    ...
    More
    >>>

       There can be zero or more elif parts, and the else part is optional. The keyword ‘elif’ is short for ‘else if’, and is useful to avoid excessive indentation. An if … elif … elif … sequence is a substitute for the switch or case statements found in other languages.


       
  • for Statements
  •    The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers (like in Pascal), or giving the user the ability to define both the iteration step and halting condition (as C), Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. For example (no pun intended):

    # Measure some strings:
    ... words = ['cat', 'window', 'defenestrate']
    >>> for w in words:
    ... print(w, len(w))
    ...
    cat 3
    window 6
    defenestrate 12
    >>>

       Code that modifies a collection while iterating over that same collection can be tricky to get right. Instead, it is usually more straight-forward to loop over a copy of the collection or to create a new collection:

    # Strategy: Iterate over a copy
    for user, status in users.copy().items():
    if status == 'inactive':
    del users[user]
    # Strategy: Create a new collection
    active_users = {}
    for user, status in users.items():
    if status == 'active':
    active_users[user] = status

       
  • The range() Function

  •    If you do need to iterate over a sequence of numbers, the built-in function range() comes in handy. It generates arithmetic progressions:

    >>> for i in range(5):
    0
    1
    2
    3
    4

       The given end point is never part of the generated sequence; range(10) generates 10 values, the legal indices for items of a sequence of length 10. It is possible to let the range start at another number, or to specify a different increment (even negative; sometimes this is called the ‘step’):

    >>> list(range(5, 10))
    [5, 6, 7, 8, 9]

    >>> list(range(0, 10, 3))
    [0, 3, 6, 9]

    >>> list(range(-10, -100, -30))
    [-10, -40, -70]

       To iterate over the indices of a sequence, you can combine range() and len() as follows:

    >>> a = ['Mary', 'had', 'a', 'little', 'lamb']
    >>> for i in range(len(a)):
    ... print(i, a[i])
    ...
    0 Mary
    1 had
    2 a
    3 little
    4 lamb

    Data Structures

       This chapter describes some things you’ve learned about already in more detail, and adds some new things as well.

       More on Lists
    The list data type has some more methods. Here are all of the methods of list objects:


       
  • list.append(x)
  •    Add an item to the end of the list. Equivalent to a[len(a):] = [x].


       
  • list.extend(iterable)
  •     Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable


       
  • list.insert(i, x)
  •     Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).


       
  • list.remove(x)
  •    Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.


       
  • list.pop([i])
  •     Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)


       
  • list.clear()
  •    Remove all items from the list. Equivalent to del a[:].


       
  • list.index(x[, start[, end]])
  •     Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item. The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.


       
  • list.count(x)
  •     Return the number of times x appears in the list.


       
  • list.sort(*, key=None, reverse=False)
  •     Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).


       
  • list.reverse()
  •     Reverse the elements of the list in place


       
  • list.copy()
  •     Return a shallow copy of the list. Equivalent to a[:].


       An example that uses most of the list methods:

    >>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
    >>> fruits.count('apple')
    2
    >>> fruits.count('tangerine')
    0
    >>> fruits.index('banana')
    3
    >>> fruits.index('banana', 4) # Find next banana starting a position 4
    6
    >>> fruits.reverse()
    >>> fruits
    ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange']
    >>> fruits.append('grape')
    >>> fruits
    ['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple', 'orange', 'grape']
    >>> fruits.sort()
    >>> fruits
    ['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi', 'orange', 'pear']
    >>> fruits.pop()
    ...
    'pear'

       You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed – they return the default None. 1 This is a design principle for all mutable data structures in Python. Another thing you might notice is that not all data can be sorted or compared. For instance, [None, 'hello', 10] doesn’t sort because integers can’t be compared to strings and None can’t be compared to other types. Also, there are some types that don’t have a defined ordering relation. For example, 3+4j < 5+7j isn’t a valid comparison


    Modules

       If you quit from the Python interpreter and enter it again, the definitions you have made (functions and variables) are lost. Therefore, if you want to write a somewhat longer program, you are better off using a text editor to prepare the input for the interpreter and running it with that file as input instead. This is known as creating a script. As your program gets longer, you may want to split it into several files for easier maintenance. You may also want to use a handy function that you’ve written in several programs without copying its definition into each program. To support this, Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode). A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Within a module, the module’s name (as a string) is available as the value of the global variable __name__. For instance, use your favorite text editor to create a file called fibo.py in the current directory with the following contents:

    # Fibonacci numbers module

    def fib(n): # write Fibonacci series up to n
    a, b = 0, 1
    while a < n:

    print(a, end=' ')
    a, b = b, a+b
    print()
    def fib2(n): # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while a < n:
    result.append(a)
    a, b = b, a+b
    return result

       Now enter the Python interpreter and import this module with the following command:

    >>> import fibo

       This does not enter the names of the functions defined in fibo directly in the current symbol table; it only enters the module name fibo there. Using the module name you can access the functions:

    >>> fibo.fib(1000)
    0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
    >>> fibo.fib2(100)
    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
    >>> fibo.__name__
    'fibo'

       If you intend to use a function often you can assign it to a local name:

    >>> fib = fibo.fib
    >>> fib(500)
    0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

    Input and Output

       There are several ways to present the output of a program; data can be printed in a human-readable form, or written to a file for future use. This chapter will discuss some of the possibilities.

       
  • Fancier Output Formatting
  •    So far we’ve encountered two ways of writing values: expression statements and the print() function. (A third way is using the write() method of file objects; the standard output file can be referenced as sys.stdout. See the Library Reference for more information on this.) Often you’ll want more control over the formatting of your output than simply printing space-separated values. There are several ways to format output.

       
  • To use formatted string literals, begin a string with f or F before the opening quotation mark or triple quotation mark. Inside this string, you can write a Python expression between { and } characters that can refer to variables or literal values.
  • year = 2016
    >>> event = 'Referendum'
    >>> f'Results of the {year} {event}'
    'Results of the 2016 Referendum'
    >>>    
  • The str.format() method of strings requires more manual effort. You’ll still use { and } to mark where a variable will be substituted and can provide detailed formatting directives, but you’ll also need to provide the information to be formatted.
  • >>> yes_votes = 42_572_654
    >>> no_votes = 43_132_495
    >>> percentage = yes_votes / (yes_votes + no_votes)
    >>> '{:-9} YES votes {:2.2%}'.format(yes_votes, percentage)
    ' 42572654 YES votes 49.67%'

       
  • Finally, you can do all the string handling yourself by using string slicing and concatenation operations to create any layout you can imagine. The string type has some methods that perform useful operations for padding strings to a given column width.

  •    When you don’t need fancy output but just want a quick display of some variables for debugging purposes, you can convert any value to a string with the repr() or str() functions. The str() function is meant to return representations of values which are fairly human-readable, while repr() is meant to generate representations which can be read by the interpreter (or will force a SyntaxError if there is no equivalent syntax). For objects which don’t have a particular representation for human consumption, str() will return the same value as repr(). Many values, such as numbers or structures like lists and dictionaries, have the same representation using either function. Strings, in particular, have two distinct representations.
    Some examples:

    >>> s = 'Hello, world.'
    >>> str(s)
    'Hello, world.'
    >>> repr(s)
    "'Hello, world.'"
    >>> str(1/7)
    '0.14285714285714285'
    >>> x = 10 * 3.25
    >>> y = 200 * 200
    >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
    >>> print(s)
    The value of x is 32.5, and y is 40000...
    >>> # The repr() of a string adds string quotes and backslashes:
    ... hello = 'hello, world\n'
    >>> hellos = repr(hello)
    >>> print(hellos)
    'hello, world\n'
    >>> # The argument to repr() may be any Python object:
    ... repr((x, y, ('spam', 'eggs')))
    "(32.5, 40000, ('spam', 'eggs'))"

       
  • Reading and Writing Files
  •    open() returns a file object, and is most commonly used with two arguments: open(filename, mode).

    >>> f = open('workfile', 'w')>

       The first argument is a string containing the filename. The second argument is another string containing a few characters describing the way in which the file will be used. mode can be 'r' when the file will only be read, 'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end. 'r+' opens the file for both reading and writing. The mode argument is optional; 'r' will be assumed if it’s omitted.

       Normally, files are opened in text mode, that means, you read and write strings from and to the file, which are encoded in a specific encoding. If encoding is not specified, the default is platform dependent (see open()). 'b' appended to the mode opens the file in binary mode: now the data is read and written in the form of bytes objects. This mode should be used for all files that don’t contain text.

        In text mode, the default when reading is to convert platform-specific line endings (\n on Unix, \r\n on Windows) to just \n. When writing in text mode, the default is to convert occurrences of \n back to platform-specific line endings. This behind-the-scenes modification to file data is fine for text files, but will corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files.

       It is good practice to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point. Using with is also much shorter than writing equivalent try-finally blocks:

    >>> with open('workfile') as f:
    ... read_data = f.read()
    >>> # We can check that the file has been automatically closed.
    >>> f.closed
    True

       If you’re not using the with keyword, then you should call f.close() to close the file and immediately free up any system resources used by it.

    Warning
    Calling f.write() without using the with keyword or calling f.close() might result in the arguments of f.write() not being completely written to the disk, even if the program exits successfully.

       After a file object is closed, either by a with statement or by calling f.close(), attempts to use the file object will automatically fail.

    f.close()
    >>> f.read()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ValueError: I/O operation on closed file.
    >>>
    Errors and Exceptions

       Until now error messages haven’t been more than mentioned, but if you have tried out the examples you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions.

       
  • Syntax Errors
  •    Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while you are still learning Python:/p> >>> while True print('Hello world')
    File "<stdin>", line 1
    while True print('Hello world')
    ^
    SyntaxError: invalid syntax

       The parser repeats the offending line and displays a little ‘arrow’ pointing at the earliest point in the line where the error was detected. The error is caused by (or at least detected at) the token preceding the arrow: in the example, the error is detected at the function print(), since a colon (':') is missing before it. File name and line number are printed so you know where to look in case the input came from a script.


       
  • Exceptions
  •    Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal: you will soon learn how to handle them in Python programs. Most exceptions are not handled by programs, however, and result in error messages as shown here:

    >>> 10 * (1/0)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ZeroDivisionError: division by zero
    >>> 4 + spam*3
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'spam' is not defined
    >>> '2' + 2
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: can only concatenate str (not "int") to str

       The last line of the error message indicates what happened. Exceptions come in different types, and the type is printed as part of the message: the types in the example are ZeroDivisionError, NameError and TypeError. The string printed as the exception type is the name of the built-in exception that occurred. This is true for all built-in exceptions, but need not be true for user-defined exceptions (although it is a useful convention). Standard exception names are built-in identifiers (not reserved keywords).

       The rest of the line provides detail based on the type of exception and what caused it.

       The preceding part of the error message shows the context where the exception occurred, in the form of a stack traceback. In general it contains a stack traceback listing source lines; however, it will not display lines read from standard input.

       Built-in Exceptions lists the built-in exceptions and their meanings.

       
  • Handling Exceptions
  •    It is possible to write programs that handle selected exceptions. Look at the following example, which asks the user for input until a valid integer has been entered, but allows the user to interrupt the program (using Control-C or whatever the operating system supports); note that a user-generated interruption is signalled by raising the KeyboardInterrupt exception.

    while True:
    ... try:
    ... x = int(input("Please enter a number: "))

    ... break
    ... except ValueError:
    ... print("Oops! That was no valid number. Try again...")

       The try statement works as follows. • First, the try clause (the statement(s) between the try and except keywords) is executed. • If no exception occurs, the except clause is skipped and execution of the try statement is finished. • If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement. • If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above.

       A try statement may have more than one except clause, to specify handlers for different exceptions. At most one handler will be executed. Handlers only handle exceptions that occur in the corresponding try clause, not in other handlers of the same try statement. An except clause may name multiple exceptions as a parenthesized tuple, for example:

    ... except (RuntimeError, TypeError, NameError):
    ... pass

       A class in an except clause is compatible with an exception if it is the same class or a base class thereof (but not the other way around — an except clause listing a derived class is not compatible with a base class). For example, the following code will print B, C, D in that order:


    class B(Exception):
    pass

    class C(B):
    pass

    class D(C):
    pass

    for cls in [B, C, D]:
    try:
    raise cls()
    except D:
    print("D")
    except C:
    print("C")
    except B:
    print("B")

       Note that if the except clauses were reversed (with except B first), it would have printed B, B, B — the first matching except clause is triggered.

       The last except clause may omit the exception name(s), to serve as a wildcard. Use this with extreme caution, since it is easy to mask a real programming error in this way! It can also be used to print an error message and then re-raise the exception (allowing a caller to handle the exception as well):

    import sys

    try:
    f = open('myfile.txt')
    s = f.readline()
    i = int(s.strip())
    except OSError as err:
    print("OS error: {0}".format(err))
    except ValueError:
    print("Could not convert data to an integer.")
    except:
    print("Unexpected error:", sys.exc_info()[0])
    raise

       The try … except statement has an optional else clause, which, when present, must follow all except clauses. It is useful for code that must be executed if the try clause does not raise an exception. For example:

    for arg in sys.argv[1:]:
    try:
    f = open(arg, 'r')
    except OSError:
    print('cannot open', arg)
    else:
    print(arg, 'has', len(f.readlines()), 'lines')
    f.close()

       The use of the else clause is better than adding additional code to the try clause because it avoids accidentally catching an exception that wasn’t raised by the code being protected by the try … except statement.

       When an exception occurs, it may have an associated value, also known as the exception’s argument. The presence and type of the argument depend on the exception type.

       The except clause may specify a variable after the exception name. The variable is bound to an exception instance with the arguments stored in instance.args. For convenience, the exception instance defines __str__() so the arguments can be printed directly without having to reference .args. One may also instantiate an exception first before raising it and add any attributes to it as desired.

    >>> try:
    ... raise Exception('spam', 'eggs')
    ... except Exception as inst:
    ... print(type(inst)) # the exception instance
    ... print(inst.args) # arguments stored in .args
    ... print(inst) # __str__ allows args to be printed directly,
    ... # but may be overridden in exception subclasses
    ... x, y = inst.args # unpack args
    ... print('x =', x)
    ... print('y =', y)
    ... <class "Exception">
    ('spam','eggs')
    ('spam','eggs')
    x = spam
    y = eggs

       If an exception has arguments, they are printed as the last part (‘detail’) of the message for unhandled exceptions.

       Exception handlers don’t just handle exceptions if they occur immediately in the try clause, but also if they occur inside functions that are called (even indirectly) in the try clause. For example:

    def this_fails():
    ... x = 1/0
    ...
    >>> try:
    ... this_fails()
    ... except ZeroDivisionError as err:
    ... print('Handling run-time error:', err)
    ...
    Handling run-time error: division by zero
    >>>
    Reference
    For a complete reference All the documentation in this page is taken from Python.org