Python 语言入门指南

Python 语言基础

本指南帮助初学者快速掌握 Python 编程语言的基础知识。

1. 数据类型与变量

Python 支持多种数据类型,例如:

  • 整数 (int):例如 10, 25, -5
  • 浮点数 (float):例如 3.14, -2.5, 1e10
  • 字符串 (str):例如 "Hello", "Python"
  • 布尔值 (bool):True 或 False

使用 = 符号可以给变量赋值,例如:

x = 10
name = "Alice"

2. 控制流语句

Python 使用缩进来表示代码块,常用的控制流语句包括:

  • 条件语句 (if-else)
if x > 5:
  print("x 大于 5")
else:
  print("x 不大于 5")
  • 循环语句 (for, while)
for i in range(5):
  print(i)

i = 0
while i < 5 xss=removed>

3. 函数

使用 def 关键字定义函数:

def greet(name):
  print("你好," + name)

greet("Bob")

4. 数据结构

Python 提供了多种数据结构,例如:

  • 列表 (list):有序、可变的元素集合,例如 [1, 2, 3]
  • 元组 (tuple):有序、不可变的元素集合,例如 (1, 2, 3)
  • 字典 (dict):键值对的集合,例如 {"name": "Alice", "age": 25}

学习资源

pdf 文件大小:521.03KB