# 这是一个简单的 Python 零基础教学示例代码
# 打印输出
print("Hello, World!") # 输出字符串 "Hello, World!"
# 变量定义和使用
name = "Alice" # 定义一个字符串变量 name
age = 25 # 定义一个整数变量 age
print(f"My name is {name} and I am {age} years old.") # 使用 f-string 格式化输出
# 条件语句
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
# 循环
for i in range(5): # 循环从 0 到 4
print(f"Number: {i}")
# 函数定义
def greet(name):
return f"Hello, {name}!"
print(greet("Bob")) # 调用函数并打印返回值
# 列表操作
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # 访问列表中的第一个元素
fruits.append("orange") # 向列表中添加元素
print(fruits) # 打印整个列表
# 字典操作
person = {
"name": "Charlie",
"age": 30,
"city": "New York"
}
print(person["name"]) # 访问字典中的值
person["age"] = 31 # 修改字典中的值
print(person)
# 文件读写
with open("example.txt", "w") as file:
file.write("This is an example text file.\n")
with open("example.txt", "r") as file:
content = file.read()
print(content)
print()
函数用于在控制台输出信息。f-string
是一种格式化字符串的方式,可以在字符串中嵌入变量。if-else
语句用于根据条件执行不同的代码块。for
循环可以遍历一个序列(如列表或范围)。def
关键字用于定义函数,函数可以通过 return
返回值。open()
函数用于打开文件,with
语句确保文件在使用后自动关闭。上一篇:python 读取xls
下一篇:python常见函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站