美文网首页
第三章 if语句

第三章 if语句

作者: 永不熄灭的火焰_e306 | 来源:发表于2019-09-26 10:52 被阅读0次

一、条件测试

1.检查是否相等(“=”是赋值,“==”是判断值是否相等)

2.检查相等时不考虑大小写

在Python中检查是否相等时区分大小写,但如果大小写无关紧要,而只想检查变量的值,可

将变量的值转换为小写。

car = 'Audi'
car.lower() == ''audi
#输出结果:True
#########
 car = 'Audi'
car.lower() == 'audi'
True 
 car
'Audi'
#不会影响原来的变量

3.检查是否不相等(!=)

4.比较数字(>、<、>=、<=)

<u>为符合编程规范,在以上符号中的两端各空一格。</u>

5.检查多个条件

使用andor(相当于java 中的&&、||)

5.1 使用and检查多个条件
age_0 = 22 
 age_1 = 18 
(age_0 >= 21) and (age_1 >= 21 )
False 
age_1 = 22 
 (age_0 >= 21) and (age_1 >= 21) 
True
5.2 使用or检查多个条件
 age_0 = 22 
 age_1 = 18 
 age_0 >= 21 or age_1 >= 21 
True 
 age_0 = 18 
 age_0 >= 21 or age_1 >= 21 
False

6. 检查特定值是否包含/不包含在列表中(关键字:in、not in)

 ###################包含(in)
 requested_toppings = ['mushrooms', 'onions', 'pineapple']
'mushrooms' in requested_toppings 
True 
 'pepperoni' in requested_toppings 
False
###############不包含(not in)
banned_users = ['andrew', 'carolina', 'david'] 
user = 'marie' 
if user not in banned_users: 
 print(user.title() + ", you can post a response if you wish.")
 #结果:Marie, you can post a response if you wish.
  1. 布尔表达式

    game_active = True 
    can_edit = False
    

    二、if 语句

    1. 简单if语句

    if conditional_test:
     do something1
        do something2
    

    缩进:缩进的作用与for循环中相同。如果测试通过了,将执行if语句后面所有缩进

    ​ 的代码行,否则将忽略它们。

    2. if-else 语句

     age = 17
    if age >= 18: 
     print("You are old enough to vote!") 
         print("Have you registered to vote yet?")
    else: 
         print("Sorry, you are too young to vote.") 
         print("Please register to vote as soon as you turn 18!")
    ##输出:
        Sorry, you are too young to vote. 
        Please register to vote as soon as you turn 18!
    
    1. if-elif-else 结构

      检查超过一个或者多个的情形

      age = 12 
      if age < 4: 
       price = 0 
      elif age < 18: 
       price = 5 
      elif age < 65: 
       price = 10 
       else: 
       price = 5 
      print("Your admission cost is $" + str(price) + ".")
      

      注意:<u>elif 的作用类似于java中的 else if</u>,其中最后的else(第8行)也可以胜率

age = 12 
if age < 4: 
 price = 0 
elif age < 18: 
 price = 5 
elif age < 65: 
 price = 10 
elif age >= 65: 
 price = 5 
print("Your admission cost is $" + str(price) + ".")

4. 测试多个条件

#不同于 if-elif-else结构,这里的每个if条件都会得到判断(类似于java中的switch语句)
requested_toppings = ['mushrooms', 'extra cheese'] 
 if 'mushrooms' in requested_toppings: 
    print("Adding mushrooms.") 
 if 'pepperoni' in requested_toppings: 
    print("Adding pepperoni.") 
 if 'extra cheese' in requested_toppings: 
    print("Adding extra cheese.") 
print("\nFinished making your pizza!")

如果你只想执行一个代码块,就使用if-elif-else结构;如果要运行多个代码块,就

使用一系列独立的if语句。

三、使用if语句处理列表

1. 检查特殊元素

requested_toppings = ['mushrooms', 'green peppers', 'extra cheese'] 
for requested_topping in requested_toppings: 
    if requested_topping == 'green peppers': 
        print("Sorry, we are out of green peppers right now.") 
    else: 
        print("Adding " + requested_topping + ".") 
print("\nFinished making your pizza!")
#输出结果
Adding mushrooms. 
Sorry, we are out of green peppers right now. 
Adding extra cheese. 
Finished making your pizza!

2. 确定列表是否为空

#在运行for循环前确定列表是否为空
 requested_toppings = [] 
if requested_toppings: 
     for requested_topping in requested_toppings: 
        print("Adding " + requested_topping + ".") 
     print("\nFinished making your pizza!") 
else: 
    print("Are you sure you want a plain pizza?")
#结果:Are you sure you want a plain pizza?

3. 使用多个列表

 available_toppings = ['mushrooms', 'olives', 'green peppers', 
                    'pepperoni', 'pineapple', 'extra cheese'] 
requested_toppings = ['mushrooms', 'french fries', 'extra cheese'] 
 for requested_topping in requested_toppings: 
    if requested_topping in available_toppings: 
        print("Adding " + requested_topping + ".") 
    else: 
        print("Sorry, we don't have " + requested_topping + ".")
 print("\nFinished making your pizza!")
 #输出:
 Adding mushrooms. 
Sorry, we don't have french fries. 
Adding extra cheese. 
Finished making your pizza!

相关文章

  • Modern Fortran Explained学习笔记4

    第三章.表达式和赋值语句 今天继续从第三章开始学习,第三章主要讲了表达式和赋值语句,其中最主要的是数组的表达和指针...

  • python第三章函数

    第三章、三元运算、文件处理、函数 三元运算 三元运算又称三目运算,是对简单的条件语句的简写,如: 简单条件语句: ...

  • 【Java】基础篇之流程控制语句-003

    第三章 流程控制语句 3.1 流程控制 3.1.1 概述 在一个程序执行的过程中,各条语句的执行顺序对程序的结果是...

  • 芦继超10.19总结

    今天老师讲了第三章程序控制语句,这些都是以前讲过的,各种语句,循环,判断,还是比较好理解的。还有一些和以前有区别的...

  • 第三章 if语句

    一、条件测试 1.检查是否相等(“=”是赋值,“==”是判断值是否相等) 2.检查相等时不考虑大小写 在Pytho...

  • 第三章 SQL语言元素(一)

    第三章 SQL语言元素(一) 命令和关键字 InterSystems SQL命令(也称为SQL语句)以关键字开头,...

  • 18/7/15 Java流程控制

    打卡Java笔记,这里是《JAVA从入口到放弃》第三章(* ´з`*) 条件语句 三元运算符: 变量 = 布尔?赋...

  • javascript高级程序设计(第3章)

    第三章:基本概念 本章内容: 语法 数据类型 流程控制语句 函数 3.1 语法 3.1.1 区别大小写 ECMAS...

  • 方法和数组就是这么简单!

    第三章 方法和数组 3.1 概述 还记得我们的HelloWorld例程吗?我们现在对于输出语句应该已经很熟悉了, ...

  • 读记 《瓦尔登湖》(三)| 简单,简单,再简单!

    第三章—— 作者描述了“我生活的地方”,也阐述了“我生活的目的”(摘录中会提及)。 本文中—— 梭罗哲思性语句的摘...

网友评论

      本文标题:第三章 if语句

      本文链接:https://www.haomeiwen.com/subject/iuzcyctx.html