Python Learning



Learning Python by myself.

Here’s some environment configuration:
Version: Python 3.6.3
Platform: macOS 10.12.6
Interpreter: terminal
Text editor: Sublime Text 3


  • Self interest
  • Courses Prerequisites: CS229, Deep Learning, etc
  • (Perhaps next semester I have the chance to teach freshman something about Python:D)



These materials should be enough. What I use is the official tutorial to get an detailed understanding of Python and Python’s most noteworthy features, and get a good idea of the language’s flavor and style.


I plan to get the hang of Python in my winter vacation.
Try to get more familiar with Python with practical applications.


Interactive Mode & Executable mode

When commands are read from a tty, the interpreter is said to be in interactive mode. To start interactive mode, type Python3 in terminal(the default Python version of macOS is Python2, so type Python would start Python2). To stop interactive mode, type exit() to quit.

If code is saved as file with .py, then type Python3 in terminal to compile and run the file.

On BSD’ish Unix systems, Python scripts can be made directly executable, like shell scripts, by putting the line #!/usr/bin/env python3.
The script can be given an executable mode, or permission, using the chmod +x command.
On Windows systems, there is no notion of an “executable mode”. The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it as a script. The extension can also be pyw, in that case, the console window that normally appears is suppressed.

Comment & Prompt

Comments in Python start with the hash character, #, and extend to the end of the physical line.
The # sign will only comment out a single line, if it’s necessary to add multi-line comments, just begin with # each line. (For multi-line comments, include the whole block in a set of triple quotation marks is okay in .py file, but not interactive mode.)

# This is a comment.
# Here's another

When commands are read from a tty, the interpreter is said to be in interactive mode. In this mode it prompts for the next command with the primary prompt, usually three greater-than signs (>>>); for continuation lines it prompts with the secondary prompt, by default three dots (...).
Generally the prompts are primary prompt. Secondary prompts are used in control flow like if, while, etc.
Primary prompt (after python3 command in my terminal):

$ python3
Python 3.6.3 (default, Oct 28 2017, 21:24:10)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Secondary prompt (example from tutorial):

>>> the_world_is_flat = True
>>> if the_world_is_flat:
... print("Be careful not to fall off!")
Be careful not to fall off!

Input and Output

Well, this part is not introduced in tutorial, but when dealing with online practice(like CodeStepByStep), it’s necessary to know how to input and output. So I’d include some information here.


It's advisable to add some prompts when asking for input, so we can add string parameters when calling `input` function. ```x = input("prompts")

The default type of x is string. If we input an integer and what to use x as an integer, use type conversion with int(). x = int(input("prompts"))

Just like MATLAB, we can output a variable’s value by typing the variable’s name, or use the print() function.
When concatenating strings in print(), we can use both , and +. When using ,, we don’t need to convert int/float to string, and every , is treated as a blank space; while we need to convert int/float to string using str() when using +, but + won’t add extra blank space.


>>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
We are the knights who say "Ni!"

The brackets and characters within them (called format fields) are replaced with the objects passed into the str.format() method.

Positional arguments
A number in the brackets can be used to refer to the position of the object passed into the str.format() method.

>>> print('{0} and {1}'.format('spam', 'eggs'))
spam and eggs
>>> print('{1} and {0}'.format('spam', 'eggs'))
eggs and spam

Keyword arguments
If keyword arguments are used in the str.format() method, their values are referred to by using the name of the argument.

>>> print('This {food} is {adjective}.'.format(
... food='spam', adjective='absolutely horrible'))
This spam is absolutely horrible.

Positional and keyword arguments can be arbitrarily combined.

Old string formatting
The % operator can also be used for string formatting.

>>> import math
>>> print('The value of PI is approximately %5.3f.' % math.pi)
The value of PI is approximately 3.142.

printf-style String Formatting

Coding Style

  • Use 4-space indentation, and no tabs.
  • Wrap lines so that they don’t exceed 79 characters.
  • 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 CamelCase for classes and lower_case_with_underscores for functions and methods. Always use self as the name for the first method argument.
  • Don’t use fancy encodings if your code is meant to be used in international environments. Python’s default, UTF-8, or even plain ASCII work best in any case.
  • Likewise, don’t use non-ASCII characters in identifiers if there is only the slightest chance people speaking a different language will read or maintain the code.


Python interpreter can act as a simple calculator. So just type math expressions will get the calculation result.

Data type


Complex numbers: use j or J suffix to indicate the imaginary part(e.g. 3+5j).

+: Addition. 2 + 2 = 4
-: Subtract. 3 - 1 = 2
*: Multiply. 2 * 2 = 4
**: Power. 2 ** 7 = 128
/: Division. Always returns a float. 10 / 3 = 3.3333333333333335
//: Floor division. Discard any fractional result and get an integer result.

_: Last printed expression(easier to continue calculations).


< less than, > greater than, == equal to, <= less than or equal to, >= greater than, != not equal to
in and not in check whether a value occurs (does not occur) in a sequence.
is and is not compare whether two objects are really the same object; this only matters for mutable objects like lists.

