12.Python提供了哪些内建类型

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

 

 

Built-in Types:https://docs.python.org/dev/library/stdtypes.html

主要的内置类型:

  numerics, sequences, mappings, classes, instances and exceptions

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

 

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

 

1.Truth Value Testing

Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below.

By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object. [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)

Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and 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...

转载于:https://www.cnblogs.com/zoe233/p/7424686.html

你可能感兴趣的:(python)