简单 Python 快乐之旅之:Python 基础语法之条件判断关键字的使用例子

1. 重要特性

1.1. 分号和回车

不同于 C/C++、PHP、Java 等语言使用分号来表示一句话的结束,Python 语言中的分号是可选的,甚至是不推荐使用的。而回车在 Python 里是有用的,它表示新的一行,或者新的一句话的开始。

1.2. 缩进和 {}

不同于 C/C++、PHP、Java 等语言使用 {} 来表示一个语句块,Python 使用缩进来表示语句块,即上下文缩进相同的语句属于同一执行语句块。

1.3. 对象和类型

在 Python 里,一切皆对象。也就是说所有的变量都是对象。而且变量不需要进行特别的类型声明,Python 根据你给它的赋值判定它的数据类型,而且变量的类型可以随意转换,就是说变量 a 前边可以存整数型,后边摇身一变还可以存字符串。这也跟其他 C/C++ 或 Java 之类的语言非常不一样。
但是变量在使用之前必须要进行声明。
Python 有三种数字类型:整数、浮点数和复数。和 C/C++ 或 Java 等其他主流语言不一样,Python 的表示整数只有 int 类型,而且可以表示任意大小的整数,没有诸如 Java 里一系列 short、int、long 等可以表示不同数据存储长度的表示整数的数据类型。

1.4. 字符串用单引号

Python 里的字符串用单引号。

1.5. 逻辑运算符

Python 里的逻辑运算符有 and、or 和 not,这里和其他诸如 C/C++、Java 之类的语言也不一样。

1.6. 关系运算符

Python 里的关系运算符有 ==、!=、>、>=、< 和 <=,这里和其他诸如 C/C++、Java 之类的语言是一样的。

1.7. 关键字

Python 保留有 33 个关键字,我们不能够将其关键字用作变量名、方法名或其他标识符,这跟其他 C/C++ 或 Java 之类的语言是一致的。
本文就从其条件判断关键字讲起。Python 的条件判断关键字包含有 if、else、elif。

2. if 的例子

基本语法:

if boolean_expression:
    statement(s)

注意布尔表达式后边的冒号是必须的。

2.1. 基本的 if 示例

# Basic Python If Example
a = 2
b = 5
if a < b:
    print(a, 'is less than', b)

执行及输出:
基本的 if 示例.jpg

2.2. 多条件表达式

单个表达式里有多条件的话需要使用逻辑操作符将其分开。

# Example – Python If with multiple conditions in the expression
c = 2
d = 5
e = 4
if c < d and c < e:
    print(a, "is less than", d, "and", e)

执行及输出:
多条件表达式.jpg
下文会对多条件表达式进一步进行示例说明。

2.3. 数字表达式

如果 if 语句的表达式是一个数字,那么非 0 (包括正数或负数)都是 true,0 是 false。

# Example – Python If with expression evaluating to a number
f = 4
g = 0
h = -4
if f:
    print(f, 'is not zero')
if g:
    print(g, 'is not zero')
if h:
    print(h, 'is not zero')

执行及输出:
数字表达式.png

3. if else 的例子

基本语法:

if boolean_expression:
    statement(s)
else:
    statement(s)

注意 if 和 else 语句后面的缩进。

# Basic Python If Else Example
i = 2
j = 4
if i < j:
    print(i, 'is less than', j)
    print('yes',i, 'is less than', j)
else:
    print(i, 'is not less than', j)
    print('no', i, 'is not less than', j)

执行和输出:
if else 的例子.png

4. elif 的例子

基本语法:

if boolean_expression_1:
	statement(s)
elif boolean_expression_2:
	statement(s)
elif boolean_expression_3:
	statement(s)
else
	statement(s)

根据需要,有多少个 elif 并没有限制。

# Example Python elif
k = 3
l = 6
if k < l:
    print(k, 'is less than', l)
elif k > l:
    print(k, 'is greater than', l)
else:
    print(k, 'equals', l)

执行和输出:
elif 的例子.png

5. if 多条件表达式

多条件表达式使用逻辑操作符将其分开。

5.1. if and

# Python IF AND
m = 7
n = 8
if m == 7:
    print('m is', m)
if n >= 0:
    print('n is not less than zero')
# combine two boolean conditions with python and logical operator
if m == 7 and n >= 0:
    print('m is', m, 'and', 'n is not less than zero')

执行和输出:
if and.png

5.2. if or

# Python IF OR
o = 9
p = 2
if o == 9:
    print('o is', o)
if p >= 0:
    print('p is not less than zero')
# combine two boolean conditions with python or logical operator
if o == 21 or p >= 0:
    print('o is 21 or', p, 'is not less than zero')
# combine two boolean conditions with python or logical operator
if o == 9 or p >= 4:
    print('o is', o, 'or', p, 'is not less than four')

执行和输出:
if or.jpg

5.3. if not

# Python IF NOT
# not boolean value
q = False
if not q:
    print('q is false')
# not boolean condition
q = 5
if not q == 5:
    print('q is not 5')
else:
    print('q is 5')

执行和输出:
if not.png

6. 总结

在进行条件判断的时候,Python 里为 False 的值有:

  • 0
  • 0.0
  • ‘’
  • None
  • False

除此之外都是 True。

参考资料

posted @ 2019-03-22 20:47  Defonds  阅读(70)  评论(0编辑  收藏  举报