Python 3.6

**Python** is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.

Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming. Python is often described as a "batteries included" language due to its comprehensive standard library.

Python uses whitespace indentation, rather than curly brackets or keywords, to delimit blocks. An increase in indentation comes after certain statements; a decrease in indentation signifies the end of the current block.

Python's large standard library, commonly cited as one of its greatest strengths, provides tools suited to many tasks. It includes modules for creating graphical user interfaces, connecting to relational databases, generating pseudorandom numbers, arithmetic with arbitrary precision decimals, manipulating regular expressions, and unit testing.

As of March 2018, the Python Package Index (PyPI), the official repository for third-party Python software, contains over 130,000 packages with a wide range of functionality

When speed is important, a Python programmer can move time-critical functions to extension modules written in languages such as C

## Libraries

Python's large standard library, commonly cited as one of its greatest strengths, provides tools suited to many tasks. For Internet-facing applications, many standard formats and protocols such as MIME and HTTP are supported. It includes modules for creating graphical user interfaces, connecting to relational databases, generating pseudorandom numbers, arithmetic with arbitrary-precision decimals, manipulating regular expressions, and unit testing.

Some parts of the standard library are covered by specifications (for example, the Web Server Gateway Interface (WSGI) implementation `wsgiref` follows PEP 333), but most modules are not. They are specified by their code, internal documentation, and test suites. However, because most of the standard library is cross-platform Python code, only a few modules need altering or rewriting for variant implementations.

As of November 2019,[update] the Python Package Index (PyPI), the official repository for third-party Python software, contains over 200,000 packages with a wide range of functionality, including:

* Automation
* Data analytics
* Databases
* Documentation
* Graphical user interfaces
* Image processing
* Machine learning
* Mobile App
* Multimedia
* Networking
* Scientific computing
* System administration
* Test frameworks
* Text processing
* Web frameworks
* Web scraping

## Anaconda

I strongly recommend installing Anaconda (Python distribution). It comes with numpy and scipy preinstalled.

Anaconda also comes with Spyder which makes it much easier to find and correct errors.

Package versions are managed by the package management system conda.

Conda was originally developed to solve difficult package management challenges faced by Python data scientists.

The big difference between Conda and the pip package manager is in how package dependencies are managed, which is a significant challenge for Python data science and the reason Conda was created. pip installs all Python package dependencies required, whether or not those conflict with other packages previously installed. So a working installation of, for example, Google TensorFlow can suddenly stop working when you pip-install a new package that needs a different version of the NumPy library. More insidiously, everything might still appear to work, but now you get different results, or you are unable to reproduce the same results elsewhere because you didn't pip-install in the same order.

Conda analyses your current environment, everything you have installed, any version limitations you specify (e.g. you only want tensorflow >= 2.0) and figures out how to install compatible dependencies. Or it will tell you that what you want can't be done.

# comments follow a pound sign

## Help

```
Help on _Helper in module _sitebuiltins object:

class _Helper(builtins.object)

```

## Sounds

To play a sound when your program is finished use:

import winsound

duration = 1000 # milliseconds

freq = 440 # hz

winsound.Beep(freq,duration)

## Time

To time your program use:

import time

StartTime=time.time()

# put the rest of your program here

EndTime=time.time()

print ('Time in seconds:', EndTime-StartTime)

## Exit

To exit your program at any point just use:

raise SystemExit()

## Math

3+5 =8

3-5 =-2

3\*5 =15

3/5 = 0.6

5//3 = 1 # Floor

-5//3 = -2 # (Rounding toward negative infinity)

round(1.8) = 2 # (Rounding toward nearest integer)

round(1.5) = 2 # (Rounding toward even)

3%5 = 3 # Mod

3\*\*5 = 243

~5 = -6 # -x-1 (Inverts bits)

1<<2 = 4 # (Shifts bits to the left)

4>>2 = 1 # (Shifts bits to the right)

Rounding towards negative infinity means that the equation `(a + b)//b == a//b + 1` is always true and that the equation `b*(a//b) + a%b == a` is valid for both positive and negative values of `a`.

Back to top

## Variables

Main article: Variables

 length\_of\_age\_string = len(age\_string)
 todays\_date = "1 jan 2018"
 it\_is\_not\_january = (it\_is\_january == false)

Back to top

### Dictionaries

"NY": "New York",
"CA": "Sacramento",
"IL": "Springfield"
}
```
{'NY': 'New York', 'CA': 'Sacramento', 'IL': 'Springfield'}
 print (capital['CA'])
```
Sacramento
 del capital['IL']
 print (capital)
```
{'NY': 'New York', 'CA': 'Sacramento'}
 capital['Illinois'] = 'Springfield'
 print (capital)
```
{'NY': 'New York', 'CA': 'Sacramento', 'Illinois': 'Springfield'}
```

### Ordered lists

External link: https://rushter.com/blog/python-lists-and-tuples/

Back to top

## Input and output

```
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

```

```
Help on built-in function input in module builtins:

input(prompt=None, /)
    Read a string from standard input.  The trailing newline is stripped.

    The prompt string, if given, is printed to standard output without a
    trailing newline before reading input.

    If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
    On *nix systems, readline is used if available.

```

name = "john"

age = 36

print(name, "is", age, "years old") # default separater is a single space. default end is new line

print("this is printed on a new line")

print("But we can change the def", "ault separator and default end", sep="", end="")

print("This is not printed on a new line")

print("a\tbx\tc\n12345678901234567890") # \t is tab and \n is new line.

print(r'a\tbx\tc\n12345678901234567890') # r = raw text. Slashes are treated like ordinary text.

your\_input = input("please enter your input: ")

```
john is 36 years old
this is printed on a new line
But we can change the default separator and default endThis is not printed on a new line
a       bx      c
12345678901234567890
a\tbx\tc\n12345678901234567890
please enter your input:

```

Back to top

## Loops

Main article: Loops

```

The "for" statement

The "for" statement is used to iterate over the elements of a sequence
(such as a string, tuple or list) or other iterable object:

   for_stmt ::= "for" target_list "in" expression_list ":" suite
                ["else" ":" suite]

The expression list is evaluated once; it should yield an iterable
object.  An iterator is created for the result of the
"expression_list".  The suite is then executed once for each item
provided by the iterator, in the order returned by the iterator.  Each
item in turn is assigned to the target list using the standard rules
for assignments (see Assignment statements), and then the suite is
executed.  When the items are exhausted (which is immediately when the
sequence is empty or an iterator raises a "StopIteration" exception),
the suite in the "else" clause, if present, is executed, and the loop
terminates.

A "break" statement executed in the first suite terminates the loop
without executing the "else" clause's suite.  A "continue" statement
executed in the first suite skips the rest of the suite and continues
with the next item, or with the "else" clause if there is no next
item.

The for-loop makes assignments to the variables(s) in the target list.
This overwrites all previous assignments to those variables including
       print(i)
       i = 5             # this will not affect the for-loop
                         # because i will be overwritten with the next
                         # index in the range

Names in the target list are not deleted when the loop is finished,
but if the sequence is empty, they will not have been assigned to at
all by the loop.  Hint: the built-in function "range()" returns an
iterator of integers suitable to emulate the effect of Pascal's "for i
:= a to b do"; e.g., "list(range(3))" returns the list "[0, 1, 2]".

