Python Wiki
Register
Advertisement

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.

Comments[]

# comments follow a pound sign

Help[]

help("help")
Help on _Helper in module _sitebuiltins object:

class _Helper(builtins.object)
 |  Define the builtin 'help'.
 |
 |  This is a wrapper around pydoc.help that provides a helpful message
 |  when 'help' is typed at the Python interactive prompt.
 |
 |  Calling help() at the Python prompt starts an interactive help session.
 |  Calling help(thing) prints help for the python object 'thing'.
 |
 |  Methods defined here:
 |
 |  __call__(self, *args, **kwds)
 |      Call self as a function.
 |
 |  __repr__(self)
 |      Return repr(self).
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)

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
Immutable Mutable
Tuple
Frozen set
 
 
Boolean
Integer
Float
String
List
Set
Dictionary
Numpy arrays


x = 9+1j Complex number
name = "john" String
age = 36 Integer
age_string = str(age) Integer turned into a string
length_of_age_string = len(age_string)
age_string[start:end] Start character is included. End character is not included. The first character is character #0. See Slicing.
todays_date = "1 jan 2018"
it_is_january = "jan" in todays_date Boolean. it_is_january = true
it_is_not_january = (it_is_january == false) it_is_not_january = false. Note the double equals signs

Back to top

Dictionaries[]

capital = {
"NY": "New York",
"CA": "Sacramento",
"IL": "Springfield"

}

print (capital)
{'NY': 'New York', 'CA': 'Sacramento', 'IL': 'Springfield'}
print (capital['CA'])
Sacramento
del capital['IL'] Dictionaries use hash tables to look up the value for each key therefore the keys must be immutable.

You can however simply delete a key and its value:

print (capital)
{'NY': 'New York', 'CA': 'Sacramento'}
capital['Illinois'] = 'Springfield' and then add another to replace it:
print (capital)
{'NY': 'New York', 'CA': 'Sacramento', 'Illinois': 'Springfield'}

Ordered lists[]

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

OrderedList = ['a', 2, 'third']
y= [OrderedList]
z = y + y
print (OrderedList)
['a', 2, 'third']
print (OrderedList[2])
third
print (OrderedList+OrderedList)
['a', 2, 'third','a', 2, 'third']
print (y)
[['a', 2, 'third']]
print (y+OrderedList)
[['a', 2, 'third'], 'a', 2, 'third']
print (y+[OrderedList])
[['a', 2, 'third'], ['a', 2, 'third']]
print (z)
[['a', 2, 'third'], ['a', 2, 'third']]
print (z[0])
['a', 2, 'third']
print (z[0][1])
2

import numpy as np
a=np.zeros(5) # array of 5 floats
b=np.ndarray.tolist(a) # list of 5 floats

produces a list of floats
print (b)
[0.0, 0.0, 0.0, 0.0, 0.0]

Back to top

Input and output[]

help("print")
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("input")
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
help("for")

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
those made in the suite of the for-loop:

   for i in range(10):
       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
help("while")
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
help("if")
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
help("def")
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_list_starargs ::= "*" [parameter] ("," defparameter)* ["," ["**" parameter [","]]]
                               | "**" 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
function, e.g.:

   def whats_on_the_telly(penguin=None):
       if penguin is None:
           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[]

help("import")
The "import" statement
**********************

   import_stmt     ::= "import" module ["as" name] ( "," module ["as" name] )*
                   | "from" relative_module "import" identifier ["as" name]
                   ( "," identifier ["as" name] )*
                   | "from" relative_module "import" "(" identifier ["as" name]
                   ( "," identifier ["as" name] )* [","] ")"
                   | "from" module "import" "*"
   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])*
                        | "from" "__future__" "import" "(" 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

help("print")
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").

class Person:

def __init__(self, name, birthday):
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[]

help("with")
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
multiple "with" statements were nested:

   with A() as a, B() as b:
       suite

is equivalent to

   with A() as a:
       with B() as b:
           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("pandas")
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.

import numpy as np

import scipy as sp
import pandas as pd

Data = [['john', 22, 'Ca'], ['mary', 45, 'Ne'], ['Joe', 34, 'Ny']] Starting with a list of data
print (Data)
[['john', 22, 'Ca'], ['mary', 45, 'Ne'], ['Joe', 34, 'Ny']]
df = pd.DataFrame( Data, index=[1,2,3], columns=['name', 'age', 'home'] ) We can easily create a dataframe from the list (the list must be 2 dimensional even if there is only one row)
print (df)
   name  age home
