Python中的数据类型!

发布时间:2023年12月24日

Python中的数据类型!

Python中的数据类型主要包括以下几种:

数字(Numbers):整数(int)、浮点数(float)、复数(complex)
字符串(Strings):文本数据,用单引号或双引号表示
列表(Lists):有序的元素集合,用方括号表示
元组(Tuples):有序的元素集合,用圆括号表示
集合(Sets):无序的、不重复的元素集合,用花括号表示
字典(Dictionaries):键值对的集合,用大括号表示
接下来将详细讲解这些数据类型的使用方法!

数字(Numbers)
整数(int):

a = 10
print(type(a))  # 输出:<class 'int'>

浮点数(float):

b = 3.14
print(type(b))  # 输出:<class 'float'>

复数(complex):

c = 2 + 3j
print(type(c))  # 输出:<class 'complex'>

字符串(Strings)
使用单引号或双引号表示:

d = "Hello, World!"
e = 'Python is fun!'
print(type(d))  # 输出:<class 'str'>
print(type(e))  # 输出:<class 'str'>

列表(Lists)
有序的元素集合,用方括号表示:

f = [1, 2, 3, 4, 5]
print(type(f))  # 输出:<class 'list'>

元组(Tuples)
有序的元素集合,用圆括号表示:

g = (1, 2, 3, 4, 5)
print(type(g))  # 输出:<class 'tuple'>

集合(Sets)
无序的、不重复的元素集合,用花括号表示:

h = {1, 2, 3, 4, 5}
print(type(h))  # 输出:<class 'set'>

字典(Dictionaries)
键值对的集合,用大括号表示:

i = {'name': 'Tom', 'age': 18, 'city': 'New York'}
print(type(i))  # 输出:<class 'dict'>

案例场景:
计算两个数的和:

a = 10
b = 20
sum_ab = a + b
print("The sum of a and b is:", sum_ab)  # 输出:The sum of a and b is: 30

判断一个数是否为偶数:

num = 6
if num % 2 == 0:
    print(num, "is an even number.")  # 输出:6 is an even number.
else:
    print(num, "is not an even number.")  # 输出:6 is not an even number.

将一个字符串转换为大写:

text = "hello world"
upper_text = text.upper()
print("The uppercase version of the text is:", upper_text)  # 输出:The uppercase version of the text is: HELLO WORLD

创建一个包含多个元素的列表:

fruits = ['apple', 'banana', 'cherry']
print("The list of fruits is:", fruits)  # 输出:The list of fruits is: ['apple', 'banana', 'cherry']

访问列表中的元素:

fruits = ['apple', 'banana', 'cherry']
first_fruit = fruits[0]
print("The first fruit in the list is:", first_fruit)  # 输出:The first fruit in the list is: apple

修改列表中的元素:

fruits = ['apple', 'banana', 'cherry']
fruits[1] = 'orange'
print("The updated list of fruits is:", fruits)  # 输出:The updated list of fruits is: ['apple', 'orange', 'cherry']

添加元素到列表的末尾:

fruits = ['apple', 'banana', 'cherry']
fruits.append('grape')
print("The list of fruits with an additional element is:", fruits)  # 输出:The list of fruits with an additional element is: ['apple', 'banana', 'cherry', 'grape']

删除列表中的元素:

fruits = ['apple', 'banana', 'cherry']
del fruits[0]
print("The list after removing the first element is:", fruits)  # 输出:The list after removing the first element is: ['banana', 'cherry']

对列表进行排序:

numbers = [5, 2, 8, 1, 9]
numbers.sort()
print("The sorted list of numbers is:", numbers)  # 输出:The sorted list of numbers is: [1, 2, 5, 8, 9]

遍历列表中的元素:

fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
    print(fruit)  # 分别输出:apple、banana、cherry

创建一个字典,并访问其中的值:

person = {'name': 'John', 'age': 30, 'city': 'New York'}
print("The name of the person is:", person['name'])  # 输出:The name of the person is: John

修改字典中的值:

person = {'name': 'John', 'age': 30, 'city': 'New York'}
person['age'] = 31
print("The updated person dictionary is:", person)  # 输出:The updated person dictionary is: {'name': 'John', 'age': 31, 'city': 'New York'}

添加键值对到字典中:

person = {'name': 'John', 'age': 30, 'city': 'New York'}
person['occupation'] = 'Engineer'
print("The updated person dictionary is:", person)  # 输出:The updated person dictionary is: {'name': 'John', 'age': 30, 'city': 'New York', 'occupation': 'Engineer'}

删除字典中的键值对:

person = {'name': 'John', 'age': 30, 'city': 'New York'}
del person['age']
print("The updated person dictionary is:", person)  # 输出:The updated person dictionary is: {'name': 'John', 'city': 'New York'}

结尾

以上就是本期的全部内容,希望对大家有帮助!

文章来源:https://blog.csdn.net/qiaomuv/article/details/135164629
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。