Conditionals
1. Modulus operator (%)
The modulus operator works on integers and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign (%). The syntax is the same as for the other operators:

The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another – if x % y is zero, then x is divisible by y.
Also, you can extract the right-most digit or digits from a number. For example, x % 10 yields the right-most digit of x (in base 10). Similarly x % 100 yields the last two digits.
2. Boolean expressions
A Boolean expression is an expression that is either true or false. The following examples use the operator ==, which compares two operands and produces True if they are equal and False otherwise:
The == operator is one of the comparison operators; the others are: !=, >, >=, <, <=. Remember that = is an assignment operator and == is a comparison operator. There is no such thing as =< or =>.
3. Logical operators
There are three logical operators: and, or, not. The semantics of these operators is similar to their meaning in English. Strictly speaking, the operands of the logical operators should be Boolean expressions but Python is not very strict. Any nonzero number is interpreted as True.

4. Conditional execution
In order to write useful programs, we almost always need the ability to check conditions and change the behavior of the program accordingly. Conditional statements give us this ability. The simplest form is the if statement. The Boolean expression after the if statement is called the condition. If it is true, then the indented statement gets executed. If not, nothing happens.
If statements have the same structure as function definitions: a header followed by and indented block. Statements like this are called compound statements.
There is no limit on the number of the statements that can appear in the body, but there has to be at least one. Occasionally, it is useful to have a body with no statements (usually as a place keeper for code you haven’t written yet). In that case, you can use the pass statement, which does nothing.

5. Alternative execution
A second form of the if statement is alternative execution, in which there are two possibilities and the condition determines which one gets executed. The syntax looks like this:

6. Chained conditionals
Sometimes there are more than two possibilities and we need more than two branches. One way to express a computation like that is a chained conditional:

elif is an abbreviation of “else if”. Again, exactly one branch will be executed. There is no limit on the number of elif statements. If there is an else clause, it has to be at the end, but there doesn’t have to be one. Each condition is checked in order. If the first false, the next is checked, and so on. If one of them is true, the corresponding branch executes, and the statement ends. Even if more than one condition is true, only the first true branch executes.
Conditionals的更多相关文章
- Conditionals with Omitted Operands (x ? : y)
Conditionals with Omitted Operands The middle operand in a conditional expression may be omitted. Th ...
- Ansible Playbook Conditionals
通常,play的结果可能取决于变量的值,facts(有关远程系统的知识)或先前的任务结果. 在某些情况下,变量的值可能取决于其他变量. 此外,可以创建其他组,以根据主机是否与其他条件匹配来管理主机. ...
- [Javascript] Ternary Conditionals
/** Ternary Conditionals */ // //**Bad** // var isArthur = false; var weapon; if(isArthur){ weapon = ...
- Learning Puppet — Variables, Conditionals, and Facts
Begin $my_variable = "A bunch of text" notify {$my_variable:} Yup, that’s a variable, all ...
- 读高性能JavaScript编程 第四章 Conditionals
if else 和 switch && 递归 if else 和 switch 一般来说,if-else 适用于判断两个离散的值或者判断几个不同的值域.如果判断多于两个离散 ...
- 3.3 Templates -- Conditionals(条件语句)
有时候你可能仅仅想展现模板的一部分,如果属性存在的话. 1. 我们可以使用{{if}}去有条件的渲染一块: {{#if person}} Welcome back, <b>{{person ...
- Use default arguments instead of short circuiting or conditionals使用默认实参代替短路和条件
- iOS编码规范
The official raywenderlich.com Objective-C style guide. This style guide outlines the coding con ...
- Elixir - Hey, two great tastes that go great together!
这是Elixir的作者 José Valim 参与的一次技术访谈,很有料,我们可以了解Elixir的一些设计初衷,目标等等. 原文在: http://rubyrogues.com/114-rr-eli ...
随机推荐
- cocos2d-x学习笔记(18)--游戏打包(windows平台)
cocos2d-x学习笔记(18)--游戏打包(windows平台) 之前做好的游戏,都是在vs2008下编译执行的.假设说想把游戏公布到网上或者和其它人一起分享游戏,那就得对游戏 ...
- 今天开始看看brpc-baidurpc
链接如下: Link 大概看了一下.没有很明显的感觉.还是喜欢机器学习相关的内容.
- bzoj-1492 货币兑换Cash (1)——平衡树维护凸包
题意: 有n天和m的初始金钱,用来购买AB两种纪念券: n天里每天都有AB的价格.每天能够进行这种操作. 1.卖出手中x%的纪念券(AB分别都卖出x%). 2.用x的金钱买入纪念券.买入AB券的比例在 ...
- Leetcode_num1_Single Number
好久没有做题啦.从今天開始刷Leetcode的题.希望坚持的时间能长一点. 先从ac率最高的Single Number開始吧. 题目: Given an array of integers, ever ...
- 上传文件 nginx 413错误
nginx : 413 Request Entity Too Large 上传文件过程发生413 Request Entity Too Large错误,翻译为请求实体过大,断定为nginx限制了请求体 ...
- STM32IAP升级-----编写IAP升级遇到的问题总结
IAP的源代码等资料我上传了,压缩包内有12个文件.,http://download.csdn.net/detail/f907279313/7524849(要积分的辛苦收集的你们就给点积分吧) 还有还 ...
- 创建一个web user control
1.创建文件 添加,然后选择web user control 2.添加控件 在工具栏搜索button,然后拖动三个button上去 <%@ Control Language="C#&q ...
- kentico api
http://devnet.kentico.com/docs/10_0/api/html/R_Project_Kentico_API.htm ScriptHelper.RegisterClientSc ...
- Elasticsearch之下载源码
第一步:进入github.com 第二步: 第三步: 第四步: 第五步: 第六步: 第七步:认识下es的源码目录结构
- android之handler机制深入解析
一.android中需要另开线程处理耗时.网络的任务,但是有必须要在UI线程中修改组件.这样做是为了: ①只能在UI线程中修改组件,避免了多线程造成组件显示混乱 ②不使用加锁策略是为了提高性能,因为a ...