Python学习[02]
来源:互联网 发布:51单片机按键 编辑:程序博客网 时间:2024/05/17 07:35
[02]Learn Python——Conditionals & Control Flow
1.Compare Closely
Let’s start with the simplest aspect of control flow: comparators. There are six:
Equal to (==)
Not equal to (!=)
Less than (<)
Less than or equal to (<=)
Greater than (>)
Greater than or equal to (>=)
Comparators check if a value is (or is not) equal to, greater than (or equal to), or less than (or equal to) another value.
2.To Be and/or Not to Be
Boolean operators compare statements and result in boolean values. There are three boolean operators:
and, which checks if both the statements are True;
or, which checks if at least one of the statements is True;
not, which gives the opposite of the statement.
""" Boolean Operators------------------------ True and True is TrueTrue and False is FalseFalse and True is FalseFalse and False is FalseTrue or True is TrueTrue or False is TrueFalse or True is TrueFalse or False is FalseNot True is FalseNot False is True"""
3.This and That (or This, But Not That!)
Boolean operators aren’t just evaluated from left to right. Just like with arithmetic operators, there’s an order of operations for boolean operators:
not is evaluated first;
and is evaluated next;
or is evaluated last.
For example, True or not False and False returns True.
4.Conditional Statement Syntax
if is a conditional statement that executes some specified code after checking if its expression is True.
Looking at the example above, in the event that some_function() returns True, then the indented block of code after it will be executed. In the event that it returns False, then the indented block will be skipped.
if some_function(): # block line one # block line two # et cetera
The else statement complements the if statement. An if/else pair says: “If this expression is true, run this indented code block; otherwise, run this code after the else statement.”
if 8 > 9: print "I don't printed!"else: print "I get printed!"
elif is short for “else if.” It means exactly what it sounds like: “otherwise, if the following expression is true, do this!”
if 8 > 9: print "I don't get printed!"elif 8 < 9: print "I get printed!"else: print "I also don't get printed!"
- Python 学习笔记02
- python学习笔记-02
- python学习02
- Python 多线程学习02
- Python学习手册 - 02
- Python学习笔记02
- python学习02
- python学习02 --__slots__
- Python学习-02
- python学习-02
- python机器学习(02)
- 02 python 学习
- python学习简记02
- Python学习[02]
- Python学习笔记02
- Python学习笔记 02 Python基础
- Python学习笔记--2016.08.02
- Python学习02:基本数据类型
- 继承
- “Paper + Code”才是研读论文的正确姿势 | PaperDaily #02
- Codeforces 867 E. Buy Low Sell High (贪心)
- “天气项目”习得(一)
- 入门训练 圆的面积
- Python学习[02]
- 基础练习 闰年判断
- 写给软件工程的你!
- 基础练习 01字串
- MFC树形控件
- 倍增
- 基础练习 字母图形
- codeforces 825E(拓扑)
- BZOJ4383: [POI2015]Pustynia