Python常用语句

  • 判断语句
  1. if语句
age = 30
print("-----if判断开始-----")
if age >= 18:
    print("-----成年-----")
print("-----if判断结束-----")

Python常用语句_python

 

 

age = 15

print("-----if判断开始-----")

if age >= 18:

    print("-----成年-----")

print("-----if判断结束-----")

Python常用语句_Python常用语句_02

 

 

注:

       每个if条件后要使用冒号(:)

Python中没有switch-case语句

 

  1. if-else语句
ticket = 1

if ticket == 1:

    print("-----可以上车-----")

    print("-----见到Ta-----")

else:

    print("-----不可以上车------")

    print("-----下次见Ta------")

 

Python常用语句_循环语句_03

 

 

  1. if-elif语句
score = 77

if score >= 90 and score <= 100:

    print('本次考试,等级为A')

elif score >= 80 and score < 90:

    print('本次考试,等级为B')

elif score >= 70 and score < 80:

    print('本次考试,等级为C')

elif score >= 60 and score < 70:

    print('本次考试,等级为D')

elif score >= 0 and score < 60:

    print('本次考试,等级为E')

Python常用语句_Python常用语句_04

注意:elif 必须和if一起使用,否则程序会出错

sex = 9   # 1为男 0为女

if sex == 1:

    print("男性")

elif sex == 0:

    print("女性")

else:

    print("第三性别")

Python常用语句_2d_05

 

4、	if嵌套

# ticket = 1
ticket = int(input("请输入一个正整数ticket:\n"))
# knife_length = 9
knife_length = int(input("请输入一个正整数knife_length:\n"))
if ticket == 1:
    print("-----有票可以进站-----")
    if knife_length < 10:
        print("通过安检")
        print("-----可以见到Ta-----")
    else:
        print("没有通过安检")
        print("-----刀子长度超过规定,等待警察处理,不可以见到Ta-----")
else:
    print("-----没票不可以进站------")
    print("-----下次见Ta------")

Python常用语句_2d_06

Python常用语句_python_07

Python常用语句_2d_08

5、	if案例——猜拳游戏

import random
player_input = input("请输入(0剪刀、1石头、2布:)\n")
player = int(player_input)
computer = random.randint(0, 2)
if (player == 0 and computer == 2) or (player == 1 and computer == 0) or (player == 2 and computer == 1):
    print("电脑出的拳头是%s,恭喜,你赢了!" %computer)

elif (player == 0 and computer == 0) or (player == 1 and computer == 1) or (player == 2 and computer == 2):
    print("电脑出的拳头是%s,打成平局了!" %computer)
else:
    print("电脑出的拳头是%s,你输了,再接再厉!" %computer)

Python常用语句_2d_09

Python常用语句_Python常用语句_10

Python常用语句_if语句_11

 

二、	循环语句
i = 0
while i < 3:
    i += 1
    print("6", end='')
# print("6")
# print("stop")

# count = 0
# while (count < 9):
#    print('The count is:', count)
#    count = count + 1

# print("Good bye!")

# i = 1
# while (i < 3):
#    print("hello world")
#    i += 1
# print("good bye!")

#for循环
for i in [0,1,2]:
	print(i)
# 计算1~100之间偶数和
i = 1
sum_result = 0
while i < 101:
    if i % 2 == 0:
        sum_result += i
    i += 1
print("1~100之间偶数和为:%s"% sum_result)

Python常用语句_if语句_12

# 打印图形
i = 1
while i < 6:
    j = 0
    while j < i:
        print("*", end='')
        j += 1
    print("\n")
    i += 1

Python常用语句_python_13

 

# 打印九九乘法表
i = 1
while i < 10:
    j = 1
    while j <= i:
        print("%d*%d=%-2d "%(i, j, i * j), end='')
        j += 1
    print("\n")
    i += 1

Python常用语句_2d_14

 

三、	其它语句
for i in range(5):
    print("--------------")
    print(i)

Python常用语句_python_15

