python基本数据类型数值类型
序列类型
集合类型
映射类型类型代码数值类型int、bool、float、 complex
序列类型不可变:str、tuple、bytes、不可变:list
集合类型set
映射类型dict
数值类型例子:
范文1:int类型# bit_length() 返回一个数的二进制长度
i = 100print(i.bit_length())'''
bit_length1 0000 0001 12 0000 0010 23 0000 0011 2'''
范文2:bool类型a = 3 > 4print(a)a1 = 20 > 10print(a1)
范文3:float类型a = float(2)print("%f" % (a))
a = float(2)
print(f"{a}")
范文4: complexprint(type(3 + 3j)) # python 用j代表虚部,j前面必须要有代表虚数部分的数print((3j)**2)
序列类型
不可变的序列:
范文1:strclass str(object='')class str(object=b'', encoding='utf-8', errors='strict')
范文2:tuplea = [1,2] #list
b = {"1":2,"3":3} #dict
c = {1,2,3,3} #setd = range(2,10,2) #rangeprint(tuple(a))print(tuple(b))print(tuple(c))print(tuple(d))
输出(1, 2)('1', '3')(1, 2, 3)(2, 4, 6, 8)
范文3:bytesprint((bytes()))#为空print((bytes([1,2,255])))#可迭代类型print(type(bytes([1,2,255])))print((bytes("China",'utf-8')))#字符串print((bytes("中国",'gbk')))#字符串
# print((bytes([1,2,256])))#报错
输出:b''b'\x01\x02\xff'b'China'b'\xd6\xd0\xb9\xfa'
范文4:list-不可变list1 = ['physics', 'chemistry', 1997, 2000]list2 = [1,2,3,4,5]list3 = ["a", "b", "c", "d"]print(list1,list2,list3)
集合类型
范文1:setprint(set([1,2,3])){1, 2, 3}print(set('123')){'1', '2', '3'}print(set())set()
输出:{1, 2, 3}{'2', '1', '3'}set()
映射类型
范文1:dictx = [1, 2, 3]y = [4, 5, 6]print (dict(zip(x, y)))
输出:{1: 4, 2: 5, 3: 6}
~~
4个作业
~~
问题:当输入当输入y或Y打印进入卸载,当输入n或N打印退出程序,输入其它则打印输入不在选项范围之内。n = 289N = 100nN = n - Nif nN
2.完成字符串的逆序以及统计设计个程序, 要求只能输入长度低于20的字符当满足需求时,则打印出字符串长度以及使用切片逆序打印出字符串当不满足需求时,打印用户重新输入.ll=[0,1,2,3,4,5,6,7,8,9]print(ll[-1:3]) #结果 : []print(ll[-1:3:-1]) #结果: [9, 8, 7, 6, 5, 4]print(ll[-1:3:-2]) #结果: [9, 7, 5]
写法2:list1=[0,1,2,3,4,5,6]two=list1[3:-1]print("切片正序实现从正数数到负数的切%s"%two)#切片从正数到负数的切[3,4, 5]
作业3
问题:‘3’+'4’会等于什么?prtint('3'+'4')
34