#1. #A:当需要的时候,python3的整数类型会自动提供额外的精度用于较大的数值 value0 = 2**100 #value0 = 1267650600228229401496703205376 value1 = 2**100 + 1 #value1 = 1267650600228229401496703205377 #2. #A:math模块包含数学工具 #B:random模块可以作为随机数字生成器和随机选择器 import math value = math.pi #value = 3.141592653589793 import random vlaue = random.random() #vlaue = 0.4847373733624073 value = random.choice([1, 5, 7]) #value = 5 #3. #A:在python中能够对序列使用反向索引、切片和len()函数来得到长度 #B:序列包括:字符串、列表、元组 #C:切片的时候,:左边默认为0,右边默认为序列长度 #D:在python中,字符串、数字、元组具有不可变性,列表、字典具有可变性 #E:python字符串类型提供了丰富的处理函数 strTem = "string" value = strTem[-1] #value = 'g' value = strTem[-len(strTem)] #value = 's' value = strTem[1:3] #value = 'tr' #strTem[0] = 's' #运行出错 strTem = "string" value = strTem.find('in') #value = 3 value = strTem.replace('in', "ab") #value = 'strabg' strTem = 'string' strTem = "str, abc,num, xyz" value = strTem.split(', ') #value = ['str', 'abc,num', 'xyz'] value = strTem.split() #value = ['str,', 'abc,num,', 'xyz'] #4.字符串的format()函数 #A:通过位置 value = '{0},{1}'.format('szn',18) #value = 'szn,18' value = '{},{}'.format('slz',17) #value = 'slz,17' #B:通过关键字参数 value = '{n0},{n1}'.format(n0=18,n1='szn') #value = '18,szn' #C:通过下标 listTem0 = ['szn',18] listTem1 = ['slz',17] vlaue = '{0[0]},{1[1]}, {1[0]},{0[1]}'.format(listTem0, listTem1) #vlaue = 'szn,17, slz,18' #D:通过属性 class CPerson: def __init__(self,name,age): self.name,self.age = name,age def __str__(self): return 'This guy is {self.name},is {self.age} old'.format(self = self) value = CPerson('szn', 18).__str__() #value = 'This guy is szn,is 18 old' #E:填充和对齐:^、<、>分别是居中、左对齐、右对齐,后面带宽度 :号后面带填充的字符,只能是一个字符,不指定的话默认是用空格填充 vlaue = '{:>8}'.format('189') #vlaue = ' 189' vlaue = '{:0>8}'.format('189') #vlaue = '00000189' vlaue = '{:a>8}'.format('189') #vlaue = 'aaaaa189' vlaue = '{:a^8}'.format('189') #vlaue = 'aa189aaa' #F:精度和类型 f、b、d、o、x分别是浮点数、二进制、十进制、八进制、十六进制。用,号还能用来做金额的千位分隔符 value = '{:.2f}'.format(321.33345) #value = '321.33' value = '{:,d}'.format(123456789) #value = '123,456,789' value = '0X{:x}'.format(12).upper() #value = '0XC' value = '{:b}'.format(12) #value = '1100' value = '{:o}'.format(12) #value = '14' #5. #A:ord(), chr() value = ord('1') #value = 0x31 vlaue = chr(0x31) #vlaue = '1' #6. #A:允许在3个引号(单/双引号)中包括多行字符串常量,所有的行都合并在一起,在行末尾添加换行符(实际上这个换行符也是自己敲进去的) msg = """aaaaa bbb 'ccc'""" #msg = "aaaaa bbb 'ccc'" #7.字符串支持模式匹配(类似正则表达式的功能) 暂不研究 #8. #A:列表解析 #B:在python中,当最后一次引用对象后,这个对象占用的内存空间将自动被释放掉 listTem = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]] value = [row[1] for row in listTem] #value = [2, 5, 8] value = [listTem[i][i] for i in[0, 1, 2]] #value = [1, 5, 9] value = (sum(row) for row in listTem) TemValue = next(value) #TemValue = 6 TemValue = next(value) #TemValue = 15 value = 's' * 2 #value = 'ss' value = [v * 2 for v in "szn"] #value = ['ss', 'zz', 'nn'] value = [1, 2] value *= 5 #value = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2] value = (1, 3) value *= 5 #value = (1, 3, 1, 3, 1, 3, 1, 3, 1, 3) value = bool([] or '' or 0 or None) #value = False #9. #A:元组具有不可变性,但是其对象可以被改变 tupleTem = (1, 2, 3) #tupleTem[0] = 0 #报错 tupleTem += (5, 3) #tupleTem = (1, 2, 3, 5, 3) value = tupleTem.index(1) #value = 0 value = tupleTem.index(3) #value = 2 #value = tupleTem.index(0) #报错 value = tupleTem.count(3) #value = 2 value = tupleTem.count(0) #value = 0 #10. #A:集合,可以通过set()或者集合常量和表达式创建,是唯一的不可变的对象的无序集合,支持一般的数学集合操作
#B:集合中的项是唯一的,不管将其添加了多少次 setA = set("abc") #setA = {'b', 'a', 'c'} setB = set("bcd") setTem = setA & setB #setTem = {'b', 'c'} setTem = setA | setB #setTem = {'b', 'c', 'a', 'd'} setTem = setA - setB #setTem = {'a'} setTem = {x ** 2 for x in[1, 2]} #setTem = {1, 4} #11. #A:分数模块:Fraction 固定精度浮点数:decimal from fractions import Fraction #fractions 英[f'rækʃnz]美[f'rækʃnz]n. <数>分数 value = Fraction(1, 3) #value = Fraction(1, 3) value += Fraction(2, 3) #value = Fraction(1, 1) import decimal value = decimal.Decimal('10.222') value += 1 #value = Decimal('11.222') decimal.getcontext().prec = 2 value = decimal.Decimal('1') / decimal.Decimal('3') #value = Decimal('0.33') decimal.getcontext().prec = 5 value = decimal.Decimal('1') / decimal.Decimal('3') #value = Decimal('0.33333')