Python literals 字面量是什么,怎么用?

文章导读
上一个 测验 下一个 什么是 Python 字面量? Python 字面量或常量是用于在源代码中表示固定值的记法。与变量不同,字面量(123、4.3、"Hello")是静态值,或者说是不随程序或应用程序运行而改变的常量。例如,在以下赋值语句中。
📋 目录
  1. 什么是 Python 字面量?
  2. 不同类型的 Python 字面量
  3. Python Integer Literal
  4. Python Float Literal
  5. Python 复数字面量
  6. Python 字符串字面量
  7. Python 列表字面量
  8. Python 元组字面量
  9. Python 字典字面量
A A

Python - 字面量



上一个
测验
下一个

什么是 Python 字面量?

Python 字面量或常量是用于在源代码中表示固定值的记法。与变量不同,字面量(123、4.3、"Hello")是静态值,或者说是不随程序或应用程序运行而改变的常量。例如,在以下赋值语句中。

x = 10

这里的 10 是一个字面量,作为表示数字 10 的数值,直接存储在内存中。然而,

y = x*2

这里,即使表达式计算结果为 20,它也没有直接包含在源代码中。你也可以使用内置的 int() function 来声明一个 int 对象。然而,这也是一种间接的实例化方式,而不是使用字面量。

x = int(10)

不同类型的 Python 字面量

Python 提供了以下字面量,本教程将对其进行说明:

  • Integer Literal
  • Float Literal
  • Complex Literal
  • String Literal
  • List Literal
  • Tuple Literal
  • Dictionary Literal

Python Integer Literal

仅涉及数字符号(0 到 9)的任何表示都会创建一个 int 类型的对象。这样声明的对象可以通过赋值运算符使用变量来引用。

Integer literals 包括三种不同字面值的类型:decimal、octal 和 hexadecimal literals。

1. Decimal Literal

Decimal literals 表示带符号或无符号的数字。使用 0 到 9 的数字来创建 decimal literal 值。

查看以下将 decimal literal 赋值给变量的语句 −

x = 10
y = -25
z = 0

2. Octal Literal

Python 允许将整数表示为八进制数或十六进制数。仅使用八个数字符号(0 到 7)但前面加上 0o 或 0O 的数字表示在 Python 中是八进制数。

查看以下将 octal literal 赋值给变量的语句 −

x = 0O34

3. Hexadecimal Literal

类似地,一系列十六进制符号(0 到 9 和 a 到 f),前面加上 0x 或 0X,在 Python 中表示十六进制形式的整数。

查看以下将 hexadecimal literal 赋值给变量的语句 −

x = 0X1C

然而,需要注意的是,即使使用 octal 或 hexadecimal literal 记法,Python 内部仍将其视为 int 类型。

示例

# 使用八进制记法
x = 0O34
print ("0O34 in octal is", x, type(x))
# 使用十六进制记法
x = 0X1c
print ("0X1c in Hexadecimal is", x, type(x))

运行此代码时,将产生以下 输出

0O34 in octal is 28 <class 'int'>
0X1c in Hexadecimal is 28 <class 'int'>

Python Float Literal

浮点数由整数部分和小数部分组成。惯例上,小数点符号 (.) 在 float 的字面量表示中分隔这两个部分。例如,

Float Literal 示例

x = 25.55
y = 0.05
z = -12.2345

对于太大或太小的浮点数,即小数点前后数字过多时,使用科学记法来提供紧凑的字面量表示。符号 E 或 e 后跟正整数或负整数,跟在整数部分之后。

Float 科学记法 Literal 示例

例如,数字 1.23E05 等价于 123000.00。类似地,1.23e-2 等价于 0.0123

# 使用普通浮点记法
x = 1.23
print ("1.23 in normal float literal is", x, type(x))
# 使用科学记法
x = 1.23E5
print ("1.23E5 in scientific notation is", x, type(x))
x = 1.23E-2
print ("1.23E-2 in scientific notation is", x, type(x))

这里,你将得到以下 输出

1.23 in normal float literal is 1.23 <class 'float'>
1.23E5 in scientific notation is 123000.0 <class 'float''>
1.23E-2 in scientific notation is 0.0123 <class 'float''>

Python 复数字面量

复数由实部和虚部组成。虚部是任意数字(整数或浮点数)乘以 −1 的平方根。

