Day8 for循环 字符串类型

一、流程控制之for循环

1.1 什么是for循环

循环就是重复做某件事,for循环是python提供第二种循环机制

1.2 为何要有for循环

论上for循环能做的事情,while循环都可以做
之所以要有for循环,是因为for循环在循环取值(遍历取值)比while循环更简洁

1.3 如何用for循环

for 变量名 in 可迭代对象:# 可迭代对象可以是:列表、字典、字符串、元组、集合
    代码1
    代码2
    代码3
    ...

1. 4 for基本使用之循环取值

 案例1:列表循环取值
 简单版
 l = ['alex_dsb', 'lxx_dsb', 'egon_nb']
 for x in l:  # x='lxx_dsb'
     print(x)

 复杂版:
 l = ['alex_dsb', 'lxx_dsb', 'egon_nb']
 i=0
 while i < 3:
     print(l[i])
     i+=1


 案例2:字典循环取值
 简单版
 dic={'k1':111,'k2':2222,'k3':333}
 for k in dic:
     print(k,dic[k])

 复杂版:while循环可以遍历字典,太麻烦了

 案例3:字符串循环取值
 简单版
 msg="you can you up,no can no bb"
 for x in msg:
     print(x)

 复杂版:while循环可以遍历字典,太麻烦了

总结:

1、相同之处:都是循环,for循环可以干的事,while循环也可以干
2、不同之处:
while循环称之为条件循环,循环次数取决于条件何时变为假
for循环称之为"取值循环",循环次数取决in后包含的值的个数

1.5for循环控制循环次数:range()

 range功能介绍
'''
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 
>>> range(1,9) # 1...8
[1, 2, 3, 4, 5, 6, 7, 8]
>>> 
>>> range(1,9,1) # 1 2 3 4 5 6 7 8 #括号里第一个数是起点,第二个数是终点(不包括在内),第三个数是间隔
[1, 2, 3, 4, 5, 6, 7, 8]
>>> range(1,9,2) # 1 3 5 7 
[1, 3, 5, 7]
'''

in后直接放一个数据类型来控制循环次数有局限性: 当循环次数过多时,数据类型包含值的格式需要伴随着增加。

 for i in range(30):
     print('===>')

1.6 for其他搭配

for循环嵌套:外层循环循环一次,内层循环需要完整的循环完毕
 for i in range(3):
     print('外层循环-->', i)
     for j in range(5):
         print('内层-->', j)

for+break: 同while循环一样

for+else:同while循环一样

for+continue:同while循环一样

补充:终止for循环只有break一种方案

1.7print的补充(了解)

# print('hello %s' % 'egon')
# 1、print之逗号的使用
# print('hello','world','egon')
# 2、换行符
# print('hello\n')
# print('world')
# 3、print值end参数的使用
# print('hello\n',end='')
# print('word')
print('hello',end='*')
print('world',end='*')

二、数据类型

2.1int类型

age = 10 # age=int(10)

类型转换:x=int(10)

2.2类型转换(了解)

2.2.1十进制转成其他进制

 10进制 -> 二进制
 11 - > 1011
 1011-> 8+2+1
 print(bin(11)) # 0b1011

 10进制 -> 八进制
 print(oct(11)) # 0o13

 10进制 -> 十六进制
 print(hex(11)) # 0xb
 print(hex(123)) # 0xb

2.2.2其他制转成其十进制

 二进制->10进制
 print(int('0b1011',2)) # 11

 二进制->8进制
 print(int('0o13',8)) # 11

 二进制->16进制
 print(int('0xb',16)) # 11

2.3float类型

salary=3.1 # salary=float(3.1)

类型转换:res=float("3.1")

2.4字符串类型

msg='hello' # msg=str('msg')

类型转换:res=str({'a':1})

2.4.1按索引取值(正向取+反向取) :只能取

msg='hello world'
 正向取
 print(msg[0])
 print(msg[5])
 反向取
 print(msg[-1])

 只能取
 msg[0]='H'

2.4.2切片:索引的拓展应用,从一个大字符串中拷贝出一个子字符串

msg='hello world'
 顾头不顾尾
 res=msg[0:5] #x
 print(res)   
 print(msg)   

 步长
 res=msg[0:5:2] # 0 2 4
 print(res) # hlo

 反向步长(了解)
 res=msg[5:0:-1]
 print(res) #" olle"