1  john   22   Ca
2  mary   45   Ne
3   Joe   34   Ny
df=df.drop([2]) We can then drop a row:
print (df)
   name  age home
1  john   22   Ca
3   Joe   34   Ny
Data2 = ['Bob', 65, 'Ar']

df2 = pd.DataFrame( [Data2], index=[4], columns=['name', 'age', 'home'] )

We can create a second dataframe. Since Data2 was 1 dimensional we used [Data2] to make it 2 dimensional.
print (df2)
  name  age home
4  Bob   65   Ar
df=df.append(df2) And append it to the first:
print (df)
   name  age home
1  john   22   Ca
3   Joe   34   Ny
4   Bob   65   Ar
df.columns = ['name', 'age', 'state'] We can change the names of the columns:
print (df)
   name  age state
1  john   22    Ca
3   Joe   34    Ny
4   Bob   65    Ar
df['married'] = ['yes', 'no', 'yes'] We can add one column:
print (df)
   name  age state married
1  john   22    Ca     yes
3   Joe   34    Ny      no
4   Bob   65    Ar     yes
We can view a single row.
print (df[2:3])
  name  age state married
4  Bob   65    Ar     yes
We can view only rows 0, 1, 2 and columns 0, 1:
print (df.iloc[0:3,0:2])
   name  age
1  john   22
3   Joe   34
4   Bob   65
We can sort by one column.
print (df.sort_values(by='name'))
   name  age state married
4   Bob   65    Ar     yes
3   Joe   34    Ny      no
1  john   22    Ca     yes
df.to_hdf('file_name.h5','name_of_our_spreadsheet') We can write the dataframe to a hdf5 file:
df = pd.read_hdf('file_name.h5','name_of_our_spreadsheet') And read it back from the file:
with pd.HDFStore("file_name.h5") as store:
l = store.keys()
print (l)
Each file can contain more than one spreadsheet. We can get a list of all the spreadsheets in the file with:

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][1]

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 = ""
          else:
             for c in line:
                if c == '<' and not quote:
                   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("array")
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)
     |  array(typecode [, initializer]) -> array
     |
     |  Return a new array whose items are restricted by typecode, and
     |  initialized from the optional initializer value, which must be a list,
     |  string or iterable over elements of the appropriate type.
     |
     |  Arrays represent basic values and behave very much like lists, except
     |  the type of objects stored in them is constrained. The type is specified
     |  at object creation time by using a type code, which is a single character.
     |  The following type codes are defined:
     |
     |      Type code   C Type             Minimum size in bytes
     |      'b'         signed integer     1
     |      'B'         unsigned integer   1
     |      'u'         Unicode character  2 (see note)
     |      'h'         signed integer     2
     |      'H'         unsigned integer   2
     |      'i'         signed integer     2
     |      'I'         unsigned integer   2
     |      'l'         signed integer     4
     |      'L'         unsigned integer   4
     |      'q'         signed integer     8 (see note)
     |      'Q'         unsigned integer   8 (see note)
     |      'f'         floating point     4
     |      'd'         floating point     8
     |
     |  NOTE: The 'u' typecode corresponds to Python's unicode character. On
     |  narrow builds this is 2-bytes on wide builds this is 4-bytes.
     |
     |  NOTE: The 'q' and 'Q' type codes are only available if the platform
     |  C compiler used to build Python supports 'long long', or, on Windows,
     |  '__int64'.
     |
     |  Methods:
     |
     |  append() -- append a new item to the end of the array
     |  buffer_info() -- return information giving the current memory info
     |  byteswap() -- byteswap all the items of the array
     |  count() -- return number of occurrences of an object
     |  extend() -- extend array by appending multiple elements from an iterable
     |  fromfile() -- read items from a file object
     |  fromlist() -- append items from the list
     |  frombytes() -- append items from the string
     |  index() -- return index of first occurrence of an object
     |  insert() -- insert a new item into the array at a provided position
     |  pop() -- remove and return item (default last)
     |  remove() -- remove first occurrence of an object
     |  reverse() -- reverse the order of the items in the array
     |  tofile() -- write all items to a file object
     |  tolist() -- return the array converted to an ordinary list
     |  tobytes() -- return the array converted to a string
     |
     |  Attributes:
     |
     |  typecode -- the typecode character used to create the array
     |  itemsize -- the length in bytes of one array item
     |
     |  Methods defined here:
     |
     |  __add__(self, value, /)
     |      Return self+value.
     |
     |  __contains__(self, key, /)
     |      Return key in self.
     |
     |  __copy__(self, /)
     |      Return a copy of the array.
     |
     |  __deepcopy__(self, unused, /)
     |      Return a copy of the array.
     |
     |  __delitem__(self, key, /)
     |      Delete self[key].
     |
     |  __eq__(self, value, /)
     |      Return self==value.
     |
     |  __ge__(self, value, /)
     |      Return self>=value.
     |
     |  __getattribute__(self, name, /)
     |      Return getattr(self, name).
     |
     |  __getitem__(self, key, /)
     |      Return self[key].
     |
     |  __gt__(self, value, /)
     |      Return self>value.
     |
     |  __iadd__(self, value, /)
     |      Implement self+=value.
     |
     |  __imul__(self, value, /)
     |      Implement self*=value.
     |
     |  __iter__(self, /)
     |      Implement iter(self).
     |
     |  __le__(self, value, /)
     |      Return self<=value.
     |
     |  __len__(self, /)
     |      Return len(self).
     |
     |  __lt__(self, value, /)
     |      Return self<value.
     |
     |  __mul__(self, value, /)
     |      Return self*value.n
     |
     |  __ne__(self, value, /)
     |      Return self!=value.
     |
     |  __new__(*args, **kwargs) from builtins.type
     |      Create and return a new object.  See help(type) for accurate signature.
     |
     |  __reduce_ex__(self, value, /)
     |      Return state information for pickling.
     |
     |  __repr__(self, /)
     |      Return repr(self).
     |
     |  __rmul__(self, value, /)
     |      Return self*value.
     |
     |  __setitem__(self, key, value, /)
     |      Set self[key] to value.
     |
     |  __sizeof__(self, /)
     |      Size of the array in memory, in bytes.
     |
     |  append(self, v, /)
     |      Append new value v to the end of the array.
     |
     |  buffer_info(self, /)
     |      Return a tuple (address, length) giving the current memory address and the length in ite
