Python-10-条件和条件语句
2024-09-02 09:34:04
num = int(input('Enter a number: '))
if num > 0:
print('The number is positive')
elif num < 0:
print('The number is negative')
else:
print('The number is zero')
x == y x 等于y
x < y x小于y
x > y x大于y
x >= y x大于或等于y
x <= y x小于或等于y
x != y x不等于y
x is y x和y是同一个对象
x is not y x和y是不同的对象
x in y x是容器(如序列) y的成员
x not in y x不是容器(如序列) y的成员
同时使用多个比较运算符,如0 < age < 100
布尔运算符and,or,not
断言
工作原理类似这样:
if not condition:
rash program
在错误条件出现时就崩溃胜过以后再崩溃
可以添加assert语句充当检查点
>>> age = 10
>>> assert 0 < age < 100
>>> age = -1
>>> assert 0 < age < 100
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AssertionError
还可以在后面加一个字符串,对断言加以说明
>>> age = -1
>>> assert 0 < age < 100, 'The age must be realistic'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AssertionError: The age must be realistic
最新文章
- navicat连接虚拟机(centos)中的mysql
- Entity Framework 实体框架的形成之旅--为基础类库接口增加单元测试,对基类接口进行正确性校验(10)
- jquermobile 安装
- python entrypoint
- 一:解决VirtualBox只能安装32位系统的问题
- xml架构管理器
- 百度地图API简单使用
- sharepoint 2010 页面添加footer方法 custom footer for sharepoint 2010 master page
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- el和jstl
- 简单实用的日志类CLog (Python版)
- 判断小数点位数不超过2位的JS代码和在删除确认框里面插JS代码
- 核心动画 CAAnimation 进阶
- CodeForces 620E New Year Tree
- 使用Github Page鼓励自己每日编程
- Python argparse用法
- Python实现的各种机器学习算法
- 利用Python半自动化生成Nessus报告
- Subway (树中心 + 树hash)
- 下载,和scp上传问题
热门文章
- FZU1989 AntiAC —— 字符串
- SpringBoot2.0之整合ActiveMQ(发布订阅模式)
- 9.1 NOIP普及组试题精解(2)
- BZOJ 1640 [Usaco2007 Nov]Best Cow Line 队列变换:贪心【字典序最小】
- BZOJ 1201 [HNOI2005]数三角形:枚举 + 前缀和
- L91
- install docker
- Nginx均衡负载配置
- 【算法模板】Binary Search 二分查找
- bzoj 1202 [HNOI2005]狡猾的商人——带偏移量的并查集