msg='hello world'
 res=msg[:] # res=msg[0:11]
 print(res)

 res=msg[::-1] # 把字符串倒过来
 print(res)

2.4.3长度len

msg='hello world'
print(len(msg))

2.4.4成员运算in和not in

 判断一个子字符串是否存在于一个大字符串中
 print("alex" in "alex is sb")
 print("alex" not in "alex is sb")
 print(not "alex" in "alex is sb") # 不推荐使用

2.4.5移除字符串左右两侧的符号strip

 默认去掉的空格
 msg='      egon      '
 res=msg.strip()
 print(msg) # 不会改变原值
 print(res) # 是产生了新值
# 默认去掉的空格 想去掉什么在()中加什么
# msg='****egon****'
# print(msg.strip('*'))

# 了解:strip只取两边,不去中间
# msg='****e*****gon****'
# print(msg.strip('*'))

2.4.6切分split:把一个字符串按照某种分隔符进行切分,得到一个列表

# # 默认分隔符是空格
 info='egon 18 male'
 res=info.split()
 print(res)
 # 指定分隔符
 info='egon:18:male'
 res=info.split(':')
 print(res)

 指定分隔次数(了解)
 info='egon:18:male'
 res=info.split(':',1)
 print(res)

2.4.7循环

 info='egon:18:male'
 for x in info:
     print(x)

2.5需要掌握

2.5.1strip,lstrip,rstrip

 msg='***lrr****'
 print(msg.strip('*'))
 print(msg.lstrip('*'))
 print(msg.rstrip('*'))

2.5.2lower,upper

 msg='AbbbCCCC'
 print(msg.lower())
 print(msg.upper())

2.5.3startswith,endswith

 print("alex is sb".startswith("alex"))
 print("alex is sb".endswith('sb'))

2.5.4format

2.5.5split,rsplit:将字符串切成列表

 info="egon:18:male"
 print(info.split(':',1)) # ["egon","18:male"]   #从左边向右
 print(info.rsplit(':',1)) # ["egon:18","male"]  #从右边向左

2.5.6join: 把列表拼接成字符串

 l=['egon', '18', 'male']
 res=l[0]+":"+l[1]+":"+l[2]
 res=":".join(l) # 按照某个分隔符号,把元素全为字符串的列表拼接成一个大字符串
 print(res)
 l=[1,"2",'aaa']
 ":".join(l)

2.5.7replace

 msg="you can you up no can no bb"
 print(msg.replace("you","YOU",))
 print(msg.replace("you","YOU",1))

2.5.8isdigit

 判断字符串是否由纯数字组成
 print('123'.isdigit())
 print('12.3'.isdigit())

 

2.6了解部分

2.6.1find,rfind,index,rindex,count

msg='hello egon hahaha'
 找到返回起始索引
 print(msg.find('e')) # 返回要查找的字符串在大字符串中的起始索引
 print(msg.find('egon'))
 print(msg.index('e'))
 print(msg.index('egon'))
 找不到
 print(msg.find('xxx')) # 返回-1,代表找不到
 print(msg.index('xxx')) # 抛出异常

 msg='hello egon hahaha egon、 egon'
 print(msg.count('egon'))

2.6.2center,ljust,rjust,zfill

 print('egon'.center(50,'*'))
 print('egon'.ljust(50,'*'))
 print('egon'.rjust(50,'*'))
 print('egon'.zfill(10))

2.6.3expandtabs

 msg='hello\tworld'
 print(msg.expandtabs(2)) # 设置制表符代表的空格数为2

2.6.4captalize,swapcase,title

 print("hello world egon".capitalize())将字符串的第一个字母变成大写,其他字母变小写
 print("Hello WorLd EGon".swapcase())用于对字符串的大小写字母进行转换
 print("hello world egon".title())所有单词都是以大写开始,其余字母均为小写

2.6.5is数字系列

2.6.6is其他

 print('abc'.islower())
 print('ABC'.isupper())
 print('Hello World'.istitle())
 print('123123aadsf'.isalnum()) # 字符串由字母或数字组成结果为True
 print('ad'.isalpha()) # 字符串由由字母组成结果为True
 print('     '.isspace()) # 字符串由空格组成结果为True
 print('print'.isidentifier())
 print('age_of_egon'.isidentifier())
 print('1age_of_egon'.isidentifier())
posted @ 2020-06-02 17:10  闲酒肆中听风吟  阅读(89)  评论(0)    收藏  举报