一个帅豆芽

导航

python的常用语句

# if 语句  满足条件
# age=503
# print("开始")
# if age>=66:
# print("我已经成年了")
# print("结束")
# if--else 语句 不满足条件
# age=503
# print("开始")
# if age>=66:
# print("我已经成年了")
# print("结束")
# else:
# print("我未成年")
# print("结束")
# eg:
# liu=("拒绝")
# if liu==("同意"):
# print("开心,她同意了")
# print("有女朋友了,美滋滋~~~")
# else:
# print("难过,她说我是个好人")
# print("没事,她至少夸我了")
# elif 必须和 if 一起使用

# if-elif语句 如果判断语句大于两种
# score=70
# if 100>=score>=90:
# print("优秀")
# elif 80<=score<=90:
# print("良好")
# elif 70<=score<=80:
# print("一般")
# elif 60<=score<=70:
# print("是个人")
# else:
# print("垃圾")
# elif score<=60:
# print("辣鸡")

# if 嵌套、
# ticket = 1
# knife_length=6
# if ticket ==1:
# print("有票,可以进站")
# if knife_length<=6:
# print("通过安检")
# print("终于可以见到她了,美滋滋~~~")
# if knife_length>6:
# print("没有通过安检")
# print("刀子长度超过规定,等待警察处理")
# else:
# print("没有车票,不能进站")
# print("不能见面了,难过")
# eg:
# 猜拳游戏
# import random
# player_input=input("请输入(0剪刀,1石头,2布:)")
# 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)


# 循环语句
# while and for 循环
# while 循环
# while 条件表达式:
# 条件满足,执行语句
# a=1
# while a==1: # 表达式永远是true
# number =int(input("请输入一个数字 :"))
# print("你输入的数字是:",number)
# print("拜拜")


# for 循环 特点 可以遍历任何序列,
# for 变量 in 序列:
# 循环语句
# 假设一个列表[0,1,2,3,4,5,6]:
# for i in [0,1,2,3,4,5,6]:
# print(i)


# py 的内置range函数,可以生成一个数字序列
# for i in range(start,end):
# 执行循环语句
# for i in range(0,10):
# print(i)
# i=0
# sum_result=0
# while i <101:
# if i % 2==0:
# sum_result += i
# i += 1
# print("1~100之间的偶数之和为:%s"% sum_result)
# while 嵌套
# i=1
# while i < 6:
# a=0
# while a < i:
# print("* ",end='')
# a +=1
# print("/n")
# i +=1
# 九九乘法表
# i=1
# while i<10:
# a=1
# while a<=i:
# print("%d*%d=%-2d "%(i, a, a*i), end=' ')
# a+=1
# print()
# i +=1

# break 语句 结束整个循环(当前循环体)
# for i in range (5): 在自己想要的地方结束循环
# print('*')
# if i==3:
# break
# print(i)
# continue 语句 结束本次循环,紧接下一次循环
# for i in range(9):
# i+=1
# print("------")
# if i==6:
# continue
# print(i)
# break and continue 只能在循环中使用,不能单独使用,用于嵌套循环时,只对最近一层循环作用

# pass语句
# 空语句 保持结构完整性 不做任何事情 占位语句
# for letter in 'liu':
# if letter == 'u':
# pass
# print("执行 pass块")
# print('当前字母 :',letter)
# print("再见") ****

# else 语句 循环后执行 break语句会跳过 else 语句块
# count=0
# while count < 6:
# print(count, " is less than 5")
# count = count + 1
# else:
# print(count, "is not less than 5")

# eg:
# 1,
# a=0
# for i in range(100):
# if (i%10):
# continue
# a+=i
# print(a) 450
# 2
# x=10,
# y=20,
# z=30,
# if x<y :
# z=x
# x=y
# y=z
# print(x,y,z)


posted on 2021-11-04 20:42  一个帅豆芽  阅读(181)  评论(0编辑  收藏  举报