Beginners Python
Beginners Python
Beginners Python
Cheat Sheet
squares = [x**2 for x in range(1, 11)]
A simple dictionary
Slicing a list
alien = {'color': 'green', 'points': 5}
finishers = ['sam', 'bob', 'ada', 'bea']
first_two = finishers[:2] Accessing a value
Variables and Strings
Copying a list print(f"The alien's color is {alien['color']}.")
Variables are used to assign labels to values. A string is a
series of characters, surrounded by single or double quotes. copy_of_bikes = bikes[:] Adding a new key-value pair
Python's f-strings allow you to use variables inside strings to
alien['x_position'] = 0
build dynamic messages.
Tuples
Hello world Looping through all key-value pairs
Tuples are similar to lists, but the items in a tuple can't be
print("Hello world!") modified. fav_numbers = {'eric': 7, 'ever': 4, 'erin': 47}
Hello world with a variable Making a tuple for name, number in fav_numbers.items():
dimensions = (1920, 1080) print(f"{name} loves {number}.")
msg = "Hello world!"
print(msg) resolutions = ('720p', '1080p', '4K')
Looping through all keys
f-strings (using variables in strings) If statements fav_numbers = {'eric': 7, 'ever': 4, 'erin': 47}
first_name = 'albert' If statements are used to test for particular conditions and for name in fav_numbers.keys():
last_name = 'einstein' respond appropriately. print(f"{name} loves a number.")
full_name = f"{first_name} {last_name}"
print(full_name) Conditional tests Looping through all the values
equal x == 42
fav_numbers = {'eric': 7, 'ever': 4, 'erin': 47}
Lists not equal x != 42
A list stores a series of items in a particular order. You greater than x > 42
for number in fav_numbers.values():
or equal to x >= 42
access items using an index, or within a loop. print(f"{number} is a favorite.")
less than x < 42
Make a list or equal to x <= 42
User input
bikes = ['trek', 'redline', 'giant'] Conditional tests with lists Your programs can prompt the user for input. All input is
Get the first item in a list 'trek' in bikes stored as a string.
'surly' not in bikes
first_bike = bikes[0] Prompting for a value
Assigning boolean values name = input("What's your name? ")
Get the last item in a list
game_active = True print(f"Hello, {name}!")
last_bike = bikes[-1]
can_edit = False
Prompting for numerical input
Looping through a list
A simple if test age = input("How old are you? ")
for bike in bikes: age = int(age)
if age >= 18:
print(bike)
print("You can vote!")
Adding items to a list pi = input("What's the value of pi? ")
If-elif-else statements pi = float(pi)
bikes = []
if age < 4:
bikes.append('trek')
ticket_price = 0
bikes.append('redline')
bikes.append('giant')
elif age < 18:
ticket_price = 10 Python Crash Course
Making numerical lists elif age < 65: A Hands-on, Project-Based
ticket_price = 40 Introduction to Programming
squares = [] else:
for x in range(1, 11): ticket_price = 15 nostarch.com/pythoncrashcourse2e
squares.append(x**2)
While loops Classes Working with files
A while loop repeats a block of code as long as a certain A class defines the behavior of an object and the kind of Your programs can read from files and write to files. Files
condition is true. While loops are especially useful when you information an object can store. The information in a class are opened in read mode by default, but can also be opened
can't know ahead of time how many times a loop should run. is stored in attributes, and functions that belong to a class in write mode and append mode.
are called methods. A child class inherits the attributes and
A simple while loop Reading a file and storing its lines
methods from its parent class.
current_value = 1 filename = 'siddhartha.txt'
while current_value <= 5: Creating a dog class with open(filename) as file_object:
print(current_value) class Dog: lines = file_object.readlines()
current_value += 1 """Represent a dog."""
for line in lines:
Letting the user choose when to quit def __init__(self, name): print(line)
msg = '' """Initialize dog object."""
while msg != 'quit': self.name = name Writing to a file
The variable referring to the file object is often shortened to f.
msg = input("What's your message? ")
print(msg) def sit(self): filename = 'journal.txt'
"""Simulate sitting.""" with open(filename, 'w') as f:
Functions print(f"{self.name} is sitting.") f.write("I love programming.")
Functions are named blocks of code, designed to do one my_dog = Dog('Peso') Appending to a file
specific job. Information passed to a function is called an
filename = 'journal.txt'
argument, and information received by a function is called a print(f"{my_dog.name} is a great dog!") with open(filename, 'a') as f:
parameter. my_dog.sit() f.write("\nI love making games.")
A simple function Inheritance
def greet_user(): class SARDog(Dog):
Exceptions
"""Display a simple greeting.""" """Represent a search dog.""" Exceptions help you respond appropriately to errors that are
print("Hello!") likely to occur. You place code that might cause an error in
def __init__(self, name): the try block. Code that should run in response to an error
greet_user() """Initialize the sardog.""" goes in the except block. Code that should run only if the try
Passing an argument super().__init__(name) block was successful goes in the else block.
Cheat Sheet - Lists existing lists, or start with an empty list and then add items to
it as the program develops.
Adding an element to the end of the list
original list unchanged.
You can sort the items in a list in alphabetical order, or
reverse alphabetical order. You can also reverse the original
order of the list. Keep in mind that lowercase and uppercase
What are lists? users.append('amy') letters may affect the sort order.
A list stores a series of items in a particular order. Lists Starting with an empty list Sorting a list permanently
allow you to store sets of information in one place, users = [] users.sort()
whether you have just a few items or millions of items. users.append('amy')
Lists are one of Python's most powerful features users.append('val') Sorting a list permanently in reverse alphabetical order
readily accessible to new programmers, and they tie users.append('bob') users.sort(reverse=True)
together many important concepts in programming. users.append('mia')
Sorting a list temporarily
Inserting elements at a particular position
Defining a list print(sorted(users))
users.insert(0, 'joe') print(sorted(users, reverse=True))
Use square brackets to define a list, and use commas to users.insert(3, 'bea')
separate individual items in the list. Use plural names for Reversing the order of a list
lists, to make it clear that the variable represents more than
one item. Removing elements users.reverse()
You can remove elements by their position in a list, or by the
Making a list value of the item. If you remove an item by its value, Python Looping through a list
users = ['val', 'bob', 'mia', 'ron', 'ned'] removes only the first item that has that value. Lists can contain millions of items, so Python provides an
Deleting an element by its position efficient way to loop through all the items in a list. When
Accessing elements you set up a loop, Python pulls each item from the list one
del users[-1] at a time and assigns it to a temporary variable, which
Individual elements in a list are accessed according to their
position, called the index. The index of the first element is 0, Removing an item by its value you provide a name for. This name should be the singular
the index of the second element is 1, and so forth. Negative version of the list name.
users.remove('mia') The indented block of code makes up the body of the
indices refer to items at the end of the list. To get a particular
element, write the name of the list and then the index of the loop, where you can work with each individual item. Any
element in square brackets. Popping elements lines that are not indented run after the loop is completed.
If you want to work with an element that you're removing Printing all items in a list
Getting the first element from the list, you can "pop" the item. If you think of the list as
first_user = users[0] a stack of items, pop() takes an item off the top of the stack. for user in users:
By default pop() returns the last element in the list, but print(user)
Getting the second element you can also pop elements from any position in the list. Printing a message for each item, and a separate
second_user = users[1] message afterwards
Pop the last item from a list
Getting the last elements most_recent_user = users.pop() for user in users:
newest_user = users[-1] print(most_recent_user) print(f"\nWelcome, {user}!")
print("We're so glad you joined!")
Pop the first item in a list
Modifying individual items first_user = users.pop(0) print("\nWelcome, we're glad to see you all!")
Once you've defined a list, you can change the value of print(first_user)
individual elements in the list. You do this by referring to the
index of the item you want to modify.
List length
Changing an element The len() function returns the number of items in a list. Python Crash Course
users[0] = 'valerie' A Hands-on, Project-Based
Find the length of a list
users[1] = 'robert' Introduction to Programming
users[-2] = 'ronald' num_users = len(users)
print(f"We have {num_users} users.") nostarch.com/pythoncrashcourse2e
The range() function Copying a list Tuples
You can use the range() function to work with a set of To copy a list make a slice that starts at the first item and A tuple is like a list, except you can't change the values
numbers efficiently. The range() function starts at 0 by ends at the last item. If you try to copy a list without using in a tuple once it's defined. Tuples are good for storing
default, and stops one number below the number passed to this approach, whatever you do to the copied list will affect information that shouldn't be changed throughout the life of a
it. You can use the list() function to efficiently generate a the original list as well. program. Tuples are usually designated by parentheses.
large list of numbers. You can overwrite an entire tuple, but you can't change
Making a copy of a list
the values of individual elements.
Printing the numbers 0 to 1000 finishers = ['kai', 'abe', 'ada', 'gus', 'zoe']
for number in range(1001): copy_of_finishers = finishers[:] Defining a tuple
print(number) dimensions = (800, 600)
Printing the numbers 1 to 1000 List comprehensions Looping through a tuple
You can use a loop to generate a list based on a range of
for number in range(1, 1001): for dimension in dimensions:
numbers or on another list. This is a common operation,
print(number) print(dimension)
so Python offers a more efficient way to do it. List
Making a list of numbers from 1 to a million comprehensions may look complicated at first; if so, use Overwriting a tuple
the for loop approach until you're ready to start using
numbers = list(range(1, 1000001)) dimensions = (800, 600)
comprehensions.
print(dimensions)
To write a comprehension, define an expression for the
Simple statistics values you want to store in the list. Then write a for loop to dimensions = (1200, 900)
There are a number of simple statistical operations you can generate input values needed to make the list. print(dimensions)
run on a list containing numerical data.
Using a loop to generate a list of square numbers
Finding the minimum value in a list squares = [] Visualizing your code
ages = [93, 99, 66, 17, 85, 1, 35, 82, 2, 77] for x in range(1, 11): When you're first learning about data structures such as
youngest = min(ages) square = x**2 lists, it helps to visualize how Python is working with the
squares.append(square) information in your program. Python Tutor is a great tool for
Finding the maximum value seeing how Python keeps track of the information in a list.
Using a comprehension to generate a list of square
ages = [93, 99, 66, 17, 85, 1, 35, 82, 2, 77] Try running the following code on pythontutor.com, and then
oldest = max(ages) numbers run your own code.
squares = [x**2 for x in range(1, 11)]
Finding the sum of all values Build a list and print the items in the list
ages = [93, 99, 66, 17, 85, 1, 35, 82, 2, 77] Using a loop to convert a list of names to upper case dogs = []
total_years = sum(ages) names = ['kai', 'abe', 'ada', 'gus', 'zoe'] dogs.append('willie')
dogs.append('hootz')
Slicing a list upper_names = [] dogs.append('peso')
for name in names: dogs.append('goblin')
You can work with any subset of elements from a list. A
upper_names.append(name.upper())
portion of a list is called a slice. To slice a list start with the for dog in dogs:
index of the first item you want, then add a colon and the Using a comprehension to convert a list of names to print(f"Hello {dog}!")
index after the last item you want. Leave off the first index upper case print("I love these dogs!")
to start at the beginning of the list, and leave off the second
index to slice through the end of the list. names = ['kai', 'abe', 'ada', 'gus', 'zoe']
print("\nThese were my first two dogs:")
old_dogs = dogs[:2]
Getting the first three items upper_names = [name.upper() for name in names]
for old_dog in old_dogs:
finishers = ['kai', 'abe', 'ada', 'gus', 'zoe'] print(old_dog)
first_three = finishers[:3] Styling your code
Readability counts del dogs[0]
Getting the middle three items dogs.remove('peso')
middle_three = finishers[1:4] Follow common Python formatting conventions: print(dogs)
• Use four spaces per indentation level.
Getting the last three items • Keep your lines to 79 characters or fewer.
last_three = finishers[-3:] • Use single blank lines to group parts of your
program visually. More cheat sheets available at
ehmatthes.github.io/pcc_2e/
Beginner's Python
Adding new key-value pairs Looping through a dictionary
You can store as many key-value pairs as you want in a You can loop through a dictionary in three ways: you can
dictionary, until your computer runs out of memory. To add a loop through all the key-value pairs, all the keys, or all the
Cheat Sheet - new key-value pair to an existing dictionary give the name of
the dictionary and the new key in square brackets, and set it
equal to the new value.
values.
Dictionaries keep track of the order in which key-value
pairs are added. If you want to process the information in a
Dictionaries
This also allows you to start with an empty dictionary and different order, you can sort the keys in your loop, using the
add key-value pairs as they become relevant. sorted() function.
Adding a key-value pair Looping through all key-value pairs
What are dictionaries? alien_0 = {'color': 'green', 'points': 5} # Store people's favorite languages.
fav_languages = {
Python's dictionaries allow you to connect pieces of alien_0['x'] = 0 'jen': 'python',
related information. Each piece of information in a alien_0['y'] = 25 'sarah': 'c',
dictionary is stored as a key-value pair. When you alien_0['speed'] = 1.5 'edward': 'ruby',
provide a key, Python returns the value associated 'phil': 'python',
Starting with an empty dictionary }
with that key. You can loop through all the key-value
alien_0 = {}
pairs, all the keys, or all the values. # Show each person's favorite language.
alien_0['color'] = 'green'
alien_0['points'] = 5 for name, language in fav_languages.items():
Defining a dictionary print(f"{name}: {language}")
Use curly braces to define a dictionary. Use colons to
connect keys and values, and use commas to separate
Modifying values Looping through all the keys
individual key-value pairs. You can modify the value associated with any key in a # Show everyone who's taken the survey.
dictionary. To do so give the name of the dictionary and the for name in fav_languages.keys():
Making a dictionary key in square brackets, then provide the new value for that print(name)
alien_0 = {'color': 'green', 'points': 5} key.
Looping through all the values
Modifying values in a dictionary
Accessing values alien_0 = {'color': 'green', 'points': 5}
# Show all the languages that have been chosen.
for language in fav_languages.values():
To access the value associated with an individual key give print(alien_0) print(language)
the name of the dictionary and then place the key in a set
of square brackets. If the key you provided is not in the # Change the alien's color and point value. Looping through all the keys in reverse order
dictionary, an error will occur. alien_0['color'] = 'yellow'
# Show each person's favorite language,
You can also use the get() method, which returns alien_0['points'] = 10
# in reverse order by the person's name.
None instead of an error if the key doesn't exist. You can print(alien_0)
for name in sorted(fav_languages.keys(),
also specify a default value to use if the key is not in the reverse=True):
dictionary. Removing key-value pairs language = fav_languages[name]
Getting the value associated with a key You can remove any key-value pair you want from a print(f"{name}: {language}")
dictionary. To do so use the del keyword and the dictionary
alien_0 = {'color': 'green', 'points': 5}
name, followed by the key in square brackets. This will Dictionary length
print(alien_0['color']) delete the key and its associated value. You can find the number of key-value pairs in a dictionary
print(alien_0['points']) Deleting a key-value pair using the len() function.
Getting the value with get() alien_0 = {'color': 'green', 'points': 5} Finding a dictionary's length
print(alien_0) num_responses = len(fav_languages)
alien_0 = {'color': 'green'}
del alien_0['points']
alien_color = alien_0.get('color')
alien_points = alien_0.get('points', 0)
print(alien_0)
Python Crash Course
alien_speed = alien_0.get('speed') A Hands-on, Project-Based
Visualizing dictionaries
print(alien_color) Introduction to Programming
Try running some of these examples on pythontutor.com,
print(alien_points) and then run one of your own programs that uses nostarch.com/pythoncrashcourse2e
print(alien_speed) dictionaries.
Nesting - A list of dictionaries Nesting - Lists in a dictionary Dictionary Comprehensions
It's sometimes useful to store a number of dictionaries in a Storing a list inside a dictionary allows you to associate more A comprehension is a compact way of generating a
list; this is called nesting. than one value with each key. dictionary, similar to a list comprehension. To make a
dictionary comprehension, define an expression for the
Storing dictionaries in a list Storing lists in a dictionary
key-value pairs you want to make. Then write a for loop to
# Start with an empty list. # Store multiple languages for each person. generate the values that will feed into this expression.
users = [] fav_languages = { The zip() function matches each item in one list to each
'jen': ['python', 'ruby'], item in a second list. It can be used to make a dictionary
# Make a new user, and add them to the list. 'sarah': ['c'], from two lists.
new_user = { 'edward': ['ruby', 'go'],
'last': 'fermi', 'phil': ['python', 'haskell'], Using a loop to make a dictionary
'first': 'enrico', } squares = {}
'username': 'efermi', for x in range(5):
} # Show all responses for each person. squares[x] = x**2
users.append(new_user) for name, langs in fav_languages.items():
print(f"{name}: ") Using a dictionary comprehension
# Make another new user, and add them as well. for lang in langs:
squares = {x:x**2 for x in range(5)}
new_user = { print(f"- {lang}")
'last': 'curie', Using zip() to make a dictionary
'first': 'marie', Nesting - A dictionary of dictionaries
'username': 'mcurie', group_1 = ['kai', 'abe', 'ada', 'gus', 'zoe']
} You can store a dictionary inside another dictionary. In this group_2 = ['jen', 'eva', 'dan', 'isa', 'meg']
users.append(new_user) case each value associated with a key is itself a dictionary.
pairings = {name:name_2
Storing dictionaries in a dictionary for name, name_2 in zip(group_1, group_2)}
# Show all information about each user.
print("User summary:") users = {
for user_dict in users: 'aeinstein': { Generating a million dictionaries
for k, v in user_dict.items(): 'first': 'albert',
You can use a loop to generate a large number of
print(f"{k}: {v}") 'last': 'einstein',
'location': 'princeton', dictionaries efficiently, if all the dictionaries start out with
print("\n") similar data.
},
You can also define a list of dictionaries directly, A million aliens
without using append(): 'mcurie': {
'first': 'marie', aliens = []
# Define a list of users, where each user 'last': 'curie',
# is represented by a dictionary. 'location': 'paris', # Make a million green aliens, worth 5 points
users = [ }, # each. Have them all start in one row.
{ } for alien_num in range(1_000_000):
'last': 'fermi', new_alien = {
'first': 'enrico', for username, user_dict in users.items(): 'color': 'green',
'username': 'efermi', full_name = f"{user_dict['first']} " 'points': 5,
}, full_name += user_dict['last'] 'x': 20 * alien_num,
{ 'y': 0
'last': 'curie', location = user_dict['location'] }
'first': 'marie',
'username': 'mcurie', print("\nUsername: " + username) aliens.append(new_alien)
}, print(f"\tFull name: {full_name.title()}")
] print(f"\tLocation: {location.title()}") # Prove the list contains a million aliens.
num_aliens = len(aliens)
# Show all information about each user.
print("User summary:") Levels of nesting print("Number of aliens created:")
for user_dict in users: Nesting is extremely useful in certain situations. However, print(num_aliens)
for k, v in user_dict.items(): be aware of making your code overly complex. If you're
print(f"{k}: {v}") nesting items much deeper than what you see here there
print("\n") are probably simpler ways of managing your data, such as More cheat sheets available at
using classes. ehmatthes.github.io/pcc_2e/
Beginner's Python
Numerical comparisons If statements
Testing numerical values is similar to testing string values. Several kinds of if statements exist. Your choice of which to
use depends on the number of conditions you need to test.
Testing equality and inequality
Cheat Sheet - >>> age = 18
>>> age == 18
You can have as many elif blocks as you need, and the
else block is always optional.
Simple if statement
If Statements
True
>>> age != 18 age = 19
False
Cheat Sheet - matches the first argument in the function call with the first
parameter in the function definition, and so forth.
With keyword arguments, you specify which parameter
a variable which the return value can be assigned to. A
function stops running when it reaches a return statement.
Returning a single value
Functions
each argument should be assigned to in the function
call. When you use keyword arguments, the order of the def get_full_name(first, last):
arguments doesn't matter. """Return a neatly formatted full name."""
full_name = f"{first} {last}"
Using positional arguments return full_name.title()
What are functions?
def describe_pet(animal, name):
Functions are named blocks of code designed to do """Display information about a pet.""" musician = get_full_name('jimi', 'hendrix')
one specific job. Functions allow you to write code print(f"\nI have a {animal}.") print(musician)
once that can then be run whenever you need to print(f"Its name is {name}.")
Returning a dictionary
accomplish the same task.
describe_pet('hamster', 'harry') def build_person(first, last):
Functions can take in the information they need,
describe_pet('dog', 'willie') """Return a dictionary of information
and return the information they generate. Using about a person.
functions effectively makes your programs easier to Using keyword arguments """
write, read, test, and fix. def describe_pet(animal, name): person = {'first': first, 'last': last}
"""Display information about a pet.""" return person
Defining a function print(f"\nI have a {animal}.")
The first line of a function is its definition, marked by the print(f"Its name is {name}.") musician = build_person('jimi', 'hendrix')
keyword def. The name of the function is followed by a set print(musician)
of parentheses and a colon. A docstring, in triple quotes, describe_pet(animal='hamster', name='harry')
Returning a dictionary with optional values
describes what the function does. The body of a function is describe_pet(name='willie', animal='dog')
indented one level. def build_person(first, last, age=None):
Default values """Return a dictionary of information
To call a function, give the name of the function followed
about a person.
by a set of parentheses. You can provide a default value for a parameter. When """
Making a function function calls omit this argument the default value will be person = {'first': first, 'last': last}
used. Parameters with default values must be listed after if age:
def greet_user(): parameters without default values in the function's definition person['age'] = age
"""Display a simple greeting.""" so positional arguments can still work correctly.
print("Hello!")
Using a default value return person
greet_user() def describe_pet(name, animal='dog'): musician = build_person('jimi', 'hendrix', 27)
"""Display information about a pet.""" print(musician)
Passing information to a function print(f"\nI have a {animal}.")
Information that's passed to a function is called an argument; print(f"Its name is {name}.") musician = build_person('janis', 'joplin')
information that's received by a function is called a print(musician)
parameter. Arguments are included in parentheses after the describe_pet('harry', 'hamster')
describe_pet('willie')
function's name, and parameters are listed in parentheses in Visualizing functions
the function's definition. Using None to make an argument optional Try running some of these examples, and some of your own
Passing a simple argument def describe_pet(animal, name=None): programs that use functions, on pythontutor.com.
def greet_user(username): """Display information about a pet."""
print(f"\nI have a {animal}.")
"""Display a simple greeting."""
print(f"Hello, {username}!") if name: Python Crash Course
print(f"Its name is {name}.")
A Hands-on, Project-Based
greet_user('jesse') Introduction to Programming
greet_user('diana') describe_pet('hamster', 'harry')
greet_user('brandon') describe_pet('snake') nostarch.com/pythoncrashcourse2e
Passing a list to a function Passing an arbitrary number of arguments Modules
You can pass a list as an argument to a function, and the Sometimes you won't know how many arguments a function You can store your functions in a separate file called a
function can work with the values in the list. Any changes the will need to accept. Python allows you to collect an arbitrary module, and then import the functions you need into the
function makes to the list will affect the original list. You can number of arguments into one parameter using the * file containing your main program. This allows for cleaner
prevent a function from modifying a list by passing a copy of operator. A parameter that accepts an arbitrary number of program files. Make sure your module is stored in the same
the list as an argument. arguments must come last in the function definition. This directory as your main program.
parameter is often named *args.
Passing a list as an argument Storing a function in a module
The ** operator allows a parameter to collect an arbitrary
File: pizza.py
def greet_users(names): number of keyword arguments. These are stored as a
"""Print a simple greeting to everyone.""" dictionary with the parameter names as keys, and the def make_pizza(size, *toppings):
for name in names: arguments as values. This is often named **kwargs. """Make a pizza."""
msg = f"Hello, {name}!" print(f"\nMaking a {size} pizza.")
print(msg) Collecting an arbitrary number of arguments print("Toppings:")
def make_pizza(size, *toppings): for topping in toppings:
usernames = ['hannah', 'ty', 'margot'] """Make a pizza.""" print(f"- {topping}")
greet_users(usernames) print(f"\nMaking a {size} pizza.")
Importing an entire module
Allowing a function to modify a list File: making_pizzas.py Every function in the module is available in
print("Toppings:")
The following example sends a list of models to a function for the program file.
printing. The first list is emptied, and the second list is filled.
for topping in toppings:
print(f"- {topping}") import pizza
def print_models(unprinted, printed):
"""3d print a set of models.""" # Make three pizzas with different toppings. pizza.make_pizza('medium', 'pepperoni')
while unprinted: make_pizza('small', 'pepperoni') pizza.make_pizza('small', 'bacon', 'pineapple')
current_model = unprinted.pop() make_pizza('large', 'bacon bits', 'pineapple')
print(f"Printing {current_model}") make_pizza('medium', 'mushrooms', 'peppers', Importing a specific function
printed.append(current_model) 'onions', 'extra cheese') Only the imported functions are available in the program file.
from pizza import make_pizza
# Store some unprinted designs, Collecting an arbitrary number of keyword arguments
# and print each of them. def build_profile(first, last, **user_info): make_pizza('medium', 'pepperoni')
unprinted = ['phone case', 'pendant', 'ring'] """Build a dictionary for a user.""" make_pizza('small', 'bacon', 'pineapple')
printed = [] user_info['first'] = first
print_models(unprinted, printed) user_info['last'] = last Giving a module an alias
import pizza as p
print(f"\nUnprinted: {unprinted}") return user_info
print(f"Printed: {printed}") p.make_pizza('medium', 'pepperoni')
Preventing a function from modifying a list # Create two users with different kinds p.make_pizza('small', 'bacon', 'pineapple')
# of information.
The following example is the same as the previous one, except the Giving a function an alias
original list is unchanged after calling print_models().
user_0 = build_profile('albert', 'einstein',
location='princeton') from pizza import make_pizza as mp
def print_models(unprinted, printed):
"""3d print a set of models.""" user_1 = build_profile('marie', 'curie', mp('medium', 'pepperoni')
while unprinted: location='paris', field='chemistry') mp('small', 'bacon', 'pineapple')
current_model = unprinted.pop()
print(f"Printing {current_model}") print(user_0) Importing all functions from a module
printed.append(current_model) print(user_1) Don't do this, but recognize it when you see it in others' code. It can
result in naming conflicts, which can cause errors.
# Store some unprinted designs,
# and print each of them.
What's the best way to structure a function? from pizza import *
original = ['phone case', 'pendant', 'ring'] There are many ways to write and call a function. When
printed = [] you're starting out, aim for something that simply works. As make_pizza('medium', 'pepperoni')
you gain experience you'll develop an understanding of the make_pizza('small', 'bacon', 'pineapple')
print_models(original[:], printed) subtle advantages of different structures such as positional
print(f"\nOriginal: {original}") and keyword arguments, and the various approaches to
print(f"Printed: {printed}") importing functions. For now if your functions do what you More cheat sheets available at
need them to, you're doing well. ehmatthes.github.io/pcc_2e/
Beginner's Python
Creating and using a class (cont.) Class inheritance
Creating an instance from a class If the class you're writing is a specialized version of another
class, you can use inheritance. When one class inherits
Cheat Sheet -
filename = 'siddhartha.txt' f_path = "/home/ehmatthes/books/alice.txt"
Cheat Sheet - TestCase and write methods that begin with test_. Save this as
test_full_names.py
import unittest
tests. This will tell you whether the changes you made affected
existing behavior.
E
class NamesTestCase(unittest.TestCase):
================================================
ERROR: test_first_last (__main__.NamesTestCase)
Test names like Janis Joplin.
"""Tests for names.py.""" ------------------------------------------------
Why test your code? Traceback (most recent call last):
def test_first_last(self): File "test_full_names.py", line 10,
When you write a function or a class, you can also """Test names like Janis Joplin.""" in test_first_last
write tests for that code. Testing proves that your code full_name = get_full_name('janis', 'joplin')
works as it's supposed to in the situations it's designed 'joplin') TypeError: get_full_name() missing 1 required
to handle, and also when people use your programs in self.assertEqual(full_name, positional argument: 'last'
unexpected ways. Writing tests gives you confidence 'Janis Joplin')
that your code will work correctly as more people ------------------------------------------------
if __name__ == '__main__': Ran 1 test in 0.001s
begin to use your programs. You can also add new unittest.main()
features to your programs and know whether or not FAILED (errors=1)
you've broken existing behavior by running your tests. Running the test
A unit test verifies that one specific aspect of your Python reports on each unit test in the test case. The dot represents Fixing the code
a single passing test. Python informs us that it ran 1 test in less than When a test fails, the code needs to be modified until the test
code works as it's supposed to. A test case is a 0.001 seconds, and the OK lets us know that all unit tests in the test passes again. Don’t make the mistake of rewriting your tests to fit
collection of unit tests which verify that your code's case passed. your new code, otherwise your code will break for anyone who's
behavior is correct in a wide variety of situations. .
using it the same way it's being used in the failing test. Here we can
make the middle name optional.
---------------------------------------
Testing a function: a passing test Ran 1 test in 0.000s def get_full_name(first, last, middle=''):
Python's unittest module provides tools for testing your """Return a full name."""
code. To try it out, we’ll create a function that returns a full OK
name. We’ll use the function in a regular program, and then if middle:
build a test case for the function. full_name = f"{first} {middle} {last}"
Testing a function: A failing test else:
A function to test Failing tests are important; they tell you that a change in the full_name = f"{first} {last}"
Save this as full_names.py code has affected existing behavior. When a test fails, you
def get_full_name(first, last): need to modify the code so the existing behavior still works. return full_name.title()
"""Return a full name.""" Modifying the function Running the test
We’ll modify get_full_name() so it handles middle names, but Now the test should pass again, which means our original
full_name = f"{first} {last}" we’ll do it in a way that breaks existing behavior. functionality is still intact.
return full_name.title()
def get_full_name(first, middle, last): .
Using the function """Return a full name.""" ---------------------------------------
Save this as names.py full_name = f"{first} {middle} {last}" Ran 1 test in 0.000s
return full_name.title()
from full_names import get_full_name
Using the function OK
janis = get_full_name('janis', 'joplin')
print(janis) from full_names import get_full_name
Pygame is a framework for making games using Setting a custom background color bullet_rect = pygame.Rect(100, 100, 3, 15)
Colors are defined as a tuple of red, green, and blue values. Each color = (100, 100, 100)
Python. Making games is fun, and it’s a great way value ranges from 0-255. The fill() method fills the screen with the
to expand your programming skills and knowledge. color you specify, and should be called before you add any other pygame.draw.rect(screen, color, bullet_rect)
Pygame takes care of many of the lower-level tasks in elements to the screen.
building games, which lets you focus on the aspects of def __init__(self): Working with images
your game that make it interesting. --snip-- Many objects in a game are images that are moved around
self.bg_color = (225, 225, 225) the screen. It’s easiest to use bitmap (.bmp) image files, but
Installing Pygame you can also configure your system to work with jpg, png,
Pygame runs on all systems, and you should be able to def run_game(self):
and gif files as well.
install it in one line. while True:
for event in pygame.event.get(): Loading an image
Installing Pygame --snip--
ship = pygame.image.load('images/ship.bmp')
$ python -m pip install --user pygame
self.screen.fill(self.bg_color) Getting the rect object from an image
pygame.display.flip()
Starting a game ship_rect = ship.get_rect()
The following code sets up an empty game window, and
Pygame rect objects Positioning an image
starts an event loop and a loop that continually refreshes the With rects, it’s easy to position an image wherever you want on
screen. Many objects in a game can be treated as simple rectangles,
rather than their actual shape. This simplifies code without the screen, or in relation to another object. The following code
An empty game window positions a ship at the bottom center of the screen, by matching the
noticeably affecting game play. Pygame has a rect object
midbottom of the ship with the midbottom of the screen.
import sys that makes it easy to work with game objects.
import pygame ship_rect.midbottom = screen_rect.midbottom
Getting the screen rect object
We already have a screen object; we can easily access the rect Drawing an image to the screen
class AlienInvasion: object associated with the screen. Once an image is loaded and positioned, you can draw it to the
screen with the blit() method. The blit() method acts on
def __init__(self): self.screen_rect = self.screen.get_rect()
the screen object, and takes the image object and image rect as
pygame.init() arguments.
Finding the center of the screen
self.screen = pygame.display.set_mode(
Rect objects have a center attribute which stores the center point. # Draw ship to screen.
(1200, 800))
pygame.display.set_caption( screen_center = self.screen_rect.center screen.blit(ship, ship_rect)
"Alien Invasion") Transforming an image
Useful rect attributes
Once you have a rect object, there are a number of attributes The transform module allows you to make changes to an image
def run_game(self): such as rotation and scaling.
while True: that are useful when positioning objects and detecting relative
positions of objects. (You can find more attributes in the Pygame rotated_image = pygame.transform.rotate(
for event in pygame.event.get():
documentation. The self variable has been left off for clarity.) ship.image, 45)
if event.type == pygame.QUIT:
sys.exit() # Individual x and y values:
screen_rect.left, screen_rect.right
pygame.display.flip() screen_rect.top, screen_rect.bottom
screen_rect.centerx, screen_rect.centery Python Crash Course
if __name__ == '__main__': screen_rect.width, screen_rect.height A Hands-on, Project-Based
ai = AlienInvasion()
Introduction to Programming
ai.run_game() # Tuples
screen_rect.center nostarch.com/pythoncrashcourse2e
screen_rect.size
Working with images (cont.) Responding to mouse events Pygame groups (cont.)
The blitme() method Pygame’s event loop registers an event any time the mouse Removing an item from a group
Game objects such as ships are often written as classes. Then a moves, or a mouse button is pressed or released. It’s important to delete elements that will never appear again in the
blitme() method is usually defined, which draws the object to the game, so you don’t waste memory and resources.
Responding to the mouse button
screen.
for event in pygame.event.get(): bullets.remove(bullet)
def blitme(self):
if event.type == pygame.MOUSEBUTTONDOWN:
"""Draw ship at current location.""" Detecting collisions
ship.fire_bullet()
self.screen.blit(self.image, self.rect)
You can detect when a single object collides with any
Finding the mouse position
member of a group. You can also detect when any member
Responding to keyboard input The mouse position is returned as a tuple.
of one group collides with a member of another group.
Pygame watches for events such as key presses and mouse mouse_pos = pygame.mouse.get_pos()
actions. You can detect any event you care about in the Collisions between a single object and a group
event loop, and respond with any action that’s appropriate Clicking a button The spritecollideany() function takes an object and a group,
You might want to know if the cursor is over an object such as a and returns True if the object overlaps with any member of the
for your game.
button. The rect.collidepoint() method returns True when a group.
Responding to key presses point overlaps a rect object.
if pygame.sprite.spritecollideany(ship, aliens):
Pygame’s main event loop registers a KEYDOWN event any time a key if button_rect.collidepoint(mouse_pos): ships_left -= 1
is pressed. When this happens, you can check for specific keys.
start_game()
for event in pygame.event.get(): Collisions between two groups
if event.type == pygame.KEYDOWN: Hiding the mouse The sprite.groupcollide() function takes two groups, and two
if event.key == pygame.K_RIGHT: booleans. The function returns a dictionary containing information
pygame.mouse.set_visible(False)
about the members that have collided. The booleans tell Pygame
ship_rect.x += 1
whether to delete the members of either group that have collided.
elif event.key == pygame.K_LEFT:
ship_rect.x -= 1
Pygame groups collisions = pygame.sprite.groupcollide(
elif event.key == pygame.K_SPACE: Pygame has a Group class which makes working with a bullets, aliens, True, True)
ship.fire_bullet() group of similar objects easier. A group is like a list, with
elif event.key == pygame.K_q: some extra functionality that’s helpful when building games. score += len(collisions) * alien_point_value
sys.exit() Making and filling a group
Responding to released keys An object that will be placed in a group must inherit from Sprite. Rendering text
When the user releases a key, a KEYUP event is triggered. from pygame.sprite import Sprite, Group You can use text for a variety of purposes in a game. For
example you can share information with players, and you
for event in pygame.event.get():
class Bullet(Sprite): can display a score.
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT: ... Displaying a message
ship.moving_right = False def draw_bullet(self): The following code defines a message, then a color for the text
... and the background color for the message. A font is defined using
def update(self): the default system font, with a font size of 48. The font.render()
The game is an object ... function is used to create an image of the message, and we get the
In the overall structure shown here (under Starting a Game), rect object associated with the image. We then center the image on
the entire game is written as a class. This makes it possible bullets = Group() the screen and display it.
to write programs that play the game automatically, and
msg = "Play again?"
it also means you can build an arcade with a collection of new_bullet = Bullet()
msg_color = (100, 100, 100)
games. bullets.add(new_bullet)
bg_color = (230, 230, 230)
Looping through the items in a group
Pygame documentation The sprites() method returns all the members of a group. f = pygame.font.SysFont(None, 48)
The Pygame documentation is really helpful when building msg_image = f.render(msg, True, msg_color,
for bullet in bullets.sprites(): bg_color)
your own games. The home page for the Pygame project is
bullet.draw_bullet() msg_image_rect = msg_image.get_rect()
at pygame.org/, and the home page for the documentation is
at pygame.org/docs/. Calling update() on a group msg_image_rect.center = screen_rect.center
The most useful part of the documentation are the screen.blit(msg_image, msg_image_rect)
Calling update() on a group automatically calls update() on each
pages about specific parts of Pygame, such as the Rect() member of the group.
class and the sprite module. You can find a list of these
bullets.update() More cheat sheets available at
elements at the top of the help pages. ehmatthes.github.io/pcc_2e/
Beginner's Python
Customizing plots Customizing plots (cont.)
Plots can be customized in a wide variety of ways. Just Emphasizing points
about any element of a plot can be modified. You can plot as much data as you want on one plot. Here we replot