Python Resources

What is Python?

Python is a free, open-source programming language used for data analysis, scientific computing, and many other applications. Its readable syntax and extensive collection of packages make it a useful tool for students learning statistics and data science. Packages provide additional tools for tasks such as managing data, creating graphs, and fitting statistical models.

What can you do with Python?

  • Manage data: Import files, handle missing values, combine datasets, and create new variables.
  • Summarize data: Calculate descriptive statistics and create frequency tables.
  • Visualize data: Create histograms, boxplots, scatterplots, and other graphs.
  • Perform statistical analyses: Fit regression models, conduct hypothesis tests, and explore relationships among variables.

Get started with Python

You can run Python in a web browser or install it on your computer.

If you are using Python for a course, follow your instructor’s recommendations for software and packages.

Basic programming examples

The following examples use fictional exam scores and Python’s built-in tools. No additional packages are required. Run the examples in order within the same Python session or notebook.

Create and display data

A list stores a collection of values. This example creates a list of five exam scores and displays it.

scores = [85, 92, 78, 88, 97] 

print(scores)

The output shows the five scores: 85, 92, 78, 88, and 97.

Calculate summary statistics

Python’s statistics module includes functions for common descriptive statistics. The stdev function calculates the sample standard deviation.

import statistics print("Number of scores:", len(scores)) 

print("Mean:", statistics.mean(scores)) 
print("Median:", statistics.median(scores)) 
print("Sample standard deviation:",
         round(statistics.stdev(scores), 2)) 
print("Minimum:", min(scores)) 
print("Maximum:", max(scores))

The mean and median are both 88. The sample standard deviation is approximately 7.18, and scores range from 78 to 97.

Select observations that meet a condition

A list comprehension creates a new list from existing values. This example selects scores of at least 90.

high_scores = [score for score in scores if score >= 90]

print("Scores of at least 90:", high_scores)
print("Number of scores of at least 90:", len(high_scores))

The selected scores are 92 and 97, so two scores meet the condition.

Learning resources