Python常用语句_Python常用语句_16

Python常用语句_python_17

注意:

       (1)break/continue语句只能在循环中使用,不能单独使用。

       (2)break/continue语句用于嵌套循环时,只会对其所处的最近的一层循环起作用。

 

pass语句

       pass是空语句,出现是为了保持程序结构的完整性。不做任何事情,一般用作占位语句。

 

 


for letter in 'Runoob':
    if letter == 'o':
        pass
        print("执行 pass 块")
    print("当前字母:", letter)
print("Good bye!")

Python常用语句_循环语句_18

else语句只在循环完成后执行

 

count = 0
while count < 5:
    print(count, "is less than 5")
    count += 1
else:
    print(count, "is not less than 5")

Python常用语句_循环语句_19

 

  • 练习题

一、填空题

在循环体中使用break语句可以跳出循环体。

elif语句是else语句和if语句的组合。

在循环体中可以使用continue语句跳过本次循环后面的代码,重新开始下一次循环。

如果希望循环是无限的,我们可以通过设置条件表达式永远为True来实现无限循环。

Python中的pass表示的是空语句。

 

 

二、判断题

elif可以单独使用。(×)

pass语句的出现是为了保持进程结构的完整性。(√)

在Python中没有switch-case语句。(√)

每个if条件后面都要使用冒号。(√)

循环语句可以嵌套使用。(√)

 

三、选择题

1、下列选项中,会输出1,2,3三个数字的是(BC)。

A.

for i in range(3):

    print(i)

B.

for i in range(3):

    print(i + 1)

C.

a_list = [0,1,2]

for i in a_list:

    print(i + 1)

D.

i = 1

while i < 3:

    print(i)

    i = i + 1

 

2、阅读下面的代码:

sum = 0

for i in range(100):

    if(i%10):

        continue

    sum = sum + i

print(sum)

  上述程序的执行结果是(C)。

    A.5050 B.4950 C.450 D.45

 

3、已知x=10,y=20,z=30:以下语句执行后x,y,z的值是(C)。

if x < y:

    z=x

    x=y

    y=z

    A.10,20,30

    B.10,20,20

    C.20,10,10

    D.20,10,30

 

 

4、有一个函数关系如下所示:

x   y

x<0 x-1

x=0 x

x>0 x+1

下面程序段中,能正确表示上面关系的是(C)。

 

A.

y = x + 1

if x >= 0

    if x == 0:

        y = x

    else:

        y = x - 1;

B.

y = x - 1

if x != 0:

    if x > 0:

        y = x + 1

    else:

        y = x

C.

if x <= 0:

    if x < 0:

        y = x - 1

    else:

        y = x

else:

    y = x + 1

D.

y = x

if x <= 0:

    if x < 0:

        y = x - 1

    else:

        y = x + 1

 

5、下列Python语句正确的是(D)。

A.min=x if x<y else y

B.max=x>y?x:y

C.if(x>y) print x

D.while True:pass

 

 

四、简答题

简述Python中pass语句的作用。

Python中的pass是空语句,它的出现是为了保持程序结构的完整性。

pass不做任何事情,一般用做占位语句。

 

简述break和continue的区别。

break语句用于结束整个循环;

continue的作用是用来结束本次循环,紧接着执行下一次的循环。

 

 

 

  • 程序题

1、编写一个程序,使用for循环输出0~10之间的整数。

for i in range(11):

       print(i)

Python常用语句_python_20

 

2、编写一个程序,判断用户输入的数是正数还是负数。

 

a=int(input("请输入一个数:"))

if a>0:

    print("a是一个正数")

elif a<0:

    print("a是一个负数")

else:

print("a等于0")

 

Python常用语句_循环语句_21

 

3、编写一个程序,输出九九乘法表。

i=1
while i<10:
    j=1
    while j<=i:
        j+=1
        print("%d*%d=%-2d "%(i,j,i*j),end='')
    print("\n")
    i+=1

Python常用语句_循环语句_22