Server Info & Course Info
“math”, …
mathimport math
print("Pi is approximately", math.pi)
print("Square root of 16:", math.sqrt(16))
help('modules')
pandas (data wizardry)requests (web communication)numpy (number crunching)import math
print("2^5 =", math.pow(2, 5))
from random import randint
print("Random dice roll:", randint(1, 6))
import pandas as pd
data = {'Birds': ['Eagle', 'Penguin'], 'Speed': [320, 8]}
df = pd.DataFrame(data)
print(df)
Combine different import styles:
from math import sqrt as sq
print("Square root of 121:", sq(121))
# Random password generator
from random import choice
chars = 'ABC123!@'
password = ''.join([choice(chars) for _ in range(8)])
print("Your new password:", password)
import datetime
now = datetime.datetime.now()
print("Current time:", now)
Output shows:
future_date = now + datetime.timedelta(days=3)
print("Date after 3 days:", future_date.date())
timedelta supports:
dayshoursminutes######
$ pip3 --version
pip 24.0 from ... (python 3.7)
$ pip3 install requests
Collecting requests...
Successfully installed requests-2.31.0
pip list$ pip3 list
Package Version
---------- -------
pip 21.2.4
requests 2.31.0
setuptools 57.4.0
$ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pandas
$ pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip.conf fileimport requests
response = requests.get("https://www.baidu.com")
print(f"Status Code: {response.status_code}")
print(f"Response Text: {response.text[:100]}...")
import requests
city = "Beijing"
url = f"http://wttr.in/{city}?format=3"
response = requests.get(url)
print(f"Weather in {city}: {response.text}")
Sample output:
Weather in Beijing: +25°C
$ pip3 install --upgrade requests
$ pip3 install requests==2.31.0
Goal: Combine web data and time handling
requests - Fetch data from websitesdatetime - Handle dates/timesTry this first:
import datetime
current_time = datetime.datetime.now()
print(f"Hello! It's {current_time:%H:%M} now!")
Here’s a simple weather API example:
import requests
def get_weather(city):
url = f"http://wttr.in/{city}?format=%t+%C"
response = requests.get(url)
return response.text
print(get_weather("London"))
Let’s make it friendly! ✨
def weather_report(city):
time_now = datetime.datetime.now().strftime("%H:%M")
weather = get_weather(city)
return f"[{time_now}] Weather in {city}: {weather}"
print(weather_report("NewYork"))
Goal: Automate file copying
os - Handle pathsshutil - File operationsPath check example:
import os
my_folder = "documents"
if not os.path.exists(my_folder):
os.makedirs(my_folder)
print(f"Created {my_folder}!")
Copy all text files:
import shutil
def backup_files(src_folder, dst_folder):
for file in os.listdir(src_folder):
if file.endswith(".txt"):
shutil.copy2(os.path.join(src_folder, file), dst_folder)
print(f"Copied {file}!")
backup_files("notes", "backup")