Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. The language provides constructs intended to enable clear programs on both a small and large scale.

The language Python is named after the BBC show Monty Python’s Flying Circus and has nothing to do with reptiles.

Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.

Python was conceived in the late 1980s, and its implementation was started in December 1989 by Guido van Rossum at CWI in the Netherlands as a successor to the ABC language (itself inspired by SETL) capable of exception handling and interfacing with the Amoeba operating system. Van Rossum is Python's principal author, and his continuing central role in deciding the direction of Python is reflected in the title given to him by the Python community, benevolent dictator for life.

## Features （语言特征）¶

The core philosophy of the language is summarized by the document "PEP 20 (The Zen of Python)", which includes aphorisms such as:

• Beautiful is better than ugly
• Explicit is better than implicit
• Simple is better than complex
• Complex is better than complicated

Python enables programs to be written compactly and readably. Programs written in Python are typically much shorter than equivalent C, C++, or Java programs, for several reasons:

• the high-level data types allow you to express complex operations in a single statement;
• statement grouping is done by indentation instead of beginning and ending brackets;
• no variable or argument declarations are necessary.

Python is extensible: if you know how to program in C it is easy to add a new built-in function or module to the interpreter, either to perform critical operations at maximum speed, or to link Python programs to libraries that may only be available in binary form (such as a vendor-specific graphics library). Once you are really hooked, you can link the Python interpreter into an application written in C and use it as an extension or command language for that application.

# Installing Python （安装Python）¶

On Linux machine, Python is usually installed by default. To install Python on other systems, check out the Python Setup and Usage section in Python help documentation.

If you are using Windows, Anaconda is a good choice.

# Using Python （使用Python）¶

The Python interpreter is usually installed as /usr/bin/python3 on those machines where it is available.

• To start a Python interpreter, type the command in your terminal: python3.

• To terminate the Python interpreter, type an end-of-file character (Control-D on Unix, Control-Z on Windows) at the primary prompt. If that doesn’t work, you can exit the interpreter by typing the following command: quit().

## Executing Python scripts （如何执行Python脚本）¶

• Write down your Python script and name it as hello.py with .py extension

• Your script contents look like this

  #!/usr/bin/python3
print('Hello World')

  chmod +x hello.py
• Run the script in your terminal

  ./hello.py

Note The line #!/usr/bin/python3 should appear at the very beginning of your file. Alternatively, you can let your environment variable to specify which Python to use as

#!/usr/bin/env python3

It is possible to use encodings different than ASCII in Python source files. The best way to do it is to put one more special comment line right after the #! line to define the source file encoding:

# -*- coding: utf-8 -*-

Your hello.py will be able handle more complicated texts. By using UTF-8, characters of most languages in the world can be used simultaneously in string literals and comments. Using non-ASCII characters in identifiers is not supported. To display all these characters properly, your editor must recognize that the file is UTF-8, and it must use a font that supports all the characters in the file.

## Using IPython shell （Python交互式编程环境）¶

IPython is a command shell for interactive computing in multiple programming languages, originally developed for the Python programming language, that offers introspection, rich media, shell syntax, tab completion, and history. IPython provides the following features:

• Interactive shells (terminal and Qt-based).
• A browser-based notebook with support for code, text, mathematical expressions, inline plots and other media.
• Support for interactive data visualization and use of GUI toolkits.
• Flexible, embeddable interpreters to load into one's own projects.
• Tools for parallel computing.

To start iPython interactive environment, type ipython in your terminal.

# Getting help （获取帮助）¶

• Help is available in Python sessions using help(function) .

• Some functions (and modules) have very long help files. When using IPython, these can be paged using the command ?function or function? so that the text can be scrolled using page up and down and q to quit. ??function or function?? can be used to type the entire function including both the docstring and the code.

# Libraries （Python库）¶

Python has a large standard library, commonly cited as one of Python's greatest strengths, providing tools suited to many tasks. This is deliberate and has been described as a "batteries included" Python philosophy. For Internet-facing applications, a large number of standard formats and protocols are supported. Modules for creating graphical user interfaces, connecting to relational databases, pseudo random number generators, arithmetic with arbitrary precision decimals, manipulating regular expressions, and doing unit testing are also included.

Some parts of the standard library are covered by specifications, but the majority of the modules are not. They are specified by their code, internal documentation, and test suite (if supplied). However, because most of the standard library is cross-platform Python code, there are only a few modules that must be altered or completely rewritten by alternative implementations.

The standard library is not essential to run Python or embed Python within an application. Blender 2.49, for instance, omits most of the standard library.

As of August 2015, the Python Package Index, the official repository of third-party software for Python, contains more than 65,000 packages offering a wide range of functionality, including:

