Python 编程快速入门指南
Python 编程快速入门指南涵盖环境搭建、基础语法、流程控制、数据结构、函数模块、文件操作及面向对象编程等内容。通过示例代码演示变量定义、运算、条件循环、列表字典等核心概念,并介绍标准库使用与后续学习方向如 Web 开发、数据分析及人工智能框架。适合初学者建立 Python 知识体系。

Python 编程快速入门指南涵盖环境搭建、基础语法、流程控制、数据结构、函数模块、文件操作及面向对象编程等内容。通过示例代码演示变量定义、运算、条件循环、列表字典等核心概念,并介绍标准库使用与后续学习方向如 Web 开发、数据分析及人工智能框架。适合初学者建立 Python 知识体系。

Python 作为当今最流行的编程语言之一,以其简洁的语法、强大的功能和丰富的生态系统赢得了全球开发者的青睐。无论你是想进入数据科学、Web 开发、自动化脚本还是人工智能领域,Python 都是绝佳的起点。本文将带你快速掌握 Python 的核心概念,助你开启编程之旅。
访问 Python 官网下载最新稳定版本,推荐 Python 3.8+。
Windows 用户注意:安装时勾选 "Add Python to PATH" 选项。
打开终端/命令行,输入:
python --version
或
python3 --version
应显示已安装的 Python 版本号。
推荐初学者使用:
创建一个 hello.py 文件,写入:
print("Hello, Python World!")
运行它:
python hello.py
# 基本数据类型
name = "Alice" # 字符串 (str)
age = 25 # 整数 (int)
price = 19.99 # 浮点数 (float)
is_student = True # 布尔值 (bool)
# 打印变量类型
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
# 算术运算
print(10 + 3) # 13
print(10 - 3) # 7
print(10 * 3) # 30
print(10 / 3) # 3.333...
print(10 // 3) # 3 (整除)
print(10 % 3) # 1 (取余)
print(10 ** 3) # 1000 (幂运算)
# 比较运算
print(10 > 3) # True
print(10 == 3) # False
print(10 != 3) # True
age = 18
if age < 12:
print("儿童")
elif age < 18:
print("青少年")
else:
print("成人")
for 循环:
# 遍历范围
for i in range(5): # 0 到 4
print(i)
# 遍历列表
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
while 循环:
count = 0
while count < 5:
print(count)
count += 1
# 创建列表
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "banana", "cherry"]
# 访问元素
print(fruits[0]) # "apple"
print(fruits[-1]) # "cherry" (倒数第一个)
# 常用操作
fruits.append("orange") # 添加元素
fruits.insert(1, "grape") # 插入元素
fruits.remove("banana") # 删除元素
print(len(fruits)) # 获取长度
# 创建字典
person = {"name": "Alice", "age": 25, "is_student": True}
# 访问元素
print(person["name"]) # "Alice"
print(person.get("age")) # 25
# 常用操作
person["email"] = "[email protected]" # 添加键值对
del person["is_student"] # 删除键值对
print("age" in person) # 检查键是否存在
# 元组(不可变)
coordinates = (10.0, 20.0)
print(coordinates[0]) # 10.0
# 集合(唯一元素)
unique_numbers = {1, 2, 3, 3, 4}
print(unique_numbers) # {1, 2, 3, 4}
def greet(name, greeting="Hello"):
"""这是一个问候函数"""
return f"{greeting}, {name}!"
print(greet("Alice")) # "Hello, Alice!"
print(greet("Bob", "Hi")) # "Hi, Bob!"
创建 calculator.py:
def add(a, b):
return a + b
def multiply(a, b):
return a * b
在另一个文件中导入:
import calculator
print(calculator.add(2, 3)) # 5
print(calculator.multiply(2, 3)) # 6
# 或者
from calculator import add
print(add(5, 7)) # 12
# 写入文件
with open("example.txt", "w") as file:
file.write("Hello, Python!\n")
file.write("This is a text file.\n")
# 读取文件
with open("example.txt", "r") as file:
content = file.read()
print(content)
# 逐行读取
with open("example.txt", "r") as file:
for line in file:
print(line.strip()) # 去除换行符
class Dog:
# 类属性
species = "Canis familiaris"
# 初始化方法
def __init__(self, name, age):
self.name = name # 实例属性
self.age = age # 实例属性
# 实例方法
def description(self):
return f"{self.name} is {self.age} years old"
def speak(self, sound):
return f"{self.name} says {sound}"
# 创建实例
buddy = Dog("Buddy", 5)
print(buddy.description()) # "Buddy is 5 years old"
print(buddy.speak("Woof!")) # "Buddy says Woof!"
Python 的强大之处在于其丰富的标准库:
示例:
import math
print(math.sqrt(16)) # 4.0
import random
print(random.randint(1, 10)) # 随机 1-10 的整数
from datetime import datetime
now = datetime.now()
print(now.year, now.month, now.day)
Python 以其 "简单但强大" 的哲学,成为了编程初学者的理想选择。通过本文,你已经掌握了 Python 的基础知识,但这只是开始。编程的真正魅力在于实践,不断尝试、犯错和学习,你将成为一名优秀的 Python 开发者!

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online