python if else写在一行_Python外壳:代码结构

42627815-3b55-4010-b6a0-993b6f664637

欢迎关注「issnali」感谢!

“一起学习,一起成长!”

1. 使用#注释

概述:在Python中使用#字符标记注释,从#开始到当前行结束的部分都是注释。

注释可以单独一行,也可与代码同行,同行放在代码后面。

特殊说明:

  • Python没有多行注释的符号。需要明确地在注释部分的每一行开始处加上一个#。
  • 如果#出现在文本串中,将回归普通字符#的角色

>>>print("No comment:quotes make the # harmless.")

No comment:quotes make the # harmless.

2. 使用连接

程序在合理的长度下是易读的。一行程序的(非强制性)最大长度建议为80个字符。

如果在该长度下写不完代码,可以使用连接符(反斜线)。把它放在一行的技术位置,python将其解释为同一行。

3. 使用if、elif和else进行比较

例子:多层判断语句的嵌套

>>>furry=True

>>>small=True

>>>if furry:

... if small:

... print("It's a cat.")

... else:

... print("It's a bear!")

... else:

... if small:

... print("It's a skink!")

... else:

... print("It's a human.Or a hairless bear.")

如果要检查超过两个条件,可以使用if、elif(即else if)和else:

>>>color="puce"

>>>if color=="red"

... print("It's a tomato.")

... elif color=="green":

... print("It's a green pepper.")

... elif color=="bee purple":

... print("I don't know what it is,but only bees can see it")

... else:

... print("I've never heard of the color

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值