Python编程基础与数据处理实战
1. 字符串模板与变量定义
在Python中,我们可以用不同的方式定义字符串模板。以下是示例代码:
# 假设已有变量定义
a = 3
b = 5.0
st = 'Python Trading'
s = 'Some sample string for splitting'
# 旧方式定义字符串模板
# 这里未给出具体代码示例,一般是使用 % 格式化
# 新方式定义字符串模板
# 这里未给出具体代码示例,一般是使用 str.format() 方法
# 使用 f-string 进行字符串替换
name = 'Gordon Gekko'
age = 43
height = 1.78
print(f'My name is {name}, I am {age} years old and {height}m tall.')
2. 数据结构
Python中有多种数据结构,下面详细介绍元组、列表和字典。
- 元组(tuple) :元组是轻量级的数据结构,是不可变的对象集合。可以使用括号或不使用括号来构造元组。
# 用括号构造元组
t1 = (a, b, st)
print(t1) # 输出: (3, 5.0, 'Python Trading')
print(type(t1)) # 输出: <class 'tuple'>
# 不使用括号构造元组
t2 = st, b, a
print(t2) # 输出: ('P