读书笔记:LearningPython第五版 (第四章 Python对象类型简介)

Chap4 Introducing Python Object Types

Python中一切皆对象(object)。

4.1 The Python Conceptual Hierarchy

python程序可以分解为module,statement,expression,object:

  1. Programs are composed of modules.
  2. Modules contain statements.
  3. Statements contain expressions.
  4. Expressions create and process objects.

4.2 Why Use Built-in Types?

built-in types是语言的一部分,性能很好

4.3 Python’s Core Data Types

对象类型例子/字面值
Numbers1234, 3.1415, 3+4j, 0b111, Decimal(), Fraction()
Strings‘spam’, “Bob’s”, b’a\x01c’, u’sp\xc4m’
Lists[1, [2, ‘three’], 4.5], list(range(10))
Dictionaries{‘food’: ‘spam’, ‘taste’: ‘yum’}, dict(hours=10)
Tuples(1, ‘spam’, 4, ‘U’), tuple(‘spam’), namedtuple
Filesopen(‘eggs.txt’), open(r’C:\ham.bin’, ‘wb’)
Setsset(‘abc’), {‘a’, ‘b’, ‘c’}
Other core typesBooleans, types, None
Program unit typesFunctions, modules, classes (Part IV, Part V, Part VI)
Implementation-related typesCompiled code, stack tracebacks (Part IV, Part VII)

4.4 Number简介

  • interger
  • float-point
  • decimals
  • complex number
  • rationals

python2.7和 python3.1 以前, 浮点数的打印会有些Bug:

>>> 3.1415 * 2 # repr: as code (Pythons < 2.7 and 3.1)
6.2830000000000004
>>> print(3.1415 * 2) # str: user-friendly
6.283

上面那种是 全精度输出,下面那种是 用户友好方式输出上面是 repr的结果,下面是 str的结果。python后来的版本就修改成了输出上面那个,但是底层结果是没变的,比如在python3.7中输入:

>>> 3.1415 * 2 # repr: as code (Pythons >= 2.7 and 3.1)
6.283
>>> 3.1415 * 2 == 6.2830000000000004
True

4.5 String 简介

4.5.1 sequence操作

  • 索引
  • 切片: 切片的开头默认为0,结尾默认为 总长度
  • 拼接
  • 重复

4.5.2 不可变

python中的每个对象都可以归类为 mutable或者immutable, 而每个string操作都是返回一个新的string,因为string是immutable

  • 可变: string, number, tuple
  • 不可变: list, dict, set
# bytearray只能操作 ASCII
>>> B = bytearray(b'spam') # A bytes/list hybrid (ahead)  
>>> B.extend(b'eggs') # 'b' needed in 3.X, not 2.X
>>> B # B[i] = ord(c) works here too
bytearray(b'spameggs')
>>> B.decode() # Translate to normal string
'spameggs' 

4.5.3 Type-Specific Methods

>>> S.find('pa') # Find the offset of a substring in S
>>> S.replace('pa', 'XYZ') # Replace occurrences of a string in S with another
>>> line.split(',') # Split on a delimiter into a list of substrings
>>> S.upper() # Upper- and lowercase conversions
>>> line.rstrip() # Remove whitespace characters on the right side

formatting:

>>> '%s, eggs, and %s' % ('spam', 'SPAM!') # Formatting expression (all)
>>> '{0}, eggs, and {1}'.format('spam', 'SPAM!') # Formatting method (2.6+, 3.0+)
>>> '{}, eggs, and {}'.format('spam', 'SPAM!') # Numbers optional (2.7+, 3.1+)

4.5.4 Getting Help: dir, help

4.5.5 其他编码String的方法

\xNN 的十六进制写法,来表示无法直接打印的字符串

>>> ord('\n')       # \n 是ASCII中第10个
10
#raw string来取消 `\`转义机制: 
r'C:\text\new'

4.5.6 Unicode Strings

python3中,str就能处理Unicode, bytes用来表示二进制(原始byte数组),并且Python2中的 u字符串内容 也兼容:

>>> 'sp\xc4m'    # 3.X: normal str strings are Unicode text
'spÄm'
>>> b'a\x01c' # bytes strings are byte-based data
b'a\x01c'
>>> u'sp\u00c4m' # The 2.X Unicode literal works in 3.3+: just str
'spÄm

python2和3都支持

  1. \x十六进制表示字符串 : \xc4
  2. \u 表示短的Unicode: \u00c4
  3. \U 表示长的Unicode: \U000000c4

也就是说 u开头的,是兼容python2中的Unicode表示方式,本身是字符串; b开头的是 bytes; \x,\u``\U 都是字符串。

4.5.7 Pattern Matching

>>> import re
>>> match = re.match('Hello[ \t]*(.*)world', 'Hello Python world')
>>> match.group(1)

4.6 Lists

4.6.1 基本操作和string差不多

[1,2,3] + [1,2,3]
[1,2,3] * 2

4.6.2 上限检查

>>> L
[123, 'spam', 'NI']
>>> L[99]
...error text omitted...
IndexError: list index out of range
>>> L[99] = 1
...error text omitted...

4.6.3 嵌套

# 嵌套list
M = [[1, 2, 3], # A 3 × 3 matrix, as nested lists
[4, 5, 6], # Code can span lines if bracketed
[7, 8, 9]]

