本文目录:
一、可变不可变类型
#可变类型: 值变了,但是id没有变,证明没有生成新的值而是在改变原值,原值是可变类型 #不可变类型:值变了,id也跟着变,证明是生成了新的值而不是在改变原值,原值是不可变 # x=10 # print(id(x)) # x=11 # print(id(x)) # y=['a','b','c'] print(id(y)) y[0]='A' print(y) print(id(y))
二、数字类型
整型int
# ======================================基本使用====================================== # 1、用途: 记录年龄等级各种号码 # 2、定义方式: # age=18 # age=int(18) # x=int('123') #只能将纯数字的字符串转换成整型 # print(type(x)) # print(int(3.7)) # 3、常用操作+内置的方法 #赋值比较算术 # ======================================该类型总结==================================== # 存一个值 # 不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash) # print(hash(10)) # print(hash([1,2,3]))
浮点型float
# ======================================基本使用====================================== # 1、用途: 记录身高体重薪资 # 2、定义方式 # salary=1.3 #salary=float(1.3) # x=float('3.1') # print(x,type(x)) # 3、常用操作+内置的方法 #赋值比较算术 # ======================================该类型总结==================================== # 存一个值 # 不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash) # x=3.1 # print(id(x)) # x=3.2 # print(id(x)) # 了解: # 复数 # x=1-2j # print(x,type(x)) # print(x.real) # print(x.imag) # 长整型 # 其他进制=>十进制 # 十进制: 0-9 # 11 = 1*10^1 + 1*10^0 # 二进制: 0 1 # 11 = 1*2^1 + 1*2^0 # 八进制: 0-7 # 11 = 1*8^1+1*8^0 # 十六进制:0-9 A-F # 11 = 1*16^1+1*16^0 # 十进制=>其他进制 print(bin(13)) # 十进制=>二进制 print(oct(13)) # 十进制=>八进制 print(hex(13)) # 十进制=>十六进制
三、字符串类型
# ======================================基本使用====================================== # 1、用途:记录描述性质的特征,比如名字地址性别 # 2、定义方式:在单引号双引号三引号内包含的一串字符 msg='aaa"bbb"' #msg=str(...) # 可以将任意类型转换成字符串 # str(1) # str(1.3) # x=str([1,2,3]) # print(x,type(x)) # 3、常用操作+内置的方法 #优先掌握的操作(*****): #1、按索引取值(正向取+反向取) :只能取 msg='hello world' # print(msg[0]) # print(msg[5]) # print(msg[len(msg)-1]) # print(msg[-1]) # msg[0]='H' #2、切片(顾头不顾尾,步长): 想要从一个大字符串中切出一个小字符串 # msg='hello world' # print(msg[0:5]) # print(msg) # print(msg[0:5:2]) #0 2 4 # 了解 msg='hello world' # print(msg[-1:-5:1]) # print(msg[-1:-5:-1]) #d l r o # print(msg[0:5:1]) # print(msg[-1::-1]) #掌握 #3、长度len # msg='你好啊a' # print(len(msg)) #4、成员运算in和not in msg='yangyuanhu 老师是一个非常虎的老师' # print('yangyuanhu' in msg) # print('虎' not in msg) # print(not '虎' in msg) #5、移除字符串左右两边的字符strip:默认去空格 # pwd=' 1 23 ' # res=pwd.strip(' ') # print(res) # pwd=input('>>: ').strip() #pwd='123' # # if pwd == '123': # print('密码输入正确') # pwd='******12*3****' # print(pwd.strip('*')) # pwd='****/&^**123**^*/*&' # print(pwd.strip('*/&^')) # print(pwd.strip('*/&')) #^**123**^ #6、切分split:针对有规律字符串按照某个字符切成列表 # info='yyhdsb|18|female' # li=info.split('|',1) # print(li) #7、循环 # msg='hello' # # for item in msg: # print(item) # ======================================该类型总结==================================== # 存一个值 # 有序 # 不可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash) # s1='hello' # print(id(s1)) # s1='world' # print(id(s1)) # 需要掌握的操作(****) #1、strip,lstrip,rstrip # print('****egon***'.strip('*')) # print('****egon***'.lstrip('*')) # print('****egon***'.rstrip('*')) #2、lower,upper # print('AAAbbbb'.lower()) # print('AAAbbbb'.upper()) #3、startswith,endswith # print('alex is sb'.startswith('alex')) # print('alex is sb'.endswith('sb')) #4、format的三种玩法 # print('my name is %s my age is %s' %('egon',18)) # print('my name is %s my age is %s' %(18,'egon')) # print('my name is {name} my age is {age} '.format(age=18,name='egon')) # 了解 # print('my name is {} my age is {} '.format(18,'egon')) # print('my name is {0} my age is {1} '.format(18,'egon')) # print('my name is {1} my age is {0} '.format(18,'egon')) #5、split,rsplit # msg='a:b:c:d:e' # print(msg.split(':',1)) # print(msg.rsplit(':',1)) #6、join # msg='a:b:c:d:e' # list1=msg.split(':') # msg1=':'.join(list1) # print(msg1) # info='egon:123:male' # list1=info.split(':') # print(list1) # print(':'.join(list1)) #7、replace # msg='alex is alex alex is hahahaha' # print(msg.replace('alex','SB',1)) #8、isdigit # print('123'.isdigit()) # 只能判断纯数字的字符串 # print('12.3'.isdigit()) # age_of_db=30 # inp_age=input('>>>: ').strip() # if inp_age.isdigit(): # inp_age=int(inp_age) # if inp_age > age_of_db: # print('too big') # elif inp_age < age_of_db: # print('too small') # else: # print('you got it') # 了解的操作(**) #1、find,rfind,index,rindex,count # msg='hello worldaa' # print(msg.index('wo')) # print(msg.index('wo',0,3)) # print(msg.find('wo',0,3)) # print(msg.find('xxxxxxx')) # print(msg.index('xxxxxxx')) # print(msg.count('l')) #2、center,ljust,rjust,zfill # name=input('>>: ').strip() # print('egon'.center(50,'=')) # print(('%s' %name).center(50,'-')) # print('egon'.ljust(50,'=')) # print('egon'.rjust(50,'=')) # print('egon'.zfill(50)) #3、expandtabs # print('hello world'.expandtabs(5)) #4、captalize,swapcase,title # print('hello world'.capitalize()) # print('Hello world'.swapcase()) # print('Hello world'.title()) #5、is数字系列 num1=b'4' #bytes num2=u'4' #unicode,python3中无需加u就是unicode num3='四' #中文数字 num4='Ⅳ' #罗马数字 #isdigit: bytes,str # print(num1.isdigit()) # print(num2.isdigit()) # print(num3.isdigit()) # print(num4.isdigit()) #isdecimal:str # print(num2.isdecimal()) # print(num3.isdecimal()) # print(num4.isdecimal()) #isnumberic:str,中文罗马 # print(num2.isnumeric()) # print(num3.isnumeric()) # print(num4.isnumeric()) #6、is其他 # print('aaasdfaA'.isalpha()) # 纯字母组成的字符串 # print('aaasdfaA123'.isalnum()) # 字母或数字组成 # print('aaasdfaA'.isalnum()) # 字母或数字组成 # print('123'.isalnum()) # print(' '.isspace()) # print(' 12'.isspace())
四、列表类型
# ======================================基本使用====================================== # 1、用途:记录多个值,比如人的多个爱好 # 2、定义方式: 在[]内用逗号分隔开多个任意类型的值 li=[1,2,3] # li=list([1,2,3]) # x=list('hello') # x=list({'a':1,'b':2,'c':3}) # print(x) # 3、常用操作+内置的方法 #优先掌握的操作: #1、按索引存取值(正向存取+反向存取):即可存也可以取 # li=['a','b','c','d'] # print(li[-1]) # li[-1]='D' # print(li) # # # li[4]='e' # del li[0] # print(li) #2、切片(顾头不顾尾,步长) # li=['a','b','c','d'] # print(li[0:3]) #3、长度 # print(len(li)) #4、成员运算in和not in # users=['egon','lxx','yxx','cxxx',[1,2,3]] # print('lxx' in users) # print([1,2,3] in users) # print(1 in users) #5、追加 # li=['a','b','c','d'] # print(id(li)) # li.append('e') # li.append([1,2,3]) # print(li,id(li)) #6、删除 # li=['a','b','c','d'] # 按照元素值去单纯地删除某个元素 # del li[1] # res=li.remove('c') # print(li) # print(res) # 按照元素的索引去删除某个元素并且拿到该元素作为返回值 # res=li.pop(1) # print(li) # print(res) #7、循环 # li=['a','b','c','d'] # for item in li: # print(item) # ======================================该类型总结==================================== # 存多个值 # 有序 # 可变(1、可变:值变,id不变。可变==不可hash 2、不可变:值变,id就变。不可变==可hash) # print(hash([1,2,3])) # 需要掌握的操作 li=['a','b','c','d','c','e'] # print(li.count('c')) # li.extend([1,2,3]) # li.append([1,2,3]) # print(li) # print(li.index('z')) # print(li.index('b')) # print(li.index('d',0,3)) # li.insert(1,'egon') # print(li) # li=[3,1,9,11] # li.reverse() # print(li) # li.sort(reverse=True) # print(li) # 练习 # 队列: 先进先出 # q=[] # # 入队 # q.append('first') # q.append('second') # q.append('third') # print(q) # # 出队 # print(q.pop(0)) # print(q.pop(0)) # print(q.pop(0)) # 堆栈: 先进后出 q=[] # 入栈 q.append('first') q.append('second') q.append('third') # 出栈 print(q.pop(-1)) print(q.pop(-1)) print(q.pop(-1))