Note: There is a subtlety when the sequence is being modified by the
  loop (this can only occur for mutable sequences, i.e. lists).  An
  internal counter is used to keep track of which item is used next,
  and this is incremented on each iteration.  When this counter has
  reached the length of the sequence the loop terminates.  This means
  that if the suite deletes the current (or a previous) item from the
  sequence, the next item will be skipped (since it gets the index of
  the current item which has already been treated).  Likewise, if the
  suite inserts an item in the sequence before the current item, the
  current item will be treated again the next time through the loop.
  This can lead to nasty bugs that can be avoided by making a
  temporary copy using a slice of the whole sequence, e.g.,

     for x in a[:]:
         if x < 0: a.remove(x)

Related help topics: break, continue, while

```

for num in range(1000, 2000, 100): # range is an iterator not a list or array

print (num) # prints 1000 to 1900 in steps of 100

```
The "while" statement

The "while" statement is used for repeated execution as long as an
expression is true:

   while_stmt ::= "while" expression ":" suite
                  ["else" ":" suite]

This repeatedly tests the expression and, if it is true, executes the
first suite; if the expression is false (which may be the first time
it is tested) the suite of the "else" clause, if present, is executed
and the loop terminates.

A "break" statement executed in the first suite terminates the loop
without executing the "else" clause's suite.  A "continue" statement
executed in the first suite skips the rest of the suite and goes back
to testing the expression.

Related help topics: break, continue, if, TRUTHVALUE

```

num = 0

while num < 100000000: # Thats 100,000,000. It runs in about 10 seconds

num = num + 1
Back to top

## Conditional statements

Main article: If Statements

```
The "if" statement

The "if" statement is used for conditional execution:

   if_stmt ::= "if" expression ":" suite
               ( "elif" expression ":" suite )*
               ["else" ":" suite]

It selects exactly one of the suites by evaluating the expressions one
by one until one is found to be true (see section Boolean operations
for the definition of true and false); then that suite is executed
(and no other part of the "if" statement is executed or evaluated).
If all expressions are false, the suite of the "else" clause, if
present, is executed.

Related help topics: TRUTHVALUE

```

young = age < 10

old = age > 65

middle\_age = (45 < age < 65)

if young: # dont forget the colon at the end

print (name, "is young")
elif old:

print (name, "is old")
elif middle\_age:

pass # Do nothing
else:

print (name, "is neither young nor old nor middle age")
Back to top

## Functions

Main article: Functions

```
Function definitions

A function definition defines a user-defined function object (see
section The standard type hierarchy):

   funcdef                 ::= [decorators] "def" funcname "(" [parameter_list]")" ["->" expression] ":" suite
   decorators              ::= decorator+
   decorator               ::= "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE
   dotted_name             ::= identifier ("." identifier)*
   parameter_list          ::= defparameter ("," defparameter)* ["," [parameter_list_starargs]]
   parameter_list_starargs ::= "*" [parameter] ("," defparameter)* ["," ["**" parameter [","]]]
   parameter               ::= identifier [":" expression]
   defparameter            ::= parameter ["=" expression]
   funcname                ::= identifier

A function definition is an executable statement.  Its execution binds
the function name in the current local namespace to a function object
(a wrapper around the executable code for the function).  This
function object contains a reference to the current global namespace
as the global namespace to be used when the function is called.

The function definition does not execute the function body; this gets
executed only when the function is called. [3]

A function definition may be wrapped by one or more *decorator*
expressions. Decorator expressions are evaluated when the function is
defined, in the scope that contains the function definition.  The
result must be a callable, which is invoked with the function object
as the only argument. The returned value is bound to the function name
instead of the function object.  Multiple decorators are applied in
nested fashion. For example, the following code

   @f1(arg)
   @f2
   def func(): pass

is roughly equivalent to

   def func(): pass
   func = f1(arg)(f2(func))

except that the original function is not temporarily bound to the name
"func".

When one or more *parameters* have the form *parameter* "="
*expression*, the function is said to have "default parameter values."
For a parameter with a default value, the corresponding *argument* may
be omitted from a call, in which case the parameter's default value is
substituted.  If a parameter has a default value, all following
parameters up until the ""*"" must also have a default value --- this
is a syntactic restriction that is not expressed by the grammar.

**Default parameter values are evaluated from left to right when the
function definition is executed.** This means that the expression is
evaluated once, when the function is defined, and that the same "pre-
computed" value is used for each call.  This is especially important
to understand when a default parameter is a mutable object, such as a
list or a dictionary: if the function modifies the object (e.g. by
appending an item to a list), the default value is in effect modified.
This is generally not what was intended.  A way around this is to use
"None" as the default, and explicitly test for it in the body of the
           penguin = []
       penguin.append("property of the zoo")
       return penguin

Function call semantics are described in more detail in section Calls.
A function call always assigns values to all parameters mentioned in
the parameter list, either from position arguments, from keyword
arguments, or from default values.  If the form ""*identifier"" is
present, it is initialized to a tuple receiving any excess positional
parameters, defaulting to the empty tuple. If the form
""**identifier"" is present, it is initialized to a new ordered
mapping receiving any excess keyword arguments, defaulting to a new
empty mapping of the same type.  Parameters after ""*"" or
""*identifier"" are keyword-only parameters and may only be passed
used keyword arguments.

Parameters may have annotations of the form "": expression"" following
the parameter name.  Any parameter may have an annotation even those
of the form "*identifier" or "**identifier".  Functions may have
"return" annotation of the form ""-> expression"" after the parameter
list.  These annotations can be any valid Python expression and are
evaluated when the function definition is executed.  Annotations may
be evaluated in a different order than they appear in the source code.
The presence of annotations does not change the semantics of a
function.  The annotation values are available as values of a
dictionary keyed by the parameters' names in the "__annotations__"
attribute of the function object.

It is also possible to create anonymous functions (functions not bound
to a name), for immediate use in expressions.  This uses lambda
expressions, described in section Lambdas.  Note that the lambda
expression is merely a shorthand for a simplified function definition;
a function defined in a ""def"" statement can be passed around or
assigned to another name just like a function defined by a lambda
expression.  The ""def"" form is actually more powerful since it
allows the execution of multiple statements and annotations.

**Programmer's note:** Functions are first-class objects.  A ""def""
statement executed inside a function definition defines a local
function that can be returned or passed around.  Free variables used
in the nested function can access the local variables of the function
containing the def.  See section Naming and binding for details.

See also:

  **PEP 3107** - Function Annotations
     The original specification for function annotations.

```

def convert\_to\_celcius(fahrenheit):

"""Converts fahrenheit to celcius""" # documentation that is printed out when we use help("convert\_to\_celcius")
return (fahrenheit-32)\*5/9
print (convert\_to\_celcius(212))

Back to top

## Importing modules

```
The "import" statement

   import_stmt     ::= "import" module ["as" name] ( "," module ["as" name] )*
                   ( "," identifier ["as" name] )*
                   ( "," identifier ["as" name] )* [","] ")"
   module          ::= (identifier ".")* identifier
   relative_module ::= "."* module | "."+
   name            ::= identifier