and, or, not
True, False.
Any non-zero integer value is true; zero is false. The condition may also be any sequence(string, list, etc): anything with a non-zero length is true, empty sequences are false.


Strings in Python can be enclosed in both single quotes and double quotes.

Escape character
Just like C, \ can be used to escape quotes in Python as well. \t,\n,etc.

Use raw strings by adding an r before the first quote can prevent \ from being treated as special characters.

>>> print(r'C:\some\name')

Strings can be concatenated with + operator, and repeated with *.
For string literals(i.e. the ones enclosed between quotes) next to each other are automatically concatenated.

Indices of strings can be non-negative(just like C array) and negative(start counting from the right).

| P | y | t | h | o | n |
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1

Like MATLAB, Python supports slicing(word[0:2]), which allows to obtain substring.
Note the start is always included, and the end always excluded.
Default: an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.

Attempting to use an index that is too large will result in an error. However, out of range slice indexes are handled gracefully when used for slicing.

Python strings are immutable, just as in Java. Thus, if it’s necessary to edit a string, just create a new one.

The built-in function len() returns the length of a string.
String Methods

Control Flow

if Statement

if ... :
elif ... :

There can be zero or more elif parts, and the else part is optional.
Note there’s no switch case in Python.

for Statement
Python’s for statement iterates over the items of any sequence(a list or a string), in the order that they appear in the sequence.

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

The range() Function
for i in range(5)
range() function may contain 1, 2 or 3 parameters.
range(term): Generate term values from 0 to (term - 1). Note that term should be positive integers or it will return an empty list.
range(begin, end): Generate values from begin to (end - 1).
range(begin, end, step): Specify a different increment(step), which can even be negative.

In many ways the object returned by range() behaves as if it is a list, but in fact it isn’t. It is an object which returns the successive items of the desired sequence when you iterate over it, but it doesn’t really make the list, thus saving space.
We say such an object is iterable, that is, suitable as a target for functions and constructs that expect something from which they can obtain successive items until the supply is exhausted.

break and continue Statements, and else Clauses on Loops

The break statement breaks out of the innermost enclosing for or while loop.
The continue statement continues with the next iteration of the loop.
These two statements are borrowed from C.

Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement.

>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print(n, 'equals', x, '*', n//x)
... break
... else:
... # loop fell through without finding a factor
... print(n, 'is a prime number')
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

pass Statements
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.

>>> while True:
... pass # Busy-wait for keyboard interrupt (⌃+C)

This is commonly used for creating minimal classes:

>>> class MyEmptyClass:
... pass

pass can be used as a place-holder for a function or conditional body when you are working on new code, allowing you to keep thinking at a more abstract level.

>>> def initlog(*args):
... pass # Remember to implement this!


The keyword def introduces a function definition. It must be followed by the function name and the parenthesized list of formal parameters. The statements that form the body of the function start at the next line, and must be indented.

The first statement of the function body can optionally be a string literal(in three single quotes ''' '''); this string literal is the function’s documentation string, or docstring. This line should begin with a capital letter and end with a period.

call by value
The actual parameters (arguments) to a function call are introduced in the local symbol table of the called function when it is called; thus, arguments are passed using call by value (where the value is always an object reference, not the value of the object). When a function calls another function, a new local symbol table is created for that call.
Actually, call by object reference would be a better description, since if a mutable object is passed, the caller will see any changes the callee makes to it (items inserted into a list).

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.

default argument values
When defining functions, it’s useful 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.
Remember that the default value is evaluated only once.

keyword argument & positional argument
From Glossary
A value passed to a function(or method) when calling the function. There are two kinds of argument:
· keyword argument. an argument preceded by an identifier(e.g. name=) in a function call or passed as a value in a dictionary preceded by **.
· positional argument. an argument that is not a keyword argument. Positional arguments can appear at the beginning of an argument list and/or be passed as elements of an iterable preceded by *.

When a final formal parameter of the form **name is present, it receives a dictionary containing all keyword arguments except for those corresponding to a formal parameter. This may be combined with a formal parameter of the form *name (described in the next subsection) which receives a tuple containing the positional arguments beyond the formal parameter list. (*name must occur before **name.)

Data Structures

Sequence Types - list, tuple, range
Set Types - set, frozenset
Mapping Types - dict


List can be written as a list of comma-separated values (items) between square brackets. (similar to Java’s ArrayList)
Lists might contain items of different types, but usually the items all have the same type. square = [1, 4, 9, 16, 25]

Index and slicing are similar to those of String.

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

+ operator: squares + [36, 49]
append method: squares.append(64)

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

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

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

Remove the first item from the list whose value is x. It is an error if there is no such item.

Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list.

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

Return the number of times x appears in the list.

Sort the items of the list in place.

Reverse the elements of the list in place.

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

List Comprehensions
List comprehensions provide a concise way to create lists.
A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for of if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.

>>> squares = []
>>> for x in range(10):
... squares.append(x**2)
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

It is equivalent to squares = [x**2 for x in range(10)]


A tuple consists of a number of values separated by commas.

Comparison with list

Type tuple list
Immutable immutable mutable
Element heterogeneous homogeneous
Access unpacking/indexing iterating

Packing and Unpacking
t = 12345, 54321, 'hello!' is an example of tuple packing

x, y, z = t is called sequence unpacking and works for any sequence on the right-hand side. Sequence unpacking requires that there are as many variables on the left side of the equals sign as there are elements in the sequence. Note that multiple assignment is really just a combination of tuple packing and sequence unpacking.


A set is an unordered collection with no duplicate elements.
Curly braces {} or the set() function can be used to create sets.
set() can be used to remove duplicated items in the list.


Dictionaries are index by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. Since lists are mutable, lists can’t be used as keys as well.

d = {key: value, ...}: a pair of braces creates an empty dictionary. Placing a comma-separated list of key:value pairs within the braces adds initial key:value pairs to the dictionary. Also can use dict() constructor.
list(d.keys()): return a list of all the keys used in the dictionary in arbitrary order.
sorted(d.keys()): return the sorted list of keys
in: check whether a single key is in the dictionary

When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the items() method.

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
... print(k, v)
gallahad the pure
robin the brave


A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended. Definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at the top level and in calculator mode). Within a module, the module’s name (as a string) is available as the value of the global variable __name__.



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

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