ms of the buffer used to hold array's contents.
     |
     |      The length should be multiplied by the itemsize attribute to calculate
     |      the buffer length in bytes.
     |
     |  byteswap(self, /)
     |      Byteswap all items of the array.
     |
     |      If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is
     |      raised.
     |
     |  count(self, v, /)
     |      Return number of occurrences of v in the array.
     |
     |  extend(self, bb, /)
     |      Append items to the end of the array.
     |
     |  frombytes(self, buffer, /)
     |      Appends items from the string, interpreting it as an array of machine values, as if it h
ad been read from a file using the fromfile() method).
     |
     |  fromfile(self, f, n, /)
     |      Read n objects from the file object f and append them to the end of the array.
     |
     |  fromlist(self, list, /)
     |      Append items to array from list.
     |
     |  fromstring(self, buffer, /)
     |      Appends items from the string, interpreting it as an array of machine values, as if it h
ad been read from a file using the fromfile() method).
     |
     |      This method is deprecated. Use frombytes instead.
     |
     |  fromunicode(self, ustr, /)
     |      Extends this array with data from the unicode string ustr.
     |
     |      The array must be a unicode type array; otherwise a ValueError is raised.
     |      Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of
     |      some other type.
     |
     |  index(self, v, /)
     |      Return index of first occurrence of v in the array.
     |
     |  insert(self, i, v, /)
     |      Insert a new item v into the array before position i.
     |
     |  pop(self, i=-1, /)
     |      Return the i-th element and delete it from the array.
     |
     |      i defaults to -1.
     |
     |  remove(self, v, /)
     |      Remove the first occurrence of v in the array.
     |
     |  reverse(self, /)
     |      Reverse the order of the items in the array.
     |
     |  tobytes(self, /)
     |      Convert the array to an array of machine values and return the bytes representation.
     |
     |  tofile(self, f, /)
     |      Write all items (as machine values) to the file object f.
     |
     |  tolist(self, /)
     |      Convert array to an ordinary list with the same items.
     |
     |  tostring(self, /)
     |      Convert the array to an array of machine values and return the bytes representation.
     |
     |      This method is deprecated. Use tobytes instead.
     |
     |  tounicode(self, /)
     |      Extends this array with data from the unicode string ustr.
     |
     |      Convert the array to a unicode string.  The array must be a unicode type array;
     |      otherwise a ValueError is raised.  Use array.tobytes().decode() to obtain a
     |      unicode string from an array of some other type.
     |
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |
     |  itemsize
     |      the size, in bytes, of one array item
     |
     |  typecode
     |      the typecode character used to create the array
     |
     |  ----------------------------------------------------------------------
     |  Data and other attributes defined here:
     |
     |  __hash__ = None

    class array(builtins.object)
     |  array(typecode [, initializer]) -> array
     |
     |  Return a new array whose items are restricted by typecode, and
     |  initialized from the optional initializer value, which must be a list,
     |  string or iterable over elements of the appropriate type.
     |
     |  Arrays represent basic values and behave very much like lists, except
     |  the type of objects stored in them is constrained. The type is specified
     |  at object creation time by using a type code, which is a single character.
     |  The following type codes are defined:
     |
     |      Type code   C Type             Minimum size in bytes
     |      'b'         signed integer     1
     |      'B'         unsigned integer   1
     |      'u'         Unicode character  2 (see note)
     |      'h'         signed integer     2
     |      'H'         unsigned integer   2
     |      'i'         signed integer     2
     |      'I'         unsigned integer   2
     |      'l'         signed integer     4
     |      'L'         unsigned integer   4
     |      'q'         signed integer     8 (see note)
     |      'Q'         unsigned integer   8 (see note)
     |      'f'         floating point     4
     |      'd'         floating point     8
     |
     |  NOTE: The 'u' typecode corresponds to Python's unicode character. On
     |  narrow builds this is 2-bytes on wide builds this is 4-bytes.
     |
     |  NOTE: The 'q' and 'Q' type codes are only available if the platform
     |  C compiler used to build Python supports 'long long', or, on Windows,
     |  '__int64'.
     |
     |  Methods:
     |
     |  append() -- append a new item to the end of the array
     |  buffer_info() -- return information giving the current memory info
     |  byteswap() -- byteswap all the items of the array
     |  count() -- return number of occurrences of an object
     |  extend() -- extend array by appending multiple elements from an iterable
     |  fromfile() -- read items from a file object
     |  fromlist() -- append items from the list
     |  frombytes() -- append items from the string
     |  index() -- return index of first occurrence of an object
     |  insert() -- insert a new item into the array at a provided position
     |  pop() -- remove and return item (default last)
     |  remove() -- remove first occurrence of an object
     |  reverse() -- reverse the order of the items in the array
     |  tofile() -- write all items to a file object
     |  tolist() -- return the array converted to an ordinary list
     |  tobytes() -- return the array converted to a string
     |
     |  Attributes:
     |
     |  typecode -- the typecode character used to create the array
     |  itemsize -- the length in bytes of one array item
     |
     |  Methods defined here:
     |
     |  __add__(self, value, /)
     |      Return self+value.
     |
     |  __contains__(self, key, /)
     |      Return key in self.
     |
     |  __copy__(self, /)
     |      Return a copy of the array.
     |
     |  __deepcopy__(self, unused, /)
     |      Return a copy of the array.
     |
     |  __delitem__(self, key, /)
     |      Delete self[key].
     |
     |  __eq__(self, value, /)
     |      Return self==value.
     |
     |  __ge__(self, value, /)
     |      Return self>=value.
     |
     |  __getattribute__(self, name, /)
     |      Return getattr(self, name).
     |
     |  __getitem__(self, key, /)
     |      Return self[key].
     |
     |  __gt__(self, value, /)
     |      Return self>value.
     |
     |  __iadd__(self, value, /)
     |      Implement self+=value.
     |
     |  __imul__(self, value, /)
     |      Implement self*=value.
     |
     |  __iter__(self, /)
     |      Implement iter(self).
     |
     |  __le__(self, value, /)
     |      Return self<=value.
     |
     |  __len__(self, /)
     |      Return len(self).
     |
     |  __lt__(self, value, /)
     |      Return self<value.
     |
     |  __mul__(self, value, /)
     |      Return self*value.n
     |
     |  __ne__(self, value, /)
     |      Return self!=value.
     |
     |  __new__(*args, **kwargs) from builtins.type
     |      Create and return a new object.  See help(type) for accurate signature.
     |
     |  __reduce_ex__(self, value, /)
     |      Return state information for pickling.
     |
     |  __repr__(self, /)
     |      Return repr(self).
     |
     |  __rmul__(self, value, /)
     |      Return self*value.
     |
     |  __setitem__(self, key, value, /)
     |      Set self[key] to value.
     |
     |  __sizeof__(self, /)
     |      Size of the array in memory, in bytes.
     |
     |  append(self, v, /)
     |      Append new value v to the end of the array.
     |
     |  buffer_info(self, /)
     |      Return a tuple (address, length) giving the current memory address and the length in ite
