一:整形int:
1.用途:记录年龄\等级\年等整数相关
2。定义方式
age = 18 #age = int(18)
数据类型转换:int可以将纯数字的字符串转换成整形
n = int('123123')
print(n,type(n))
3.常用操作+nei'内置的方法
#数学运算&比较运算
整形int总结:
只能存一个值;是不可变类型;
x = 10
print(id(x))
x += 1
print(id(x))
二:浮点型float:
基本使用介绍:
1:用途:记录薪资,身高,体重等小数相关
2:定义方式:
salary = 3.1 #salary = float(3.1)
数据类型转换:int可以将纯数字的字符串zhua转换成整形
sal = flaot(‘3.1’)
print(sal,type(sal))
3.常用操作+内置的方法
#数字运算&比较运算
浮点型float总结:
只能存一个zhi值;是属于不可变类型
x = 3,1
print(id(x))
x += 1.1
print(id(x))
复数:
x = 1 + 2j
print(x,real)
print(x,imag)
十进制,八进制,十六进制的使用:
print(bin())
print(oct())
print(hex())
三:字符串str:
1。用途:记录名字\性别等描述性质
2.定义方式:在‘’\" "\''' ''' 内包含一系列的字符
name = 'kavin' #name = str'kevin'
数据类型转换:str可以将任意类型都转成字符串类型
n = str([1,2,3]) #'[,1,2,3]'
print(type(n))
3:常用操作+内置的方法
1.你索引取值(正向取+反向取):只能取
msg = ‘hello word’
print(msg[0]) 'h'
print(msg[-1]) 'd'
2.切片(顾头不顾尾,步长):从一个大的字符串中切出一个小字符串
msg = 'hello word'
print(msg[0:5:1]) hello
print(msg[0:5:2]) hlo
info = 'egon 18'
print(info[0:4]) egon
msg = 'hello word'
print(msg[-1:-12:-1]) drow olleh
prinnt(msg[-1::-1]) drow olleh
print(msg[::-1]) drow olleh
3.长度len
print(len('ad你好')) 4
4.成员运算in和not in :判断一个子字符串是否存在一个大字符串中
msg = 'name age sex'
print('sex in msg')
print('sex' not in msg)
5.移除空白strip:移除字符串左右两边的字符
msg = ’******egon****‘
res = msg。strip('*')
print(res) egon
msg = ' egon'
print(res) egon
name = input('name:').strip()
pwd = input('pwd:').strip()
6.切分split:把一个字符串按照某种分隔符切成一个列表,从而方便取值
info = 'egon:18:male'
res = info.split(':')
print(res[0]) egon
print(res) 'egon','18','male'
info1 = res[0] + ':' + res[1] + ':' + res[2]
info1 = ':'.join(res) #res中所有的元素必须是str类型
print(infol)
7.循环:
msg = 'hello'
for item in msg:
print(item)
#1、strip,lstrip,rstrip
# print('****egon***'.strip('*'))
# print('****egon***'.lstrip('*'))
# print('****egon***'.rstrip('*'))
#2、lower,upper
# print('AdaEs1'.lower())
# print('AdaEs1'.upper())
#3、startswith,endswith
# print('alex is dsb'.startswith('alex'))
# print('alex is dsb'.startswith('al'))
# print('alex is dsb'.endswith('sb'))=
4.format的三种玩法:
msg = 'my name is %s my age is %s' %('egon',18)
print(msg)
# msg='my name is {name} my age is {age}'.format(age=18,name='egon')
# print(msg)
# msg='my name is {} my age is {}'.format('egon',18)
# print(msg)
# msg='my name is {1}{0}{0}{0} my age is {1}{1}'.format('egon',18)
# print(msg)
#5、split,rsplit
info='egon:18:male'
# print(info.split(':',1))
# print(info.rsplit(':',1))
#6、replace
# msg='kevin is dsb kevin'
# res=msg.replace('kevin','dsb',1)
# print(res)
#7、isdigit:当字符串是由纯数字组成时返回True
# print('123123'.isdigit())
# age_of_db=18
# inp_age=input('>>: ').strip()
# if inp_age.isdigit():
# inp_age=int(inp_age)
# print(inp_age == age_of_db)
# else:
# print('年龄必须是数字')
知识扩充;
1、find,rfind,index,rindex,count
find:查找子字符串在大字符串中的起始位置
count:统计子字符串在大字符串中出现的次数
#2、center,ljust,rjust,zfill
# username=input('>>>: ').strip()
# print(username.center(50,'*'))
# print('egon'.ljust(50,'#'))
# print('egon'.rjust(50,'#'))
# print('egon'.rjust(50,'0'))
# print('egon'.zfill(50))
#3、captalize,swapcase,title
# print('abdef adsfasdf'.capitalize())
# print('AbC'.swapcase())
# print('my name is egon'.title())
#4、is数字系列
# print('123213'.isdigit())
# print('Ⅳ'.isnumeric())
# print('贰'.isnumeric())
#5、is其他
# name='My Nmae'
# print(name.isalnum()) #字符串由字母或数字组成
# print(name.isalpha()) #字符串只由字母组成
# print(name.isidentifier())
# print(name.islower())
# print(name.isupper())
# print(name.isspace())
# print(name.istitle())
str字符串类型总结:
只能存一个值
有序
是属于不可变类型
列表list:
1:用途:记录多个同种属性的值
2.定义方式:在[]内用逗号隔开多个任意类型的值
l = [1,'a',[1,2]] #l = list([1,'a',[1,2])
数据转换类型:list
rse = list('hello')
print(res)
for k in {'a':1,'b':2}:
print(k)
res = list({'a':1,'b':2})
print(res)
3.常用操作+内置大的方法
1.按索引取值(正向存取+方向存取):即可存也可取
l = ['a','b','c','d','e']
print(l[-1]) 'e'
print(id(l))
l[0] = 'A'
print(l)
print(id(l))
2.切片(顾头不顾尾,步长)
l = ['a','b','c','d','e']
print(l[0:3]) 'a','d'
3.长度:
l = ['a','b','c','d','e']
print(len(l)) 4
4.成员运算in和not in
l = ['a','b','c','d','e']
print('xxx', not in l)
5.追加a'p'append&插入insret
l = ['a','b','c','d','e']
l,.append('aaa') l = ['a','b','c','d','e','aaa']
l,.append('bbbb')
print(l) l = ['a','b','c','d','e','aaa','bbbb']
l.inster(l,'aaa')
print(l)
6.删除:
l = ['a','b','c','d','e']
del l[0]
print(l) l = ['b','c','d','e']
#del 单纯的删除,没有返回值
res = l。remove(’a‘)#没有返回值
print(l) l = ['b','c','d','e']
print(res) remove没有赋予值功能 即 res = None(空)
#remove 从列表中取走一个值
res = l.pop(0)
print(l) l = ['b','c','d','e']
print(res) res ='a'
7.循环:
l = ['a','b','c','d','e']
for item in l;
print(item)
列表list总结:
可以存多个值
有序
是属于可变类型
#小练习
1.入队出队
先进先出
2.出栈:
先进后出