The basic import statement (no "from" clause) is executed in two
steps:

1. find a module, loading and initializing it if necessary

2. define a name or names in the local namespace for the scope
   where the "import" statement occurs.

When the statement contains multiple clauses (separated by commas) the
two steps are carried out separately for each clause, just as though
the clauses had been separated out into individual import statements.

The details of the first step, finding and loading modules are
described in greater detail in the section on the import system, which
also describes the various types of packages and modules that can be
imported, as well as all the hooks that can be used to customize the
import system. Note that failures in this step may indicate either
that the module could not be located, *or* that an error occurred
while initializing the module, which includes execution of the
module's code.

If the requested module is retrieved successfully, it will be made
available in the local namespace in one of three ways:

* If the module name is followed by "as", then the name following
  "as" is bound directly to the imported module.

* If no other name is specified, and the module being imported is a
  top level module, the module's name is bound in the local namespace
  as a reference to the imported module

* If the module being imported is *not* a top level module, then the
  name of the top level package that contains the module is bound in
  the local namespace as a reference to the top level package. The
  imported module must be accessed using its full qualified name
  rather than directly

The "from" form uses a slightly more complex process:

1. find the module specified in the "from" clause, loading and
   initializing it if necessary;

2. for each of the identifiers specified in the "import" clauses:

   1. check if the imported module has an attribute by that name

   2. if not, attempt to import a submodule with that name and then
      check the imported module again for that attribute

   3. if the attribute is not found, "ImportError" is raised.

   4. otherwise, a reference to that value is stored in the local
      namespace, using the name in the "as" clause if it is present,
      otherwise using the attribute name

Examples:

   import foo                 # foo imported and bound locally
   import foo.bar.baz         # foo.bar.baz imported, foo bound locally
   import foo.bar.baz as fbb  # foo.bar.baz imported and bound as fbb
   from foo.bar import baz    # foo.bar.baz imported and bound as baz
   from foo import attr       # foo imported and foo.attr bound as attr

If the list of identifiers is replaced by a star ("'*'"), all public
names defined in the module are bound in the local namespace for the
scope where the "import" statement occurs.

The *public names* defined by a module are determined by checking the
module's namespace for a variable named "__all__"; if defined, it must
be a sequence of strings which are names defined or imported by that
module.  The names given in "__all__" are all considered public and
are required to exist.  If "__all__" is not defined, the set of public
names includes all names found in the module's namespace which do not
begin with an underscore character ("'_'").  "__all__" should contain
the entire public API. It is intended to avoid accidentally exporting
items that are not part of the API (such as library modules which were
imported and used within the module).

The wild card form of import --- "from module import *" --- is only
allowed at the module level.  Attempting to use it in class or
function definitions will raise a "SyntaxError".

When specifying what module to import you do not have to specify the
absolute name of the module. When a module or package is contained
within another package it is possible to make a relative import within
the same top package without having to mention the package name. By
using leading dots in the specified module or package after "from" you
can specify how high to traverse up the current package hierarchy
without specifying exact names. One leading dot means the current
package where the module making the import exists. Two dots means up
one package level. Three dots is up two levels, etc. So if you execute
"from . import mod" from a module in the "pkg" package then you will
end up importing "pkg.mod". If you execute "from ..subpkg2 import mod"
from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The
specification for relative imports is contained within **PEP 328**.

"importlib.import_module()" is provided to support applications that
determine dynamically the modules to be loaded.

# Future statements

A *future statement* is a directive to the compiler that a particular
module should be compiled using syntax or semantics that will be
available in a specified future release of Python where the feature
becomes standard.

The future statement is intended to ease migration to future versions
of Python that introduce incompatible changes to the language.  It
allows use of the new features on a per-module basis before the
release in which the feature becomes standard.

   future_statement ::= "from" "__future__" "import" feature ["as" name]
                        ("," feature ["as" name])*
                        ("," feature ["as" name])* [","] ")"
   feature          ::= identifier
   name             ::= identifier

A future statement must appear near the top of the module.  The only
lines that can appear before a future statement are:

* the module docstring (if any),

* comments,

* blank lines, and

* other future statements.

The features recognized by Python 3.0 are "absolute_import",
"division", "generators", "unicode_literals", "print_function",
"nested_scopes" and "with_statement".  They are all redundant because
they are always enabled, and only kept for backwards compatibility.

A future statement is recognized and treated specially at compile
time: Changes to the semantics of core constructs are often
implemented by generating different code.  It may even be the case
that a new feature introduces new incompatible syntax (such as a new
reserved word), in which case the compiler may need to parse the
module differently.  Such decisions cannot be pushed off until
runtime.

For any given release, the compiler knows which feature names have
been defined, and raises a compile-time error if a future statement
contains a feature not known to it.

The direct runtime semantics are the same as for any import statement:
there is a standard module "__future__", described later, and it will
be imported in the usual way at the time the future statement is
executed.

The interesting runtime semantics depend on the specific feature
enabled by the future statement.

Note that there is nothing special about the statement:

   import __future__ [as name]

That is not a future statement; it's an ordinary import statement with
no special semantics or syntax restrictions.

Code compiled by calls to the built-in functions "exec()" and
"compile()" that occur in a module "M" containing a future statement
will, by default, use the new syntax or semantics associated with the
future statement.  This can be controlled by optional arguments to
"compile()" --- see the documentation of that function for details.

A future statement typed at an interactive interpreter prompt will
take effect for the rest of the interpreter session.  If an
interpreter is started with the "-i" option, is passed a script name
to execute, and the script includes a future statement, it will be in
effect in the interactive session started after the script is
executed.

See also:

  **PEP 236** - Back to the __future__
     The original proposal for the __future__ mechanism.

Related help topics: MODULES

```

There is nothing fancy about a module. Its just a program containing function definitions. The program is executed when it is imported unless it was previously imported. You can easily create your own modules.

After importing a module you can use help("module name") to get info.

Template:Help("time")
Template:Help("math")
dir(module\_name) # returns a list of all the functions in the module. Easier to read if converted to an array.

import time

start = time.time()

time.sleep(15) # delay for 15 seconds

end = time.time()

print(end - start) # time in seconds

import math

x = math.sqrt(9) # x = sqrt(9) wont work. You must use math.sqrt

import math as mt

x = mt.sqrt(9)

from math import sqrt

x = sqrt(9) # no need for math.sqrt since sqrt has been imported into the current namespace

import random

rand1 = random.randint(0,999999) # random integer between 0 and 999,999

Back to top

## Classes

Tutorial

```
Class definitions

A class definition defines a class object (see section The standard
type hierarchy):

   classdef    ::= [decorators] "class" classname [inheritance] ":" suite
   inheritance ::= "(" [argument_list] ")"
   classname   ::= identifier

A class definition is an executable statement.  The inheritance list
usually gives a list of base classes (see Metaclasses for more
advanced uses), so each item in the list should evaluate to a class
object which allows subclassing.  Classes without an inheritance list
inherit, by default, from the base class "object"; hence,

   class Foo:
       pass

is equivalent to

   class Foo(object):
       pass