>>> with open('workfile') as f:
... read_data =

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

read & write
Reads some quantity of data and returns it as a string (in text mode) or bytes object(in binary mode). size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, will return an empty string('').

Reads a single line from the file; a newline character(\n) is left at the end of the string, and is only omitted on the last line of the file if the file doesn’t end in a newline. This makes the return value unambiguous: if f.readline() returns an empty string, the end of the file has been reached, while a blank line is represented by '\n', a string containing only a single newline.

Looping over the file object:

>>> for line in f:
... print(line, end='')

Read all the lines of a file in a list: list(f) or f.readlines().

f.write(string) writes the contents of string to the file, returning the number of characters written. Other types of objects need to be converted - either to a string (in text mode) or a bytes object (in binary mode) - before writing them.


Errors detected during execution are called exceptions.
Exceptions come in different types, and the type is printed as part of the message. The string printed as the exception type is the name of the built-in exception that occurred.(true for all built-in exceptions, but need not be true for user-defined exceptions)

All built-in exceptions are listed here

Handling Exceptions

The try statement works as follows.

  • First, the try clause is executed.
  • If no exceptions occurs, the except clause is skipped and execution of the try statement is finished.
  • If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement.
  • If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handlers is found, it is an unhandled exception and execution stops with a message.

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

Some tips

The keyword argument end can be used to avoid the newline after the output, or end the output with a different string.

>>> a, b = 0, 1
>>> while b < 1000:
... print(b, end=',')
... a, b = b, a+b

in keyword tests whether or not a sequence contains a certain value.

The square brackets in the method signature denote that the parameter is optional, not that you should type square brackets at that position. This is frequent in the Python Library Reference.

Looping Techniques

When looping through dictionaries, the key and corresponding value can be retrieved at the same time using the items() method.

>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'}
>>> for k, v in knights.items():
... print(k, v)

When looping through a sequence, the position index and corresponding value can be retrieved at the same time using the enumerate() function.

>>> for i, v in enumerate(['tic', 'tac', 'toe']):
... print(i, v)
0 tic
1 tac
2 toe

To loop over two or more sequence at the same time, the entries can be paired with the zip() function.

>>> questions = ['name', 'quest', 'favorite color']
>>> answers = ['lancelot', 'the holy grail', 'blue']
>>> for q, a in zip(questions, answers):
... print('What is your {0}? It is {1}.'.format(q, a))
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.

To loop over a sequence in reverse, first specify the sequence in a forward direction and then call the reversed() function.

>>> for i in reversed(range(1, 10, 2)):
... print(i)

To loop over a sequence in sorted order, use the sorted() function which returns a new sorted list while leaving the source unaltered.

>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> for f in sorted(set(basket)):
... print(f)


Eval: Built-in Function
The return value is the result of the evaluated expression. Syntax errors are reported as exceptions.
This function can also be used to execute arbitrary code objects (such as those created by compile()). In this case pass a code object instead of a string.

Split with multiple delimiters

Something to be detailed

Here stores something that is introduced in tutorial briefly, waiting to be discussed in detail in the future.

Arbitrary Argument Lists and Unpacking Argument Lists
Introduced in Section 4.7,

A method is a function that ‘belongs’ to an object and is named obj.methodname, where obj is some object (this may be an expression), and methodname is the name of a method that is defined by the object’s type. Different types define different methods. Methods of different types may have the same name without causing ambiguity.

Lambda Expressions
Small anonymous functions can be created with the lambda keyword. This function returns the sum of its two arguments: lambda a, b: a+b. 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:

>>> def make_incrementor(n):
... return lambda x: x + n
>>> f = make_incrementor(42)
>>> f(0)
>>> f(1)

list.sort(key=None, reverse=False)
The arguments can be used for sort customization, see sorted().

The del statement
Remove an item from a list given its index instead of its value. The del statement can also be used to remove slices from a list or clear the entire list.

>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a

del can also be used to delete entire variables.
del can delete a key:value pair in dict, the parameter is just dict’s key.