(&√ −1)。在字面量表示中 ($&\sqrt{−1}$) 使用 "j" 或 "J" 表示。因此,复数的字面量表示形式为 x+yj。

复数类型字面量的示例

#使用复数的字面量表示法
x = 2+3j
print ("2+3j complex literal is", x, type(x))
y = 2.5+4.6j
print ("2.5+4.6j complex literal is", x, type(x))

这段代码将产生以下输出

2+3j complex literal is (2+3j) <class 'complex'>
2.5+4.6j complex literal is (2+3j) <class 'complex'>

Python 字符串字面量

字符串对象是 Python 中的序列数据类型之一。它是一个不可变的 Unicode 码点序列。码点是根据 Unicode 标准对应于字符的数字。字符串是 Python 内置类 'str' 的对象。

字符串字面量通过将字符序列括在单引号 ('hello')、双引号 ("hello") 或三引号 ('''hello''' 或 """hello""") 中来书写。

字符串字面量的示例

var1='hello'
print ("'hello' in single quotes is:", var1, type(var1))
var2="hello"
print ('"hello" in double quotes is:', var1, type(var1))
var3='''hello'''
print ("''''hello'''' in triple quotes is:", var1, type(var1))
var4="""hello"""
print ('"""hello""" in triple quotes is:', var1, type(var1))

这里,你将得到以下输出

'hello' in single quotes is: hello <class 'str'>
"hello" in double quotes is: hello <class 'str'>
''''hello'''' in triple quotes is: hello <class 'str'>
"""hello""" in triple quotes is: hello <class 'str'>

字符串字面量中包含双引号的示例

如果需要在字符串中嵌入双引号作为字符串的一部分,则应将字符串本身放在单引号中。另一方面,如果需要嵌入单引号文本,则应将字符串写成双引号。

var1='Welcome to "Python Tutorial" from '
print (var1)
var2="Welcome to 'Python Tutorial' from "
print (var2)

它将产生以下输出

Welcome to "Python Tutorial" from 
Welcome to 'Python Tutorial' from 

Python 列表字面量

Python 中的列表对象是其他数据类型的对象集合。列表是有序的项目集合,这些项目不一定是相同类型。集合中的单个对象通过从零开始的索引来访问。

列表对象的字面量表示使用一个或多个项目,这些项目用逗号分隔并括在方括号 [] 中。

列表类型字面量的示例

L1=[1,"Ravi",75.50, True]
print (L1, type(L1))

它将产生以下输出

[1, 'Ravi', 75.5, True] <class 'list'>

Python 元组字面量

Python 中的元组对象是其他数据类型的对象集合。元组是有序的项目集合,这些项目不一定是相同类型。集合中的单个对象通过从零开始的索引来访问。

元组对象的字面量表示使用一个或多个项目,这些项目用逗号分隔并括在圆括号 () 中。

元组类型字面量的示例

T1=(1,"Ravi",75.50, True)
print (T1, type(T1))

它将产生以下输出

(1, 'Ravi', 75.5, True) <class 'tuple'>

不带圆括号的元组类型字面量示例

Python 序列的默认分隔符是圆括号,这意味着不带圆括号的逗号分隔序列也等同于元组的声明。

T1=1,"Ravi",75.50, True
print (T1, type(T1))

这里,你也将得到相同的输出

(1, 'Ravi', 75.5, True) <class 'tuple'>

Python 字典字面量

与 list 或 tuple 类似,dictionary 也是集合数据类型。然而,它不是一个序列。它是一个无序的项目集合,其中每个项目都是一个键值对。值通过 ":" 符号绑定到键。一个或多个用逗号分隔的 key:value 对放在大括号内,形成一个 dictionary 对象。

字典类型字面量的示例

capitals={"USA":"New York", "France":"Paris", "Japan":"Tokyo",
"India":"New Delhi"}
numbers={1:"one", 2:"Two", 3:"three",4:"four"}
points={"p1":(10,10), "p2":(20,20)}

print (capitals, type(capitals))
print (numbers, type(numbers))
print (points, type(points))

键应该是不可变对象。数字、字符串或元组可以用作键。在一个集合中,键不能出现多次。如果一个键出现多次,只会保留最后一次。值可以是任何数据类型。一个值可以分配给多个键。例如,

staff={"Krishna":"Officer", "Rajesh":"Manager", "Ragini":"officer", "Anil":"Clerk", "Kavita":"Manager"}