
数字类型(Numeric Types):
整数(int):
 在 Python 中,整数是不可变的,可以表示正整数、负整数和零。
 Python 中整数的大小仅受限于计算机的内存。
# 定义整数变量
num1 = 10
num2 = -5
num3 = 0
# 输出整数变量
print(num1)  # 输出:10
print(num2)  # 输出:-5
print(num3)  # 输出:0
# 整数运算
result = num1 + num2
print(result)  # 输出:5
浮点数(float):
 浮点数用于表示带有小数点的数字。
 浮点数在计算机中是以近似值存储的,因此可能存在精度问题。
# 定义浮点数变量
num1 = 3.14
num2 = -2.5
# 输出浮点数变量
print(num1)  # 输出:3.14
print(num2)  # 输出:-2.5
# 浮点数运算
result = num1 * num2
print(result)  # 输出:-7.85
复数(complex):
 复数由实部和虚部组成,形式为a + bj,其中a是实部,b是虚部。
 Python 中使用 j 表示虚数单位。
# 定义复数变量
num1 = 2 + 3j
num2 = 1 - 2j
# 输出复数变量
print(num1)  # 输出:(2+3j)
print(num2)  # 输出:(1-2j)
# 复数运算
result = num1 * num2
print(result)  # 输出:(8+1j)
字符串(Strings):
字符串的表示:
 在 Python 中,字符串可以使用单引号、双引号或三引号表示。
 例如:‘hello’、“world”、’’‘Hello, world!’’’。
 字符串的表示和基本操作:
# 定义字符串变量
str1 = 'hello'
str2 = "world"
str3 = '''Hello, world!'''
# 输出字符串变量
print(str1)  # 输出:hello
print(str2)  # 输出:world
print(str3)  # 输出:Hello, world!
# 字符串连接
result = str1 + ' ' + str2
print(result)  # 输出:hello world
# 字符串重复
repeated_str = str1 * 3
print(repeated_str)  # 输出:hellohellohello
# 字符串格式化
name = 'Alice'
age = 30
formatted_str = 'My name is %s and I am %d years old.' % (name, age)
print(formatted_str)  # 输出:My name is Alice and I am 30 years old.
字符串的索引和切片:
# 字符串索引
my_string = 'Python'
print(my_string[0])  # 输出:P
print(my_string[-1])  # 输出:n
# 字符串切片
print(my_string[1:4])  # 输出:yth
print(my_string[:3])   # 输出:Pyt
print(my_string[3:])   # 输出:hon
字符串方法的使用:
# 字符串方法示例
my_string = '   hello world   '
# 去除字符串两端的空格
trimmed_str = my_string.strip()
print(trimmed_str)  # 输出:hello world
# 将字符串转换为大写
upper_str = my_string.upper()
print(upper_str)    # 输出:   HELLO WORLD
# 将字符串转换为小写
lower_str = my_string.lower()
print(lower_str)    # 输出:   hello world
# 字符串分割
words = my_string.strip().split()
print(words)        # 输出:['hello', 'world']



















