Python , , , , . , , , , , .
>>> an_integer = 1
>>> an_integer
1
>>> AN_INTEGER
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'AN_INTEGER' is not defined
>>> An_Integer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'An_Integer' is not defined
>>> an_inteGer
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'an_inteGer' is not defined
:
( ):
"<stdin>", 1, <>
NameError: '<>'
.
Python .
Python , . , , , . humansize.py:
if __name__ == '__main__':
print(approximate_size(1000000000000, False))
print(approximate_size(1000000000000))
C, Python == = . C, Python , . |
, if ? , , __name__ (). , . , __name__ , .
>>> import humansize
>>> humansize.__name__
'humansize'
, , __name__ __main__. Python if, , if. , .
c:\home\diveintopython3> c:\python31\python.exe humansize.py
1.0 TB
931.3 GiB
Python!
- PEP 257: Docstring Conventions , .
- Python Tutorial: Documentation Strings .
- PEP 8: Style Guide for Python Code .
- Python Reference Manual , Python , , .
- , , . . .
- string (). . .
|
|
, , . |
Python . Python , . ? , Python .
Python . :
- , True () False ().
- , (1 2), (1.1 1.2), (1/2 2/3), .
- , , HTML-.
- , , JPEG.
- .
- .
- .
- -.
, . Python , , , , , , , . : , , . . ; .
, , . .
.
: . Python True ( . true ) False ( . false ), . . (, if), Python , . . , Python . , , , , . ( .)
, humansize.py:
if size < 0:
raise ValueError(' ')
size 0 , < . size < 0 . Python:
>>> size = 1
>>> size < 0
False
>>> size = 0
>>> size < 0
False
>>> size = -1
>>> size < 0
True
|
|
- , Python 2, . True 1, False 0.
>>> True + True
2
>>> True - False
1
>>> True * False
0
>>> True / False
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: int division or modulo by zero
:
( ):
"<stdin>", 1, <>
ZeroDivisionError:
--! ! , .
. , , . Python , . ; Python .
>>> type(1) ①
<class 'int'>
>>> isinstance(1, int) ②
True
>>> 1 + 1 ③
2
>>> 1 + 1.0 ④
2.0
>>> type(2.0)
<class 'float'>
① | type() . , 1 int (). |
② | isinstance() . |
③ | int int. |
④ | int float float. Python int float, float. |