Python Code Examples Python代码示例!

Python is a general purpose programming language which is dynamically typed, interpreted, and known for its easy readability with great design principles.

是一种通用编程语言,它具有很好的设计原则,因其易于阅读而动态地输入,解释和着名。

freeCodeCamp has one of the most popular courses on Python. It's completely free (and doesn't even have any advertisements). You can

freeCodeCamp是Python上最受欢迎的课程之一。它是完全免费的(甚至没有任何广告)。您可以 watch it on YouTube here .

。Python资源共享群:626017123

 

 

 

 

 

Some general information about floating point numbers and how they work in Python, can be found

可以找到有关浮点数以及它们如何在Python中工作的一些常规信息 here .

Nearly all implementations of Python follow the IEEE 754 specification: Standard for Binary Floating-Point Arithmetic. More information found on the

几乎所有Python的实现都遵循IEEE 754规范:二进制浮点运算标准。更多信息发现于 IEEE site.

Float objects can be created using

可以使用创建浮动对象 floating point literals :

>>> 3.14
3.14
>>> 314\.    # Trailing zero(s) not required.
314.0
>>> .314    # Leading zero(s) not required.
0.314
>>> 3e0
3.0
>>> 3E0     # 'e' or 'E' can be used.
3.0
>>> 3e1     # Positive value after e moves the decimal to the right.
30.0
>>> 3e-1    # Negative value after e moves the decimal to the left.
0.3
>>> 3.14e+2 # '+' not required but can be used for exponent part.
314.0

Numeric literals do not contain a sign, however creating negative float objects is possible by prefixing with a unary

数字文字不包含符号,但是可以通过为一元添加前缀来创建负浮点对象 - (minus) operator with no space before the literal:

(减号)运算符在文字前没有空格:

>>> -3.141592653589793
-3.141592653589793
>>> type(-3.141592653589793)
<class 'float'>

Likewise, positive float objects can be prefixed with a unary

同样,正浮动对象可以以一元为前缀 + (plus) operator with no space before the literal. Usually

(加)运算符在字面之前没有空格。平时 + is omitted:

省略:

>>> +3.141592653589793
3.141592653589793

Note that leading and trailing zero(s) are valid for floating point literals.

请注意,前导零和尾随零对浮点文字有效。

>>> 0.0
0.0
>>> 00.00
0.0
>>> 00100.00100
100.001
>>> 001e0010      # Same as 1e10
10000000000.0

The

该 float constructor is another way to create

是另一种创造方式 float objects.

对象。

Creating

创建 float objects with floating point literals is preferred when possible:

在可能的情况下,首选具有浮点文字的对象:

>>> a = 3.14         # Prefer floating point literal when possible.
>>> type(a)
<class 'float'>
>>> b = int(3.14)    # Works but unnecessary.
>>> type(b)
<class 'float'>

However, the float constructor allows for creating float objects from other number types:

但是,float构造函数允许从其他数字类型创建float对象:

>>> a = 4
>>> type(a)
<class 'int'>
>>> print(a)
4
>>> b = float(4)
>>> type(b)
<class 'float'>
>>> print(b)
4.0
>>> float(400000000000000000000000000000000)
4e+32
>>> float(.00000000000000000000000000000004)
4e-32
>>> float(True)
1.0
>>> float(False)
0.0

The

该 float constructor will also make

构造函数也会 float objects from strings that represent number literals:

来自表示数字文字的字符串的对象:

>>> float('1')
1.0
>>> float('.1')
0.1
>>> float('3.')
3.0
>>> float('1e-3')
0.001
>>> float('3.14')
3.14
>>> float('-.15e-2')
-0.0015

The

该 float constructor can also be used to make numeric representations of

构造函数也可用于进行数值表示 NaN (Not a Number), negative

(不是数字),否定 infinity and

和 infinity (note that strings for these are case insensitive):

(请注意,这些字符串不区分大小写):

>>> float('nan')
nan
>>> float('inf')
inf
>>> float('-inf')
-inf
>>> float('infinity')
inf
>>> float('-infinity')
-inf

bool() is a built-in function in Python 3. This function returns a Boolean value, i.e. True or False. It takes one argument,

是Python 3中的内置函数。此函数返回一个布尔值,即True或False。这需要一个论点, x .

Arguments{#arguments}

It takes one argument,

这需要一个论点, x .

。 x is converted using the standard

使用标准转换 Truth Testing Procedure .

Return Value{#return-value}

If

如果 x is false or omitted, this returns

是false或省略,返回 False ; otherwise it returns

;否则它会返回 True .

Code Sample{#code-sample}

print(bool(4 > 2)) # Returns True as 4 is greater than 2
print(bool(4 < 2)) # Returns False as 4 is not less than 2
print(bool(4 == 4)) # Returns True as 4 is equal to 4
print(bool(4 != 4)) # Returns False as 4 is equal to 4 so inequality doesn't holds
print(bool(4)) # Returns True as 4 is a non-zero value
print(bool(-4)) # Returns True as -4 is a non-zero value
print(bool(0)) # Returns False as it is a zero value
print(bool('dskl')) # Returns True as the string is a non-zero value
print(bool([1, 2, 3])) # Returns True as the list is a non-zero value
print(bool((2,3,4))) # Returns True as tuple is a non-zero value
print(bool([])) # Returns False as list is empty and equal to 0 according to truth value testing

and ,

, or ,

, not

Python Docs - Boolean Operations

These are the Boolean operations, ordered by ascending priority:

这些是布尔运算,按升序排序:

OperationResultNotes x or y if x is false, then y, else x (1) x and y if x is false, then x, else y (2) not x if x is false, then True, else False (3).

OperationR

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值