Python入门学习笔记3:Python的基本类型
1 #print("hello word!") 2 3 #数据类型 Number数字 4 print(type(1)) #<class 'int'> 5 print(type(-1)) #<class 'int'> 6 print(type(1.1))#<class 'float'> 7 print(1+1)#2 8 print(type(1+0.1))#<class 'float'> 9 print(type(1+1.0))#<class 'float'> 10 print(type(1*1))#<class 'int'> 11 print(type(1*1.0))#<class 'float'> 12 print(type(2/2))#<class 'float'> 13 print(type(2//2))#<class 'int'> 14 #进制数 15 print(0b10,0b11,0b100,0b101,0b110,0b111,0b1000,0b1001,0b1010)#二进制:0b+二进制数 2 3 4 5 6 7 8 9 10 16 print(0o10,0o11,0o20)#八进制:0o+八进制数 8 9 16 17 print(0x10,0x11,0x1F)#16进制:0x+16进制数 16 17 31 18 #进制转换 19 print(bin(10),bin(0xE))#将其他进制数转为二进制 0b1010 0b1110 20 print(int(0b111),int(0o77))#将其他进制数转为十进制 7 63 21 print(oct(0b111),oct(0x777))#将其他进制数转为八进制 0o7 0o3567 22 print(hex(888),hex(0o777))#将其他进制数转为十六进制 0x378 0x1ff 23 24 25 #数据类型:bool布尔类型:表示真、假 26 print(True,False,type(True),type(False),int(True),int(False),bool(1),bool(0))#True False <class 'bool'> <class 'bool'> 1 0 True False 27 print(bool(2),bool(2.2),bool(-1.1),bool(0),bool(0b01),bool(0b0))#bool()方法中除了0为假False,其他都为真 True True True True False True False 28 print(bool('abc'),bool(''),bool([1,2,3]),bool([]),bool({1,2,3}),bool({}),bool(None))#bool()空字符串或数组为空或为None时均为False,其余为True True False True False True False False 29 #数据类型:complex复数 30 print(36j)#复数格式 31 32 #数据类型 字符串str 单引号、双引号、三引号 33 print('hello world',"hello world",type('1'),type(1),"'let's go",'let"go',"let\'s go") #hello world hello world <class 'str'> <class 'int'> 'let's go let"go let's go 34 print(''' 35 hello world 36 hello world 37 hello world 38 ''') 39 print(""" 40 hello world 41 hello world 42 hello world 43 """) 44 print("""hello world\nhello world\nhello world""")#三引号的作用 \n换行 45 print('hello\ 46 world') 47 #转义字符 \n 换行 \'单引号 \t 横向制表符 \r回车 \将特殊字符变为字符串 48 print('hello\n \' \t \r world') 49 print(r'hello\n \' \t \r world',R'hello\n \' \t \r world')#字符串前增加r则将后面全部变为字符串 50 #字符串运算 51 print('hello'+'world','hello'*3,"hello world"[0],"hello world"[-1]) #helloworld hellohellohello h d 52 print("hello world"[0:5],"hello world"[0:-1],"hello world"[6:11],"hello world"[6:0],"hello world"[6:],"hello world"[:-5],"hello world"[-5:])#[起点0为起点:步长] hello hello worl world world hello world
1 #组 2 #列表 列表中可以嵌套列表且可以支持多种类型在同一列表中 3 print(type([1,2,3,4,5,6])) 4 print(type(["hello","world",1,9,True,False])) 5 print(type([["hello","world"],[1,9],[True,False]])) 6 7 #列表截取 与字符串截取类似 8 print(["新月打击","苍白瀑布","月之降临","月神冲刺"][0]) #新月打击 9 print(["新月打击","苍白瀑布","月之降临","月神冲刺"][0:2])#切片 ['新月打击', '苍白瀑布'] 10 print(["新月打击","苍白瀑布","月之降临","月神冲刺"][-1:])#['月神冲刺'] 11 12 #列表合并操作 13 print(["新月打击","苍白瀑布","月之降临","月神冲刺"]+["点燃","虚弱"])#['新月打击', '苍白瀑布', '月之降临', '月神冲刺', '点燃', '虚弱'] 14 print(["新月打击","苍白瀑布","月之降临","月神冲刺"]*3)#['新月打击', '苍白瀑布', '月之降临', '月神冲刺', '新月打击', '苍白瀑布', '月之降临', '月神冲刺', '新月打击', '苍白瀑布', '月之降临', '月神冲刺'] 15 print(['巴西','克罗地亚','墨西哥','喀麦隆'],[],[],[],[]) 16 17 #元组 18 print((1,2,3,4,'-1',True)) #(1, 2, 3, 4, '-1', True) 19 print((1,2,3,4,'-1',True)[0])# 1 20 print((1,2,3,4,'-1',True)[0:2])#(1, 2) 21 print((1,2,3)+(4,5,6))#(1, 2, 3, 4, 5, 6) 22 print((1,2,3)*3)#(1, 2, 3, 1, 2, 3, 1, 2, 3) 23 print(type((1,2,3)))#<class 'tuple'> 24 #注:若()只有一个元素的元组会直接当做运算符号 25 print(type((1)),type(('hello')))#<class 'int'> <class 'str'> 26 print((1+1)*2)#4 27 28 print((1,),type((1,)))#表示一个元素的元组 (1,) <class 'tuple'> 29 print((),type(()))#表示空元组 () <class 'tuple'> 30 print(int,float,bool,str,list,tuple)#<class 'int'> <class 'float'> <class 'bool'> <class 'str'> <class 'list'> <class 'tuple'> 31 32 #元素在数组中的操作 33 print(3 in [1,2,3,4,5,6],10 in [1,2,3,4,5,6]) # 元素是否在数组中 True False 34 print(3 not in [1,2,3,4,5,6],10 not in [1,2,3,4,5,6])# 元素是否在数组中 False True 35 print(len([1,2,3,4,5,6]))#列表的长度 6 36 print(max([1,2,3,4,5,6]),max("helloworld"))#列表中的最大值 6 w(计算的是ascll码) 37 print(min([1,2,3,4,5,6]),min("helloworld"))#列表中的最小值 1 d(计算的是ascll码) 38 print(ord('w'),ord('d'),ord(' '))# ascll码 119 100 32 39 40 #集合 set 特性:无序、去重 ()元组 []列表 {}集合 41 print(type({1,2,3,4,5,6})) #<class 'set'> 42 print({1,1,2,2,3,3,4,4,5,5,6,6})#{1, 2, 3, 4, 5, 6} 43 print(len({1,2,3}),1 in {1,2,3},1 not in {1,2,3})#3 True False 44 print({1,2,3} - {2,3})#差集:两个集合的差集 {1} 45 print({1,2,3,4,5,6} & {3,4})#交集:两个集合共有的元素 {3, 4} 46 print({1,2,3,4,5,6} | {3,4,7})#并集:且去重 {1, 2, 3, 4, 5, 6, 7} 47 print(set(),len(set()),type(set()))#空的集合 set() 0 <class 'set'> 48 49 #字典 50 print(type({"key1":1,"key2":2}))#<class 'dict'> 51 print({"Q":"新月打击","W":"苍白瀑布","E":"月之降临","R":"月神冲刺"}["Q"])#通过Key获取value 新月打击 52 print({1:"新月打击","1":"苍白瀑布","E":"月之降临","R":"月神冲刺"}['1'])# 1与"1"是两个不同的key 苍白瀑布 53 print(type({1:"新月打击","1":"苍白瀑布","E": {1,1},"R":"月神冲刺"}))# 字典中的value无限制,key是不可变的类型 <class 'dict'> 54 print(type({}))#空的字典 <class 'dict'>