提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
python循环使用
前言
提示:这里可以添加本文要记录的大概内容:
循环语句 while 、break、continue、 for-in、 循环& range、 for-else的应用场景、 字符串定义和下标、 字符串的遍历、字符串切片、字符串常用方法、
提示:以下是本篇文章正文内容,下面案例可供参考
一、break的作用
- break 只能用在循环里
- break 的作用是用来结束循环, 不管循环还有多少次
while True:
print('hello')
break
print('World')
print('over')
二、continue的作用
- continue 也只能用在循环里
- continue 的作用是结束本次循环
def test_func():
i = 1
while i <= 5:
i += 1
print('hello')
continue
print('world')
test_func()
三、 循环嵌套
def test_func():
i = 1
while i < 3:
# 被嵌套的循环
j = 1
while j < 5:
print(f'{i} --- {j}')
# break
continue
j += 1
i += 1
test_func()
四、 循环嵌套练习
'''
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
'''
def test_func1():
print('* * * * *')
print('* * * * *')
print('* * * * *')
print('* * * * *')
print('* * * * *')
def test_func2():
i = 1
while i <= 5:
print('* ' * 5)
i += 1
def test_func3():
i = 0
while i < 5:
j = 0
while j < 5:
print('*', end=' ')
j += 1
# 由于上面内循环中改变了输出函数的结束符号,所以不会换行
# 在内循环外面,加入一个输出,让内循环执行完成后,加入一个换行
print()
i += 1
# 思考题目:不使用修改print结束标记的方式 ,实现图形的输出
test_func1()
test_func2()
test_func3()
五、循环嵌套练习2
'''
*
**
***
****
*****
'''
def test_func1():
i = 0
while i < 5:
j = 0
while j < 5:
print('*', end='')
if j == i:
break
j += 1
print()
i += 1
def test_func2():
i = 0
while i < 5:
j = 0
while j <= i:
print('*', end='')
j += 1
print()
i += 1
def test_func3():
i = 0
while i < 5:
# 定义一个空字符串,用来拼接使用
s = ''
j = 0
while j <= i:
s += '*' # s = s + '*'
j += 1
print(s)
i +=1
'''
输出下列图形:
1
12
123
1234
12345
输出行数由参数控制
'''
test_func1()
test_func2()
test_func3()
六、打印99乘法表
def nine_nine_table():
i = 1
while i <= 9:
j = 1
while j <= i:
print('%d*%d=%-3d' % (j, i, j*i), end=' ')
j += 1
print()
i += 1
nine_nine_table()
七、for-in 循环使用方式
def test_func1():
# 得到字符串中的所有字符
for c in 'abcdefg':
# 将小写字母变成大写输出
print(c.upper())
# for-in 循环如果需要计数,需要配合 range() 实现
# range 有两个参数,参数一是起始值 ,参数二是终止值
# 得到一个数字区间,是一个左闭右开区间, [start, end)
# 如果只给一个参数,那么默认是终止值 ,起始值默认是 0
def test_func2():
for i in range(10, 20):
print(i)
def test_func3():
# 如果需 要计数,但又不需 要用到这个数字时,可以没有循环变量
for _ in range(5):
print('hello', _)
test_func1()
test_func2()
test_func3()
八、循环后面有else的场景
def test_func1(s):
for c in s:
print(c)
if c == 'A':
print('字符串里有A')
break
else:
print('没有A')
def test_func2():
i = 0
while i < 10:
print(i)
if i == 50:
print('中断结束循环')
break
i += 1
else:
print('程序没有被中断,正常结束')
test_func1('helAlo')
test_func2()
九、字符串的定义和下标访问
def defined_str():
# 空字符串
print('')
print("")
print('''''')
print("""""")
def defined_str_space():
# 带一个空格的字符串
print(' ')
print(" ")
print(''' ''')
print(""" """)
# 定义一个函数,用来使用下标访问字符串
def use_index_access_str():
s1 = 'abcde'
s2 = '01234'
print(s1[0])
print(s1[2])
print(s1[4])
print(s2[0])
print(s2[2])
print(s2[4])
# print(s1[5]) # IndexError: string index out of range # 字符串下标越界
# 字符串不允许通过下标来修改字符串中的内容
# 字符串是不允许修改的,因为字符串是一个不可变对象
# s2[2] = '9' # TypeError: 'str' object does not support item assignment
十、字符串的遍历
'''
遍历:依次取到字符串中的每一个字符
'''
s = 'Hello World'
# 遍历方式一 for-in
for c in s:
print(c, c.upper())
print('*' * 20)
# 遍历方式二 - for-in-range-配合下标
# len() 函数 是 length 的简写, 用来获取参数的长度(元素个数)
length = len(s)
for i in range(0, length):
c = s[i]
print(c, c.upper())
print('*' * 10)
# 遍历方式三 while-配合下标
i = 0
while i < length:
c = s[i]
print(c, c.upper())
i += 1
# s = 'ab'cd'ef'
# s = 'ab\'cd\'ef'
# s = 'ab"cd"ef'
# s = 'ab """cd""" ef'
s = 'ab \'''cd''\' ef'
sql = '''select * from table while name = "tom"''';
print(s)
十一、字符串切片
'''
现有一个字符串,想得到 该字符串的第3到5个字符
'''
def str_slice(src_s, start,stop):
# 要返回的切片后的结果
s = ''
i = 0
while i < len(src_s):
if i >= start and i < stop:
s += src_s[i]
i += 1
return s
# print(str_slice('0123456789', 3, 5))
# 字符串切片
s = '0123456789'
print(s[0:5:1])
print(s[0:5:2])
print(s[3:6]) # 默认步长可以不写,默认为1
print(s[:5]) # 开始索引也可以不写,默认从头开始
print(s[5:]) # 结束也可以不写,默认到最后
print(s[:]) # 全默认,默认截取整串
print(s)
print(s[10:20]) # 切片时不会出现下标越界错误
# 切片的下标还可是以负数
# 负数是,是从右向左切片,起始下标为 -1
print(s[-1:-5])
print(s[-1:-5:-1])
# 特殊需要记住的切片方式
# 使用切片实现字符串逆序
print(s[::-1])