from math import sqrt
result = sqrt(9)
print(result)3.0
Python’s power comes from its libraries - collections of pre-written code for specific tasks.
math, datetime, randompandas, numpy, matplotlibLibraries save time and provide tested, optimized solutions to common problems.
Import specific functions from a library:
Use the function directly without a prefix.
Exercise 1 (with Gemini): Ask Gemini to “write Python code that imports the sqrt function from math and uses it”
Exercise 2 (on your own): Type from math import pi then print(pi) and run it.
Import the whole library and access its contents:
import math
result = math.sqrt(9)
print(f"Square root: {result}")
print(f"Pi: {math.pi}")
print(f"Cosine of 0: {math.cos(0)}")Square root: 3.0
Pi: 3.141592653589793
Cosine of 0: 1.0
Use library.function() syntax.
Exercise 1 (with Gemini): Ask Gemini to “write Python code that imports the math library and calculates the square root of 16”
Exercise 2 (on your own): Type import math then print(math.sqrt(25)) and run it.
Abbreviate library names with as:
Especially useful for libraries with long names!
Exercise 1 (with Gemini): Ask Gemini to “write Python code that imports math as m and uses it to calculate pi times 2”
Exercise 2 (on your own): Type import math as m then print(m.pi) and run it.
These conventions are universally used:
import pandas as pd # Data manipulation
import numpy as np # Numerical arrays
import matplotlib.pyplot as plt # Plotting
import seaborn as sns # Statistical plotting
import statsmodels.api as sm # StatisticsImportant: AI tools like Gemini understand these conventions!
If you ask about pd.DataFrame, it knows you mean pandas.
Three common patterns:
Exercise 1 (with Gemini): Ask Gemini to “explain the difference between ‘import math’ and ‘from math import sqrt’”
Exercise 2 (on your own): Type import math then from math import pi then print(math.sqrt(9), pi) and run it.
Use pip to install third-party libraries:
# Install a library
!pip install yfinance
# Install specific version
!pip install yfinance==0.2.50
# Upgrade to latest
!pip install --upgrade yfinance
# Check installed packages
!pip listNote: In Colab, use !pip. In a terminal, use pip without !.
Data Science: - pandas - data manipulation - numpy - numerical computing - scipy - scientific computing
Visualization: - matplotlib - basic plotting - seaborn - statistical plots - plotly - interactive plots
Finance: - yfinance - stock data - pandas-datareader - financial data
import math
from datetime import date, timedelta
# Use math
circle_area = math.pi * (5 ** 2)
print(f"Circle area: {circle_area:.2f}")
# Use datetime
today = date.today()
next_week = today + timedelta(weeks=1)
print(f"Today: {today}")
print(f"Next week: {next_week}")Circle area: 78.54
Today: 2025-11-30
Next week: 2025-12-07
Exercise 1 (with Gemini): Ask Gemini to “write Python code that imports and uses both math and datetime libraries”
Exercise 2 (on your own): Type import math then from datetime import date then print(math.pi, date.today()) and run it.
import mathfrom math import sqrtimport pandas as pdpd, np, plt, sns, smNext: We’ll use pandas for data manipulation!