2018.09.14python学习第四天part1

本文详细介绍了Python中if判断的四种语法结构,包括基本的if语句、带有else的if语句、if的嵌套使用以及包含elif的多条件判断。通过实例展示了如何根据不同的条件执行相应的代码块,使计算机能够进行逻辑判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

流程控制之if判断

1.什么是if判断?(what)

判断一个if条件,如果成立则做什么,如果不成立则做什么

2.为什么要有if判断?(why)

为了让电脑像人一样有判断能力。(赋予它逻辑判断)

3.如何使用if判断?(how)

#语法一:

 if条件1:

  code1

  code2

  ........

age=18
if age==18:
    print("hello i love u")

#语法二:

if条件:

  code1

  code2

  .......

else:

  code1

  code2

  ..........

age=18
sex="female"
is_beautiful=True

if age>16 and age<30 and sex=="female" and is_beautiful:
    print("i love u)
else:
    print("阿姨你好")

#语法三:(if的嵌套)

if条件1:

  if条件2:

    code1

    code2

    .....

  code2

  code3

  .......

age=18
sex=female
is_beautiful=True
is_successful=True

if age>16 and age<30 and sex==female and is_beautiful:
    print("开始表白")
    if is_successful:
        print("在一起")
    else:
        print("我有钱")
else:
    print("阿姨再见")

#语法四:

if 条件1:

  code1

  code2

elif 条件2:

  code3

  code4

elif 条件3:

  code5

  code6

..........

else:

  code7

  code8

score = input(" your score")
score=int(score)
if score>90:
    print("very good")
elif score<=80:
    print("good")
elif score<=70:
    print("just so so")
else:
    print("bad")

 

ps:  1.注意if条件结束以后一定要用:

  2.如果一行代码太长的话可以用/分隔后另起一行

  3.同一子代码块中的代码缩进应该相同

 

转载于:https://www.cnblogs.com/hello-yuanjing/p/9646903.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值