Python控制语句

Python 控制语句

利用布尔值做决定程序的执行方向.

布尔运算符 运算结果为True or Flase
< 小于
> 大于
<= 小于等于
>= 大于等于
== 等于
!= 不等于
and 逻辑与
or 逻辑或
not 逻辑非

1 选择语句 if

1.1 if 基本语句

if

Python中if语句基本语法:

if booleanExpression:
   # suite of Python statemants
# rest of the Python program
  • Python中缩进对代码块非常重要. 有相同的缩进量, 表示当前语句属于并列结构, 没有嵌套.
    # suite of Python statements# rest of the Python program 缩进不一样, 代码执行也不一样.
  • 注意if语句后的冒号:

1.2 if-else 语句

if_else
Python中if-else语句基本语法.

if booleanExpression:
   # suite executed for a True boolean result
else:
   # suite executed for a False boolean result 
# rest of the Pthon program
  • 注意if语句后和else语句后的冒号.

在Python命令行下实现:

>>> points = 10
>>> seconds = 20
>>> if points > seconds:
...:   print "Lead is safe."
...: else:
...:   print "Lead is not safe."

Lead is not safe.
  • 注意连续语句的输入. else:语句需要左顶格, else:if语句属于两个同级语句块, 所以缩进位置一样, 切勿将else:纳入if的语句块下.
  • 两次回车, 表示程序输入完成.

if_else

1.3 if-elif-else 语句

if_elif_else
Python中if-elif-else的基本语法:

if booleanExpression1:
    # suit1
elif booleanExpression2:
    # suit2
elif 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值