for循环,数字类型,字符串类型

Posted on 2018-11-21 17:14  大白不白  阅读(192)  评论(0编辑  收藏  举报

for 循环:
  l=['a','b','c']
  for i in l :
  print(i)


while循环和for循环
  while循环:条件循环,循环的次数取决于条件何时为False
  for循环:循环的次数取决于数据的包含的元素的个数


for循环专门用来取值,在循环取值方面比while循环要强大,以后但凡遇到循环取值的场景,就应该用for循环


for+break
  names=['a','b','c','d']
  for name in names:
  if name == 'c':
    break
  print(name)

  for+continue
  for+else


数字类型,整型int
  用途:记录年龄,等级,号码等
  定义方式:
  age=10 age=int(10)
  类型转换
  十进制转二进制
  bin(13)
  转八进制
  oct(13)
  转16进制
  hex(13)
  存一个值
  不可变


浮点型float
  用途:记录身高,体重,薪资等
  定义
  salary=13.1
  类型转换
  存一个值
  不可变

 

字符串类型str
  用途:描述性质的状态,名字,性别等
  定义方式
  msg=‘Hello world’
  类型转换:可以把任意类型专场字符串类型
  优先掌握的操作:
  按索引取值
  print(msg[0])
  print (msg[-1])
  切片
  msg[0:5]
  msg[0:5:1]
  msg[-1:-5:-1]
  长度len
  len(msg)
  成员运算in和not in
  print ('ho' in msg)
  print ('ho' not in msg)
  移除空白strip
  移除字符串左右两边的某些字符
  print(‘ hello ’.strip(‘ ’))
  切分split
  把有规律的字符串切成列表从而方便取值
  info=‘egon:18:180:180’
  res=info.split(':')分成列表
  print(res[1])

  s1=':'.join(res)组成字符串
  print(s1)
  循环
  for i in 'hello':
  print(i)

  存一个值
  有序
  不可变

 

#strip
name='*egon**'
print(name.strip('*'))
print(name.lstrip('*'))
print(name.rstrip('*'))

#lower,upper
name='egon'
print(name.lower())
print(name.upper())

#startswith,endswith
name='alex_SB'
print(name.endswith('SB'))
print(name.startswith('alex'))

#format的三种玩法
res='{} {} {}'.format('egon',18,'male')
res='{1} {0} {1}'.format('egon',18,'male')
res='{name} {age} {sex}'.format(sex='male',name='egon',age=18)

#split
name='root:x:0:0::/root:/bin/bash'
print(name.split(':')) #默认分隔符为空格
name='C:/a/b/c/d.txt' #只想拿到顶级目录
print(name.split('/',1))

name='a|b|c'
print(name.rsplit('|',1)) #从右开始切分

#join
tag=' '
print(tag.join(['egon','say','hello','world'])) #可迭代对象必须都是字符串

#replace
name='alex say :i have one tesla,my name is alex'
print(name.replace('alex','SB',1))

#isdigit:可以判断bytes和unicode类型,是最常用的用于于判断字符是否为"数字"的方法
age=input('>>: ')
print(age.isdigit())

 

Copyright © 2024 大白不白
Powered by .NET 8.0 on Kubernetes