Skip to main content
Version: python

变量

Python

变量

# this is the first comment
spam = 1 # and this is the second comment
# ... and now a third!
text = "# This is not a comment because it's inside quotes."

数据类型

布尔

a = True
a2 = False

b = 3 > 5

# and运算是与运算,只有所有都为True,and运算结果才是True
d = True and True

# or运算是或运算,只要其中有一个为True,or运算结果就是True
e = True or True

# not运算是非运算,它是一个单目运算符,把True变成False,False变成True
f = not True

整数

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06

a = 10_000_000_000
a2 = 10000000000

# 十六进制用0x前缀和0-9,a-f表示
b = 0xa1b2_c3d4

浮点数

a = 1.24
b = -0.9

# 1.23x109
c = 1.23e9

字符串

如果不希望前置 \ 的字符转义成特殊字符,可以使用 原始字符串,在引号前添加 r 即可:

>>> print('C:\some\name')  # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name') # note the r before the quote
C:\some\name

空值

a = None