


























Python 是目前最受欢迎的编程语言之一,语法简单易学,应用领域广泛。这份指南将帮助你从零开始学习 Python。
Python 有以下优势:
访问 python.org 下载最新版本的 Python。安装时记得勾选”Add Python to PATH”。
推荐使用以下编辑器:
# 字符串
name = "小明"
message = f"你好,{name}!"
# 数字
age = 25
height = 1.75
# 布尔值
is_student = True
# 列表
fruits = ["苹果", "香蕉", "橙子"]
# 字典
person = {
"姓名": "小明",
"年龄": 25,
"城市": "上海"
}
# 条件判断
if age >= 18:
print("已成年")
else:
print("未成年")
# 循环
for fruit in fruits:
print(f"我喜欢{fruit}")
# while 循环
count = 0
while count < 3:
print(f"计数:{count}")
count += 1
def greet(name, age=18):
"""
问候函数
"""
return f"你好,{name}!你今年{age}岁。"
# 调用函数
message = greet("小红", 22)
print(message)
class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
self.courses = []
def add_course(self, course):
self.courses.append(course)
print(f"{self.name} 选修了 {course}")
def get_info(self):
return f"学生:{self.name},年级:{self.grade}"
# 创建对象
student = Student("小李", "高二")
student.add_course("数学")
print(student.get_info())
import requests
response = requests.get("https://api.github.com/users/octocat")
data = response.json()
print(data["name"])
import pandas as pd
# 读取 CSV 文件
df = pd.read_csv("data.csv")
# 数据查看
print(df.head())
print(df.describe())
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.title("简单线图")
plt.xlabel("X 轴")
plt.ylabel("Y 轴")
plt.show()
def calculator():
print("简单计算器")
print("支持操作:+, -, *, /")
while True:
try:
num1 = float(input("请输入第一个数字:"))
operator = input("请输入操作符 (+, -, *, /):")
num2 = float(input("请输入第二个数字:"))
if operator == "+":
result = num1 + num2
elif operator == "-":
result = num1 - num2
elif operator == "*":
result = num1 * num2
elif operator == "/":
if num2 != 0:
result = num1 / num2
else:
print("错误:除数不能为零!")
continue
else:
print("错误:不支持的操作符!")
continue
print(f"结果:{num1} {operator} {num2} = {result}")
if input("继续计算?(y/n):").lower() != 'y':
break
except ValueError:
print("错误:请输入有效的数字!")
calculator()
import requests
def get_weather(city):
"""
获取指定城市的天气信息
需要申请 API Key
"""
api_key = "your_api_key"
url = f"http://api.openweathermap.org/data/2.5/weather"
params = {
"q": city,
"appid": api_key,
"units": "metric",
"lang": "zh_cn"
}
try:
response = requests.get(url, params=params)
data = response.json()
if response.status_code == 200:
temp = data["main"]["temp"]
desc = data["weather"][0]["description"]
humidity = data["main"]["humidity"]
print(f"{city} 当前天气:")
print(f"温度:{temp}°C")
print(f"天气:{desc}")
print(f"湿度:{humidity}%")
else:
print(f"获取天气信息失败:{data['message']}")
except Exception as e:
print(f"网络错误:{e}")
# 使用示例
city_name = input("请输入城市名称:")
get_weather(city_name)
Python 的世界很精彩,希望这份指南能帮助你开启编程之旅!记住,编程最重要的是解决问题的思维,语法只是工具。坚持练习,你一定能成为优秀的程序员。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。