python基础之条件语句
检查相等和不等

多个检查条件
age1 = 22
age2 = 19
s1 = age1 > 21 and age2 > 19
print(s1)
s2 = age1 > 21 or age2 > 19
print(s2)

检查是否包含在列表内
requested_toppings = ['mushrooms','onions','pineapple']
s1 = 'mushrooms' in requested_toppings
print(s1)
s2 = 'pepperoni' in requested_toppings
print(s2)

检查不包含
requested_toppings = ['mushrooms','onions','pineapple']
s1 = 'mushrooms' not in requested_toppings
print(s1)
s2 = 'pepperoni' not in requested_toppings
print(s2)

if语句
age = 19
if age >= 18:
print('You are old enough to vote')
if age >= 20:
print('is no print')

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) + '.')

检查特殊元素
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 + '.')

requested_toppings= []
if requested_toppings:
for requested_topping in requested_toppings:
print('Adding ' + requested_topping + '.')
else:
print('Are you sure you want a plain pizza?')

available_toppings = ['mushrooms','olives','green peppers','pepperoni']
requested_toppings= ['mushrooms','french fries','green peppers']
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 + '.')

python基础之条件语句的更多相关文章
- Python基础之条件语句和循环
条件语句 Python中的条件语句分为 if ...else . if ...elif...else 以及if ...else 的嵌套使用: username = input('请输入您的用户名:' ...
- Python基础、条件语句和基本数据类型
1. 第一句python - 后缀名是可以是任意? - 导入模块时,如果不是.py文件 ==> 以后文件后缀名是 .py 2. 两种执行方式 python解释器 py文件路径 python 进入 ...
- 【Python基础】条件语句
Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: Python程序语言指定任何非0和非空(null)值为tr ...
- python学习-4 python基础-2 条件语句(if的简单用法1)
条件语句的原理: 2.举个例子:比大小 #!/usr/bin/env python # -*- coding:utf8 -*- a=input("请输入a:") b=input(& ...
- python学习-5 python基础-2 条件语句(if的简单用法2---elif)
1.if的基本语句 if条件: 内部代码块 else: ........ print(‘.......’) 2.if语句支持嵌套 if条件: 内部代码块 if条件: 内部代码块 else: ..... ...
- 【Python】-NO.99.Note.4.Python -【Python3 条件语句 循环语句】
1.0.0 Summary Tittle:[Python]-NO.99.Note.4.Python -[Python3 条件语句 循环语句] Style:Python Series:Python Si ...
- JAVA基础——Switch条件语句
JAVA基础——switch 条件语句 switch语句结构: switch(表达式){ case值1: 语句体1: break: case值2: 语句体2: break: case值3: 语句体3: ...
- python基础之条件循环语句
前两篇说的是数据类型和数据运算,本篇来讲讲条件语句和循环语句. 0x00. 条件语句 条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语 ...
- Python新手学习基础之条件语句——if/else语句
if/else语句 条件语句实际在前面的文章里我们已经使用过几次了,在这里我们需要再次隆重的来介绍一下它,条件语句是通过对一条或多条语句判断的返回结果(True或False)来选择执行下一步命令的,比 ...
随机推荐
- Chrome FeHelper 插件下载地址
方便大家下载 下载可用,本人亲自测试 下载地址: 地址链接
- 【论文阅读】An Empirical Study of Architectural Decay in Open-Source Software
2020-06-19这篇文章是我学习 软件架构与中间件 课程时分享的论文.可以说,这篇文章塑造了我基本的科研观,也养成了我如今看论文的习惯.感谢老师们,也感谢恒恒对我的帮助. 论文地址: https: ...
- 优雅手撕bind函数(面试官常问)
优雅手撕bind函数 前言: 为什么面试官总爱让实现一个bind函数? 他想从bind中知道些什么? 一个小小的bind里面内有玄机? 今天来刨析一下实现一个bind要懂多少相关知识点,也方便我们将零 ...
- Learn day5 迭代器\生成器\高阶函数\推导式\内置函数\模块(math.time)
1.迭代器 # ### 迭代器 """能被next调用,并不断返回下一个值的对象""" """ 特征:迭代器会 ...
- python进程间共享数值、字典、列表变量
##python进程间共享数值.字典.列表变量 关注公众号"轻松学编程"了解更多. import multiprocessing def worker(num, mgr_dict, ...
- 华为云FusionInsight MRS:助力企业构建“一企一湖,一城一湖”
摘要:华为云FusionInsight MRS新一代的数据湖,让大数据越用越快.越用越易.越用越稳.越用越省!让数据价值近在眼前! 10月30日,以"携手共赢·数创未来"为主题的第 ...
- nginx下配置php5和php7
用的是lnmp 一键安装的 php5.6版本网上百度Ubuntu安装多版本PHP就行 参考文章原链接:http://blog.csdn.net/21aspnet/article/details/476 ...
- 1.流程控制--if
流程控制--if -*- coding:utf-8 -*- #定义字符编码 1.判断条件if age = input("输入年龄:") #将交互式输入内容赋值给age,默认内容为字 ...
- Ques1,debug模式打不开的原因
我尝试过一下三种方法打开pycharm环境下Flask的debug模式,但是都失败了.这三种具体方法如下: 1, 2, 3, 可以说上述方法都是常规操作,可是为啥还是不能打开debug模式呢? 因为: ...
- Spider_知识目录_基础
知识目录 静态网页抓取 Spider_基础总结1_Request(get/post__url传参_headers_timeout)+Reponse Spider_基础总结2_Requests异常 Sp ...