python100个常用语句,Python常用语句

Python常用语句

判断语句

if语句

age = 30

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

if age >= 18:

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

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

f2ea56b6875cce1441c7f2517ea6db45.png

age = 15

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

if age >= 18:

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

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

e148d4e90ffeadd565f6492199365f4c.png

注:

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

Python中没有switch-case语句

if-else语句

ticket = 1

if ticket == 1:

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

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

else:

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

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

b222b18edd18c1df6d8aaf1b60f618ef.png

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

2b9cb5cd278760a5566e93fb4d968df2.png

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

sex = 9   # 1为男 0为女

if sex == 1:

print("男性")

elif sex == 0:

print("女性")

else:

print("第三性别")

a0f8f0fa6401e8b76d61cd7ff3ea8d40.png

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

31f8c7589fbc472d8f544c4bdeb59946.png

90a35d491776b4c8c6cc234bab9e7cc0.png

c5b07e50ddb93176b2fad3756797372c.png

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)

75452f1791604032f28f8e12a4636bed.png

1922c0325036ea8f86c6c89b5155f913.png

e7350d3e4f8a983699a453736f03ea4b.png

二、循环语句

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)

3d6016403ddb1cf46a3a1656710523dd.png

# 打印图形

i = 1

while i < 6:

j = 0

while j < i:

print("*", end='')

j += 1

print("\n")

i += 1

5cc0427736ffda9d8b3034eee9134175.png

# 打印九九乘法表

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

bf2779f308e7add62b86dee82b0b5cf0.png

三、其它语句

for i in range(5):

print("--------------")

print(i)

1f8140e2b6f43ec28a3d642e1148f22f.png

820258707e2b0a07676b4a90b9a75384.png

a82924deca1ed6bf05e456b0b4b6c1fa.png

注意:

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

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

pass语句

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

for letter in 'Runoob':

if letter == 'o':

pass

print("执行 pass 块")

print("当前字母:", letter)

print("Good bye!")

35e6d395a1611d8586972c96fbea1a4a.png

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

count = 0

while count < 5:

print(count, "is less than 5")

count += 1

else:

print(count, "is not less than 5")

b9d8b582dfb381eecaf5e043f8269ddf.png

练习题

一、填空题

在循环体中使用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 xB.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)

901c1dced1301d941913de8d00b67619.png

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

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

if a>0:

print("a是一个正数")

elif a<0:

print("a是一个负数")

else:

print("a等于0")

73750afb3f33c2760f490e86b73de38c.png

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

663a8bda16916594efc89607c30d7e69.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值