数字与字符串类型

拓展知识:

python3 input()用户输入任何值,都存成字符串类型

python2 raw_input()  用户输入任何值,都存成字符串类型

              input('>>:')  必须输入明确的数据类型,输入什么类型就存成什么类型

了解知识点:

while+else的使用

count=1

while count<6:

     print(count)

     count+=1

else:

       print(‘===》‘)

但是如果while循环里有break的话 则 else不能进行

count=1
while count < 6:
print(count)
count+=1
break
else:
print('===>:')

count = 0
while count <= 5 :
count += 1
if count == 3:break
print("Loop",count)

else:
print("循环正常执行完啦")
print("-----out of while loop ------")
数字类型:
整型int
用途:年龄,qq号等
定义方式 age=10 #age=int(10)
常用操作:加减乘除
浮点型flaot
用途:身高,薪资,体重等
定义方式 weight=66.5 #weight=flaot(66.5)
常用操作:加减乘除
十进制的转换:
bin把十进制转换为二进制
print(bin(3))
0b11
0b用来表示二进制
oct把十进制转换为八进制
print(oct(8))
0o10
0o用来表示八进制
hex把十进制转换成十六进制
print(hex(22))
0x16
0x用来表示十六进制
字符串类型:
字符串str
用途:描述性数据 如学历,国际,名字等
定义方式:name=’egon' #name=str(‘egon’)
常用操作:(重点)*
1.按索引取值(正向取值+反向取值)只能取
name='egon'
print(name[0])
print(name[-1])
2.切片(顾头不顾尾,步长)
msg='alex say love'
print(msg[0:3])取前三个字母
print(msg[-1:-5:-1])取后四个字母(由右往左)
print(msg[5:1:-1])由右往左取s xe
print(msg[:])全取
3.长度len
msg='alex say love'
print(len(mag))
4.成员运算in和not in
msg='alex say love'
print('a'in msg)
print('b'in msg)
5.移除空白strip

msg=' alex say love***'
print(msg.strip())
print(msg.strip('*'))
6.切分split
info='egon:123:ad'
res=info.split(':')
print(res)
7.循环
msg='else'
i=0
while i<len(msg):
print(msg[i])
i+=1
msg='alex'
for item in msg:
print(item)










原文地址:https://www.cnblogs.com/yftzw/p/8603363.html