The class's suite is then executed in a new execution frame (see
Naming and binding), using a newly created local namespace and the
original global namespace. (Usually, the suite contains mostly
function definitions.)  When the class's suite finishes execution, its
execution frame is discarded but its local namespace is saved. [4] A
class object is then created using the inheritance list for the base
classes and the saved local namespace for the attribute dictionary.
The class name is bound to this class object in the original local
namespace.

The order in which attributes are defined in the class body is
preserved in the new class's "__dict__".  Note that this is reliable
only right after the class is created and only for classes that were
defined using the definition syntax.

Class creation can be customized heavily using metaclasses.

Classes can also be decorated: just like when decorating functions,

   @f1(arg)
   @f2
   class Foo: pass

is roughly equivalent to

   class Foo: pass
   Foo = f1(arg)(f2(Foo))

The evaluation rules for the decorator expressions are the same as for
function decorators.  The result is then bound to the class name.

**Programmer's note:** Variables defined in the class definition are
class attributes; they are shared by instances.  Instance attributes
can be set in a method with "self.name = value".  Both class and
instance attributes are accessible through the notation ""self.name"",
and an instance attribute hides a class attribute with the same name
when accessed in this way.  Class attributes can be used as defaults
for instance attributes, but using mutable values there can lead to
unexpected results.  Descriptors can be used to create instance
variables with different implementation details.

See also: **PEP 3115** - Metaclasses in Python 3 **PEP 3129** -
  Class Decorators

Related help topics: CLASSES, SPECIALMETHODS

```

Calling methods is very similar to calling functions in a module. Instead of module.function(arguments) you use class.method(arguments).

str.capitalize("string") # returns the string with first letter capitalized

In a perfect world modules and classes would be the same thing because every module would correspond to exactly one class of object. But we dont live in that world.

type(any\_object) # returns the class of the object

When we call a method, the object itself is passed as the first parameter (called "self").

self.name = name
self.birthday = birthday
def age(self):
today = datetime.date.today()
age = today.year - self.birthdate.year
return age
person1 = Person("Bill", datetime.date(1981, 12, 27) )

Back to top

## Text files

```
The "with" statement

The "with" statement is used to wrap the execution of a block with
methods defined by a context manager (see section With Statement
Context Managers). This allows common "try"..."except"..."finally"
usage patterns to be encapsulated for convenient reuse.

   with_stmt ::= "with" with_item ("," with_item)* ":" suite
   with_item ::= expression ["as" target]

The execution of the "with" statement with one "item" proceeds as
follows:

1. The context expression (the expression given in the "with_item")
   is evaluated to obtain a context manager.

2. The context manager's "__exit__()" is loaded for later use.

3. The context manager's "__enter__()" method is invoked.

4. If a target was included in the "with" statement, the return
   value from "__enter__()" is assigned to it.

   Note: The "with" statement guarantees that if the "__enter__()"
     method returns without an error, then "__exit__()" will always be
     called. Thus, if an error occurs during the assignment to the
     target list, it will be treated the same as an error occurring
     within the suite would be. See step 6 below.

5. The suite is executed.

6. The context manager's "__exit__()" method is invoked.  If an
   exception caused the suite to be exited, its type, value, and
   traceback are passed as arguments to "__exit__()". Otherwise, three
   "None" arguments are supplied.

   If the suite was exited due to an exception, and the return value
   from the "__exit__()" method was false, the exception is reraised.
   If the return value was true, the exception is suppressed, and
   execution continues with the statement following the "with"
   statement.

   If the suite was exited for any reason other than an exception, the
   return value from "__exit__()" is ignored, and execution proceeds
   at the normal location for the kind of exit that was taken.

With more than one item, the context managers are processed as if
       suite

is equivalent to

           suite

Changed in version 3.1: Support for multiple context expressions.

See also:

  **PEP 343** - The "with" statement
     The specification, background, and examples for the Python "with"
     statement.

Related help topics: CONTEXTMANAGERS, EXCEPTIONS, yield

```

with open("TextFile.txt", "r") as file:

contents = file.read()
with open("TextFile.txt", "r") as file:

List\_of\_lines = file.readlines()
with open("TextFile.txt", "w") as output\_file:

output\_file.write(contents)
Back to top

## Database

Creating:

import sqlite3

con = sqlite3.connect('test.db')

cur = con.cursor()

cur.execute('CREATE TABLE test(name TEXT, age INTEGER)')

cur.execute('INSERT INTO test VALUES("John", 33)')

cur.execute('INSERT INTO test VALUES("Alice", 21)')

x=["Bob", 18]

cur.execute('INSERT INTO test VALUES(?, ?)', x)

con.commit() # changes are not committed until this command

cur.execute('SELECT \* FROM test WHERE age > 5 ORDER BY name')

whole\_table = cur.fetchall()

print (whole\_table)

con.close()

Updating"

import sqlite3

con = sqlite3.connect('test.db')

cur = con.cursor()

cur.execute('DELETE FROM test WHERE name = "John"')

cur.execute('UPDATE test SET age = 99 WHERE name = "Alice"') # if there is more than one Mary then they both get updated

cur.execute('SELECT \* FROM test WHERE age > 5 ORDER BY name')

whole\_table = cur.fetchall()

print (whole\_table)

con.close()

Back to top

## Pandas

Tutorial and Cheatsheet

```
Help on package pandas:

NAME
    pandas

DESCRIPTION
    pandas - a powerful data analysis and manipulation library for Python

    **pandas** is a Python package providing fast, flexible, and expressive data
    structures designed to make working with "relational" or "labeled" data both
    easy and intuitive. It aims to be the fundamental high-level building block for
    doing practical, **real world** data analysis in Python. Additionally, it has
    the broader goal of becoming **the most powerful and flexible open source data
    analysis / manipulation tool available in any language**. It is already well on
    its way toward this goal.

    Main Features

    Here are just a few of the things that pandas does well:

      - Easy handling of missing data in floating point as well as non-floating
        point data.
      - Size mutability: columns can be inserted and deleted from DataFrame and
        higher dimensional objects
      - Automatic and explicit data alignment: objects can be explicitly aligned
        to a set of labels, or the user can simply ignore the labels and let
        `Series`, `DataFrame`, etc. automatically align the data for you in
        computations.
      - Powerful, flexible group by functionality to perform split-apply-combine
        operations on data sets, for both aggregating and transforming data.
      - Make it easy to convert ragged, differently-indexed data in other Python
        and NumPy data structures into DataFrame objects.
      - Intelligent label-based slicing, fancy indexing, and subsetting of large
        data sets.
      - Intuitive merging and joining data sets.
      - Flexible reshaping and pivoting of data sets.
      - Hierarchical labeling of axes (possible to have multiple labels per tick).
      - Robust IO tools for loading data from flat files (CSV and delimited),
        Excel files, databases, and saving/loading data from the ultrafast HDF5
        format.
      - Time series-specific functionality: date range generation and frequency
        conversion, moving window statistics, moving window linear regressions,
        date shifting and lagging, etc.

PACKAGE CONTENTS
    _libs (package)
    _version
    api (package)
    compat (package)
    computation (package)
    conftest
    core (package)
    errors (package)
    formats (package)
    io (package)
    json
    lib
    parser
    plotting (package)
    testing
    tests (package)
    tools (package)
    tseries (package)
    tslib
    types (package)
    util (package)

