python字面量的理解

这个“字面量”听上去仿佛很高大上,其实只不过是具体的数据赋值的式子而已,大家平常都用过的,不是啥新东西。

在编程中,字面量是代码中表示特定值的语法表示法。Python 中有各种类型的字面量,每种都表示不同类型的数据。以下是一些常见的 Python 字面量及其理解方式:

 

1. 整数字面量:

x = 42

2. 浮点数字面量:

y = 3.14

3. 字符串字面量:

s = "Hello, World!"

4. 布尔字面量:

is_true = True

5. 列表字面量:

numbers = [1, 2, 3, 4]

6. 元组字面量:

point = (2, 5)

7. 集合字面量:

fruits = {"apple", "orange", "banana"}

8. 字典字面量:

person = {"name": "Alice", "age": 30, "city": "Wonderland"}

 

posted @ 2024-02-04 17:30  AlphaGeek  阅读(15)  评论(0)    收藏  举报