首页 > 其他分享 >Day03 - 关键字和循环

Day03 - 关键字和循环

时间:2023-01-23 16:33:32浏览次数:38  
标签:Day03 test 关键字 while 循环 字符串 print def

0. 倒序

'''
实现 传入一个数字,来控制输出的次数,倒序输出数字
'''


# 定义一个函数
def test_func(n):
    i = n
    while i > 0:
        print(i)
        i -= 1


test_func(10)

1. break

1.break 只能用在循环里
2.break 的作用是用来结束循环,不管循环还有多少次
'''
break 用来中断循环
'''


def test_func():
    i = 1
    print('enter loop')
    while i <= 100:
        print(i)
        if i % 2 == 0:
            break
        i += 1
        # break
    print('exit loop')





# break  #SyntaxError: 'break' outside loop

test_func()

2. continue

1. continue 也只能用在循环里
2. continue 的作用是用来结束本次循环,不管循环体中还有多少代码没有执行,进入下一次循环
'''
continue 使用
'''


def test_func():
    i = 1
    while i <= 5:
        i += 1
        print('hello')
        continue
        print('world')


test_func()

3. while 循环嵌套

'''
循环嵌套
'''


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()

4 . 打印正方形

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
'''
循环嵌套练习

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

'''

# test_func1
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()

5. 打印三角形

*
**
***
****
*****
'''
   *
   **
   ***
   ****
   *****

'''


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()

6. 打印九九乘法表

1*1=1
1*2=2  2*2=4
1*3=3  2*3=6 3*3=9

...
1*9=9 ..... 9*9=81
'''
打印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()

7. for-in 循环& range

'''
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()

8. 循环后面有else的场景

'''
循环后面有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()

9. 字符串定义和下标

字符串的概念: 以引号引起来的若干字符 
定义方式:


下标:
'''
字符串的定义和下标访问
'''

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






# defined_str()
# defined_str_space()
use_index_access_str()

10. 字符串的遍历

遍历: 依次访问字符串中的每一个字符

方式一:
	for c in s:
		print(c)

方式二:
	字符串的长度 = len(字符串)
	for i in range(len(字符串)):
		print(s[i])

方式三:
	i = 0
	while i < len(字符串):
		print(s[i])
		i += 1
'''
字符串的遍历
遍历:依次取到字符串中的每一个字符
'''


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)

11. 字符串切片

''[::-1]
s = ''
''[::-1]
'''
字符串切片


现有一个字符串,想得到 该字符串的第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])

'''
练习作业:
自己实现字符串逆序
'''

12. 字符串常用方法

a.查找_替换_统计
	find()  掌握
	rfind() 了解
	index() 了解
	rindex() 了解 
	replace() 掌握
	count()	掌握

b. 分割_连接
	split()	掌握
	partition() 了解
	rpartition() 了解
	splitlines() 了解
	join() 掌握

c. 判断
	startswith() 掌握
	endswith()   掌握
	isupper()	 了解 
	islower()
	isdigit()
	isalpha()
	isalnum()
	isspace()


d. 转换 (了解)
	upper()
	lower()
	title()
	capitalize()

e. 对齐(了解)
	center()
	rjust()
	ljust()

f. 去除空白(了解)
	strip()
	lstrip()
	rstrip()

API 应用程序接口文档
Application InterfaceDay03 - 关键字和循环

标签:Day03,test,关键字,while,循环,字符串,print,def
From: https://www.cnblogs.com/lehoso/p/17065274.html

相关文章

  • 汇编语言.text段.global关键字
    .text段表明是代码段,是用来写你的逻辑代码的段.global关键字用来让一个符号对链接器可见,可以供其他链接对象模块使用。.global_start让_start符号成为可见的标示符,这样链......
  • Day04-if分支+循环语句
    一、if判断语句1、语句结构if条件语句:    满足条件运行的代码1    满足条件运行的代码22、if嵌套结构if条件语句1:  满足条件运行的代码1满足条件运......
  • JavaScript 循环引用
    JavaScript中的循环引用是指两个或多个对象之间相互引用的情况。这种情况下,这些对象就不能被垃圾回收机制正常回收,会导致内存泄漏。循环引用通常发生在对象之间相互包含......
  • 06 While循环详解
    While循环packagecom.zhan.base_2;publicclassTest06_While{publicstaticvoidmain(String[]args){inti=0;intn=0;intsu......
  • 07 do···while循环
    do···while循环代码packagecom.zhan.base_2;publicclassTest07_DoWhile{publicstaticvoidmain(String[]args){//输出1+2+3+···+100......
  • 08 For循环详解
    For循环知识点代码packagecom.zhan.base_2;publicclassTest08_For{publicstaticvoidmain(String[]args){//对比for循环与while循环......
  • 10 增强 for 循环
    增强for循环packagecom.zhan.base_2;publicclassTest10{publicstaticvoidmain(String[]args){int[]numbers={10,20,30,40,50};//定义一个......
  • java for循环改造多线程-线程池原理
    通过ThreadPoolExecutor类自定义:publicThreadPoolExecutor(intcorePoolSize,intmaximumPoolSize,l......
  • JavaScript 中 this 关键字的作用和如何改变其上下文
    一、this关键字的作用JavaScript中的this关键字引用了所在函数正在被调用时的对象。在不同的上下文中,this的指向会发生变化。在全局上下文中,this指向全局对象(在浏......
  • C++概述、选择结构、循环结构
    目录1C++概述1.1计算两个整数相加之和1.2计算三个整数相加之和2选择结构2.1小老鼠走迷宫1(if语句)2.2小老鼠走迷宫1(if语句)(多个单分支结构)2.3小老鼠走迷宫2(switch语句)2......