控制流程之for循环

基本语法

for 变量名(会拿到容器类元素的每一个值) in 容器类元素:
print(变量名)
for i in range(5):
print(i) # 打印结果:
0
1
2
3
4

while可以循环一切事物

for 循环提供了一种手段,不依赖索引取值

for+break

for i in range(1, 101):    # 取1-100,range顾头不顾尾
if i == 50:
break # 中断循环,只取到1-49
print(i)

for+continue

for i in range(1, 101):
if i == 51:
continue # continue跳出本次循环,不执行下面代码
print(i)

for+else

for i in range(1, 101):
if i == 51:
break
print(i)
print('没有被break中断我就出来')

for循环不被break终止就执行else后面的代码,否则就不执行

for循环打印lodaing

import time
print('Loading', end='')
for i in range(10):
print('.', end='')
time.sleep(0.2)
print(1, end='*')   # print后面的end控制打印的状态
print(1, end='*')
print(1, end='*')
print(1, end='*') # 打印结果:1*1*1*1*

数字类型内置方法

整型 int

作用:描述年龄

定义方式:x = 10

使用方法:+ - * / % // **

有序or无序:数字类型没有有序无序一说

可变不可变:(值变id值变,为不可变;值变id值不变,为可变)整型不可变

浮点型 float

作用:描述薪资

定义方式:x = 2.1

使用方法:+ - * / % // **

有序or无序:数字类型没有有序无序一说

可变不可变:(值变id值变,为不可变;值变id值不变,为可变)浮点型不可变

字符串内置方法

作用:描述姓名

定义:单引号 / 双引号 / 三单引号 / 三双引号

使用方法:

优先掌握:

  1. 索引取值
s = 'hello world'
print(s[1]) # 取到e
  1. 切片
s = 'hello world'
print(s[0:4]) # hell
print(s[4:0:-1]) # olle
print(s[:]) # hello world
  1. 长度 len
s = 'hello world'
print(len(s)) # 字符长度为11
  1. 成员运算 in / not in
s = 'hello world'
print('hello' in s) # True
print('hello' not in s) # False
  1. 移除strip
s = '     hello world    '
print(s.strip()) # hello world 默认去除两端空白 s = ' **hello world **'
print(s.strip('* ')) # hello world
  1. split切分
s = 'cwz|19|180|140'
print(s.split('|')) # ['cwz', '19', '180', '140']
  1. 循环
s = 'cwz123'
for i in s:
print(i) # 打印结果:
c
w
z
1
2
3

需要掌握:

  1. startswith / endswith 以...开始 / 以...结束
s = 'hello world'
print(s.startswith('hello')) # True
print(s.endswith('world')) # True
  1. lstrip或rstrip
s1 = '**hello world**'
s2 = '**hello world**'
print(s1.lstrip('*')) # hello world**
print(s2.rstrip("*")) # **hello world
  1. lower或upper

s3 ='HellO WOrLD'

print(s3.lower())

print(s3.upper())


4. rsplit ```python
s4 = 'cwz|123|neo|140'
print(s4.split('|',1)) # ['cwz', '123|neo|140']
print(s4.rsplit('|',1)) # ['cwz|123|neo', '140']
  1. join
s4 = 'cwz|123|neo|140'
s_list = s4.split('|')
print('*'.join(s_list) # cwz*123*neo*140
  1. replace
s5 = 'hello world'
print(s5.replace('h', '6')) # 6ello world
  1. isdigit和isalpha
s6 = '122324'
print(s6.isdigit()) # True 判断是否全为数字
print(s6.isalpha()) # False 判断是否全为字母

其他操作:

  1. find / rfind / index / rindex / count
s = 'my name is cwz, age is 20'

print(s.find('m'))  # 0  find查找索引值,找到第一个就不往后找了,找不到返回-1

print(s.rfind('s'))    # 21  rfind从右往左查找索引值

print(s.index('s'))  # 9  返回索引值

print(s.rindex('s'))  # 21

print(s.count('s'))  # 2  计数
  1. center()、ljust()、rjust()、zfill()
s = 'hello world'

print(s.center(20,'*'))   # ****hello world*****  居中

print(s.ljust(20, '*'))   # hello world*********  居左

print(s.rjust(20, '#'))   # #########hello world  居右

print(s.zfill(20))   # 默认用0填充    000000000hello world
  1. expandtabs()
s = 'aaa\tbbb'
print(s.expandtabs()) # aaa bbb 默认制表符8个字符
  1. capitalize()、swapcase()、title()
s = 'HeLlO WoRlD'

print(s.capitalize())   # Hello world  首字母大写,其他小写

print(s.swapcase())   # hElLo wOrLd  大小写转换

print(s.title())    # Hello World   每个单词首字母大写

有序or无序: 字符串可以索引,有序

可变or不可变:字符串值变id值改变,--》字符串不可变

列表内置方法

作用:[]内存储多个值,各元素以逗号隔开

定义方式:lt = [1,23,4]

使用方法:

优先掌握:

  1. 索引取值 / 索引修改值
lt = [1,2,3,4,5]
print(lt[1]) # 2 lt[0] = 66
print(lt) # [66, 2, 3, 4, 5]
  1. 切片
lt = [1,2,3,4,5]
print(lt[0:4]) # [1, 2, 3, 4]
  1. 成员运算
lt = [1,2,3,4,5]
print(1 in lt) # True print(0 in lt) # False
  1. for 循环
lt = [1,2,3,4,5]
for i in lt:
print(i)
  1. append 追加值
lt = [1,2,45]
lt.append([12,3]) # [1, 2, 45, [12, 3]]
print(lt)
  1. del 删除值
lt = [1,2,3,4,5]
del lt[4]
print(lt) # [1,2,3,4]
  1. len
lt = [1,23,4,[8,5],'www']
print(len(lt)) # 5

需要掌握:

  1. sort
# sort
lt = [1,23,8,9,4,21,412]
lt.sort() # 排序
print(lt) # [1, 4, 8, 9, 21, 23, 412]
  1. reverse
# reverse 反转
lt = [9,23,1,4,0,8]
lt.reverse() # 列表反转
print(lt) # [8, 0, 4, 1, 23, 9]
  1. remove
# remove
lt = [9,23,1,4,0,8]
lt.remove(9) # 按值删除
print(lt) # [23, 1, 4, 0, 8]
  1. pop
lt = [9,23,1,4,0,8]
lt.pop(1) # 按索引删除值
print(lt) # [9, 1, 4, 0, 8]
  1. extend
# extend
lt1 = [9,23,1,4,0,8]
lt2 = [1111,2222]
lt1.extend(lt2) # 扩展列表
print(lt1) # [9, 23, 1, 4, 0, 8, 1111, 2222]
  1. copy
# copy
print([1,23,4,0].copy()) # [1, 23, 4, 0]
  1. clear
# clear
lt = [9,23,1,4,0,8]
lt.clear() # 清空列表
print(lt) # []
  1. count
# count
lt = [9,23,1,4,0,1,23,4]
print(lt.count(1)) # 计数 2
  1. index
# index
lt = [9,23,1,4,0,8]
print(lt.index(0)) # 返回索引值 4
  1. insert
# insert
lt = [9,23,1,4,0,8]
lt.insert(0, 777) # 按索引前插入值
print(lt) # [777, 9, 23, 1, 4, 0, 8]

有序or无序:能索引取值,列表有序

可变or不可变:列表可变

05-11 17:57