ms of the buffer used to hold array's contents.
     |
     |      The length should be multiplied by the itemsize attribute to calculate
     |      the buffer length in bytes.
     |
     |  byteswap(self, /)
     |      Byteswap all items of the array.
     |
     |      If the items in the array are not 1, 2, 4, or 8 bytes in size, RuntimeError is
     |      raised.
     |
     |  count(self, v, /)
     |      Return number of occurrences of v in the array.
     |
     |  extend(self, bb, /)
     |      Append items to the end of the array.
     |
     |  frombytes(self, buffer, /)
     |      Appends items from the string, interpreting it as an array of machine values, as if it h
ad been read from a file using the fromfile() method).
     |
     |  fromfile(self, f, n, /)
     |      Read n objects from the file object f and append them to the end of the array.
     |
     |  fromlist(self, list, /)
     |      Append items to array from list.
     |
     |  fromstring(self, buffer, /)
     |      Appends items from the string, interpreting it as an array of machine values, as if it h
ad been read from a file using the fromfile() method).
     |
     |      This method is deprecated. Use frombytes instead.
     |
     |  fromunicode(self, ustr, /)
     |      Extends this array with data from the unicode string ustr.
     |
     |      The array must be a unicode type array; otherwise a ValueError is raised.
     |      Use array.frombytes(ustr.encode(...)) to append Unicode data to an array of
     |      some other type.
     |
     |  index(self, v, /)
     |      Return index of first occurrence of v in the array.
     |
     |  insert(self, i, v, /)
     |      Insert a new item v into the array before position i.
     |
     |  pop(self, i=-1, /)
     |      Return the i-th element and delete it from the array.
     |
     |      i defaults to -1.
     |
     |  remove(self, v, /)
     |      Remove the first occurrence of v in the array.
     |
     |  reverse(self, /)
     |      Reverse the order of the items in the array.
     |
     |  tobytes(self, /)
     |      Convert the array to an array of machine values and return the bytes representation.
     |
     |  tofile(self, f, /)
     |      Write all items (as machine values) to the file object f.
     |
     |  tolist(self, /)
     |      Convert array to an ordinary list with the same items.
     |
     |  tostring(self, /)
     |      Convert the array to an array of machine values and return the bytes representation.
     |
     |      This method is deprecated. Use tobytes instead.
     |
     |  tounicode(self, /)
     |      Extends this array with data from the unicode string ustr.
     |
     |      Convert the array to a unicode string.  The array must be a unicode type array;
     |      otherwise a ValueError is raised.  Use array.tobytes().decode() to obtain a
     |      unicode string from an array of some other type.
     |
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |
     |  itemsize
     |      the size, in bytes, of one array item
     |
     |  typecode
     |      the typecode character used to create the array
     |
     |  ----------------------------------------------------------------------
     |  Data and other attributes defined here:
     |
     |  __hash__ = None

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("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("matplotlib")
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)
     |  A dictionary object including validation
     |
     |  validating functions are defined and associated with rc parameters in
     |  :mod:`matplotlib.rcsetup`
     |
     |  Method resolution order:
     |      RcParams
     |      collections.abc.MutableMapping
     |      collections.abc.Mapping
     |      collections.abc.Collection
     |      collections.abc.Sized
     |      collections.abc.Iterable
     |      collections.abc.Container
     |      builtins.dict
     |      builtins.object
     |
     |  Methods defined here:
     |
     |  __getitem__(self, key)
     |      x.__getitem__(y) <==> x[y]
     |
     |  __init__(self, *args, **kwargs)
     |      Initialize self.  See help(type(self)) for accurate signature.
     |
     |  __iter__(self)
     |      Yield sorted list of keys.
     |
     |  __repr__(self)
     |      Return repr(self).
     |
     |  __setitem__(self, key, val)
     |      Set self[key] to value.
     |
     |  __str__(self)
     |      Return str(self).
     |
     |  find_all(self, pattern)
     |      Return the subset of this RcParams dictionary whose keys match,
     |      using :func:`re.search`, the given ``pattern``.
     |
     |      .. note::
     |
     |          Changes to the returned dictionary are *not* propagated to
     |          the parent RcParams dictionary.
     |
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |
     |  __dict__
     |      dictionary for instance variables (if defined)
     |
     |  __weakref__
     |      list of weak references to the object (if defined)
     |
     |  ----------------------------------------------------------------------
     |  Data and other attributes defined here:
     |
     |  __abstractmethods__ = frozenset({'__delitem__', '__len__'})
     |
     |  msg_backend_obsolete = 'The {} rcParam was deprecated in version 2.2. ...
     |
     |  msg_depr = '%s is deprecated and replaced with %s; please use the latt...
     |
     |  msg_depr_ignore = '%s is deprecated and ignored. Use %s instead.'
     |
     |  msg_depr_set = '%s is deprecated. Please remove it from your matplotli...
     |
     |  msg_obsolete = '%s is obsolete. Please remove it from your matplotlibr...
     |
     |  validate = {'_internal.classic_mode': <function validate_bool>, 'agg.p...
     |
     |  ----------------------------------------------------------------------
     |  Methods inherited from collections.abc.MutableMapping:
     |
     |  __delitem__(self, key)
     |
     |  clear(self)
     |      D.clear() -> None.  Remove all items from D.
     |
     |  pop(self, key, default=<object object at 0x0000004EB9BBC050>)
     |      D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
     |      If key is not found, d is returned if given, otherwise KeyError is raised.
     |
     |  popitem(self)
     |      D.popitem() -> (k, v), remove and return some (key, value) pair
     |      as a 2-tuple; but raise KeyError if D is empty.
     |
     |  setdefault(self, key, default=None)
     |      D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
     |
     |  update(*args, **kwds)
     |      D.update([E, ]**F) -> None.  Update D from mapping/iterable E and F.
     |      If E present and has a .keys() method, does:     for k in E: D[k] = E[k]
     |      If E present and lacks .keys() method, does:     for (k, v) in E: D[k] = v
     |      In either case, this is followed by: for k, v in F.items(): D[k] = v
     |
     |  ----------------------------------------------------------------------
     |  Methods inherited from collections.abc.Mapping:
     |
     |  __contains__(self, key)
     |
     |  __eq__(self, other)
     |      Return self==value.
     |
     |  get(self, key, default=None)
     |      D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.
     |
     |  items(self)
     |      D.items() -> a set-like object providing a view on D's items
     |
     |  keys(self)
     |      D.keys() -> a set-like object providing a view on D's keys
     |
     |  values(self)
     |      D.values() -> an object providing a view on D's values
     |
     |  ----------------------------------------------------------------------
     |  Data and other attributes inherited from collections.abc.Mapping:
     |
     |  __hash__ = None
     |
     |  __reversed__ = None
     |
     |  ----------------------------------------------------------------------
     |  Class methods inherited from collections.abc.Collection:
     |
     |  __subclasshook__(C) from abc.ABCMeta
     |      Abstract classes can override this to customize issubclass().
     |
     |      This is invoked early on by abc.ABCMeta.__subclasscheck__().
     |      It should return True, False or NotImplemented.  If it returns
     |      NotImplemented, the normal algorithm is used.  Otherwise, it
     |      overrides the normal algorithm (and the outcome is cached).
     |
     |  ----------------------------------------------------------------------
     |  Methods inherited from collections.abc.Sized:
     |
     |  __len__(self)
     |
     |  ----------------------------------------------------------------------
     |  Methods inherited from builtins.dict:
     |
     |  __ge__(self, value, /)
     |      Return self>=value.
     |
     |  __getattribute__(self, name, /)
     |      Return getattr(self, name).
     |
     |  __gt__(self, value, /)
     |      Return self>value.
     |
     |  __le__(self, value, /)
     |      Return self<=value.
     |
     |  __lt__(self, value, /)
     |      Return self<value.
     |
     |  __ne__(self, value, /)
     |      Return self!=value.
     |
     |  __new__(*args, **kwargs) from builtins.type
     |      Create and return a new object.  See help(type) for accurate signature.
     |
     |  __sizeof__(...)
     |      D.__sizeof__() -> size of D in memory, in bytes
     |
     |  copy(...)
     |      D.copy() -> a shallow copy of D
     |
     |  fromkeys(iterable, value=None, /) from abc.ABCMeta
     |      Returns a new dict with keys from iterable and values equal to value.

    class Verbose(builtins.object)
     |  A class to handle reporting.  Set the fileo attribute to any file
     |  instance to handle the output.  Default is sys.stdout
     |
     |  Methods defined here:
     |
     |  __init__(self)
     |      .. deprecated:: 2.2
     |          matplotlib.verbose is deprecated;
     |      Command line argument --verbose-LEVEL is deprecated.
     |      This functionality is now provided by the standard
     |      python logging library.  To get more (or less) logging output:
     |          import logging
     |          logger = logging.getLogger('matplotlib')
     |          logger.set_level(logging.INFO)
     |
     |      \
     |
     |  ge(self, level)
     |      .. deprecated:: 2.2
     |          matplotlib.verbose is deprecated;
     |      Command line argument --verbose-LEVEL is deprecated.
     |      This functionality is now provided by the standard
     |      python logging library.  To get more (or less) logging output:
     |          import logging
     |          logger = logging.getLogger('matplotlib')
     |          logger.set_level(logging.INFO)
     |
     |      return true if self.level is >= level
     |
     |  report(self, s, level='helpful')
     |      .. deprecated:: 2.2
     |          matplotlib.verbose is deprecated;
     |      Command line argument --verbose-LEVEL is deprecated.
     |      This functionality is now provided by the standard
     |      python logging library.  To get more (or less) logging output:
     |          import logging
     |          logger = logging.getLogger('matplotlib')
     |          logger.set_level(logging.INFO)
     |
     |      print message s to self.fileo if self.level>=level.  Return
     |      value indicates whether a message was issued
     |
     |  set_fileo(self, fname)
     |      .. deprecated:: 2.2
     |          matplotlib.verbose is deprecated;
     |      Command line argument --verbose-LEVEL is deprecated.
     |      This functionality is now provided by the standard
     |      python logging library.  To get more (or less) logging output:
     |          import logging
     |          logger = logging.getLogger('matplotlib')
     |          logger.set_level(logging.INFO)
     |
     |      \
     |
     |  set_level(self, level)
     |      .. deprecated:: 2.2
     |          matplotlib.verbose is deprecated;
     |      Command line argument --verbose-LEVEL is deprecated.
     |      This functionality is now provided by the standard
     |      python logging library.  To get more (or less) logging output:
     |          import logging
     |          logger = logging.getLogger('matplotlib')
     |          logger.set_level(logging.INFO)
     |
     |      set the verbosity to one of the Verbose.levels strings
     |
     |  wrap(self, fmt, func, level='helpful', always=True)
     |      .. deprecated:: 2.2
     |          matplotlib.verbose is deprecated;
     |      Command line argument --verbose-LEVEL is deprecated.
     |      This functionality is now provided by the standard
     |      python logging library.  To get more (or less) logging output:
     |          import logging
     |          logger = logging.getLogger('matplotlib')
     |          logger.set_level(logging.INFO)
     |
     |      return a callable function that wraps func and reports it
     |      output through the verbose handler if current verbosity level
     |      is higher than level
     |
     |      if always is True, the report will occur on every function
     |      call; otherwise only on the first time the function is called
     |
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |
     |  __dict__
     |      dictionary for instance variables (if defined)
     |
     |  __weakref__
     |      list of weak references to the object (if defined)
     |
     |  ----------------------------------------------------------------------
     |  Data and other attributes defined here:
     |
     |  levels = ('silent', 'helpful', 'debug', 'debug-annoying')
     |
     |  vald = {'debug': 2, 'debug-annoying': 3, 'helpful': 1, 'silent': 0}

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
    URL_REGEX = re.compile('http://|https://|ftp://|file://|file:\\\\')
    __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[]

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

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[]

Copyright[]

"Copyright © 2001-2018 Python Software Foundation; All Rights Reserved"
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.
Advertisement