• graphical user interfaces, web frameworks, multimedia, databases, networking and communications
• test frameworks, automation and web scraping, documentation tools, system administration
• scientific computing, text processing, image processing

# Text Editors/IDEs （Python编辑器）¶

To edit Python code, you just need a handy text editor. There are many available, check out the following pages

# Using Python as a Calculator （Python基本使用）¶

In [1]:
3 + 2 +4

Out[1]:
9
In [4]:
5 + 4*3

Out[4]:
17

## Division （除法）¶

The return type of a division (/) operation depends on its operands. If both operands are of type int, floor division is performed and an int is returned. If either operand is a float, classic division is performed and a float is returned. The // operator is also provided for doing floor division no matter what the operands are.

In [5]:
8/5 #In Python 2, int / int -> int, you will get 1 instead of 1.6. But in Python 3 will get precise division

Out[5]:
1.6
In [6]:
8/5.0 # int / float -> float

Out[6]:
1.6
In [5]:
8//5.0 # explicit floor division discards the fractional part

Out[5]:
1.0

The remainder can be calculated with the % operator

In [8]:
17 % 3

Out[8]:
2

Note To always get precise division in Python 2, use

from __future__ import division

## Powers （幂）¶

With Python, it is possible to use the ** operator to calculate powers. Note Caret (^) in Python behaves differently. It invokes the exclusive OR of the object: a logical operation that outputs true only when both inputs differ (one is true, the other is false).

In [3]:
5**2

Out[3]:
25
In [4]:
2^3

Out[4]:
1

The equal sign (=) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt:

In [10]:
a = 3
b = 5
c = a + b
c

Out[10]:
8

In interactive mode, the last printed expression is assigned to the variable _. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations.

In [10]:
100/3.0

Out[10]:
33.333333333333336
In [11]:
_

Out[11]:
33.333333333333336

## Strings （字符串）¶

Python can also manipulate strings, which can be expressed in several ways. They can be enclosed in single quotes ('...') or double quotes ("...") with the same result.

In [11]:
LastName = "Kang"

In [12]:
FirstName = "Yanfei"


\ can be used to escape quotes:

In [14]:
print("Hello \n World!")

Hello
World!


If you don't want characters prefaced by \ to be interpreted as special characters, you can use raw strings by adding an r before the first quote:

In [15]:
print(r"Hello \n World!")

Hello \n World!

In [6]:
print("Hello \\n World")

Hello \\n World


Strings can be concatenated (glued together) with the + operator, and repeated with *:

In [7]:
"I " + 'L' + 'o'*5  + 've' + ' you'

Out[7]:
'I Looooove you'
In [8]:
n = 10
"G"+"o"*n+"gle"

Out[8]:
'Goooooooooogle'

The built-in function len() returns the length of a string:

In [1]:
len("Yanfei Kang")

Out[1]:
11

Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated. This feature is particularly useful when you want to break long strings:

In [2]:
"Yanfei" "Kang"

Out[2]:
'YanfeiKang'
In [3]:
print("Hi, my name is Yanfei Kang."
" And I am from Beijing.")

Hi, my name is Yanfei Kang. And I am from Beijing.


Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one:

In [4]:
Name = "Yanfei Kang"
Name[0]

Out[4]:
'Y'
In [5]:
Name[-1]

Out[5]:
'g'
In [6]:
Name[-2]

Out[6]:
'n'

In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain a substring:

In [9]:
Name[0:6]

Out[9]:
'Yanfei'
In [10]:
Name[0:6]
Name[6:11]

Out[10]:
' Kang'
###### Remember that [0:6] mathematically means [0, 6).¶
In [11]:
Name[:6]

Out[11]:
'Yanfei'
In [12]:
Name[6:]

Out[12]:
' Kang'
In [14]:
Name[-4:]

Out[14]:
'Kang'

However, out of range slice indexes are handled gracefully when used for slicing:

In [15]:
Name[6:100]

Out[15]:
' Kang'
In [16]:
Name2 = "Yanfei.Kang"
Name2.lower().title().split(".")

Out[16]:
['Yanfei', 'Kang']

## Lists （列表）¶

Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.

In [20]:
values = [1,5,7,9,12]

In [20]:
len(values)

Out[20]:
5
In [30]:
values[0]

Out[30]:
1
In [31]:
values[-2:]

Out[31]:
[9, 12]

Lists also supports operations like concatenation:

In [35]:
values + ["Hello","World"]*3

Out[35]:
[1,
2,
1000,
4,
67,
22,
9999,
'Hello',
'World',
'Hello',
'World',
'Hello',
'World']

Lists are a mutable type, i.e. it is possible to change their content:

In [22]:
values = [1,2,3,4,67,22]
values

Out[22]:
[1, 2, 3, 4, 67, 22]
In [23]:
values[2] = 1000
values