SUBMODULES
    _hashtable
    _lib
    _tslib
    offsets

DATA
    IndexSlice = <pandas.core.indexing._IndexSlice object>
    NaT = NaT
    __docformat__ = 'restructuredtext'
    datetools = <module 'pandas.core.datetools' from 'C:\\Users\...\lib\\s...
    describe_option = <pandas.core.config.CallableDynamicDoc object>
    get_option = <pandas.core.config.CallableDynamicDoc object>
    json = <module 'pandas.json' from 'C:\\Users\\EM3RY_000\\Anaconda3\\li...
    lib = <module 'pandas.lib' from 'C:\\Users\\EM3RY_000\\Anaconda3\\lib\...
    options = <pandas.core.config.DictWrapper object>
    parser = <module 'pandas.parser' from 'C:\\Users\\EM3RY_0...naconda3\\...
    plot_params = {'xaxis.compat': False}
    reset_option = <pandas.core.config.CallableDynamicDoc object>
    set_option = <pandas.core.config.CallableDynamicDoc object>
    tslib = <module 'pandas.tslib' from 'C:\\Users\\EM3RY_000\\Anaconda3\\...

VERSION
    0.23.0

FILE
    c:\users\em3ry_000\anaconda3\lib\site-packages\pandas\__init__.py

```

pandas = panels of data (multi-dimensional data)

A pandas dataframe (spreadsheet) is normally created from a list or a list of lists. We could create a dataframe from an array but then all the data would have to be of the same type.

Working with dataframes is faster than working with lists because dataframes are two dimensional numpy arrays. Each data type is stored in a separate block of memeory. All the integers are together in one block. All the floats are together in one block...etc.

numpy dtypes that are also accepted in pandas:

```
[numpy.generic,
 [[numpy.number,
   [[numpy.integer,
     [[numpy.signedinteger,
       [numpy.int8,
        numpy.int16,
        numpy.int32,
        numpy.int64,
        numpy.int64,
        numpy.timedelta64]],
      [numpy.unsignedinteger,
       [numpy.uint8,
        numpy.uint16,
        numpy.uint32,
        numpy.uint64,
        numpy.uint64]]]],
    [numpy.inexact,
     [[numpy.floating,
       [numpy.float16, numpy.float32, numpy.float64, numpy.float128]],
      [numpy.complexfloating,
       [numpy.complex64, numpy.complex128, numpy.complex256]]]]]],
  [numpy.flexible,
   [[numpy.character, [numpy.bytes_, numpy.str_]],
    [numpy.void, [numpy.record]]]],
  numpy.bool_,
  numpy.datetime64,
  numpy.object_]]
```

Pandas also adds two dtypes: categorical and datetime64[ns, tz]

Back to top

## Internet

The following code prints each line of the webpage minus most of the html markup.

```
tag = False
quote = False
import urllib.request
with urllib.request.urlopen("https://en.wikipedia.org/wiki/Hiroshi_Sakagami") as webpage:
       for line in webpage:
          out = ""
          line = line.strip()
          line = line.decode('utf-8')
          if line == "":
             print (line)
          elif line[0:7] == "<script":
             line = ""
          elif line[-7:] == "script>":
             line = ""
                   tag = True
                elif c == '>' and not quote:
                   tag = False
                elif (c == '"' or c == "'") and tag:
                   quote = not quote
                elif not tag:
                   out = out + c
             if out != "":
                print(out)

x=input("press enter when done")

```

Back to top

## Numpy Arrays

```
Help on built-in module array:

NAME
    array

DESCRIPTION
    This module defines an object type which can efficiently represent
    an array of basic values: characters, integers, floating point
    numbers.  Arrays are sequence types and behave very much like lists,
    except that the type of objects stored in them is constrained.

CLASSES
    builtins.object
        array

    ArrayType = class array(builtins.object)
ms of the buffer used to hold array's contents.
ad been read from a file using the fromfile() method).
ad been read from a file using the fromfile() method).

    class array(builtins.object)
ms of the buffer used to hold array's contents.
ad been read from a file using the fromfile() method).
ad been read from a file using the fromfile() method).

DATA
    typecodes = 'bBuhHiIlLqQfd'

FILE
    (built-in)

```

numpy arrays are fast because they work with continuous blocks of memory.

numpy.random.randint(low, high, size) # returns array of random integers that are less than "high"

import numpy as np

x = np.array([[1,2,3],[4,5,6]])

print (x)

```
[[1 2 3]
 [4 5 6]]

```

Math operations + and \* operate elementwise and create a new matrix.

To modify an existing array use += and \*=

For matrix multiplication use @

The identity matrix can be created easily with numpy.eye(n):

print (np.eye(4))

```
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

```

The inverse of matrix m is:

np.linalg.inv(m)

Back to top

## Scipy

tutorial

To import two subpackages you would use:

from scipy import subpackage1, subpackage2

import scipy as sp

from scipy import constants

print (constants.physical\_constants["alpha particle mass"]) # produces the following tuple:

```
(6.64465723e-27, 'kg', 8.2e-35)

```

Back to top

## Sympy

```
Help on package sympy:

NAME
    sympy

DESCRIPTION
    SymPy is a Python library for symbolic mathematics. It aims to become a
    full-featured computer algebra system (CAS) while keeping the code as simple
    as possible in order to be comprehensible and easily extensible.  SymPy is
    written entirely in Python. It depends on mpmath, and other external libraries
    may be optionally for things like plotting support.

    See the webpage for more information and documentation:

        http://sympy.org

PACKAGE CONTENTS
    abc
    assumptions (package)
    benchmarks (package)
    calculus (package)
    categories (package)
    codegen (package)
    combinatorics (package)
    concrete (package)
    conftest
    core (package)
    crypto (package)
    deprecated (package)
    diffgeom (package)
    external (package)
    functions (package)
    galgebra
    geometry (package)
    holonomic (package)
    integrals (package)
    interactive (package)
    liealgebras (package)
    logic (package)
    matrices (package)
    ntheory (package)
    parsing (package)
    physics (package)
    plotting (package)
    polys (package)
    printing (package)
    release
    sandbox (package)
    series (package)
    sets (package)
    simplify (package)
    solvers (package)
    stats (package)
    strategies (package)
    tensor (package)
    unify (package)
    utilities (package)
    vector (package)

SUBMODULES
    add
    array
    ask_generated
    assume
    basic
    bivariate
    boolalg
    cache
    class_registry
    combinatorial
    common
    compatibility
    conditionset
    containers
    contains
    continued_fraction
    coreerrors
    cse_main
    cse_opts
    curve
    decorator
    decorators
    dense
    deutils
    elementary
    ellipse
    entity
    enumerative
    epathtools
    evalf
    exceptions
    expr
    expr_with_intlimits
    expr_with_limits
    expressions
    exprtools
    factor_
    facts
    fancysets
    function
    generate
    gosper
    immutable
    index_methods
    indexed
    inequalities
    inference
    iterables
    line
    magic
    manualintegrate
    meijerint
    memoization
    misc
    mod
    mul
    multidimensional
    multinomial
    numbers
    ode
    operations
    parabola
    partitions_
    pde
    plane
    point
    polygon
    polysys
    power
    primetest
    products
    recurr
    relational
    residue_ntheory
    rules
    runtests
    singleton
    singularityfunctions
    sparse
    special
    summations
    symbol
    timeutils
    transforms
    traversaltools
    trigonometry
    util

