
第9課: Python基礎知識的詳細解釋

1. 数据类型:
整数 (`int`): 这是最基本的数字类型。例如:`5`, `-3`, `42`。
浮点数 (`float`): 代表小数点数值。例如:`3.14`, `-0.001`, `2.71`。
字符串 (`str`): 用于表示文本。在Python中,可以使用单引号或双引号来创建字符串。例如:`"Hello"`, `'Python'`.
列表 (`list`): 一个有序的值集合,可以包含任何类型的数据。例如:`[1, 2, 3]`, `["apple", "banana", "cherry"]`.
字典 (`dict`): 键值对的集合。例如:`{"name": "John", "age": 30}`.
集合 (`set`): 一个无序的、不含重复值的集合。例如:`{1, 2, 3}`, `{“apple”, “banana”}`。
元组 (`tuple`): 一个不可变的有序值集合。例如:`(1, 2, 3)`, `("apple", "banana", "cherry")`.
2. 控制结构:
if语句: 用于条件判断。例如:
x = 10
if x greater than 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")
for循环: 用于迭代一系列数据(例如列表或元组):
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
while循环: 根据条件重复执行代码块:
x = 5
while x greater than 0:
print(x)
x -= 1
3. 函数:
内置函数: Python提供了许多内置函数,例如:`print()`, `len()`, `type()`, `int()`, `str()` 等。
自定义函数: 可以创建自己的函数来执行特定任务。例如:
def greet(name):
return "Hello, " + name + "!"
message = greet("Alice")
print(message) # 输出: Hello, Alice!
