AI programming with python-Intro to python

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值