1, python 中的算术符号:
addition +
subtraction -
multiplication *
division /
exponentiation
∗
∗
**
∗∗ (注意乘方不是^)
modulo %
integer division // (无论正数、负数,都向下取整)
2, 赋值操作
=,-=,+=等等(直接在变量后++在python中不可用,在C中和perl中都可以)
3, 对标准类型进行分类
以访问模型分类:
分类 | python类型 |
---|---|
直接访问 | 数字 |
顺序访问 | 字符串,列表,元祖 |
映射访问 | 字典 |
以标准类型分类:
数据类型 | 存储模型 | 更新模型 | 访问模型 |
---|---|---|---|
数字 | 标量 | 不可更改 | 直接访问 |
字符串 | 标量 | 不可更改 | 顺序访问 |
列表 | 容器 | 可更改 | 顺序访问 |
元祖 | 容器 | 不可更改 | 顺序访问 |
字典 | 容器 | 可更改 | 映射访问 |
集合访问也没有顺序,分为可变集和不可变集。可变集合是不可哈希的,不能作为字典的键和其它集合的元素;不可变集合有固定的哈希值,可以作为字典的键和其它集合的元素。
4, membership操作
字符串和列表都可以用in或者not in来判断有没有某个元素存在
5, 应用在list中的函数
min(), max(), sorted(),如果是对只有数字的列表进行比较,则根据数值的大小;如果是对只有字符串的列表进行比较,则根据字符的排序顺序(ASCII码)决定大小;如果既包含数字,又包含字母,则报错。
列表是允许不同类型的元素混合在一起的,如int型和str型混合;使用join函数时需将元素都转化成str。
6, 对字典进行赋值
dict_test = {‘a’:1, ‘b’:2}
dict_test[‘a’]输出为1
7, identity操作
is, is not
==, !=
is比较两个变量是否有相同的内存地址,
=
=
==
==则判断两个变量是否相同(比如调用__eq__等方法)。比较是否为None对象时,要用is
8,python将bool值判为0的情况
1,constants defined to be false: None and False
2,zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
3,empty sequences and collections: '"", (), [], {}, set(), range(0)
9, iterable对象
an object that can return one of its elements at a time
10,pop()函数,取出list的最后一个元素,跟append()功能相反
11,用yield可以在函数中创建迭代器,可以进行i.e. for … in …之类的循环操作(普通的函数进行这种操作就会报错)。创建的迭代器是一次性的,用一次就失效了,看下面的代码:
def my_range(x):
i = 0
while i < x:
yield i
i += 1
y = my_range(5)
for x in y:
print(x)
for x in y:
print(x)
# 整个代码的输出为
0
1
2
3
4
# 第二次迭代y根本没有起到作用
12,方括号生成列表解析式,圆括号生成发生器:
sq_list = [x**2 for x in range(10)] # this produces a list of squares
sq_iterator = (x**2 for x in range(10)) # this produces an iterator of squares
13,exceptions for python
ValueError: An object of the correct type but inappropriate value is passed as input to a built-in operation or function.
AssertionError: An assert statement fails.
IndexError: A sequence subscript is out of range.
KeyError: A key can’t be found in a dictionary.
TypeError: An object of an unsupported type is passed as input to an operation or function.
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StopAsyncIteration
+-- ArithmeticError
| +-- FloatingPointError
| +-- OverflowError
| +-- ZeroDivisionError
+-- AssertionError
+-- AttributeError
+-- BufferError
+-- EOFError
+-- ImportError
| +-- ModuleNotFoundError
+-- LookupError
| +-- IndexError
| +-- KeyError
+-- MemoryError
+-- NameError
| +-- UnboundLocalError
+-- OSError
| +-- BlockingIOError
| +-- ChildProcessError
| +-- ConnectionError
| | +-- BrokenPipeError
| | +-- ConnectionAbortedError
| | +-- ConnectionRefusedError
| | +-- ConnectionResetError
| +-- FileExistsError
| +-- FileNotFoundError
| +-- InterruptedError
| +-- IsADirectoryError
| +-- NotADirectoryError
| +-- PermissionError
| +-- ProcessLookupError
| +-- TimeoutError
+-- ReferenceError
+-- RuntimeError
| +-- NotImplementedError
| +-- RecursionError
+-- SyntaxError
| +-- IndentationError
| +-- TabError
+-- SystemError
+-- TypeError
+-- ValueError
| +-- UnicodeError
| +-- UnicodeDecodeError
| +-- UnicodeEncodeError
| +-- UnicodeTranslateError
+-- Warning
+-- DeprecationWarning
+-- PendingDeprecationWarning
+-- RuntimeWarning
+-- SyntaxWarning
+-- UserWarning
+-- FutureWarning
+-- ImportWarning
+-- UnicodeWarning
+-- BytesWarning
+-- ResourceWarning
14, with关键字在文件访问完自动关闭
This with keyword allows you to open a file, do operations on it, and automatically close it after the indented code is executed.
15, python中__name__ == 'main’的作用是方便其他脚本调用这个module时,不用执行该module中的命令。
16,Useful Third-Party Packages
IPython - A better interactive Python interpreter
requests - Provides easy to use methods to make web requests. Useful for accessing web APIs.
Flask - a lightweight framework for making web applications and APIs.
Django - A more featureful framework for making web applications. Django is particularly good for designing complex, content heavy, web applications.
Beautiful Soup - Used to parse HTML and extract information from it. Great for web scraping.
pytest - extends Python's builtin assertions and unittest module.
PyYAML - For reading and writing YAML files.
NumPy - The fundamental package for scientific computing with Python. It contains among other things a powerful N-dimensional array object and useful linear algebra capabilities.
pandas - A library containing high-performance, data structures and data analysis tools. In particular, pandas provides dataframes!
matplotlib - a 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments.
ggplot - Another 2D plotting library, based on R's ggplot2 library.
Pillow - The Python Imaging Library adds image processing capabilities to your Python interpreter.
pyglet - A cross-platform application framework intended for game development.
Pygame - A set of Python modules designed for writing games.
pytz - World Timezone Definitions for Python