Python is a simple yet powerful programming language widely used for data analysis and scientific computing. To successfully use Python for data analysis, it is crucial to understand the different data types and their applications. This article will introduce commonly used Python data types and provide specific code examples.
Code example:
x = 5 y = 2.5 z = 3 + 2j print(x + y) # 输出:7.5 print(x * y) # 输出:12.5 print(z.real) # 输出:3.0 print(z.imag) # 输出:2.0
Code example:
message = "Hello, World!" name = "John" age = 25 print(message) # 输出:Hello, World! print("My name is " + name) # 输出:My name is John print(f"I am {age} years old") # 输出:I am 25 years old
Code example:
fruits = ['apple', 'banana', 'orange'] print(fruits[0]) # 输出:apple fruits.append('grape') # 添加元素 print(fruits) # 输出:['apple', 'banana', 'orange', 'grape'] fruits.remove('banana') # 删除元素 print(fruits) # 输出:['apple', 'orange']
Code example:
point = (3, 4) date = (2021, 8, 15) print(point[0]) # 输出:3 print(date[1]) # 输出:8
Code examples:
student = {'name': 'John', 'age': 25, 'grade': 'A'} print(student['name']) # 输出:John print(student.get('age')) # 输出:25 student['age'] = 26 # 修改值 print(student) # 输出:{'name': 'John', 'age': 26, 'grade': 'A'} student['country'] = 'USA' # 添加键值对 print(student) # 输出:{'name': 'John', 'age': 26, 'grade': 'A', 'country': 'USA'}
The above is a brief introduction and code examples of commonly used data types and their applications in Python. Mastering the basic concepts and operations of these data types is very important for using Python for data analysis and scientific computing. I hope this article can help readers better understand and use data types in Python.
The above is the detailed content of Python Data Type Guide: Master Common Data Types and Their Applications. For more information, please follow other related articles on the PHP Chinese website!