Out[23]:
[1, 2, 1000, 4, 67, 22]

You can also add new items at the end of the list, by using the append() method

In [24]:
values.append(9999)
values

Out[24]:
[1, 2, 1000, 4, 67, 22, 9999]

Assignment to slices is also possible, and this can even change the size of the list or clear it entirely:

In [25]:
values[2:4] = [2,3,4]
values

Out[25]:
[1, 2, 2, 3, 4, 67, 22]
In [27]:
values[:] = []
values
len(values)

Out[27]:
0

## Building Functions （常用内置函数）¶

The Python interpreter has a number of functions built into it that are always available. They are listed here in alphabetical order. Use e.g. help(abs) to see the function help.

abs()   divmod()    input()     open()
staticmethod()      all()   enumerate()
int()   ord()   str()        any()
eval()  isinstance()    pow()   sum()
basestring()    execfile()  issubclass()
print()     super() bin()   file()
iter()  property()  tuple() bool()
filter()    len()   range()     type()
bytearray()     float()     list()
raw_input()     unichr() callable()
format()    locals()    reduce()    unicode()
vars() classmethod()    getattr()   map()
repr()  xrange() cmp()  globals()
max()   reversed()  zip() compile()
hasattr()   memoryview()    round()
__import__() complex()  hash()  min()
set()   apply() delattr()   help()
next()  setattr()   buffer()
dict()  hex()   object()    slice()
coerce() dir()  id()    oct()
sorted()    intern()

## Import modules （导入模块）¶

To import a module (like math) that is not in Python's default module, use

In [38]:
import math


Then you can use all the mathematical functions inside math module as:

In [39]:
math.exp(0)

Out[39]:
1.0

Alternatively, you can do the following changes

In [40]:
import math as mt
mt.exp(1)

Out[40]:
2.718281828459045

If you just want to import one or two functions from a module

In [41]:
from math import exp
exp(3)

Out[41]:
20.085536923187668
In [42]:
from math import exp as myexp

myexp(1)

Out[42]:
2.718281828459045

## Control Flow Tools （控制语句）¶

### The if statements （条件表达）¶

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

In [3]:
x = -5

if x < 0:
x = 0
print('Negative changed to zero')
elif x == 0:
print('Zero')
elif x == 1:
print('Single')
else:
print('More')

Negative changed to zero


Note

• the comma/colon sign(:) should be right after if, elif and else statement.
• the indentation is very important. The first non-blank line after the first line of the string determines the amount of indentation for the entire documentation string.

### The for Statements （for循环）¶

In [2]:
words = ['cat', 'window', 'defenestrate']

for j in words:
print(j,len(j))

print("I am done!")

cat 3
window 6
defenestrate 12
I am done!


## Defining Functions （函数定义）¶

We can create a function that writes the Fibonacci series to an arbitrary boundary.

