Professional Documents
Culture Documents
2. Pok�mon Overview
The Pok�mon game centers around players called trainers that try to collect
fictional animals called Pok�mon.
3. Pok�mon Overview
These Pok�mon animals roam the fictional universe where the game takes place. When
a trainer encounters a Pok�mon, they try to capture that Pok�mon to add to their
collection.
4. Pok�mon Overview
If a trainer successfully captures a Pok�mon, it is stored in a tool called a
Pok�dex.
5. Pok�mon Description
Each Pok�mon comes with its own set of metadata.
6. Pok�mon Description
This metadata contains a name for each Pok�mon. It also has the generation of each
Pok�mon specifying what version of the game the Pok�mon appears in. Here, Squirtle,
a Pok�mon from generation one, is shown.
7. Pok�mon Description
The metadata also includes the Pok�mon's Type and whether or not it belongs to a
special category called Legendary.
8. Pok�mon Description
Each Pok�mon has a set of statistics that are numerical values for certain
categories like Health Points (called HP), Attack, and others. We'll use a dataset
that contains pieces of this metadata for the remainder of the chapter.
9. Combining objects
Suppose we have two lists: one of Pok�mon names and another of each Pok�mon's
Health Points. We want to combine these lists so that each Pok�mon is stored next
to its Health Points. We can iterate over the names list using enumerate and grab
each Pok�mon's corresponding Health Points using the index variable.
14. collections.Counter()
Using Counter from the collections module is a more efficient approach. Just import
Counter and provide the object to be counted. No need for a loop! Counter returns a
Counter dictionary of key-value pairs. When printed, it's ordered by highest to
lowest counts. If comparing runtime times, we'd see that using Counter takes half
the time as the standard dictionary approach!
18. itertools.combinations()
The combinations generator from itertools provides a more efficient solution.
First, we import combinations and then create a combinations object by providing
the poke_types list and the length of combinations we desire. combinations returns
a combinations object, which we unpack into a list and print to see the result. If
comparing runtimes, we'd see using combinations is significantly faster than the
nested loop.
Got It!
1. Set theory
In this lesson, we'll continue to use the Pok�mon dataset to enhance our skills
when it comes to comparing objects. Let's dive in.
2. Set theory
Often, we'd like to compare two objects to observe similarities and differences
between their contents. When doing this type of comparison, it's best to leverage a
branch of mathematics called set theory. As you know, Python comes with a built-in
set data type. Sets come with some handy methods we can use for comparing. We'll
explore each of these methods later on. The main takeaway is that when we'd like to
compare objects multiple times and in different ways, we should consider storing
our data in sets to leverage these elegant and efficient methods. Another nice
feature of Python sets is their ability to quickly check if a value exists within
its members. We call this membership testing. In this lesson, we'll show that using
the in operator with a set is much faster than using it with a list or tuple. Let's
explore a few examples.
Got It!
1. Eliminating loops
In this lesson, we'll discuss the concept of looping. Although using loops when
writing Python code isn't necessarily a bad design pattern, using extraneous loops
can be inefficient and costly. Let's explore some tools that can help us eliminate
the need to use loops in our code.
2. Looping in Python
Python comes with a few looping patterns that can be used when we want to iterate
over an object's contents. For loops iterate over elements of a sequence piece-by-
piece. While loops execute a loop repeatedly as long as some Boolean condition is
met. Nested loops use multiple loops inside one another. Although all of these
looping patterns are supported by Python, we should be careful when using them.
Because most loops are evaluated in a piece-by-piece manner, they are often an
inefficient solution.
Got It!
1. Writing better loops
We've discussed how loops can be costly and inefficient. But, sometimes you can't
eliminate a loop. In this lesson, we'll explore how to make loops more efficient
when looping is unavoidable.
2. Lesson caveat
Before diving in, some of the loops we'll discuss can be eliminated using
techniques covered in previous lessons. For demonstrative purposes, we'll assume
the use cases shown here are instances where a loop is unavoidable.