python语法学习二——条件结构

if语句

#结构,if表达式后必须要有冒号:必须要缩进
if expression:
    expr_true_suite
#代码
sd1=3
sd2=3
if(sd2==sd2):
    print(sd1*sd2)

else语句

#结构,if,else表达式后必须要有冒号:必须要缩进
if expression:
    expr_true_suite
else:
    expr_false_suite
#代码
sd1=int(input('1:'))
sd2=int(input('2:'))
if(sd1==sd2):
    print(sd1*sd2)
else:
    print(sd1)

elif语句

#语法
if expression:
    expr_true_suite
elif expression2:
    expr2_true_suite
、、、
elif expressionN:
    exprN_true_suite
else:
    none_of_the_above_suite
#代码
sd1=int(input('index:'))
if sd1==1:
    print('circle')
elif sd1==2:
    print('oval')
else:
    print('rectangle')

条件嵌套

#条件里面套条件
sd1=int(input('index1:'))
if sd1==1:
    print('circle')
elif sd1==2:
    sd2=int(input('index2:'))
    if sd1==sd2:
        print(sd1)
    else:2
        print(sd2)
else:
    print('rectangle')

猜数字游戏

from random import randint
x=randint(0,300)
digit=int(input('one:'))
if digit==x:
   print('bingo')
elif digit>x:
   print('too large')
else:
   print('too small')
#改写成条件嵌套
from random import randint
x=randint(0,300)
digit=int(input('one:'))
if digit==x:
   print('bingo')
else:
    if digit>x:
       print('too large')
    else:
       print('too small')

简单表达式

#条件表达式
#形式x if else y
x=5
t=1 if x>=0 else 0

range 函数

#语法
range(start,end,step=1)#起始值包含,终止值不包含,步长不能为0
range(start,end)
range(end)
>>> list(range(3,11,2))
[3, 5, 7, 9]
>>> list(range(3,11))
[3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(11))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值