美文网首页
python基础3-if语句

python基础3-if语句

作者: 牧_青 | 来源:发表于2023-05-06 11:54 被阅读0次

if语句

cars = ['audi', 'bmw', 'subaru', 'toyata']

for car in cars:
  if car == 'bmw':
    print(car.upper())
  else:
    print(car.title())

1.1 条件测试

1.1.1 是否相等

在python中检查是否相等时,会区分大小写,所以可以灵活使用title(),upper(),low()。

if car == 'bmw'

1.1.2 是否不相等

要判断两个值是否不等,可结合使用惊叹号和等号(!=),其中惊叹号表示不等。

requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
  print("Hold the anchovies!")

1.1.3 比较数字

常用符号<, <=, >, >=, =, !=

age = 17
if age == 18:
  print("age is 18")
else
    print("age is not 18")

1.2 检查多个条件

需要检测两个及以上的条件时,可以使用andor关键字

  1. 使用and检查多个条件

    age = 17
    if age >= 21 and age <= 25
     print("age is between 21 and 25")
    
  1. 使用or检查多个条件

    age = 17
    if age <=10 or age >=28
     print("ok")
    

1.3 检查特定值是否包含在列表中

  1. 要判断特定的值是否包含在列表中,可以使用关键字in

    request_toppings = ['mushrooms', 'onions', 'pineapple']
    if 'mushrooms' in request_toppings:
      print("ok")
    
  1. 要判断特定值不包含在列表中,可以使用关键字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.")
    

1.4 布尔表达式

布尔表达式的结果要么为True, 要么为False

game_active = True
can_edit = False

1.5 if-else语句

经常需要在条件测试通过了时执行一个操作,并在没有通过时执行另一个操作,可以使用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!")

经常需要检查超过两个的情形,为此可使用Python提供的if-else-else结构。

age = 12
if age < 4:
  print("Your adminssion cost is $0.")
elif age < 18
    print("Your adminssion cost is $5.")
else:
  print("Your adminssion cost is $10.")

可以根据实际情况,缺省掉最后的else

age = 12
if age < 4:
    ...
elif age <10:
  ...
elif age < 20:
  ...

1.6 使用if语句处理列表

1.6.1 检查特殊元素

通过在for循环列表时,添加if判断条件,处理特殊元素

// 辣椒使用完了
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.")

1.6.1 确定列表不为空

requested_toppings = []

if requested_toppings:  // 判断列表是否为空
  for requested_topping in requested_toppings:
    ...
  ...
else:
  print("Are you sure you want a plain pizza?")

1.7 良好的格式设置习惯

在诸如 == , >= 等比较运算符两边各加一个空格,这样比较利于阅读代码

相关文章

  • 2.0 Python - 子目录

    0.0 总目录 2.0.1什么是Python 2.2Python基础 2.2.1基础语句 2.2.2Python的...

  • Python基础知识

    Python基础 语句和语法 Python语句中有一些基本规则和特殊字符: 井号(#)表示之后的Python字符为...

  • 2020-09-22python语言基础和变量

    day1-python语言基础和变量 python基础语法 语句(一条有效的程序)一条语句占一行,一行结束后不用写...

  • Python基础语句

    20170320Python基础语句 试着更改了下Pyhon文件夹到熟悉的目录中,提示【python不是内部或外部...

  • python基础(语句)

    1.判别语句 1.1if语句 if 要判断条件 : 条件城里要做的事 elif 判断条件: 条件成立要做的...

  • 廖雪峰Python教程读书笔记(一)

    Python基础#### Python语法简单,采用缩进,当语句以冒号结尾时,缩进的语句是为代码块,约定使用4个空...

  • python基础-06-条件判断、循环语句

    python基础-条件判断、循环语句 1.条件语句: 形式: 意义: 例子: 获取随机数: 2.循环语句 **1....

  • Python入门小程序(一)

    学习了FishC的Python零基础入门第4节,本次的内容是Python的while循环语句和条件语句。 1. 用...

  • Python爬虫实践记录(2019.04)

    一、Python基础篇 [Pt_01] Python常用的数据类型 [Pt_02] Python条件判断语句&循环...

  • python基础语法复习总结

    一、python基础语法、基本数据类型、运算符、变量 1.python基础语法: 注释:语句: 结束没有分号,一行...

网友评论

      本文标题:python基础3-if语句

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