三元操作符

small = x if x < y else y

如果x


assert 断言

当这个关键字后边的条件为假的时候,程序自动崩溃并抛出AssertionError的异常


while 条件:

    循环体


for循环

for 目标 in 表达式:

    循环体


favourite = "Test"

for i in favourite:

    print(i, end=" ")

→T e s t


range([start,] stop[, step=1])

range(5)

→range(0,5)


list(range(5))

→[0,1,2,3,4]


for i in range(2,9)

    print(i)

2

3

4

5

6

7

8


for i in range(5)

    print(i)

0

1

2

3

4


for i in range(1,10,2)        2为step,步径

    print(i)

1

3

5

7

9


两个关键的语句

break和continue

break:符合条件后,终止循环并跳出循环体

continue:符合条件后重新开始循环,条件不成立跳出循环


answer = "我要出去"

qustion = input("请输入恰当语句:")


while True:

    if qustion == answer:

        break

    qustion = input("输入正确后才能退出啊~~")


print("厉害了")


for i in range(10):

    if  i%2 != 0:

        print(i)

        continue

    i += 2

    print(i)


2

1

4

3

6

5

8

7

10

9