Student Handbook

Student Handbook

Open in ChatGPT
Ask ChatGPT about this page
Open in Claude
Ask Claude about this page

Overview

A short reference for the Python you use in cs17. Each page covers one function or idea, in plain words, with an example you can run and the mistake people make most often.

How to read a page

Section What it tells you
Synopsis How to write the call. Words in the call, like value, are placeholders.
Description What it does, and anything surprising.
Parameters What each value in the parentheses means.
Return value What you get back, and its type.
Example Code and exactly what it prints. Typed answers to input are shown after the prompt.
Common mistake Code that goes wrong, what Python says, and the fix.

Every example was run with Python 3.12, the version on your laptop, so the output and error messages match what you will see.

Want more detail? Every page links to the official Python documentation. You can also type help(print) at the Python prompt: run python3 with no file name, and leave with exit().

Built-in functions

Functions that are always there. You never import them.

Name What it does
print Shows values on the screen.
input Asks the user to type something, and returns it as a string.
int Converts a value to a whole number.
float Converts a value to a number with a decimal point.
str Converts a value to a string (text).
type Tells you the type of a value.
len Returns how many items a string or list has.
range Makes a sequence of whole numbers, most often for a for loop.
round Rounds a number.
abs Returns how far a number is from zero.
min Returns the smallest of several values.
max Returns the largest of several values.
sum Adds up all the numbers in a list.

Strings

Working with text. Most of these are methods, called with a dot: text.lower().

Name What it does
f-strings Puts values into text.
str.lower Returns a copy of a string in lowercase.
str.upper Returns a copy of a string in capitals.
str.strip Removes spaces (or other characters) from both ends of a string.
str.split Breaks a string into a list of parts.
str.join Joins a list of strings into one string.
str.replace Returns a copy of a string with one piece of text swapped for another.
str.startswith Checks how a string begins (or, with endswith, how it ends).
in Checks whether a piece of text, or an item, is inside another.

Lists

A list holds several values in order: [3, 1, 2].

Name What it does
list.append Adds an item to the end of a list.
list.pop Removes an item from a list and returns it.
list.sort Puts a list in order.
Indexing and slicing Gets one item, or a range of items, from a string or list by position.

math

Maths beyond + - * /. Start the file with import math.

Name What it does
math.sqrt Returns the square root of a number.
math.floor Rounds a number down.
math.ceil Rounds a number up.

random

Random numbers and choices, for games and simulations. Start the file with import random.

Name What it does
random.randint Picks a random whole number between two numbers.
random.choice Picks a random item from a list or a string.
Last updated 2 weeks ago