The first line should always be a short, concise summary of the object's purpose. For brevity, it should not explicitly state the object's name or type, since these are available by other means (except if the name happens to be a verb describing a function's operation). This line should begin with a capital letter and end with a period.

If there are more lines in the documentation string, the second line should be blank, visually separating the summary from the rest of the description. The following lines should be one or more paragraphs describing the object's calling conventions, its side effects, etc.

The first statement of the function body can optionally be a string literal; this string literal is the function’s documentation string, or docstring. There are tools which use docstrings to automatically produce online or printed documentation, or to let the user interactively browse through code; it's good practice to include docstrings in code that you write, so make a habit of it.

In [31]:
def fib(n = 200):    # write Fibonacci series up to n
"""
Print a Fibonacci series up to n.

Usage

fib(n)
""" # the function help
a, b = 0, 1
while a < n:
print(a, end="  ")
a, b = b, a+b

In [32]:
help(fib)

Help on function fib in module __main__:

fib(n=200)
Print a Fibonacci series up to n.

Usage

fib(n)


In [33]:
fib(200)

0  1  1  2  3  5  8  13  21  34  55  89  144
In [34]:
fib()

0  1  1  2  3  5  8  13  21  34  55  89  144

EXERCISE

Write a function to find the roots of $ax^2+bx+c=0$.

### Function with default values （函数参数默认值）¶

The most useful form is to specify a default value for one or more arguments. This creates a function that can be called with fewer arguments than it is defined to allow. For example:

In [56]:
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
while True:
ok = input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
if ok in ('n', 'no', 'nop', 'nope'):
return False
retries = retries - 1
if retries < 0:
raise IOError('refusenik user')
print(complaint)


Note

raw_input() only works with Python 2. To make it it work with Python 3, change raw_input() into input.

The return statement returns with a value from a function. return without an expression argument returns None. Falling off the end of a function also returns None.

In [58]:
ask_ok("Do you really want to go?")

Do you really want to go?hao
Do you really want to go?keyi
Do you really want to go?yes

Out[58]:
True

### Anonymous functions （匿名函数）¶

Small anonymous functions can be created with the lambda keyword. Lambda functions can be used wherever function objects are required. They are syntactically restricted to a single expression. Semantically, they are just syntactic sugar for a normal function definition. Like nested function definitions, lambda functions can reference variables from the containing scope. The example uses a lambda expression to return a function

In [1]:
def make_incrementor(n):
return lambda x: x + n
f = make_incrementor(42)

In [2]:
f(0)

Out[2]:
42
In [4]:
f(1)

Out[4]:
43
In [8]:
f(2)

Out[8]:
44

# Classes （Python的类）¶

Compared with other programming languages, Python's class mechanism adds classes with a minimum of new syntax and semantics. Python classes provide all the standard features of Object Oriented Programming:

• the class inheritance mechanism allows multiple base classes,
• a derived class can override any methods of its base class or classes, and
• a method can call the method of a base class with the same name.

Class definitions, like function definitions (def statements) must be executed before they have any effect. (You could conceivably place a class definition in a branch of an if statement, or inside a function.)

In practice, the statements inside a class definition will usually be function definitions, but other statements are allowed, and sometimes useful. The function definitions inside a class normally have a peculiar form of argument list, dictated by the calling conventions for methods.

When a class definition is entered, a new namespace is created, and used as the local scope — thus, all assignments to local variables go into this new namespace. In particular, function definitions bind the name of the new function here.

Often, the first argument of a method is called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python. Note, however, that by not following the convention your code may be less readable to other Python programmers, and it is also conceivable that a class browser program might be written that relies upon such a convention.

When a class definition is left normally (via the end), a class object is created. This is basically a wrapper around the contents of the namespace created by the class definition; The original local scope (the one in effect just before the class definition was entered) is reinstated, and the class object is bound here to the class name given in the class definition header. Objects can contain arbitrary amounts and kinds of data. As is true for modules, classes partake of the dynamic nature of Python: they are created at runtime, and can be modified further after creation.

## Class Objects （Python类的对象）¶

Class objects support two kinds of operations: attribute references and instantiation.

In [6]:
class MyClass:
"""A simple example class"""
i = 12345
def f(self):
return 'hello world'

In [7]:
MyClass()

Out[7]:
<__main__.MyClass at 0x7f6628388208>

Attribute references use the standard syntax used for all attribute references in Python: obj.name. Valid attribute names are all the names that were in the class’s namespace when the class object was created.

In [3]:
MyClass.i

Out[3]:
12345
In [4]:
MyClass.f

Out[4]:
<function __main__.MyClass.f>

MyClass.i and MyClass.f are valid attribute references, returning an integer and a function object, respectively. Class attributes can also be assigned to, so you can change the value of MyClass.i by assignment.

In [5]:
MyClass.i = 3
MyClass.i

Out[5]:
3

The instantiation operation ("calling" a class object) creates an empty object. Many classes like to create objects with instances customized to a specific initial state. Therefore a class may define a special method named __init__(), like this:

In [6]:
def __init__(self):
self.data = []


When a class defines an __init__() method, class instantiation automatically invokes __init__() for the newly-created class instance. So in this example, a new, initialized instance can be obtained by calling

In [7]:
x = MyClass()
print(x)

<__main__.MyClass object at 0x7f50f839deb8>


Of course, the __init__() method may have arguments for greater flexibility. In that case, arguments given to the class instantiation operator are passed on to __init__().

In [8]:
class Complex:
def __init__(self, realpart, imagpart):
self.r = realpart
self.i = imagpart

x = Complex(3.0, -4.5)
x.r, x.i

Out[8]:
(3.0, -4.5)

# Coding Style （Python编程规范）¶

• Use 4-space indentation, and no tabs. 4 spaces are a good compromise between small indentation (allows greater nesting depth) and large indentation (easier to read). Tabs introduce confusion, and are best left out.

• Wrap lines so that they don't exceed 79 characters. This helps users with small displays and makes it possible to have several code files side-by-side on larger displays.

• Use blank lines to separate functions and classes, and larger blocks of code inside functions.

• When possible, put comments on a line of their own.

• Use docstrings.

• Use spaces around operators and after commas, but not directly inside bracketing constructs: a = f(1, 2) + g(3, 4).

• Name your classes and functions consistently; the convention is to use CamelCase for classes and lower_case_with_underscores for functions and methods. Always use self as the name for the first method argument (see A First Look at Classes for more on classes and methods).

• Don’t use fancy encodings if your code is meant to be used in international environments. Plain ASCII works best in any case.