Flow control statements if

if

He said: if. Is the keyword in python.

if True if it is true, to tell you it's true

1. Single if

format:

If space colon condition

Indent results

2.if else (must choose one)

format:

If space colon condition

Indent results

Otherwise colon

Indent results

n = input("请输入数字:")

if 3<int(n):

     print(1)

else:

     print(2)

print(6)

3.if elif elif (a multiple choice or zero)

format

If space colon condition

Indent results

If the conditions of the colon and then a space

Indent results

If the conditions of the colon and then a space

Indent results

print(110)
if 3<2: # 如果
    print("A")
elif 3>8: # 再如果
    print("B")
elif 5<0:
    print("C")
print(112)
#输出 110 112

4.if elif else (one of many)

format

If space colon condition

Indent results

If the conditions of the colon and then a space

Indent results

Otherwise, the space colon condition

Indent results

if 3 == 2:
    print(1)
elif 3<2:
    print(3)
elif 3>10:
    print(4)
else:
    print(9)
#输出 9

5.if if if (multiple choice)

format:

If space colon condition

Indent results

If space colon condition

Indent results

If space colon condition

Indent results

if 3>2:
    print("A")
print(456)
if 3<6:
    print("B")
 # 输出 A 456 B

6.if nesting

format:

If space colon condition

If the conditions of the colon space indentation

Indent results

Otherwise colon out

Indent results

Otherwise colon protrudes out

Indent results

sex = "女"
age = 35
if sex == "女":
    if age == 35:
        print("进来坐一坐")
    else:
        print("你去隔壁找春生")
else:
    print("你去对门找alex")

Guess you like

Origin www.cnblogs.com/shengjunqiye/p/11355216.html