Number数字类型包括整型、浮点型、布尔型、复数四种类型python
short int long
之区分,python
中简化了分类python
中float
自己表明的是双精度print(1) print(type(1)) # 利用type来测量他的类型 print(type(1.11111111)) print(type(1 + 0.1)) # 在加法计算过程当中,有一个浮点数,获得的结果就是浮点数 print(1 + 1.0) print(type(1 + 1.0)) # 混合型运算时 只要获得的是小数(包括2.0)也是float print(type(1 * 1.0)) # 在乘法计算过程当中,有一个浮点数,获得的结果就是浮点数 print(type(2 / 2)) # 两个整型相除获得的是一个float 若是是3//2获得的是整型,整除运算,整除是向下取整 # 输出结果 1 <class 'int'> <class 'float'> <class 'float'> 2.0 <class 'float'> <class 'float'> <class 'float'>
print(type(True)) print(int(False)) # 将bool类型强制转化为int类型,转化为0 print(bool(0)) # 将int类型转化为bool类型,若是为0,那么显示的是False print(bool(100)) # 只要是非零的数值,都表示为True print(bool('')) # 将空字符串进行bool转化,为False print(bool([])) # 将空列表进行bool转化,为False print(bool({})) # 将“空字典”进行bool转化,为False print(bool(None)) # 将None进行bool转化,为False # 输出结果 <class 'bool'> 0 False True False False False False
# 复数 print(36j) # 纯虚数 print(1+2j) # 一个复数 # 输出结果 36j (1+2j)
# 2进制 8进制 16进制 10进制 print(0b10) # 表示一个二进制的数字 会直接输出十进制,python默认十进制 print(0o10) # 表示一个八进制 print(0x1f) # 表示一个十六进制 print(bin(10)) # 转化为二进制 bin(0o7) bin(oxe) print(type(bin(10))) # 获得的是一个str类型 print(int(0o11)) # 转化为十进制 print(type(int(0o11))) # 获得的是一个int类型 print(hex(0b111)) # 转化为十六进制 获得的是一个str类型 print(oct(0x777)) # 转化为八进制 获得的是一个str类型 # 输出结果 2 8 31 0b1010 <class 'str'> 9 <class 'int'> 0x7 0o3567
print('hello world') print("let's go") # 单引号和双引号能够嵌套 print('''这是一个多行字符串 能够进行换行nei''') # 多行字符串 三引号能够将字符串进行换行 # 输出结果 hello world let's go 这是一个多行字符串 能够进行换行nei
\n 换行 \' 单引号 \t 横向制表符 \r 回车 \\ 输出\ print('hello \\n world') # 想把\n输出出来 print(r'c:\nfrewen\ndatabase') # 当一个字符串前面加一个r 里面的\n就再也不当作转义字符 会当作原始字符串 # 输出结果 hello \n world c:\nfrewen\ndatabase
print("hello" + "world") # 字符串拼接 print('hello ' * 3) # 字符串重复输出3次 string = 'hello' print(string[4]) print(string[-3]) print(string[0:3]) print(string[0:-1]) print(string[1:]) # 输出结果 helloworld hello hello hello o l hel hell ello
列表类型在其余语言中像是数组数组
arr1 = [1, 2, 3, 4, 5, 6, "abc", False] # 定义一个列表 arr2 = [[1, 2], [True, False]] # 嵌套列表 print(type(arr2)) # 输出结果 <class 'list'>
arr1 = [1, 2, 3, 4, 5, 6, "abc", False] # 定义一个列表 print(arr1[5]) print(arr1[0:2]) # 返回的是一个列表 print(arr1[-1:]) arr3 = [7, 8, 9] print(arr1 + arr3) print(arr3*3) # 输出结果 6 [1, 2] [False] [1, 2, 3, 4, 5, 6, 'abc', False, 7, 8, 9] [7, 8, 9, 7, 8, 9, 7, 8, 9]
tuple1 = (1, 2, 3, 4, 5) tuple2 = (1, 'abc', True) print(type(tuple1)) print(tuple1[4]) print(tuple1[0:-1]) # 获得的是一个元组类型 print(tuple2*3) print(type((1))) # 小括号在python里面既能够表示元祖,也能够表示优先级括号(1+1)*2 print(type(('hello'))) # python规定若是小括号里面只有一个元素,那么就是表示优先级括号 # 输出结果 <class 'tuple'> 5 (1, 2, 3, 4) (1, 'abc', True, 1, 'abc', True, 1, 'abc', True) <class 'int'> <class 'str'>
# 如何定义只有一个元素的元祖 print(type((1,))) # 如何定义没有元素的元祖 print(type(())) # 输出结果 <class 'tuple'> <class 'tuple'>
在python里面 str list tuple 均可以概括为序列类型序列共有的操做编码
print("hello world"[0:8:2])
+ *
in/ not in
len()
max() min()
print(3 in [1, 2, 3]) print("hello world"[0:6:3]) # 每次隔着3个字符 print(len((1, 2, 3, 5, 6))) print(max('hello world')) print(min('hello world')) # 编码方式:ASCII ord() 该方法将字符转化为ASCII码 print(ord('w')) print(ord(' ')) # 输出结果 True hl 5 w 119 32
set1 = {1, 2, 3, 4, 1, 3} print(type(set1)) print(set1) # 无序性和互异性 print(len(set1)) # max min print(1 in set1) set2 = {1, 2, 3, 4, 5, 6} set3 = {3, 4, 7} set4 = set() # 定义了一个空的集合 print(set2 - set3) # 求两个集合的差集 print(set2 & set3) # 求两个集合的交集 print(set2 | set3) # 求两个集合的并集 print(type({})) # 并不是定义了一个空的集合 是一个空的字典类型 print(len(set4)) # 定义了一个空的集合 # 输出结果 <class 'set'> {1, 2, 3, 4} 4 True {1, 2, 5, 6} {3, 4} {1, 2, 3, 4, 5, 6, 7} <class 'dict'> 0
str int float list set tuple dict
dict1 = {'name': 'frewen', 'age': 15, 1: '字典的键能够不是字符串', '1': '123'} dict2 = {} # 空的字典 print(dict1) print(type(dict1)) print(dict1['name']) # 取值 print(dict1[1]) # 输出结果 {'name': 'frewen', 'age': 15, 1: '字典的键能够不是字符串', '1': '123'} <class 'dict'> frewen 字典的键能够不是字符串