我的Python学习日志

Python基础入门

一、Python简介

Python由Guido van Rossum于1991年创建,是一种解释型、面向对象的高级编程语言...

二、基本数据类型

Python支持多种数据类型,每种类型都有其特定的用途和方法:

类型 示例 描述
整数(int) 5, -3, 0b1010 不带小数点的数字
浮点数(float) 3.14, 2.0e-5 带小数点的数字
字符串(str) "Hello", 'Python' 文本序列
布尔值(bool) True, False 逻辑值
列表(list) [1, 2, "a"] 有序可变集合
元组(tuple) (1, 2, "a") 有序不可变集合
字典(dict) {"name": "John", "age": 30} 键值对集合

三、控制结构

控制程序执行流程的基本结构:

条件语句

# if-elif-else结构
age = 18
if age < 13:
    print("儿童")
elif age < 18:
    print("青少年")
else:
    print("成年人")

循环结构

# for循环遍历序列
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

# while循环
count = 0
while count < 5:
    print(count)
    count += 1

四、函数基础

函数是可重用的代码块:

# 定义函数
def calculate_area(width, height):
    """计算矩形面积"""
    return width * height

# 调用函数
print(calculate_area(5, 10))  # 输出: 50
返回首页