Python入门-条件语句

1. if 语句

import expression
if expression:
    expr_true_suite
  • if 语句的 expr_true_suite 代码块只有当条件表达式 expression 结果为真时才执行,否则将继续执行紧跟在该代码块后面的语句。
  • 单个 if 语句中的 expression 条件表达式可以通过布尔操作符 andornot 实现多重条件判断

【例子】

if 2 > 1 and not 2 > 3:
    print('Correct Judgement!')

# Correct Judgement!

2. if - else 语句 

if expression:
    expr_true_suite
else:
    expr_false_suite
  • Python 提供与 if 搭配使用的 else,如果 if 语句的条件表达式结果布尔值为假,那么程序将执行 else 语句后的代码。

【例子】

temp = input("猜一猜小姐姐想的是哪个数字?")
guess = int(temp) # input 函数将接收的任何数据类型都默认为 str。
if guess == 666:
    print("你太了解小姐姐的心思了!")
    print("哼,猜对也没有奖励!")
else:
    print("猜错了,小姐姐现在心里想的是666!")
print("游戏结束,不玩儿啦!")
def input(*args, **kwargs): # real signature unknown
    """
    Read a string from standard input.  The trailing newline is stripped.
    
    The prompt string, if given, is printed to standard output without a
    trailing newline before reading input.
    
    If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
    On *nix systems, readline is used if available.
    """
    pass

if语句支持嵌套,即在一个if语句中嵌入另一个if语句,从而构成不同层次的选择结构。

【例子】Python 使用缩进而不是大括号来标记代码块边界,因此要特别注意else的悬挂问题。

hi = 6
if hi > 2:
    if hi > 7:
        print('好棒!好棒!')
else:
    print('切~')

# 无输出

【例子】

temp = input("猜一猜小姐姐想的是哪个数字?")
guess = int(temp)
if guess > 8:
    print("大了,大了")
else:
    if guess == 8:
        print("你太了解小姐姐的心思了!")
        print("哼,猜对也没有奖励!")
    else:
        print("小了,小了")
print("游戏结束,不玩儿啦!")

3. if - elif - else 语句

if expression1:
    expr1_true_suite
elif expression2:
    expr2_true_suite
    .
    .
elif expressionN:
    exprN_true_suite
else:
    expr_false_suite
  • elif 语句即为 else if,用来检查多个表达式是否为真,并在为真时执行特定代码块中的代码。

【例子】

temp = input('请输入成绩:')
source = int(temp)
if 100 >= source >= 90:
    print('A')
elif 90 > source >= 80:
    print('B')
elif 80 > source >= 60:
    print('C')
elif 60 > source >= 0:
    print('D')
else:
    print('输入错误!')

4. assert 关键词

  • assert这个关键词我们称之为“断言”,当这个关键词后边的条件为 False 时,程序自动崩溃并抛出AssertionError的异常。

【例子】

my_list = ['lsgogroup']
my_list.pop(0)
assert len(my_list) > 0

# AssertionError

【例子】在进行单元测试时,可以用来在程序中置入检查点,只有条件为 True 才能让程序正常工作。

assert 3 > 7

# AssertionError

The assert statement
Assert statements are a convenient way to insert debugging assertions into a program:
assert_stmt ::=  "assert" expression ["," expression]
The simple form, assert expression, is equivalent to
if __debug__:
   if not expression: raise AssertionError

The extended form, assert expression1, expression2, is equivalent to
if __debug__:
   if not expression1: raise AssertionError(expression2)

These equivalences assume that __debug__ and AssertionError refer to the built-in variables with those names. In the current implementation, the built-in variable __debug__ is True under normal circumstances, False when optimization is requested (command line option -O). The current code generator emits no code for an assert statement when optimization is requested at compile time. Note that it is unnecessary to include the source code for the expression that failed in the error message; it will be displayed as part of the stack trace.
Assignments to __debug__ are illegal. The value for the built-in variable is determined when the interpreter starts.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值