M[1][2]

4.6.4 Comprehensions

list comprehension:

>>> col2 = [row[1] for row in M] # Collect the items in column 2

>>> [row[1] for row in M if row[1] % 2 == 0] # Filter out odd items

>>> [[x ** 2, x ** 3] for x in range(4)] # Multiple values, "if" filters

generator:

>>> G = (sum(row) for row in M) # Create a generator of row sums
>>> next(G) # Run the iteration protocol next()
>>> next(G) # Run the iteration protocol next()

map:

>>> list(map(sum, M)) # Map sum over items in M

创建set/dict:

>>> {sum(row) for row in M} # Create a set of row sums
>>> {i : sum(M[i]) for i in range(3)} # Creates key/value table of row sums

4.7 字典

4.7.1 创建字典

D1 = {}
D2 = {'food': 'Spam', 'quantity': 4, 'color': 'pink'}

D3 = dict()
D4 = dict(name='Bob', job='dev', age=40)   # 传入 key-value 
D5 = dict(zip(['name', 'job', 'age'], ['Bob', 'dev', 40]))   # 传入 key-value的tuple们

4.7.2 测试是否存在

>>> 'f' in D

>>> value = D.get('x', 0)      # 获取key对应的value,key不存在则返回默认值

>>> value = D['x'] if 'x' in D else 0 # if/else expression form

4.7.3 遍历

iterable对象是遵循了 遍历协议: iter方法调用返回一个对象,它能够响应next方法来一个一个取值,当没有值的时候,触发一个 Exception

string,generator,list,list comprehension, file对象, dict对象都是iterable的, 其中dict对象对它使用 next方法就会直接返回 key,不需要调用 keys():

>>> for key in sorted(D):
print(key, '=>', D[key])

list comprehension, map函数等等,一般都比for循环速度快

4.8 Tuple (空)

4.9 File

>>> f = open('data.txt', 'w') # Make a new file in output mode ('w' is write)
>>> f.write('Hello\n') # Write strings of characters to it
6
>>> f.write('world\n') # Return number of items written in Python 3.X
6
>>> f.close()

>>> for line in open('data.txt'): print(line)     # 便利读取

4.9.1 二进制文件

文本文件在 写入和读出的时候,都会自动的Unicode编码和解码。

Python的Struct包可以 创建和解开 二进制数据:

>>> import struct
>>> packed = struct.pack('>i4sh', 7, b'spam', 8) # Create packed binary data
>>> packed # 10 bytes, not objects or text
b'\x00\x00\x00\x07spam\x00\x08'
>>>
>>> file = open('data.bin', 'wb') # Open binary output file
>>> file.write(packed) # Write packed binary data
10
>>> file.close()


>>> data = open('data.bin', 'rb').read() # Open/read binary data file
>>> data # 10 bytes, unaltered
b'\x00\x00\x00\x07spam\x00\x08'
>>> data[4:8] # Slice bytes in the middle
b'spam'
>>> list(data) # A sequence of 8-bit bytes
[0, 0, 0, 7, 115, 112, 97, 109, 0, 8]
>>> struct.unpack('>i4sh', data) # Unpack into objects again
(7, b'spam', 8)

4.9.2 Unicode文本文件

指定 encoding参数

>>> file = open('unidata.txt', 'w', encoding='utf-8') # Write/encode UTF-8 text
>>> text = open('unidata.txt', encoding='utf-8').read() # Read/decode UTF-8 text

>>> text.encode('utf-8') # Manual encode to bytes
b'sp\xc3\x84m'
>>> raw.decode('utf-8') # Manual decode to str
'spÄm

4.10 其他核心类型

4.10.1 Sets

>>> X = set('spam') # Make a set out of a sequence in 2.X and 3.X
>>> Y = {'h', 'a', 'm'} # Make a set with set literals in 3.X and 2.7
>>> X, Y # A tuple of two sets without parentheses
({'m', 'a', 'p', 's'}, {'m', 'a', 'h'})
>>> X & Y # Intersection
{'m', 'a'}
>>> X | Y # Union
{'m', 'h', 'a', 'p', 's'}
>>> X - Y # Difference
>>> X > Y # Superset
False
>>> {n ** 2 for n in [1, 2, 3, 4]} # Set comprehensions in 3.X and 2.7
{16, 1, 4, 9}

4.10.2 decimal

>>> import decimal # Decimals: fixed precision
>>> d = decimal.Decimal('3.141')
>>> d + 1
Decimal('4.141')
>>> decimal.getcontext().prec = 2
>>> decimal.Decimal('1.00') / decimal.Decimal('3.00')
Decimal('0.33')

4.10.3 fractional

>>> from fractions import Fraction # Fractions: numerator+denominator
>>> f = Fraction(2, 3)
>>> f + 1
Fraction(5, 3)
>>> f + Fraction(1, 2)
Fraction(7, 6)

4.10.4 查看类型

>>> type(L) # 3.X: types are classes, and vice versa
<class 'list'>
>>> type(type(L)) # See Chapter 32 for more on class types
<class 'type'>

# 判断类型
>>> if type(L) == type([]): # Type testing, if you must...
print('yes')
yes
>>> if type(L) == list: # Using the type name
print('yes')
yes
>>> if isinstance(L, list): # Object-oriented tests
print('yes')
yes
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值