PythonCodeExamplesPython代码⽰例!
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.
python新手代码大全pdf0.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).
OperationResultNotes x或y如果x为假,则为y,否则x(1)x和y如果x为假,则为x,否则y(2)不为x如果x为假,则为True,否则为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 a == b is interpreted as not (a == b), and a == not b is a
syntax error.
Examples:{#examples-}
not : {#not-}
>>> not True
False
>>> not False
True
and : {#and-}
>>> True and False # Short-circuited at first argument.
False
>>> False and True # Second argument is evaluated.
False
>>> True and True # Second argument is evaluated.
True
or : {#or-}
>>> True or False # Short-circuited at first argument.
True
>>> False or True # Second argument is evaluated.
True
>>> False or False # Second argument is evaluated.
False
Three commonly used built-in constants:
三种常⽤的内置常量:
True : The true value of the bool type. Assignments to True raise a SyntaxError .
False : The false value of the bool type. Assignments to False raise a SyntaxError .
None : The sole value of the type NoneType . None is frequently used to represent the absence of a value, as when default arguments are not passed to a function. Assignments to None raise a SyntaxError .
Other built-in constants:
其他内置常量:
NotImplemented : Special value which should be returned by the binary special methods, such
as __eg__() , __add__() , __rsub__() , etc.) to indicate that the operation is not implemented with respect to the other type.
Ellipsis : Special value used mostly in conjunction with extended slicing syntax for user-defined container data types.
__debug__ : True if Python was not started with an -o option.
Constants added by the site module. The site module (which is imported automatically during startup, except if the -S command-line option is given) adds several constants to the built-in namespace. They are useful for the interactive interpreter shell and should not be used in programs.
站点模块添加的常量。站点模块(在启动期间⾃动导⼊,除⾮给出-S命令⾏选项)将向内置命名空间添加⼏个常量。它们对交互式解释器shell很有⽤,不应在程序中使⽤。
Objects that, when printed, print a message like "Use quit() or Ctrl-D (i.e. EOF) to exit", and when called, raise SystemExit with the specified exit code:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论