DATA
    C = <sympy.deprecated.class_registry.ClassRegistry object>
    CC = CC
    Catalan = Catalan
    E = E
    EX = EX
    EulerGamma = EulerGamma
    FU = {'L': <function L>, 'TR0': <function TR0>, 'TR1': <function TR1>,...
    GoldenRatio = GoldenRatio
    I = I
    Id = Lambda(_x, _x)
    Q = <sympy.assumptions.ask.AssumptionKeys object>
    QQ = QQ
    RR = RR
    S = S
    SYMPY_DEBUG = False
    ZZ = ZZ
    false = False
    grevlex = ReversedGradedLexOrder()
    grlex = GradedLexOrder()
    igrevlex = InverseOrder()
    igrlex = InverseOrder()
    ilex = InverseOrder()
    lex = LexOrder()
    nan = nan
    oo = oo
    pi = pi
    plot_backends = {'default': <class 'sympy.plotting.plot.DefaultBackend...
    sieve = <Sieve with 6 primes sieved: 2, 3, 5, ... 11, 13>
    true = True
    zoo = zoo

VERSION
    1.1.1

```

Galgebra tutorial

Back to top

## Matplotlib

Tutorial The Matplotlib API

```
Help on package matplotlib:

NAME
    matplotlib - This is an object-oriented plotting library.

DESCRIPTION
    A procedural interface is provided by the companion pyplot module,
    which may be imported directly, e.g.::

        import matplotlib.pyplot as plt

    or using ipython::

        ipython

    at your terminal, followed by::

        In [1]: %matplotlib
        In [2]: import matplotlib.pyplot as plt

    at the ipython shell prompt.

    For the most part, direct use of the object-oriented library is
    encouraged when programming; pyplot is primarily for working
    interactively.  The
    exceptions are the pyplot commands :func:`~matplotlib.pyplot.figure`,
    :func:`~matplotlib.pyplot.subplot`,
    :func:`~matplotlib.pyplot.subplots`, and
    :func:`~pyplot.savefig`, which can greatly simplify scripting.

    Modules include:

        :mod:`matplotlib.axes`
            defines the :class:`~matplotlib.axes.Axes` class.  Most pylab
            commands are wrappers for :class:`~matplotlib.axes.Axes`
            methods.  The axes module is the highest level of OO access to
            the library.

        :mod:`matplotlib.figure`
            defines the :class:`~matplotlib.figure.Figure` class.

        :mod:`matplotlib.artist`
            defines the :class:`~matplotlib.artist.Artist` base class for
            all classes that draw things.

        :mod:`matplotlib.lines`
            defines the :class:`~matplotlib.lines.Line2D` class for
            drawing lines and markers

        :mod:`matplotlib.patches`
            defines classes for drawing polygons

        :mod:`matplotlib.text`
            defines the :class:`~matplotlib.text.Text`,
            :class:`~matplotlib.text.TextWithDash`, and
            :class:`~matplotlib.text.Annotate` classes

        :mod:`matplotlib.image`
            defines the :class:`~matplotlib.image.AxesImage` and
            :class:`~matplotlib.image.FigureImage` classes

        :mod:`matplotlib.collections`
            classes for efficient drawing of groups of lines or polygons

        :mod:`matplotlib.colors`
            classes for interpreting color specifications and for making
            colormaps

        :mod:`matplotlib.cm`
            colormaps and the :class:`~matplotlib.image.ScalarMappable`
            mixin class for providing color mapping functionality to other
            classes

        :mod:`matplotlib.ticker`
            classes for calculating tick mark locations and for formatting
            tick labels

        :mod:`matplotlib.backends`
            a subpackage with modules for various gui libraries and output
            formats

    The base matplotlib namespace includes:

        :data:`~matplotlib.rcParams`
            a global dictionary of default configuration settings.  It is
            initialized by code which may be overridden by a matplotlibrc
            file.

        :func:`~matplotlib.rc`
            a function for setting groups of rcParams values

        :func:`~matplotlib.use`
            a function for setting the matplotlib backend.  If used, this
            function must be called immediately after importing matplotlib
            for the first time.  In particular, it must be called
            **before** importing pylab (if pylab is imported).

    matplotlib was initially written by John D. Hunter (1968-2012) and is now
    developed and maintained by a host of others.

    Occasionally the internal documentation (python docstrings) will refer
    to MATLAB®, a registered trademark of The MathWorks, Inc.

PACKAGE CONTENTS
    _animation_data
    _cm
    _cm_listed
    _color_data
    _constrained_layout
    _contour
    _image
    _layoutbox
    _mathtext_data
    _path
    _png
    _pylab_helpers
    _qhull
    _tri
    _version
    _windowing
    afm
    animation
    artist
    axes (package)
    axis
    backend_bases
    backend_managers
    backend_tools
    backends (package)
    bezier
    blocking_input
    category
    cbook (package)
    cm
    collections
    colorbar
    colors
    compat (package)
    container
    contour
    dates
    docstring
    dviread
    figure
    font_manager
    fontconfig_pattern
    ft2font
    gridspec
    hatch
    image
    legend
    legend_handler
    lines
    markers
    mathtext
    mlab
    offsetbox
    patches
    path
    patheffects
    projections (package)
    pylab
    pyplot
    quiver
    rcsetup
    sankey
    scale
    sphinxext (package)
    spines
    stackplot
    streamplot
    style (package)
    table
    testing (package)
    texmanager
    text
    textpath
    ticker
    tight_bbox
    tight_layout
    transforms
    tri (package)
    ttconv
    type1font
    units
    widgets

SUBMODULES
    _backports
    subprocess

CLASSES
    builtins.dict(builtins.object)
        RcParams(collections.abc.MutableMapping, builtins.dict)
    builtins.object
        Verbose
    collections.abc.MutableMapping(collections.abc.Mapping)
        RcParams(collections.abc.MutableMapping, builtins.dict)

    class RcParams(collections.abc.MutableMapping, builtins.dict)

    class Verbose(builtins.object)

FUNCTIONS
    checkdep_dvipng()

    checkdep_ghostscript()

    checkdep_inkscape()

    checkdep_pdftops()

    checkdep_ps_distiller(s)

    checkdep_tex()
        .. deprecated:: 2.1
            The checkdep_tex function was deprecated in version 2.1.

        \

    checkdep_usetex(s)

    checkdep_xmllint()
        .. deprecated:: 2.1
            The checkdep_xmllint function was deprecated in version 2.1.

        \

    compare_versions(a, b)
        return True if a is greater than or equal to b

    get_backend()
        Return the name of the current backend.

    get_cachedir = wrapper(*args, **kwargs)
        Return the location of the cache directory.

        The procedure used to find the directory is the same as for
        _get_config_dir, except using `$XDG_CACHE_HOME`/`~/.cache` instead.

    get_configdir = wrapper(*args, **kwargs)
        Return the string representing the configuration directory.

        The directory is chosen as follows:

        1. If the MPLCONFIGDIR environment variable is supplied, choose that.

        2a. On Linux, follow the XDG specification and look first in
            `$XDG_CONFIG_HOME`, if defined, or `$HOME/.config`.

        2b. On other platforms, choose `$HOME/.matplotlib`.

        3. If the chosen directory exists and is writable, use that as the
           configuration directory.
        4. If possible, create a temporary directory, and use it as the
           configuration directory.
        5. A writable directory could not be found or created; return None.

    get_data_path = wrapper(*args, **kwargs)

    get_home = wrapper(*args, **kwargs)
        Find user's home directory if possible.
        Otherwise, returns None.

        :see:
            http://mail.python.org/pipermail/python-list/2005-February/325395.html

    get_py2exe_datafiles()

    interactive(b)
        Set interactive mode to boolean b.

        If b is True, then draw after every plotting command, e.g., after xlabel

    is_interactive()
        Return true if plot mode is interactive

    is_url(filename)
        Return True if string is an http, ftp, or file URL path.

    matplotlib_fname()
        Get the location of the config file.

        The file location is determined in the following order

        - `$PWD/matplotlibrc`

        - `$MATPLOTLIBRC` if it is a file (or a named pipe, which can be created
          e.g. by process substitution)

        - `$MATPLOTLIBRC/matplotlibrc`

        - `$MPLCONFIGDIR/matplotlibrc`

        - On Linux,

              - `$XDG_CONFIG_HOME/matplotlib/matplotlibrc` (if
                $XDG_CONFIG_HOME is defined)

              - or `$HOME/.config/matplotlib/matplotlibrc` (if
                $XDG_CONFIG_HOME is not defined)

        - On other platforms,

             - `$HOME/.matplotlib/matplotlibrc` if `$HOME` is defined.

        - Lastly, it looks in `$MATPLOTLIBDATA/matplotlibrc` for a
          system-defined copy.

    rc(group, **kwargs)
        Set the current rc params.  Group is the grouping for the rc, e.g.,
        for ``lines.linewidth`` the group is ``lines``, for
        ``axes.facecolor``, the group is ``axes``, and so on.  Group may
        also be a list or tuple of group names, e.g., (*xtick*, *ytick*).
        *kwargs* is a dictionary attribute name/value pairs, e.g.,::

          rc('lines', linewidth=2, color='r')

        sets the current rc params and is equivalent to::

          rcParams['lines.linewidth'] = 2
          rcParams['lines.color'] = 'r'

        The following aliases are available to save typing for interactive
        users:

        =====   =================
        Alias   Property
        =====   =================
        'lw'    'linewidth'
        'ls'    'linestyle'
        'c'     'color'
        'fc'    'facecolor'
        'ec'    'edgecolor'
        'mew'   'markeredgewidth'
        'aa'    'antialiased'
        =====   =================

        Thus you could abbreviate the above rc command as::

              rc('lines', lw=2, c='r')

        Note you can use python's kwargs dictionary facility to store
        dictionaries of default parameters.  e.g., you can customize the
        font rc as follows::

          font = {'family' : 'monospace',
                  'weight' : 'bold',
                  'size'   : 'larger'}

          rc('font', **font)  # pass in the font dict as kwargs

        This enables you to easily switch between several configurations.  Use
        ``matplotlib.style.use('default')`` or :func:`~matplotlib.rcdefaults` to
        restore the default rc params after changes.

    rc_context(rc=None, fname=None)
        Return a context manager for managing rc settings.

        This allows one to do::

            with mpl.rc_context(fname='screen.rc'):
                plt.plot(x, a)
                with mpl.rc_context(fname='print.rc'):
                    plt.plot(x, b)
                plt.plot(x, c)

        The 'a' vs 'x' and 'c' vs 'x' plots would have settings from
        'screen.rc', while the 'b' vs 'x' plot would have settings from
        'print.rc'.

        A dictionary can also be passed to the context manager::

            with mpl.rc_context(rc={'text.usetex': True}, fname='screen.rc'):
                plt.plot(x, a)

        The 'rc' dictionary takes precedence over the settings loaded from
        'fname'.  Passing a dictionary only is also valid. For example a
        common usage is::

            with mpl.rc_context(rc={'interactive': False}):
                fig, ax = plt.subplots()
                ax.plot(range(3), range(3))
                fig.savefig('A.png', format='png')
                plt.close(fig)

    rc_file(fname)
        Update rc params from file.

    rc_file_defaults()
        Restore the rc params from the original rc file loaded by Matplotlib.

    rc_params(fail_on_error=False)
        Return a :class:`matplotlib.RcParams` instance from the
        default matplotlib rc file.

    rc_params_from_file(fname, fail_on_error=False, use_default_template=True)
        Return :class:`matplotlib.RcParams` from the contents of the given file.

        Parameters

        fname : str
            Name of file parsed for matplotlib settings.
        fail_on_error : bool
            If True, raise an error when the parser fails to convert a parameter.
        use_default_template : bool
            If True, initialize with default parameters before updating with those
            in the given file. If False, the configuration class only contains the
            parameters specified in the file. (Useful for updating dicts.)

    rcdefaults()
        Restore the rc params from Matplotlib's internal defaults.

        See Also

        rc_file_defaults :
            Restore the rc params from the rc file originally loaded by Matplotlib.
        matplotlib.style.use :
            Use a specific style file.  Call ``style.use('default')`` to restore
            the default style.

    test(verbosity=None, coverage=False, switch_backend_warn=True, recursionlimit=0, **kwargs)
        run the matplotlib test suite

    tk_window_focus()
        Return true if focus maintenance under TkAgg on win32 is on.
        This currently works only for python.exe and IPython.exe.
        Both IDLE and Pythonwin.exe fail badly when tk_window_focus is on.

    use(arg, warn=True, force=False)
        Set the matplotlib backend to one of the known backends.

        The argument is case-insensitive. *warn* specifies whether a
        warning should be issued if a backend has already been set up.
        *force* is an **experimental** flag that tells matplotlib to
        attempt to initialize a new backend by reloading the backend
        module.

        .. note::

            This function must be called *before* importing pyplot for
            the first time; or, if you are not using pyplot, it must be called
            before importing matplotlib.backends.  If warn is True, a warning
            is issued if you try and call this after pylab or pyplot have been
            loaded.  In certain black magic use cases, e.g.
            :func:`pyplot.switch_backend`, we are doing the reloading necessary to
            make the backend switch work (in some cases, e.g., pure image
            backends) so one can set warn=False to suppress the warnings.

        To find out which backend is currently set, see
        :func:`matplotlib.get_backend`.

DATA
    __bibtex__ = '@Article{Hunter:2007,\n  Author    = {Hunter, J. ...ishe...
    __version__numpy__ = '1.7.1'
    __warningregistry__ = {'version': 9, ("matplotlib.verbose is deprecate...
    absolute_import = _Feature((2, 5, 0, 'alpha', 1), (3, 0, 0, 'alpha', 0...
    defaultParams = {'_internal.classic_mode': [False, <function validate_...
    default_test_modules = ['matplotlib.tests', 'matplotlib.sphinxext.test...
    division = _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192...
    print_function = _Feature((2, 6, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0)...
    rcParams = RcParams({'_internal.classic_mode': False,
         ...nor.widt...
    rcParamsDefault = RcParams({'_internal.classic_mode': False,
         ...n...
    rcParamsOrig = {'_internal.classic_mode': False, 'agg.path.chunksize':...
    verbose = <matplotlib.Verbose object>

VERSION
    2.2.2

```

Bizarrely, in matplotlib.pyplot states are preserved across function calls.

### Line or scatter plot

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,1,num=21, endpoint=True)

fig=plt.gcf()
fig.set\_size\_inches(14.4,9.6)
plt.title('Your title')
plt.gcf().set\_facecolor('green') # gcf = get current figure
plt.ylabel('y-axis label', color='blue')
plt.xlabel('x-axis label', color='red')
plt.axis([0, 1, 0, 1]) # [xmin, xmax, ymin, ymax]
plt.grid(which='major', axis='both', color='black', linestyle='-') # axis='x' or 'y'.
plt.minorticks\_on()
plt.grid(which='minor', axis='x', color='grey', linestyle='--')

ax=plt.gca()
ax.set\_facecolor('yellow')
ax.set\_xscale('linear')
ax.set\_yscale('linear')
plt.plot(x, x\*\*0.5, color='orange', linestyle='solid', marker='o', markerfacecolor='orange', markersize=8)
plt.plot(x,

x\*\*1,
linestyle='dashed',
marker='v',
markersize=8,
color='red',
alpha=1.0,
fillstyle='full',
markerfacecolor='red',
markeredgewidth=1,
markeredgecolor='#FFFFFF')
plt.plot(x,

x\*\*1.5,
linestyle='dashdot',
marker='^',
markersize=8,
color='purple',
alpha=1.0,
fillstyle='full',
markerfacecolor='purple',
markeredgewidth=1,
markeredgecolor='#FFFFFF')
plt.plot(x,

x\*\*2,
linestyle='dotted',
marker='s',
markersize=8,
color='blue',
alpha=1.0,
fillstyle='full',
markerfacecolor='blue',
markeredgewidth=1,
markeredgecolor='#FFFFFF')
plt.plot(x,

x\*\*2.5,
linestyle='dotted',
marker='D',
markersize=8,
color='green',
alpha=1.0,
fillstyle='full',
markerfacecolor='green',
markeredgewidth=1,
markeredgecolor='#FFFFFF')

plt.text(0.2,0.8,"Your text", fontsize=30, bbox=dict(facecolor='red', alpha=1)) # bbox is optional
plt.text(0.8,0.2,r'$\int\_1^2 {Latex}^y$', fontsize=24, bbox=dict(facecolor='red', alpha=1)) # Latex code between dollar signs
plt.annotate('annotation\npointing toward\nLatex text', xy=(0.8, 0.2), xytext=(0.1, 0.5), arrowprops=dict(facecolor='black', shrink=0.05))

plt.savefig("image\_name.png", facecolor='green', dpi=100) # must come before the show command. Savefig ignore Facecolor given above so it must be given again. (unless you want to switch to a different color)
plt.show()

If we had wanted a log log plot we would have used:

ax=plt.gca()

ax.set\_xscale('log')

ax.set\_yscale('log')

### Bar graph

Bar graph:

import numpy as np

import matplotlib.pyplot as plt

fig=plt.gcf()

fig.set\_size\_inches(14.4,1.3\*9.6)

data = [ 0, 5, 1, 16, 20, 2, 26, 26, 30, 32]

positions = np.arange(10)

width = 0.75

ax=plt.gca()

ax.set\_axisbelow(True)

ax.set\_xscale('linear')

ax.set\_yscale('linear')

plt.grid(which='major', axis='both', color='black', linestyle='-')

plt.minorticks\_on()

plt.grid(which='minor', axis='both', color='#CCCCCC', linestyle='--')

plt.axis([0, 10, 0, 35])

p1 = plt.bar(positions, data, width)

plt.ylabel('y-axis label')

plt.title('Title')

plt.savefig("File.name2.png", dpi=100)

plt.show()

See also: subplots

Back to top

## Jupyter

Tutorial

Back to top

## Intel® Distribution for Python

Math Kernel Library

Numba

https://developer.nvidia.com/how-to-cuda-python

Back to top

## External links

* https://snakify.org/en/ (for teachers)
* https://www.packtpub.com/tech/Python

## Copyright

 Are there copyright restrictions on the use of Python?: You can do anything you want with the source, as long as you leave the copyrights in and display those copyrights in any documentation about Python that you produce. If you honor the copyright rules, it’s OK to use Python for commercial use, to sell copies of Python in source or binary form (modified or unmodified), or to sell products that incorporate Python in some form. We would still like to know about all commercial use of Python, of course.
PSF LICENSE AGREEMENT FOR PYTHON 3.7.0

```
1. This LICENSE AGREEMENT is between the Python Software Foundation ("PSF"), and
   the Individual or Organization ("Licensee") accessing and otherwise using Python
   3.7.0 software in source or binary form and its associated documentation.

2. Subject to the terms and conditions of this License Agreement, PSF hereby
   grants Licensee a nonexclusive, royalty-free, world-wide license to reproduce,
   analyze, test, perform and/or display publicly, prepare derivative works,
   distribute, and otherwise use Python 3.7.0 alone or in any derivative
   version, provided, however, that PSF's License Agreement and PSF's notice of
   copyright, i.e., "Copyright © 2001-2018 Python Software Foundation; All Rights
   Reserved" are retained in Python 3.7.0 alone or in any derivative version
   prepared by Licensee.

3. In the event Licensee prepares a derivative work that is based on or
   incorporates Python 3.7.0 or any part thereof, and wants to make the
   derivative work available to others as provided herein, then Licensee hereby
   agrees to include in any such work a brief summary of the changes made to Python
   3.7.0.

4. PSF is making Python 3.7.0 available to Licensee on an "AS IS" basis.
   PSF MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  BY WAY OF
   EXAMPLE, BUT NOT LIMITATION, PSF MAKES NO AND DISCLAIMS ANY REPRESENTATION OR
   WARRANTY OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE
   USE OF PYTHON 3.7.0 WILL NOT INFRINGE ANY THIRD PARTY RIGHTS.

5. PSF SHALL NOT BE LIABLE TO LICENSEE OR ANY OTHER USERS OF PYTHON 3.7.0
   FOR ANY INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES OR LOSS AS A RESULT OF
   MODIFYING, DISTRIBUTING, OR OTHERWISE USING PYTHON 3.7.0, OR ANY DERIVATIVE
   THEREOF, EVEN IF ADVISED OF THE POSSIBILITY THEREOF.

6. This License Agreement will automatically terminate upon a material breach of
   its terms and conditions.

7. Nothing in this License Agreement shall be deemed to create any relationship
   of agency, partnership, or joint venture between PSF and Licensee.  This License
   Agreement does not grant permission to use PSF trademarks or trade name in a
   trademark sense to endorse or promote products or services of Licensee, or any
   third party.

8. By copying, installing or otherwise using Python 3.7.0, Licensee agrees
   to be bound by the terms and conditions of this License Agreement.

```

## References

1. ↑ https://stackoverflow.com/a/27232309/10366044