This document listed key language features for those who need to learn Python fast.
This document focuses more on the learning path and resources, rather than the content itself.
Slicing and Striding
sequence : A
step : Extract every
step elements of the
0 for the first element,
-1 for the counterpart inversely.
sequence[start] sequence[start:end] sequence[start:end:step]
Sequence Types — list, tuple, range
Named Tuple: Factory Function for Tuples with Named Fields
namedtuple : (typename, field_names, *, rename=False, defaults=None, module=None)
Item = namedtuple('typename',"field_name_x, field_name_y, ...") i = Item(field_name_x=..., field_name_y=..., ...)
Using Lists as Queues
It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one).
To implement a queue, use [
collections.deque](dfile:///Users/kamitora/Library/Application Support/Dash/DocSets/Python_3/Python 3.docset/Contents/Resources/Documents/doc/library/collections.html#collections.deque) which was designed to have fast appends and pops from both ends. For example:
from collections import deque queue = deque(["Eric", "John", "Michael"]) queue.append("Terry") # Terry arrives queue.append("Graham") # Graham arrives queue.popleft() # The first to arrive now leaves 'Eric' queue.popleft() # The second to arrive now leaves 'John' queue # Remaining queue in order of arrival deque(['Michael', 'Terry', 'Graham'])
A Snippet for Range + List :
for i in range(len(L)): L[i] = process(L[i])
Common Sequence Operations
Mutable Sequence Types
Functions & Methods
iter() : Return an [iterator](dfile:///Users/kamitora/Library/Application Support/Dash/DocSets/Python_3/Python 3.docset/Contents/Resources/Documents/doc/glossary.html#term-iterator) object.
Assignment statements in Python do not copy objects, they create bindings between a target and an object. For collections that are mutable or contain mutable items, a copy is sometimes needed so one can change one copy without changing the other. This module provides generic shallow and deep copy operations (explained below).
copy.copy(x) Return a shallow copy of x.
copy.deepcopy(x[, memo]) Return a deep copy of x.
exception copy.error Raised for module specific errors.
Docstring and Sphinx