1. 变量与数据类型

Python 的变量无需声明类型,直接赋值即可。例如:

name = "Python"  
age = 30  
is_fun = True  
Python_Variable
常用数据类型包括整数、浮点数、字符串、布尔值等,可点击 [/zh/communities/blog/python/data_types](/zh/communities/blog/python/data_types) 深入了解。

2. 条件语句

使用 if-elif-else 控制程序逻辑:

if x > 0:  
    print("正数")  
elif x == 0:  
    print("零")  
else:  
    print("负数")  

📌 提示:条件判断中注意缩进格式,这是 Python 的语法要求。

3. 循环结构

3.1 for 循环

for i in range(5):  
    print(i)  
Python_Loop

3.2 while 循环

count = 0  
while count < 3:  
    print("循环中...")  
    count += 1  

🔄 注意:避免无限循环,确保循环条件最终会变为 False。

4. 函数定义与调用

def 关键字定义函数:

def greet(name):  
    return f"Hello, {name}!"  

print(greet("世界"))  
Python_Function
想了解更多进阶用法?可前往 [/zh/communities/blog/python/advances](/zh/communities/blog/python/advances) 学习。