There are mutable and Immutable types of Pythons built in types Mutable built-in types:

List

Set

Dictionary

Immutable built-in types:

String

Tuple

Number

 

 

主要的内置类型:

  numerics, sequences, mappings, classes, instances and exceptions

  数字、序列、映射、类、实例和异常。

 

mutable built-in types,可以add, subtract, rearrange their numbers in place。

 

1.Truth Value Testing

while condition or as operand of the Boolean operations below.

[1]Here are most of the built-in objects considered false:

  • constants defined to be false: None and False.
  • zero of any numeric type: 00.00jDecimal(0)Fraction(0, 1)
  • empty sequences and collections: ''()[]{}set()range(0)

and always return one of their operands.)

布尔值True or False:

默认情况下,除非一个对象的__bool__()方法返回的是False,__len__()方法返回值为0,其他情况下的对象默认返回的bool值为True.

下面是常见的内置对象被默认为False的情况:

  • None 或 False
  • 0值
  • 空值,空集合等

 

2.Boolean Operations--and,or,not

These are the Boolean operations, ordered by ascending priority:

Operation Result Notes
or y if x is false, then y, else x (1)
and y if x is false, then x, else y (2)
not x if x is false, then True, else False (3)

Notes:

  1. This is a short-circuit operator, so it only evaluates the second argument if the first one is false.
  2. This is a short-circuit operator, so it only evaluates the second argument if the first one is true.
  3. not has a lower priority than non-Boolean operators, so not == b is interpreted as not (a == b), and == not b is a syntax error.

 

and so on...

相关文章: