Introduction To Python Programming - WEB
Introduction To Python Programming - WEB
Programming
©2024 Rice University. Textbook content produced by OpenStax is licensed under a Creative Commons
Attribution 4.0 International License (CC BY 4.0). Under this license, any user of this textbook or the textbook
contents herein must provide proper attribution as follows:
- If you redistribute this textbook in a digital format (including but not limited to PDF and HTML), then you
must retain on every page the following attribution:
“Access for free at openstax.org.”
- If you redistribute this textbook in a print format, then you must include on every physical page the
following attribution:
“Access for free at openstax.org.”
- If you redistribute part of this textbook, then you must retain in every digital format page view (including
but not limited to PDF and HTML) and on every physical printed page the following attribution:
“Access for free at openstax.org.”
- If you use this textbook as a bibliographic reference, please include
https://openstax.org/details/books/introduction-python-programming in your citation.
For questions regarding this licensing, please contact [email protected]. This book utilizes the OpenStax
Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.
Trademarks
The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, OpenStax CNX logo,
OpenStax Tutor name, Openstax Tutor logo, Connexions name, Connexions logo, Rice University name, and
Rice University logo are not subject to the license and may not be reproduced without the prior and express
written consent of Rice University.
OpenStax provides free, peer-reviewed, openly licensed textbooks for introductory college and Advanced
Placement® courses and low-cost, personalized courseware that helps students learn. A nonprofit ed tech
initiative based at Rice University, we’re committed to helping students access the tools they need to complete
their courses and meet their educational goals.
RICE UNIVERSITY
OpenStax is an initiative of Rice University. As a leading research university with a distinctive commitment to
undergraduate education, Rice University aspires to path-breaking research, unsurpassed teaching, and
contributions to the betterment of our world. It seeks to fulfill this mission by cultivating a diverse community
of learning and discovery that produces leaders across the spectrum of human endeavor.
PHILANTHROPIC SUPPORT
OpenStax is grateful for the generous philanthropic partners who advance our mission to improve educational
access and learning for everyone. To see the impact of our supporter community and our most updated list of
Arthur and Carlyse Ciocca Charitable Foundation The Open Society Foundations
Bill & Melinda Gates Foundation The Bill and Stephanie Sick Fund
The William and Flora Hewlett Foundation Robin and Sandy Stuart Foundation
Preface 1
1 Statements 7
Introduction 7
1.1 Background 8
1.2 Input/output 10
1.3 Variables 14
1.4 String basics 16
1.5 Number basics 20
1.6 Error messages 24
1.7 Comments 27
1.8 Why Python? 31
1.9 Chapter summary 34
2 Expressions 39
Introduction 39
2.1 The Python shell 40
2.2 Type conversion 42
2.3 Mixed data types 45
2.4 Floating-point errors 48
2.5 Dividing integers 51
2.6 The math module 54
2.7 Formatting code 60
2.8 Python careers 65
2.9 Chapter summary 67
3 Objects 71
Introduction 71
3.1 Strings revisited 71
3.2 Formatted strings 76
3.3 Variables revisited 79
3.4 List basics 83
3.5 Tuple basics 85
3.6 Chapter summary 88
4 Decisions 91
Introduction 91
4.1 Boolean values 91
4.2 If-else statements 96
4.3 Boolean operations 100
4.4 Operator precedence 104
4.5 Chained decisions 107
4.6 Nested decisions 113
4.7 Conditional expressions 116
4.8 Chapter summary 118
5 Loops 121
Introduction 121
5.1 While loop 121
5.2 For loop 125
5.3 Nested loops 129
5.4 Break and continue 133
5.5 Loop else 137
5.6 Chapter summary 140
6 Functions 145
Introduction 145
6.1 Defining functions 145
6.2 Control flow 149
6.3 Variable scope 153
6.4 Parameters 158
6.5 Return values 163
6.6 Keyword arguments 168
6.7 Chapter summary 170
7 Modules 173
Introduction 173
7.1 Module basics 174
7.2 Importing names 177
7.3 Top-level code 180
7.4 The help function 184
7.5 Finding modules 189
7.6 Chapter summary 194
8 Strings 197
Introduction 197
8.1 String operations 197
8.2 String slicing 200
8.3 Searching/testing strings 204
8.4 String formatting 209
9 Lists 223
Introduction 223
9.1 Modifying and iterating lists 223
9.2 Sorting and reversing lists 226
9.3 Common list operations 229
9.4 Nested lists 231
9.5 List comprehensions 234
9.6 Chapter summary 238
10 Dictionaries 241
Introduction 241
10.1 Dictionary basics 241
10.2 Dictionary creation 243
10.3 Dictionary operations 245
10.4 Conditionals and looping in dictionaries 250
10.5 Nested dictionaries and dictionary comprehension 256
10.6 Chapter summary 260
11 Classes 265
Introduction 265
11.1 Object-oriented programming basics 265
11.2 Classes and instances 267
11.3 Instance methods 272
11.4 Overloading operators 276
11.5 Using modules with classes 281
11.6 Chapter summary 283
12 Recursion 287
Introduction 287
12.1 Recursion basics 287
12.2 Simple math recursion 289
12.3 Recursion with strings and lists 292
12.4 More math recursion 294
12.5 Using recursion to solve problems 297
12.6 Chapter summary 301
13 Inheritance 303
Introduction 303
13.1 Inheritance basics 303
13.2 Attribute access 306
13.3 Methods 310
13.4 Hierarchical inheritance 316
13.5 Multiple inheritance and mixin classes 320
13.6 Chapter summary 323
14 Files 327
Introduction 327
14.1 Reading from files 327
14.2 Writing to files 331
14.3 Files in different locations and working with CSV files 335
14.4 Handling exceptions 339
14.5 Raising exceptions 344
14.6 Chapter summary 347
Index 403
Preface
About OpenStax
OpenStax is part of Rice University, which is a 501(c)(3) nonprofit charitable corporation. As an educational
initiative, it's our mission to improve educational access and learning for everyone. Through our partnerships
with philanthropic organizations and our alliance with other educational resource companies, we're breaking
down the most common barriers to learning. Because we believe that everyone should and can have access to
knowledge.
Because our books are openly licensed, you are free to use the entire book or select only the sections that are
most relevant to the needs of your course. Feel free to remix the content by assigning your students certain
chapters and sections in your syllabus, in the order that you prefer. You can even provide a direct link in your
syllabus to the sections in the web view of your book.
Instructors also have the option of creating a customized version of their OpenStax book. Visit the Instructor
Resources section of your book page on OpenStax.org for more information.
Art attribution
In Introduction to Python Programming, most photos and third-party illustrations contain attribution to their
creator, rights holder, host platform, and/or license within the caption. Because the art is openly licensed,
anyone may reuse the art as long as they provide the same attribution to its original source. To maximize
readability and content flow, some art does not include attribution in the text. This art is part of the public
domain or under a CC0 or similar license, and can be reused without attribution. For illustrations (e.g. graphs,
charts, etc.) that are not credited, use the following attribution: Copyright Rice University, OpenStax, under CC
BY 4.0 license.
Errata
All OpenStax textbooks undergo a rigorous review process. However, like any professional-grade textbook,
errors sometimes occur. Since our books are web-based, we can make updates periodically when deemed
pedagogically necessary. If you have a correction to suggest, submit it through the link on your book page on
OpenStax.org. Subject matter experts review all errata suggestions. OpenStax is committed to remaining
transparent about all updates, so you will also find a list of past and pending errata changes on your book
page on OpenStax.org.
Format
You can access this textbook for free in web view or PDF through OpenStax.org. The web view is the
recommended format because it is the most accessible—including being WCAG 2.1 AA compliant—and most
current.
Introduction to Python Programming is an interactive offering that teaches basic programming concepts,
problem-solving skills, and the Python language using hands-on activities. The resource includes a unique,
integrated code runner, through which students can immediately apply what they learn to check their
understanding. Embedded videos, critical thinking exercises, and explorations of external programming tools
and activities all contribute to a meaningful and supportive learning experience.
The content is organized in chapters, with each chapter containing 6-8 sections. Each section follows the
pattern:
• Learning objectives
• 1–3 subsections
• Programming practice
The learning objectives are designed to help readers identify the section's focus. Each objective completes the
sentence, "By the end of this section you should be able to". The programming practice aligns with the
learning objectives and gives readers an opportunity to apply concepts learned in the section.
Pedagogical Foundation
Concise text and video-based animations
Introduction to Python Programming is designed to foster active learning and student engagement. It focuses
on interactivity and practice through its integrated code runner, videos, and links to external environments and
activities. With that focus, the material is often more concise, with less text and more activity.
Each section's content is organized in subsections. The subsection begins with a concise introduction to the
concept, with key term definitions and brief context for its relevance and importance. The concept is then
explained in detail using video-based animations and interactive learning questions.
Animation videos use a step-by-step approach to show the execution of Python code. Explanations for each
step describe Python syntax, show how fundamental programming concepts are used, illustrate how variables
are assigned, emphasize how code executes line by line, apply problem solving to create programs, and more.
CHECKPOINT
Learning questions
After introducing a new concept and explaining the concept with a video-based animation, each subsection
includes engagement in the form of learning questions. These questions reinforce the concepts taught,
explain concepts in more depth, directly address misconceptions and errors commonly made by new
programmers, and teach related concepts.
Explanations are provided for the incorrect answers. Incorrect answers' explanations include why the answer is
incorrect and help guide the reader to the correct answer.
Incorrect answer choices typically represent a misconception or are the result of making a common mistake.
Even if the correct answer is achieved, readers are encouraged to explore the explanations to gain awareness
of these common misconceptions.
The code runner requires the reader to pre-enter any input before running a program.
Bold
Indicates vocabulary words when first defined in the chapter.
Italic
Indicates emphasized text, filenames, and file extensions.
Constant width
Used for code listings and code elements within paragraphs. Code elements include variable names, Python
keywords, etc.
Ex:
Abbreviation for "Example:"
4 Preface
Senior contributing authors, left to right: Udayan Das, Aubrey Lawson, Chris Mayfield, and Narges Norouzi.
Udayan Das, PhD, is an Associate Professor and Program Director of Computer Science at Saint Mary's College
of California. He received his PhD in Computer Science and a master's in Computer Engineering from the
Illinois Institute of Technology. His research interests include wireless networks, computer science education
and broadening participation in computing, and knowledge graph backed language models for technical
document processing. He is also strongly committed to incorporating ethics into computer science and
engineering education, and the Computer Science program that he has developed and launched at Saint
Mary's College of California centers ethics and social justice while teaching students to be high-quality
computing professionals.
Aubrey Lawson is a CS Content Developer at zyBooks. She received her bachelor's and master's degrees in
Computer Science from Clemson University, and her PhD research focuses on CS education.
Chris Mayfield, PhD, is a Professor of Computer Science at James Madison University. His research focuses on
CS education and professional development at the undergraduate and high school levels. He received a PhD in
Computer Science from Purdue University and bachelor’s degrees in CS and German from the University of
Utah.
Narges Norouzi received her MS and PhD from the University of Toronto, focusing on applied deep learning.
She has since been involved in working on applied machine learning projects with a focus on biology and
education. Her CS education research focuses on using artificial intelligence in the classroom to close the
equity gap and leading student-centered programs that promote equity and access.
Contributing Authors
Yamuna Rajasekhar, PhD, is Director of Content, Authoring, and Research at Wiley. She works across disciplines
on research strategy, authoring pedagogy and training, and content development for Computer Science and
IT. She received her MS and PhD from University of North Carolina at Charlotte, focusing on Computer
Engineering education. Prior to joining Wiley as a content author, Yamuna was an Assistant Professor of
Computer Engineering at Miami University, where her research was focused on assistive technology with
embedded systems and Computer Engineering education.
Reed Kanemaru earned a BS in Computer Science from University of California, Riverside in 2020 and an MS in
Computer Science from University of California, Riverside in 2021. Since graduating, he has worked as a
Content/Software Developer at zyBooks.
Reviewers
Mel Akhimiemona, Community College of Baltimore County
Linda Tansil
Academic Integrity
Academic integrity builds trust, understanding, equity, and genuine learning. While students may encounter
significant challenges in their courses and their lives, doing their own work and maintaining a high degree of
authenticity will result in meaningful outcomes that will extend far beyond their college career. Faculty,
administrators, resource providers, and students should work together to maintain a fair and positive
experience.
We realize that students benefit when academic integrity ground rules are established early in the course. To
that end, OpenStax has created an interactive to aid with academic integrity discussions in your course.
6 Preface
Visit our academic integrity slider (https://www.openstax.org/r/academic-integrity-slider). Click and drag icons
along the continuum to align these practices with your institution and course policies. You may then include
the graphic on your syllabus, present it in your first course meeting, or create a handout for students.
At OpenStax we are also developing resources supporting authentic learning experiences and assessment.
Please visit this book's page for updates. For an in-depth review of academic integrity strategies, we highly
recommend visiting the International Center of Academic Integrity (ICAI) website at
https://academicintegrity.org/ (https://academicintegrity.org/).
Community Hubs
OpenStax partners with the Institute for the Study of Knowledge Management in Education (ISKME) to offer
Community Hubs on OER Commons—a platform for instructors to share community-created resources that
support OpenStax books, free of charge. Through our Community Hubs, instructors can upload their own
materials or download resources to use in their own courses, including additional ancillaries, teaching
material, multimedia, and relevant course content. We encourage instructors to join the hubs for the subjects
most relevant to your teaching and research as an opportunity both to enrich your courses and to engage with
other faculty. To reach the Community Hubs, visit www.oercommons.org/hubs/openstax
(https://oercommons.org/groups/openstax-introduction-to-python-programming/14678/?__hub_id=27).
Technology partners
As allies in making high-quality learning materials accessible, our technology partners offer optional low-cost
tools that are integrated with OpenStax books. To access the technology options for your text, visit your book
page on OpenStax.org.
Chapter Outline
1.1 Background
1.2 Input/output
1.3 Variables
1.4 String basics
1.5 Number basics
1.6 Error messages
1.7 Comments
1.8 Why Python?
1.9 Chapter summary
Introduction
Computers and programs are everywhere in today's world. Programs affect many aspects of daily life and
society as a whole. People depend on programs for communication, shopping, entertainment, health care, and
countless other needs. Learning how to program computers opens the door to many careers and
opportunities for building a better world.
Programs consist of statements to be run one after the other. A statement describes some action to be
carried out.
The statement print("Good morning") instructs Python to output the message "Good morning" to the
user. The statement count = 0 instructs Python to assign the integer 0 to the variable count.
This chapter introduces statements for input and output, assigning variables, and basic arithmetic. Making
mistakes is a normal part of programming, and the chapter includes advice on understanding error messages.
The chapter ends with a short history of Python and discusses why Python has become so popular today.
8 1 • Statements
1.1 Background
Learning Objectives
By the end of this section you should be able to
• Name two examples of computer programs in everyday life.
• Explain why Python is a good programming language to learn.
Computer programs
A computer is an electronic device that stores and processes information. Examples of computers include
smartphones, tablets, laptops, desktops, and servers. Technically, a program is a sequence of instructions that
a computer can run. Programs help people accomplish everyday tasks, create new technology, and have fun.
The goal of this book is to teach introductory programming and problem solving. Writing programs is a
creative activity, inherently useful, and rewarding! No prior background in computer science is necessary to
read this book. Many different types of programs exist, as shown in the illustration below. This book will focus
on general purpose programs that typically run "behind the scenes."
CHECKPOINT
CONCEPTS IN PRACTICE
EXPLORING FURTHER
Later chapters of this book show how to write analysis programs using real data. Example libraries that
provide access to online streaming services include Spotipy (https://openstax.org/r/100spotipy), Pytube
(https://openstax.org/r/100pytube), and Pydora (https://openstax.org/r/100pydora). Python-related tools
often have the letters "py" in their name.
One reason why Python is popular is because many libraries exist for doing real work. A library is a collection
of code that can be used in other programs. Python comes with an extensive Standard Library
(https://openstax.org/r/100pythlibrary) for solving everyday computing problems like extracting data from files
and creating summary reports. In addition, the community develops many other libraries for Python. Ex:
Pandas (https://openstax.org/r/100pandas) is a widely used library for data analysis.
Another reason why Python is popular is because the syntax is concise and straightforward. The syntax of a
language defines how code must be structured. Syntax rules define the keywords, symbols, and formatting
used in programs. Compared to other programming languages, Python is more concise and straightforward.
EXAMPLE 1.1
print("Hello, World!")
In contrast, the hello world program is five lines in Java (a different language).
However, conciseness is not the only consideration for which language is used. In different situations
different languages may be more appropriate. Ex: Java is often used in Android development.
CHECKPOINT
CONCEPTS IN PRACTICE
6. In the example programs above, what syntax required by Java is not required by Python?
a. semicolons
b. parentheses
c. quote marks
TRY IT
Favorite song
The program below asks for your name and displays a friendly greeting. Run the program and see what
happens. In the error message, EOF stands for End of File.
• Many of the programs in this chapter expect input from the user. Enter your name in the Input box
below the code. Run the program again, and see what changes.
• Copy the following lines to the end of the program: print("What is your favorite song?")
song = input() print("Cool! I like", song, "too.")
• The modified program reads two lines of input: name and song. Add your favorite song to the Input
box below your name, and run the program again.
The next section of the book will explain how print() and input() work. Feel free to experiment with this
code until you are ready to move on.
1.2 Input/output
Learning objectives
By the end of this section you should be able to
Basic output
The print() function displays output to the user. Output is the information or result produced by a program.
The sep and end options can be used to customize the output. Table 1.1 shows examples of sep and end.
Multiple values, separated by commas, can be printed in the same statement. By default, each value is
separated by a space character in the output. The sep option can be used to change this behavior.
By default, the print() function adds a newline character at the end of the output. A newline character tells
the display to move to the next line. The end option can be used to continue printing on the same line.
Code Output
CHECKPOINT
CONCEPTS IN PRACTICE
a. 555 0123
b. 5550123-
c. 555-0123
Spaces and newline characters are not inherently important. However, learning to be precise is an essential
skill for programming. Noticing little details, like how words are separated and how lines end, helps new
programmers become better.
Basic input
Computer programs often receive input from the user. Input is what a user enters into a program. An input
statement, variable = input("prompt"), has three parts:
1. A variable refers to a value stored in memory. In the statement above, variable can be replaced with any
name the programmer chooses.
2. The input() function reads one line of input from the user. A function is a named, reusable block of code
that performs a task when called. The input is stored in the computer's memory and can be accessed later
using the variable.
3. A prompt is a short message that indicates the program is waiting for input. In the statement above,
"prompt" can be omitted or replaced with any message.
CHECKPOINT
CONCEPTS IN PRACTICE
a. Value = six
b. Value = 6
c. Value = number
TRY IT
Frost poem
Write a program that uses multiple print() statements to output the following poem by Robert Frost
(https://openstax.org/r/100robertfrost). Each print() statement should correspond to one line of output.
Tip: You don't need to write the entire program all at once. Try writing the first print() statement, and
then click the Run button. Then write the next print() statement, and click the Run button again.
Continue writing and testing the code incrementally until you finish the program.
TRY IT
Shakira
What do you like? singing
Output a blank line after reading the input. Then output the following message based on the input:
14 1 • Statements
1.3 Variables
Learning objectives
By the end of this section you should be able to
Assignment statement
Variables allow programs to refer to values using names rather than memory locations. Ex: age refers to a
person's age, and birth refers to a person's date of birth.
A statement can set a variable to a value using the assignment operator (=). Note that this is different from
the equal sign of mathematics. Ex: age = 6 or birth = "May 15". The left side of the assignment statement
is a variable, and the right side is the value the variable is assigned.
CHECKPOINT
CONCEPTS IN PRACTICE
city = "Chicago"
total = input()
A name should be short and descriptive, so words are preferred over single characters in programs for
readability. Ex: A variable named count indicates the variable's purpose better than a variable named c.
Python has reserved words, known as keywords, which have special functions and cannot be used as names
for variables (or other objects).
CONCEPTS IN PRACTICE
c. import
8. Given the variable name, DogBreed, which improvement conforms to Python's style guide?
a. dog_breed
b. dogBreed
c. dog-breed
TRY IT
Final score
Write a Python computer program that:
Quote marks
A string is a sequence of characters enclosed by matching single (') or double (") quotes. Ex: "Happy
birthday!" and '21' are both strings.
To include a single quote (') in a string, enclose the string with matching double quotes ("). Ex: "Won't this
work?" To include a double quote ("), enclose the string with matching single quotes ('). Ex: 'They said "Try
it!", so I did'.
"17" or '17' 17
"I hope you aren't sad." 'I hope you aren't sad.'
'The teacher said "Correct!" ' "The teacher said "Correct!" "
CONCEPTS IN PRACTICE
len() function
A common operation on a string object is to get the string length, or the number of characters in the string.
The len() function, when called on a string value, returns the string length.
CHECKPOINT
1-4-string-basics)
CONCEPTS IN PRACTICE
number = "12"
number_of_digits = len(number)
print("Number", number, "has", number_of_digits, "digits.")
Concatenation
Concatenation is an operation that combines two or more strings sequentially with the concatenation operator
(+). Ex: "A" + "part" produces the string "Apart".
CHECKPOINT
CONCEPTS IN PRACTICE
String concatenation
8. Which produces the string "10"?
a. 1 + 0
b. "1 + 0"
c. "1" + "0"
a. "wake" + "A"
b. "A + wake"
c. "A" + "wake"
TRY IT
Name length
Write a program that asks the user to input their first and last name separately. Use the following prompts
(example input in bold):
The program should then output the length of each name. Based on the example input above, the output
would be:
TRY IT
Punctuation
Write a Python computer program that:
CHECKPOINT
CONCEPTS IN PRACTICE
print(x, type(x))
a. 1 'int'.
b. 1.0 <class 'float'>.
c. 1 <class 'int'>.
print(y, type(y))
"12.0"
a. string
b. int
c. float
Basic arithmetic
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication,
and division.
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
CHECKPOINT
CONCEPTS IN PRACTICE
4. Given the following lines of code, what is the output of the code?
c = 0
c = x - z
c = c + 1
print(c)
a. 1
b. 5
c. 6
a = 3.5 - 1.5
a. 2
b. 2.0
22 1 • Statements
c. 2.5
Operator precedence
When a calculation has multiple operators, each operator is evaluated in order of precedence. Ex: 1 + 2 * 3
is 7 because multiplication takes precedence over addition. However, (1 + 2) * 3 is 9 because parentheses
take precedence over multiplication.
() Parentheses (1 + 2) * 3 9
** Exponentiation 2 ** 4 16
*, / Multiplication, division 2 * 3 6
+, - Addition, subtraction 1 + 2 3
CHECKPOINT
CONCEPTS IN PRACTICE
result = -2 ** 3
a. 1
b. 2
c. 3
TRY IT
1. Defines an integer variable named 'int_a' and assigns 'int_a' with the value 10.
2. Defines a floating-point variable named 'float_a' and assigns 'float_a' with the value 10.0.
3. Defines a string variable named 'string_a' and assigns 'string_a' with the string value "10".
4. Prints the value of each of the three variables along with their type.
TRY IT
Meters to feet
Write a Python computer program that:
When an error occurs, Python displays a message with the following information:
Ex: Typing print "Hello!" without parentheses is a syntax error. In Python, parentheses are required to use
print. When attempting to run print "Hello!", Python displays the following error:
The caret character (^) shows where Python found the error. Sometimes the error may be located one or two
lines before where the caret symbol is shown because Python may not have discovered the error until then.
Traceback is a Python report of the location and type of error. The word traceback suggests a programmer
trace back in the code to find the error if the error is not seen right away.
Learning to read error messages carefully is an important skill. The amount of technical jargon can be
overwhelming at first. But this information can be very helpful.
CHECKPOINT
CONCEPTS IN PRACTICE
Parts of an error
Given the following error message:
word = input("Type a SyntaxError: EOL The closing quote marks are missing. As a result,
word: ) while scanning the string does not terminate before the end of
string literal line (EOL).
CONCEPTS IN PRACTICE
Types of errors
For each program below, what type of error will occur?
4. print("Breakfast options:")
print("A. Cereal")
print("B. Eggs")
print("C. Yogurt")
choice = input("What would you like? ")
a. IndentationError
b. NameError
c. SyntaxError
a. IndentationError
b. NameError
c. SyntaxError
6. print("Breakfast options:")
print(" A. Cereal")
print(" B. Eggs")
print(" C. Yogurt")
choice = intput("What would you like? ")
a. IndentationError
b. NameError
c. SyntaxError
TRY IT
Three errors
The following program has three errors.
• Run the program to find the first error, and correct the corresponding line of code.
• Then run the program again to find and correct the second error.
• Keep running and correcting the program until no errors are found.
TRY IT
Wrong symbols
This code is based on an earlier example, but the code contains several mistakes.
• One line is missing required punctuation, and another line uses incorrect symbols.
• Run the program to find the first error, and correct the corresponding line of code.
• Keep running and correcting the program until no errors are found.
1.7 Comments
Learning objectives
By the end of this section you should be able to
• Write concise, meaningful comments that explain intended functionality of the code.
• Write a docstring (more verbose comment) that describes the program functionality.
• The # character should be followed by a single space. Ex: # End of menu is easier to read than #End of
menu.
• Comments should explain the purpose of the code, not just repeat the code itself. Ex: # Get the user's
preferences is more descriptive than # Input item1 and item2.
28 1 • Statements
EXAMPLE 1.2
CONCEPTS IN PRACTICE
Simple comments
1. The main purpose of writing comments is to _____.
a. avoid writing syntax errors
b. explain what the code does
c. make the code run faster
Code quality
The example program above had two parts: (1) display the menu options, and (2) get the user's preferences.
Together, the blank lines and comments show the overall structure of the program.
Programmers spend more time reading code than writing code. Therefore, making code easier for others to
read and understand is important. Two ways to improve code quality include:
• Separate each part (lines that have a similar purpose) with a blank line.
• Write a comment before each part. Not every line needs a comment.
CHECKPOINT
Comments in a program
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
1-7-comments)
CONCEPTS IN PRACTICE
Code quality
4. Which comment is most useful for the following code?
a. After line 1
b. After line 2
c. After line 3
Documentation
Python programs may optionally begin with a string known as a docstring. A docstring is documentation
written for others who will use the program but not necessarily read the source code. Most of the official
documentation at docs.python.org (https://openstax.org/r/100docstrings) is generated from docstrings.
Documentation can be long, so docstrings are generally written as multi-line strings ("""). Common
elements of a docstring include a one-line summary, a blank line, and a longer description.
CHECKPOINT
Vacations docstring
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
1-7-comments)
30 1 • Statements
CONCEPTS IN PRACTICE
Documentation
7. The main purpose of writing docstrings is to . . .
a. summarize the program's purpose or usage.
b. explain how each part of the code works.
c. maintain a list of ideas for new features.
TRY IT
Whose birthday
Add two comments to the following program: one for the input, and one for the output. Separate the input
and output with a blank line. Then, compare your comments with the sample solution, and ask yourself the
following questions:
TRY IT
Gravity calculation
Write a docstring for the following program. The first line of the docstring should explain, in one short
sentence, what the program is. The second line of the docstring should be blank. The third and subsequent
lines should include a longer explanation of what the program does. At the end of the docstring, add a line
that says "Author: " followed by your name.
• Name two historical facts about how Python was first created.
• Describe two ways Python is considered a popular language.
Historical background
Python has an interesting history. In 1982, Guido van Rossum (https://openstax.org/r/100vanRossum), the
creator of Python, started working at CWI (https://openstax.org/r/100CWI), a Dutch national research institute.
He joined a team that was designing a new programming language, named ABC, for teaching and prototyping.
ABC's simplicity was ideal for beginners, but the language lacked features required to write advanced
programs.
Several years later, van Rossum joined a different team at CWI working on an operating system. The team
needed an easier way to write programs for monitoring computers and analyzing data. Languages common in
the 1980's were (and still are) difficult to use for these kinds of programs. van Rossum envisioned a new
language that would have a simple syntax, like ABC, but also provide advanced features that professionals
would need.
At first, van Rossum started working on this new language as a hobby during his free time. He named the
language Python because he was a fan of the British comedy group Monty Python (https://openstax.org/r/
100MontyPython). Over the next year, he and his colleagues successfully used Python many times for real
work. van Rossum eventually decided to share Python with the broader programming community online. He
freely shared Python's entire source code so that anyone could write and run Python programs.
Python's first release, known as Version 0.9.0, appeared in 1991, about six years after C++ and four years
before Java. van Rossum's decisions to make the language simple yet advanced, suitable for everyday tasks,
and freely available online contributed to Python's long-term success.
CHECKPOINT
Key decisions
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/1-8-why-
python)
CONCEPTS IN PRACTICE
Python history
1. The Python programming language was named after a _____.
32 1 • Statements
EXPLORING FURTHER
For more details about Python's history, see "A brief history of Python (https://openstax.org/r/100history)"
by Vasilisa Sheromova, and "History and Philosophy of Python (https://openstax.org/r/100sheromova)" by
Bernd Klein.
Popularity of Python
Over the years, Python has become a nonprofit organization with a thriving community. Millions of
programmers around the world use Python for all kinds of interesting projects. Hundreds of thousands of
Python libraries have been released as open source software. The Python community is very active and
supportive online, answering questions and sharing code.
One way to see Python's popularity is the TIOBE index (https://openstax.org/r/100TIOBE). TIOBE is a Dutch
company that provides products and services for measuring software code quality. Since 2001, TIOBE has
tracked the popularity of programming languages and posted the results online. Figure 1.2 shows the TIOBE
index over time for five of the most popular languages.
The TIOBE index is based on the number of search engine results for each language. The percentage refers to
how many results belong to that language. Python has been among the top 10 languages every year since
2004. In October 2021, Python became the #1 language on the TIOBE index. No other language but C and Java
had been #1 for the previous 20 years.
Another way to see Python's popularity is to analyze how frequently Python is discussed online. Stack Overflow
(https://openstax.org/r/100overflow) is a question-and-answer website for programmers. Figure 1.3 shows the
number of questions asked each month that were tagged with Python, JavaScript, and so forth. In recent
years, Python has become the most asked about language in programming forums.
Figure 1.3 Stack Overflow questions per month. Source: data.stackexchange.com (https://data.stackexchange.com/)
CONCEPTS IN PRACTICE
Python popularity
4. According to the TIOBE Index, which two languages were most popular from 2001 to 2021?
34 1 • Statements
a. C and C++
b. Java and C
c. Python and JavaScript
5. In what year did Python become the most asked about language on Stack Overflow?
a. 2018
b. 2019
c. 2020
At this point, you should be able to write programs that ask for input, perform simple calculations, and output
the results. The programming practice below ties together most topics presented in the chapter.
Function Description
print(values) Outputs one or more values, each separated by a space, to the user.
If present, prompt is output to the user. The function then reads a line of input from the
input(prompt) user.
type(value) Returns the type (or class) of a value. Ex: type(123) is <class 'int'>.
Operator Description
= Assigns (or updates) the value of a variable. In Python, variables begin to exist when
(Assignment) assigned for the first time.
Function Description
+
(Concatenation) Appends the contents of two strings, resulting in a new string.
+
(Addition) Adds the values of two numbers.
-
(Subtraction) Subtracts the value of one number from another.
*
(Multiplication) Multiplies the values of two numbers.
/
(Division) Divides the value of one number by another.
**
Raises a number to a power. Ex: 3**2 is three squared.
(Exponentiation)
Syntax Description
#
(Comment) All text is ignored from the # symbol to the end of the line.
' or " Strings may be written using either kind of quote. Ex: 'A' and "A" represent the same
(String) string. By convention, this book uses double quotes (") for most strings.
""" Used for documentation, often in multi-line strings, to summarize a program's purpose
(Docstring) or usage.
TRY IT
Fun facts
Write a program that assigns a variable named number to any integer of your choice. Ex: number = 74.
Then, use this variable to calculate and output the following results:
74 squared is 5476
74 cubed is 405224
One tenth of 74 is 7.4
36 1 • Statements
Run the program multiple times, using a different integer each time. Your output should be mathematically
correct for any integer that you choose.
The point of this exercise is to perform basic arithmetic within a print statement. Do not use any other
variables besides number. Your program should have only one assignment statement (at the beginning).
TRY IT
Mad lib
A mad lib is a word game in which one person asks others for words to substitute into a pre-written story.
The story is then read aloud with the goal of making people laugh.
This exercise is based the Vacations Mad Lib (https://openstax.org/r/100madlibs) available on the Printables
(https://openstax.org/r/100printable) section of MadLibs.com. Write a program that asks the user to input
two adjectives and two nouns (user input in bold):
tranquil
Adjective: scandalous
Noun: pancake
Noun: field
Adjective: tranquil
Adjective: scandalous
Noun: pancake
Noun: field
Use input() to display each prompt exactly as shown. The user's input should be on the same line as the
prompt. Each colon must be followed by exactly one space. After reading the input, the program should
output the following three lines:
A vacation is when you take a trip to some tranquil place with your scandalous
family.
Usually you go to some place that is near a/an pancake or up on a/an field.
Notice that the first line should be completely blank. Replace the bold words (from the above example) with
the actual words input by the user.
Your final program should have four input statements, three print statements, and at least two comments.
Figure 2.1 credit: modification of work "Grace Hopper at Univac I console", courtesy of the Computer History Museum
Chapter Outline
2.1 The Python shell
2.2 Type conversion
2.3 Mixed data types
2.4 Floating-point errors
2.5 Dividing integers
2.6 The math module
2.7 Formatting code
2.8 Python careers
2.9 Chapter summary
Introduction
A computer program is a sequence of statements that run one after the other. In Python, many statements
consist of one or more expressions. An expression represents a single value to be computed. Ex: The
expression 3*x - 5 evaluates to 7 when x is 4. Learning to recognize expressions opens the door for
programming all kinds of interesting calculations.
Expressions are often a combination of literals, variables, and operators. In the previous example, 3 and 5 are
literals, x is a variable, and * and - are operators. Expressions can be arbitrarily long, consisting of many
calculations. Expressions can also be as short as one value. Ex: In the assignment statement x = 5, the literal
5 is an expression.
The Statements chapter introduced simple expressions like 1 * 2 and "Hi " + "there". This chapter
explores other kinds of expressions for working with numbers and strings. The first section shows a great way
to experiment with expressions using a Python shell. Later sections present more details about integers and
floating-point numbers, explain how to import and use the math module, and show how to make long lines of
code easier to read.
40 2 • Expressions
The interpreter
Python is a high-level language, meaning that the source code is intended for humans to understand.
Computers, on the other hand, understand only low-level machine code made up of 1's and 0's. Programs
written in high-level languages must be translated into machine code to run. This translation process can
happen all at once, or a little at a time, depending on the language.
Python is an interpreted language: the source code is translated one line at a time while the program is
running. The Python interpreter translates source code into machine code and runs the resulting program. If
and when an error occurs, the interpreter stops translating the source code and displays an error message.
Most development environments include a Python shell for experimenting with code interactively. A shell, also
called a console or terminal, is a program that allows direct interaction with an interpreter. The interpreter
usually runs an entire program all at once. But the interpreter can run one line of code at a time within a
Python shell.
CHECKPOINT
CONCEPTS IN PRACTICE
2. Which of the following is the most basic line of code the interpreter can run?
a. print(1 + 1)
b. 1 + 1
c. 1
3. What result does the shell display after running the line name = input()?
a. the name that was input
b. nothing (except for >>>)
results, and then try something else. When an error occurs, an error message is displayed, but the program
keeps running. That way, the user can edit the previous line and correct the error interactively.
The acronym REPL (pronounced "rep ul") is often used when referring to a shell. REPL stands for "read-eval-
print loop," which describes the repetitive nature of a shell:
Most shells maintain a history of every line of code the user types. Pressing the up or down arrow key on the
keyboard displays the history. The up arrow displays the previous line; the down arrow displays the next line.
That way, the user can repeat a line without having to type the line again.
CHECKPOINT
Correcting a typo
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/2-1-the-
python-shell)
CONCEPTS IN PRACTICE
TRY IT
• x = 5
• 3*x - 5
• 3 * (x-5)
• x
• type(1)
• type('1')
42 2 • Expressions
• str(1)
• int('1')
• abs(-5)
• abs(5)
• len("Yo")
• len("HoHo")
• round(9.49)
• round(9.50)
Note: These functions (type, str, int, len, and round) will be explored in more detail later in the chapter.
You can read more about the built-in functions (https://openstax.org/r/100builtin) in the Python
documentation.
TRY IT
Correcting mistakes
Open a Python shell on your computer, or use the one at python.org/shell (https://openstax.org/r/
100pythonshell). Run the following two statements in the shell:
• x = 123
• y = 456
Making mistakes is common while typing in a shell. The following lines include typos and other errors. For
each line: (1) run the line in a shell to see the result, (2) press the up arrow to repeat the line, and (3) edit
the line to get the correct result.
• print("Easy as", X)
• print("y divided by 2 is", y / 0)
• name = intput("What is your name? ")
• print(name, "is", int(name), "letters long.")
• print("That's all folks!)
• Easy as 123
• y divided by 2 is 228.0
• (no error/output)
• Stacie is 6 letters long.
• That's all folks!
CHECKPOINT
CONCEPTS IN PRACTICE
x = 42.0
x = x * 1
a. float
b. integer
• int() converts a data type to an integer. Any fractional part is removed. Ex: int(5.9) produces 5.
• float() converts a data type to a float. Ex: float(2) produces 2.0.
• str() converts a data type to a string. Ex: str(3.14) produces "3.14".
44 2 • Expressions
CHECKPOINT
CONCEPTS IN PRACTICE
4. Which function converts the input number of slices to a data type that can be used in the calculation?
a. float()
b. input()
c. int()
CONCEPTS IN PRACTICE
6. y
a. 4
b. 5
7. str(x)
a. 4.5
b. "4.5"
8. float(y)
a. 4.0
b. 4.5
TRY IT
Grade average
The following program computes the average of three predefined exam grades and prints the average
twice. Improve the program to read the three grades from input and print the average first as a float, and
then as an integer, using explicit type conversion. Ignore any differences that occur due to rounding.
TRY IT
Cups of water
The following program should read in the ounces of water the user drank today and compute the number
of cups drank and the number of cups left to drink based on a daily goal. Assume a cup contains 8 ounces.
Fix the code to calculate cups_drank and cups_left and match the following:
TRY IT
Product as float
The following program reads two integers in as strings. Calculate the product of the two integers, and print
the result as a float.
• Identify the data types produced by operations with integers, floats, and strings.
• Use operators and type conversions to combine integers, floats, and strings.
quantity = int(input())
price = float(input())
total = quantity * price
print(total)
quantity is an integer, and price is a float. So what is the data type of total? For input 3 and 5.0, total is
46 2 • Expressions
Combining an integer and a float produces a float. A float is by default printed with at least one figure after the
decimal point and has as many figures as needed to represent the value. Note: Division using the / operator
always produces a float.
CHECKPOINT
CONCEPTS IN PRACTICE
2. 2 * 9
a. 18
b. 18.0
3. 20 / 2
a. 10
b. 10.0
4. 7 / 2
a. 3.0
b. 3.5
5. 12.0 / 4
a. 3
b. 3.0
6. 8 - 1.0
a. 7.0
b. 7
7. 5 - 0.25
a. 4.5
b. 4.75
an error in the program because the input() function by default stores the number as a string. Strings and
numeric data types are incompatible for addition, subtraction, and division. One of the operands needs to be
explicitly converted depending on the goal of arithmetic or string concatenation.
The * operator also serves as the repetition operator, which accepts a string operand and an integer operand
and repeats the string. Ex: "banjo" * 3 produces "banjobanjobanjo".
CHECKPOINT
CONCEPTS IN PRACTICE
10. '50' * 3
a. '150'
b. 150
c. '505050'
11. str(5.2) + 7
a. 12.2
b. '12.2'
c. Error
a. 'this'
b. 'thisthis'
c. Error
TRY IT
TRY IT
Print n times
Write a program that reads in two strings, str1 and str2, and an integer, count. Concatenate the two
strings with a space in between and a newline ("\n") at the end. Print the resulting string count times.
Floating-point errors
Computers store information using 0's and 1's. All information must be converted to a string of 0's and 1's. Ex:
5 is converted to 101. Since only two values, 0 or 1, are allowed the format is called binary.
Floating-point values are stored as binary by Python. The conversion of a floating point number to the
underlying binary results in specific types of floating-point errors.
A round-off error occurs when floating-point values are stored erroneously as an approximation. The
difference between an approximation of a value used in computation and the correct (true) value is called a
round-off error.
Ex: Storing the float (0.1)10 results in binary values that actually produce
(0.1000000000000000055511151231257827021181583404541015625)10 when converted back, which is not
Table 2.1 Round-off error. (The example above shows a formatted string or f-string, which are introduced in the
Objects chapter.)
An overflow error occurs when a value is too large to be stored. The maximum and minimum floating-point
values that can be represented are and , respectively. Attempting to store a floating-
point value outside the range leads to an overflow error.
Below, and can be represented, but is too large and causes an overflow error.
CONCEPTS IN PRACTICE
Floating-point errors
For each situation, which error occurs?
1. The statement result = 2.0 * (10.0 ** 500) assigns the variable result with too large of a value.
a. round-off
b. overflow
Ex:
• round(2.451, 2) = 2.45
• round(2.451) = 2
CHECKPOINT
CONCEPTS IN PRACTICE
TRY IT
Inaccurate tips
The following code calculates the tip amount, given a bill amount and the tip ratio. Experiment with the
following bill amounts and tip ratios and see if any inaccuracies may result in calculating the tip amount.
TRY IT
Area of a triangle
Complete the following steps to calculate a triangle's area, and print the result of each step. The area of a
triangle is , where b is the base and h is the height.
• True division (/) converts numbers to floats before dividing. Ex: 7 / 4 becomes 7.0 / 4.0, resulting in
1.75.
• Floor division (//) computes the quotient, or the number of times divided. Ex: 7 // 4 is 1 because 4 goes
into 7 one time, remainder 3. The modulo operator (%) computes the remainder. Ex: 7 % 4 is 3.
Note: The % operator is traditionally pronounced "mod" (short for "modulo"). Ex: When reading 7 % 4 out loud,
a programmer would say "seven mod four."
CHECKPOINT
CONCEPTS IN PRACTICE
1. 13 / 5
a. 2
b. 2.6
c. 3
2. 13 % 5
a. 2
b. 2.6
c. 3
3. 1 // 4
a. 0
b. 0.25
c. 1
4. 2 % 0
a. 0
b. 2
c. Error
Unit conversions
Division is useful for converting one unit of measure to another. To convert centimeters to meters, a variable is
divided by 100. Ex: 300 centimeters divided by 100 is 3 meters.
Amounts often do not divide evenly as integers. 193 centimeters is 1.93 meters, or 1 meter and 93
centimeters. A program can use floor division and modulo to separate the units:
Programs often use floor division and modulo together. If one line of code floor divides by m, the next line will
likely modulo by m. The unit m by which an amount is divided is called the modulus. Ex: When converting
centimeters to meters, the modulus is 100.
CHECKPOINT
CONCEPTS IN PRACTICE
Unit conversions
5. What is the modulus for converting minutes to hours?
a. 40
b. 60
c. 280
6. A program has the line pounds = ounces // 16. What is likely the next line of code?
a. ounces = ounces % 16
b. pounds = ounces % 16
c. ounces = ounces - pounds * 16
TRY IT
Arrival time
Having a mobile device can be a lifesaver on long road trips. Programs like Google Maps find the shortest
route and estimate the time of arrival. The time of arrival is based on the current time plus how long the
trip will take.
Write a program that (1) inputs the current time and estimated length of a trip, (2) calculates the time of
arrival, and (3) outputs the results in hours and minutes. Your program should use the following prompts
(user input in bold):
13
Current minute (0-59)? 25
Trip time (in minutes)? 340
In this example, the current time is 13:25 (1:25pm). The trip time is 340 minutes (5 hours and 40 minutes).
340 minutes after 13:25 is 19:05 (7:05pm). Your program should output the result in this format:
Arrival hour is 19
Arrival minute is 5
The arrival hour must be between 0 and 23. Ex: Adding 120 minutes to 23:00 should be 1:00, not 25:00. The
arrival minute must be between 0 and 59. Ex: Adding 20 minutes to 8:55 should be 9:15, not 8:75.
Hint: Multiply the current hour by 60 to convert hours to minutes. Then, calculate the arrival time, in total
minutes, as an integer.
Your code must not use Python keywords from later chapters, such as if or while. The solution requires
54 2 • Expressions
TRY IT
Change machine
Self-checkout aisles are becoming increasingly popular at grocery stores. Customers scan their own items,
and a computer determines the total purchase amount. Customers who pay in cash insert dollar bills, and a
machine automatically dispenses change in coins.
That's where this program comes into the story. Your task is to calculate how many of each coin to
dispense. Your program should use the following prompts (user input in bold):
18.76
Cash payment? 20
You may assume that the cash paid will always be a whole number (representing dollar bills) that is greater
than or equal to the total amount. The program should calculate and output the amount of change due
and how many dollars, quarters, dimes, nickels, and pennies should be dispensed:
Dollars: 1
Quarters: 0
Dimes: 2
Nickels: 0
Pennies: 4
Hint: Calculate the total change, in cents, as an integer. Use the round() function to avoid floating-point
errors.
Your code must not use Python keywords from later chapters, such as if or while. The solution requires
only subtraction, multiplication, division, and modulo.
Importing modules
Python comes with an extensive standard library (https://openstax.org/r/100pythlibrary) of modules. A
module is previously written code that can be imported in a program. The import statement defines a
variable for accessing code in a module. Import statements often appear at the beginning of a program.
The standard library also defines built-in functions such as print(), input(), and float(). A built-in
function is always available and does not need to be imported. The complete list of built-in functions
(https://openstax.org/r/100builtin) is available in Python's official documentation.
A commonly used module in the standard library is the math module (https://openstax.org/r/
100mathmodule). This module defines functions such as sqrt() (square root). To call sqrt(), a program
must import math and use the resulting math variable followed by a dot. Ex: math.sqrt(25) evaluates to
5.0.
The following program imports and uses the math module, and uses built-in functions for input and output.
EXAMPLE 2.1
import math
CHECKPOINT
CONCEPTS IN PRACTICE
Mathematical functions
Commonly used math functions and constants are shown below. The complete math module listing
(https://openstax.org/r/100mathmodule) is available in Python's official documentation.
math.tau The ratio of the circumference to the radius of a circle. Tau is equal to 2π.
Number-theoretic
math.log(math.e) 1.0
math.log(x) The natural logarithm of x (to base e). math.log(0) ValueError:
math domain error
math.sqrt(9) 3.0
math.sqrt(-9)
math.sqrt(x) The square root of x.
ValueError: math domain
error
Trigonometric
math.cos(0) 1.0
math.cos(x) The cosine of x radians.
math.cos(math.pi) -1.0
math.sin(0) 0.0
math.sin(x) The sine of x radians.
math.sin(math.pi/2) 1.0
math.tan(0) 0.0
math.tan(math.pi/4)
math.tan(x) The tangent of x radians. 0.999
(Round-off error; the result
should be 1.0.)
CONCEPTS IN PRACTICE
b. math.pi * r**2
c. math.pi * r*2
TRY IT
Quadratic formula
In algebra, a quadratic equation is written as . The coefficients a, b, and c are known
values. The variable x represents an unknown value. Ex: has the coefficients , ,
and . The quadratic formula provides a quick and easy way to solve a quadratic equation for x:
The plus-minus symbol indicates the equation has two solutions. However, Python does not have a plus-
minus operator. To use this formula in Python, the formula must be separated:
Write the code for the quadratic formula in the program below. Test your program using the following
values for a, b, and c:
a b c x1 x2
1 0 -4 2.0 -2.0
1 2 -3 1.0 -3.0
2 1 -1 0.5 -1.0
Table 2.5
0 1 1 division by zero
Table 2.5
TRY IT
Cylinder formulas
In geometry, the surface area and volume of a right circular cylinder can be computed as follows:
Write the code for these two formulas in the program below. Hint: Your solution should use both math.pi
and math.tau. Test your program using the following values for r and h:
r h area volume
0 0 0.0 0.0
1 1 12.57 3.14
1 2 18.85 6.28
Table 2.6
Recommended spacing
Most spaces in Python code are ignored when running programs; however, spaces at the start of a line are very
important. The following two programs are equivalent:
• Good spacing:
• Poor spacing:
One might argue that missing or extra spaces do not matter. After all, the two programs above run exactly the
same way. However, the "poor spacing" version is more difficult to read. Code like name=input and place+
might lead to confusion.
Good programmers write code that is as easy to read as possible. That way, other programmers are more
likely to understand the code. To encourage consistency, the Python community has a set of guidelines about
where to put spaces and blank lines, what to name variables, how to break up long lines, and other important
topics.
PEP 8 (https://openstax.org/r/100PEP8) is the official style guide for Python. PEP stands for Python
Enhancement Proposal. Members of the Python community write PEPs to document best practices and
propose new features. The table below is based on guidelines from PEP 8 under the heading Whitespace in
Expressions and Statements (https://openstax.org/r/100whitespace).
name=input("Your name?
")
Assignment: one space before and after name = input("Your name= input("Your
the =. name? ") name? ")
name =input("Your
name? ")
print("Hi", name+"!")
Concatenation: one space before and
print("Hi", name + "!") print("Hi", name+ "!")
after the +.
print("Hi", name +"!")
x ** 2 + 5 * x - 8
Arithmetic: use space to show lower x**2 + 5*x - 8 x ** 2+5 * x-8
precedence. x**2+5*x-8
CONCEPTS IN PRACTICE
Recommended spacing
1. Which statement is formatted properly?
a. name = input("What is your name? ")
b. name = input ("What is your name? ")
c. name = input( "What is your name? " )
a. b**2 - 4*a*c
b. b ** 2 - 4 * a * c
c. b**2 - 4*a * c
Automatic concatenation
Long strings make Python programs difficult to read. Ex: This program prints the first sentence of the US
Declaration of Independence (https://openstax.org/r/100declaration):
print("The unanimous Declaration of the thirteen united States of America, When in the Course of human
events, it becomes necessary for one people to dissolve the political bands which have connected them with
another, and to assume among the powers of the earth, the separate and equal station to which the Laws of
Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they
should declare the causes which impel them to the separation.")
PEP 8 recommends that each line of code be less than 80 characters long. That way, programmers won't need
to scroll horizontally to read the code. The above program can be rewritten by breaking up the original string:
For convenience, Python automatically concatenates multiple strings. The + operator is not required in this
situation.
CHECKPOINT
String concatenation
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
2-7-formatting-code)
CONCEPTS IN PRACTICE
Multi-line statements
Most statements in a Python program need only one line of code. But occasionally longer statements need to
span multiple lines. Python provides two ways to write multi-line statements:
Implicit line joining is more common, since many statements and expressions use parentheses anyway. PEP 8
recommends avoiding the use of explicit line joining whenever possible.
CONCEPTS IN PRACTICE
Multi-line statements
8. Which character is used for explicit line joining?
a. /
b. \
c. |
TRY IT
Spaced out
The following code works correctly but is formatted poorly. In particular, the code does not include spaces
recommended by PEP 8. Furthermore, two of the lines are about 90 characters long. Reformat the code to
follow the guidelines in this section. Be careful not to change the behavior of the code itself.
TRY IT
Five quotes
Write a program that prints the following five quotes (source: BrainyQuote (https://openstax.org/r/
100brainyquote)) from Guido van Rossum, the creator of Python. Your program should have exactly five
print statements, one for each quote:
1. "If you're talking about Java in particular, Python is about the best fit
you can get amongst all the other languages. Yet the funny thing is, from a
language point of view, JavaScript has a lot in common with Python, but it is sort
of a restricted subset."
2. "The second stream of material that is going to come out of this project is
a programming environment and a set of programming tools where we really want to
focus again on the needs of the newbie. This environment is going to have to be
extremely user-friendly."
3. "I have this hope that there is a better way. Higher-level tools that
actually let you see the structure of the software more clearly will be of
tremendous value."
4. "Now, it's my belief that Python is a lot easier than to teach to students
programming and teach them C or C++ or Java at the same time because all the
details of the languages are so much harder. Other scripting languages really don't
work very well there either."
5. "I would guess that the decision to create a small special purpose language
or use an existing general purpose language is one of the toughest decisions that
anyone facing the need for a new language must make."
Notice that all of these lines are longer than 80 characters, and some contain single quote marks. Format
the code using multi-line statements and escape sequences as necessary.
Many professionals use Python to support the work they do. Python programs can automate tasks and solve
problems quickly. Table 2.8 shows a few examples of Python outside of computing fields. Knowing how to
program is a useful skill that can enhance any career.
Python is a versatile language that supports many kinds of applications. Table 2.9 shows a few examples of
programs that can be written in Python. Given Python's usefulness and popularity, Python is a great language
to learn. A supportive community of professionals and enthusiasts is ready to help.
Fine arts An artist writes a Python program to operate an interactive art display.
Humanities A linguist writes a Python program to analyze changes in slang word usage.
Science A biologist writes a Python program to analyze DNA sequences for cancer.
Artificial An engineer develops models to support image and voice recognition. Ex: TensorFlow
intelligence library (https://openstax.org/r/100tensorflow).
Data A statistician creates charts to make sense of large amounts of data. Ex: Matplotlib library
visualization (https://openstax.org/r/100matplotlib).
General Most programs in this book are general. Ex: Read inputs, perform calculations, print
purpose results.
Scientific Python is very useful for conducting experiments and analyzing data. Ex: The SciPy project
computing (https://openstax.org/r/100scipyproject).
Web Python can run interactive websites. Ex: Instagram is built with Django
development (https://openstax.org/r/100django), a Python framework.
CONCEPTS IN PRACTICE
EXPLORING FURTHER
For more examples of applications that can be built with Python, see "Top 12 Fascinating Python
Applications in Real-World" (https://openstax.org/r/100top12apps) by Rohit Sharma. For more information
about Python related careers, see "What Does a Python Developer Do?" (https://openstax.org/r/100careers)
in BrainStation's career guide.
At this point, you should be able to write programs that ask for input of mixed types, perform mathematical
calculations, and output results with better formatting. The programming practice below ties together most
topics presented in the chapter.
Function Description
round(x, Rounds x to ndigits places after the decimal point. If ndigits is omitted, returns the
ndigits) nearest integer to x.
Operator Description
s * n
Creates a string with n copies of s. Ex: "Ha" * 3 is "HaHaHa".
(Repetition)
x / y
Divides x by y and returns the entire result as a float. Ex: 7 / 4 is 1.75.
(Real division)
x // y
Divides x by y and returns the quotient as an integer. Ex: 7 // 4 is 1.
(Floor division)
x % y
Divides x by y and returns the remainder as an integer. Ex: 7 % 4 is 3.
(Modulo)
TRY IT
Baking bread
The holidays are approaching, and you need to buy ingredients for baking many loaves of bread. According
to a recipe by King Arthur Flour (https://openstax.org/r/100kingarthurflr), you will need the following
ingredients for each loaf:
Write a program that inputs the following variables: bread_weight (float), serving_size (float), and
num_guests (int). The output will look like the following:
Note: The measures the program comes up with are exact, but to bake, the baker would have to use some
approximation. Ex: 9.765625 cups all-purpose flour really means 9 and 3/4 cups.
In the above output, bread_weight is 16.0 ounces, serving_size is 2.5 ounces, and num_guests is 25
people. Use these three variables to calculate the number of loaves needed.
Make sure your output matches the above example exactly. Notice that each line of the ingredients begins
with two spaces.
TRY IT
Tip calculator
Google has a variety of search tricks (https://openstax.org/r/100googletricks) that present users with
instant results. If you search on Google for tip calculator (https://openstax.org/r/100tipcalculatr), an
interactive tool is included at the top of the results. The goal of this exercise is to implement a similar tip
calculator.
Begin by prompting the user to input the following values (user input in bold):
43.21
Percentage to tip: 18
Number of people: 2
Then calculate the tip amount and total amount for the bill, based on the user input. Output the results
using this format:
Your program should output all dollar amounts rounded to two decimal places. The output should be
exactly six lines, as shown above. Notice the blank line before each section of the output. Notice also the
space before but not after the dollar sign.
Figure 3.1 credit: modification of work "Port of Melbourne", by Chris Phutully/Flickr, CC BY 2.0
Chapter Outline
3.1 Strings revisited
3.2 Formatted strings
3.3 Variables revisited
3.4 List basics
3.5 Tuple basics
3.6 Chapter summary
Introduction
An object is a single unit of data in a Python program. So far, this book has introduced three types of objects:
strings, integers, and floats. This chapter takes a closer look at how strings are represented and how integers
and floats can be formatted. To better understand what an object actually is, the relationship between
variables and objects is emphasized. The chapter also introduces two types of containers: lists and tuples. A
container is an object that can hold an arbitrary number of other objects. At the end of this chapter, you will
be able to solve more complex problems using fewer variables.
Indexes
A string is a sequence of zero or more characters. Each character has an index that refers to the character's
position. Indexes are numbered from left to right, starting at 0. Indexes are also numbered from right to left,
72 3 • Objects
starting at -1.
CHECKPOINT
String indexes
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
3-1-strings-revisited)
CONCEPTS IN PRACTICE
String indexes
1. What is the index of the second character in a string?
a. 1
b. 2
c. -2
Unicode
Python uses Unicode, the international standard for representing text on computers. Unicode defines a
unique number, called a code point, for each possible character. Ex: "P" has the code point 80, and "!" has
the code point 33.
The built-in ord() function converts a character to a code point. Ex: ord("P") returns the integer 80.
Similarly, the built-in chr() function converts a code point to a character. Ex: chr(33) returns the string "!".
Unicode is an extension of ASCII, the American Standard Code for Information Interchange. Originally, ASCII
defined only 128 code points, enough to support the English language. Unicode defines over one million code
32 (space) 64 @ 96 `
33 ! 65 A 97 a
34 " 66 B 98 b
35 # 67 C 99 c
36 $ 68 D 100 d
37 % 69 E 101 e
38 & 70 F 102 f
39 ' 71 G 103 g
40 ( 72 H 104 h
41 ) 73 I 105 i
42 * 74 J 106 j
43 + 75 K 107 k
44 , 76 L 108 l
45 - 77 M 109 m
46 . 78 N 110 n
47 / 79 O 111 o
48 0 80 P 112 p
49 1 81 Q 113 q
50 2 82 R 114 r
51 3 83 S 115 s
52 4 84 T 116 t
53 5 85 U 117 u
54 6 86 V 118 v
55 7 87 W 119 w
56 8 88 X 120 x
57 9 89 Y 121 y
58 : 90 Z 122 z
59 ; 91 [ 123 {
60 < 92 \ 124 |
61 = 93 ] 125 }
62 > 94 ^ 126 ~
63 ? 95 _ 127 (delete)
Table 3.1 Character values. This table shows code points 32 to 127 as defined by ASCII and Unicode. Code
points 0 to 31 are non-printable characters that were used for telecommunications.
CONCEPTS IN PRACTICE
c. 97
Special characters
An escape sequence uses a backslash (\) to represent a special character within a string.
Escape
Meaning Example Screen output
sequence
print("I heard
A double quote; an alternative to I heard you said
\" you said
enclosing the string in single quotes. "Yes"
\"Yes\"")
print("This
\\ A backslash character. This prints a \
prints a \\")
CONCEPTS IN PRACTICE
TRY IT
Hopper quote
Grace Hopper (https://openstax.org/r/100gracehopper) (1906–1992) was a famous computer scientist (and
rear admiral in the US Navy!) who came up with the idea of machine-independent programming languages.
She envisioned a programming language based on English and made many contributions that paved the
way for modern programming languages, including Python.
Write a program that prints the following text, including the quotation marks. Your program may not use
single quotes (') anywhere in the code. The last line must be indented with a tab character.
TRY IT
Shift cipher
During the Roman Empire, Julius Caesar (100–44 BCE) used a simple technique to encrypt private
messages. Each letter of the message was replaced with the third next letter of the alphabet. Ex: If the
message was CAT, the C became F, the A became D, and the T became W, resulting in the message FDW.
This technique is known as a shift cipher because each letter is shifted by some amount. In Caesar's case,
76 3 • Objects
the amount was three, but other amounts (besides 0) would work too.
Write a program that prompts the user to input the following two values (example input in bold):
The program should then shift each letter of the word by the desired amount. Based on the example above,
the output would be:
Hint: Use the ord() function to convert each letter to an integer, add the shift amount to each integer, use
the chr() function to convert each integer to a character, and concatenate the resulting characters.
F-strings
A formatted string literal (or f-string) is a string literal that is prefixed with "f" or "F". A replacement field
is an expression in curly braces ({}) inside an f-string. Ex: The string f"Good morning, {first} {last}!"
has two replacement fields: one for a first name, and one for a last name. F-strings provide a convenient way
to combine multiple values into one string.
CHECKPOINT
Printing an f-string
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
3-2-formatted-strings)
CONCEPTS IN PRACTICE
Basic f-strings
1. What is the output of the following code?
animal = "dog"
says = "bark"
print(f"My {animal} says {says} {says} {says}")
temp = "hot"
food = "potato"
print("{temp} {food}")
a. {temp} {food}
b. hot potato
c. Error
a. print(f"x + y = {x+y}")
b. print(f"{x + y} = {x + y}")
c. print(f"{x} + {y} = {x+y}")
Formatting numbers
Programs often need to display numbers in a specific format. Ex: When displaying the time, minutes are
formatted as two-digit integers. If the hour is 9 and the minute is 5, then the time is "9:05" (not "9:5").
In an f-string, a replacement field may include a format specifier introduced by a colon. A format specifier
defines how a value should be formatted for display. Ex: In the string f"{hour}:{minute:02d}", the format
specifier for minute is 02d.
Table 3.3 Example format specifiers. The table shows common ways that numbers can be formatted. Many more formatting
options are available and described in Python's Format Specification Mini-Language (https://docs.python.org/3/library/
string.html#formatspec).
78 3 • Objects
Table 3.3 Example format specifiers. The table shows common ways that numbers can be formatted. Many more formatting
options are available and described in Python's Format Specification Mini-Language (https://docs.python.org/3/library/
string.html#formatspec).
CONCEPTS IN PRACTICE
Formatting numbers
4. What f-string formats a date as MM/DD/YYYY?
a. f"{month:02d}/{day:02d}/{year}"
b. f"{month:2d}/{day:2d}/{year}"
c. f"{month}/{day}/{year}"
5. What statement displays the variable money rounded to two decimal places?
a. print(f"{money:2d}")
b. print(f"{money:2f}")
c. print(f"{money:.2f}")
6. What format specifier displays a floating-point number with comma separators and rounded to two
decimal places?
a. .2,f
b. ,.2f
c. ,d.2f
TRY IT
Most of the code for this mad lib is already written. Complete the code below by writing the f-string.
TRY IT
Wage calculator
You just landed a part-time job and would like to calculate how much money you will earn. Write a program
that inputs the time you start working, the time you stop working, and your hourly pay rate (example input
in bold):
Starting hour: 9
Starting minute: 30
Stopping hour: 11
Stopping minute: 0
Hourly rate: 15
Based on the user input, your program should calculate and display the following results:
For this exercise, you need to write code that (1) calculates the total payment and (2) formats the three
output lines. Use f-strings and format specifiers to display two-digit minutes, one decimal place for hours,
and two decimal places for payment. The input code has been provided as a starting point.
Assume the use of a 24-hour clock. Ex: 16:15 is used instead of 4:15pm.
References to objects
In Python, every variable refers to an object. The assignment statement message = "Hello" makes the
variable message refer to the object "Hello". Multiple variables may refer to the same object. Ex: greeting
= message makes greeting refer to the same object as message. A memory diagram shows the
relationship between variables and objects.
CHECKPOINT
CONCEPTS IN PRACTICE
EXPLORING FURTHER
Python Tutor (https://openstax.org/r/100pythontutor) is a free online tool for visualizing code execution. A
user can enter any Python code, click Visualize Execution, and then click the Next button to run the code
one line at a time. Here is the rating and score example (https://openstax.org/r/100ratingscore) from the
animation above.
Python Tutor is also useful for drawing memory diagrams similar to the ones in this book. Before clicking
Visualize Execution, change the middle option from "inline primitives, don't nest objects [default]" to
"render all objects on the heap (Python/Java)" as shown in the following screenshot:
Figure 3.2
Properties of objects
Every object has an identity, a type, and a value:
• An object's identity is a unique integer associated with the object. Generally, this integer refers to the
memory location where the object is stored. Once created, an object's identity never changes. The built-in
id() function returns the object's identity.
• An object's type determines the possible values and operations of an object. Ex: Integers and floats can be
"divided" using the / operator, but strings cannot. The built-in type() function returns the object's type.
• An object's value represents the current state of the object. Many objects, such as numbers and strings,
cannot be modified once created. Some objects, such as lists (introduced later), are designed to be
modified.
CHECKPOINT
CONCEPTS IN PRACTICE
EXPLORING FURTHER
As shown in a memory diagram, variables and objects are two separate ideas. Calling a function like id()
or type() returns information about an object, not a variable. In fact, a variable doesn't have an identity or
a type, as shown in this example:
One might incorrectly think that the rating variable's type or identity changes. However, the only thing that
changes is which object the rating variable refers to.
TRY IT
Three variables
1. Draw a memory diagram for the following code:
a = 1
b = 2
c = b
b = a
a = c
TRY IT
Different types
1. Draw a memory diagram for the following code:
name = "Chocolate"
length = len(name)
price = 1.99
lower = min(length, price)
product = name
name = name * 2
Lists
A list object can be used to bundle elements together in Python. A list is defined by using square brackets []
with comma separated values within the square brackets. Ex: list_1 = [1, 2, 4].
• list_1 = []
• list_1 = list()
Lists can be made of elements of any type. Lists can contain integers, strings, floats, or any other type. Lists
can also contain a combination of types. Ex: [2, "Hello", 2.5] is a valid list.
Python lists allow programmers to change the contents of the list in various ways.
CHECKPOINT
Lists
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/3-4-list-
basics)
CONCEPTS IN PRACTICE
Lists
1. Which is the correct way to make a list of the numbers 3, 4, and 5?
a. new_list == [3, 4, 5]
b. new_list = [3, 4, 5]
c. new_list = (3, 4, 5)
84 3 • Objects
Using indexes
Individual list elements can be accessed directly using an index. Indexes begin at 0 and end at one less than
the length of the sequence. Ex: For a sequence of 50 elements, the first position is 0, and the last position is 49.
The index number is put in square brackets [] and attached to the end of the name of the list to access the
required element. Ex: new_list[3] accesses the 4th element in new_list. An expression that evaluates to an
integer number can also be used as an index. Similar to strings, negative indexing can also be used to address
individual elements. Ex: Index -1 refers to the last element and -2 the second-to-last element.
The len() function, when called on a list, returns the length of the list.
EXAMPLE 3.1
[2, 3, 5, 9, 11]
Length: 5
4th element: 9
[2, 3, 5, 7, 11]
CONCEPTS IN PRACTICE
4. What is the correct way to modify the element "Tom" to "Tim" in the following list?
a. name_list[-1] = "Tim"
b. name_list[4] = "Tim"
c. name_list[end] = "Tim"
TRY IT
List basics
Write a program to complete the following:
Each element is accessed by index, starting with the first element at index 0.
tuple_1 = (2, 3, 4)
print(f'tuple_1: {tuple_1}') tuple_1: (2, 3, 4)
print(tuple_1[1]) 3
print()
data_13 = ('Aimee Perry', 96, [94, 100, data_13: ('Aimee Perry', 96, [94, 100,
97, 93]) 97, 93])
print(f'data_13: {data_13}') [94, 100, 97, 93]
print(data_13[2])
CONCEPTS IN PRACTICE
Tuple properties
How do tuples compare to lists? Tuples are ordered and allow duplicates, like lists, but have different
mutability. An immutable object cannot be modified after creation. A mutable object can be modified after
creation. Tuples are immutable, whereas lists are mutable.
CHECKPOINT
CONCEPTS IN PRACTICE
Using tuples
4. Consider the final program in the example above. What is the value of my_tuple?
a. (0.693, 0.414, 3.142)
b. (0.693, 1.414, 3.142)
c. Error
6. A programmer wants to create a sequence of constants for easy reference that can't be changed
anywhere in the program. Which sequence would be the most appropriate?
a. list
b. tuple
Tuples are immutable and have a fixed size, so tuples use less memory. Overall, tuples are faster to create
and access, resulting in better performance that can be noticeable with large amounts of data.
TRY IT
TRY IT
Given input:
88 3 • Objects
x
y
15
20
Code Description
Code Description
Figure 4.1 credit: modification of work "Fork In The Road", by Ian Sane/Flickr, CC BY 2.0
Chapter Outline
4.1 Boolean values
4.2 If-else statements
4.3 Boolean operations
4.4 Operator precedence
4.5 Chained decisions
4.6 Nested decisions
4.7 Conditional expressions
4.8 Chapter summary
Introduction
The Python interpreter follows a single path of execution when executing a program. What if a programmer
wants to define multiple possible paths? Ex: Instead of always taking the left path, a program uses the path
width to decide which path to take. If the left path is wider, take the right path. Else, take the left path.
A branch is a group of statements that execute based on a condition. The Expressions chapter introduced
expressions. This chapter explores how expressions can be used as conditions to make decisions in programs.
CHECKPOINT
CONCEPTS IN PRACTICE
is_fruit = "True"
is_vegetable = 0
is_dessert = False
is_dessert = 0
print(type(is_dessert))
CHECKPOINT
CONCEPTS IN PRACTICE
8. bool(-1)
a. True
b. False
9. bool("")
a. True
b. False
10. bool("0")
a. True
b. False
a. True
b. False
CONCEPTS IN PRACTICE
12. float(is_on)
a. 0.0
b. 1.0
13. str(is_on)
a. "is_on"
b. "True"
14. int(is_on)
a. 0
b. 1
Comparison operators
Programmers often have to answer questions like "Is the current user the admin?" A programmer may want to
compare a string variable, user, to the string, "admin". Comparison operators are used to compare values,
and the result is either true or false. Ex: is_admin = (user == "admin"). user is compared with "admin"
using the == operator, which tests for equality. The Boolean variable, is_admin, is assigned with the Boolean
result.
• equal to: ==
• not equal to: !=
• greater than: >
• less than: <
• greater than or equal to: >=
• less than or equal to: <=
CHECKPOINT
CONCEPTS IN PRACTICE
Comparing values
For each new variable, what is the value of compare_result?
15. x = 14
compare_result = (x <= 13)
a. True
b. False
16. w = 0
compare_result = (w != 0.4)
a. True
b. False
17. v = 4
compare_result = (v < 4.0)
a. True
b. False
18. y = 2
compare_result = (y > "ab")
a. True
b. False
c. Error
19. z = "cilantro"
compare_result = (z == "coriander")
a. True
b. False
20. a = "dog"
compare_result = (a < "cat")
a. True
b. False
96 4 • Decisions
= VS ==
A common mistake is using = for comparison instead of ==. Ex: is_zero = num=0 will always assign
is_zero and num with 0, regardless of num's original value. The = operator performs assignment and will
modify the variable. The == operator performs comparison, does not modify the variable, and produces
True or False.
EXPLORING FURTHER
TRY IT
Friday Boolean
"It's Friday, I'm in love" —from "Friday I'm in Love," a song released by the Cure in 1992.
Write a program that reads in the day of the week. Assign the Boolean variable, in_love, with the result of
whether the day is Friday or not.
TRY IT
Even numbers
Write a program that reads in an integer and prints whether the integer is even or not. Remember, a
number is even if the number is divisible by 2. To test this use number % 2 == 0. Ex: If the input is 6, the
output is "6 is even: True".
• Identify which operations are performed when a program with if and if-else statements is run.
• Identify the components of an if and if-else statement and the necessary formatting.
• Create an if-else statement to perform an operation when a condition is true and another operation
otherwise.
if statement
If the weather is rainy, grab an umbrella! People make decisions based on conditions like if the weather is
rainy, and programs perform operations based on conditions like a variable's value. Ex: A program adds two
The if statement's body must be grouped together and have one level of indentation. The PEP 8 style guide
recommends four spaces per indentation level. The Python interpreter will produce an error if the body is
empty.
CHECKPOINT
A Boolean variable already has a value of True or False and can be used directly in a condition rather than
using the equality operator. Ex: if is_raining == True: can be simplified to if is_raining:.
CONCEPTS IN PRACTICE
Using if statements
1. Given the following, which part is the condition?
a. age
b. age < 12
c. print("Discount for children available")
a. 1, 2, 3
b. 1, 2, 4
c. 1, 2, 3, 4
3. Given the following (same as above), which lines execute if the condition is False?
2 if is_raining:
3 print("Don't forget an umbrella!")
4 print("See you soon.")
a. 1, 2, 3
b. 1, 2, 4
c. 1, 2, 3, 4
if num < 0:
num = 25
if num < 100:
num = num + 50
a. -10
b. 40
c. 75
a. 10
b. 0
c. Error
if-else statement
An if statement defines actions to be performed when a condition is true. What if an action needs to be
performed only when the condition is false? Ex: If the restaurant is less than a mile away, we'll walk. Else, we'll
drive.
An else statement is used with an if statement and contains a body of statements that is executed when the
if statement's condition is false. When an if-else statement is executed, one and only one of the branches
is taken. That is, the body of the if or the body of the else is executed. Note: The else statement is at the
same level of indentation as the if statement, and the body is indented.
1 # Statements before
2
3 if condition:
4 # Body
5 else:
6 # Body
7
8 # Statements after
CHECKPOINT
CONCEPTS IN PRACTICE
if x >= 15:
# Do something
else:
# Do something else
a. x >= 15
b. x <= 15
c. x < 15
if x > 30:
y = x - 10
else:
y = x + 10
a. 30
b. 40
c. 50
if x < 50:
y = y / 2
else:
y = y * 2
y = y + 5
a. 30
b. 55
c. 105
100 4 • Decisions
TRY IT
Improved division
The following program divides two integers. Division by 0 produces an error. Modify the program to read in
a new denominator (with no prompt) if the denominator is 0.
TRY IT
Calculate new_temp, the result of converting temp from Fahrenheit to Celsius or Celsius to Fahrenheit
based on unit. Calculate new_unit: "c" if unit is "f" and "f" if unit is "c".
Conversion formulas:
Python has three logical operators: and, or, and not. The and operator takes two condition operands and
returns True if both conditions are true.
p q p and q
CHECKPOINT
CONCEPTS IN PRACTICE
2. Consider the example above. Darcy tries to enter when the capacity is 3000. For what values of
hrs_to_close will Darcy to be able to enter?
a. hrs_to close > 1.0
b. no such value
3. Given is_admin = False and is_online = True, what is the value of is_admin and is_online?
a. True
b. False
a. 0
b. 5
102 4 • Decisions
Logical operator: or
Sometimes a decision only requires one condition to be true. Ex: If a student is in the band or choir, they will
perform in the spring concert. The or operator takes two condition operands and returns True if either
condition is true.
p q p or q
CHECKPOINT
CONCEPTS IN PRACTICE
a. yes
b. no
a. age >= 12
b. age <= 65
c. (age >= 12) and (age <= 65)
# Test passed
else:
# Test failed
a. yes
b. no
not is a useful operator that can make a condition more readable and can be used to toggle a Boolean's value.
Ex: is_on = not is_on.
p not p
True False
False True
CHECKPOINT
CONCEPTS IN PRACTICE
a. yes
b. no
10. Given is_turn = False and timer = 65, what is the final value of is_turn?
104 4 • Decisions
a. True
b. False
TRY IT
Speed limits
Write a program that reads in a car's speed as an integer and checks if the car's speed is within the freeway
limits. A car's speed must be at least 45 mph but no greater than 70 mph on the freeway.
If the speed is within the limits, print "Good driving". Else, print "Follow the speed limits".
Precedence
When an expression has multiple operators, which operator is evaluated first? Precedence rules provide the
priority level of operators. Operators with the highest precedence execute first. Ex: 1 + 2 * 3 is 7 because
multiplication takes precedence over addition. However, (1 + 2) * 3 is 9 because parentheses take
precedence over multiplication.
Operator Meaning
() Parentheses
+, - Addition, subtraction
Operator Meaning
or Logical or operator
CHECKPOINT
Operator precedence
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
4-4-operator-precedence)
CONCEPTS IN PRACTICE
Precedence rules
Which part of each expression is evaluated first?
1. x ** 2 + 6 / 3
a. 6 / 3
b. x ** 2
c. 2 + 6
2. not 3 * 5 > 10
a. 3 * 5
b. not 3
c. 5 > 10
Associativity
What if operators beside each other have the same level of precedence? Associativity determines the order of
operations when precedence is the same. Ex: 8 / 4 * 3 is evaluated as (8/4) * 3 rather than 8 / (4*3)
because multiplication and division are left associative. Most operators are left associative and are evaluated
from left to right. Exponentiation is the main exception (noted above) and is right associative: that is, evaluated
from right to left. Ex: 2 ** 3 ** 4 is evaluated as 2 ** (3**4).
When comparison operators are chained, the expression is converted into the equivalent combination of
106 4 • Decisions
comparisons and evaluated from left to right. Ex. 10 < x <= 20 is evaluated as 10 < x and x <= 20.
CHECKPOINT
Operation precedence
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
4-4-operator-precedence)
CONCEPTS IN PRACTICE
Associativity
How is each expression evaluated?
4. 10 + 3 * 2 / 4
a. 10 + (3 * (2 / 4))
b. 10 + ((3 * 2) / 4)
c. (10 + 3) * (2 / 4)
5. 2 * 2 ** 2 ** 3
a. 2 * ((2 ** 2) ** 3)
b. 2 * (2 ** (2 ** 3))
c. ((2*2) ** 2) ** 3
CHECKPOINT
Using parentheses
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
4-4-operator-precedence)
CONCEPTS IN PRACTICE
Using parentheses
7. Consider the example above. Why was the evaluation order different from what the programmer
wanted?
a. Equality has precedence over and.
The PEP 8 style guide recommends consistent spacing around operators to avoid extraneous and confusing
whitespace.
• Avoid multiple spaces and an unequal amount of whitespace around operators with two operands.
Avoid: x= y * 44
Better: x = y * 44
• Avoid spaces immediately inside parentheses.
Avoid: x = ( 4 * y )
Better: x = (4 * y)
• Surround the following operators with one space: assignment, augment assignment, comparison,
Boolean.
Avoid: x= y<44
Better: x = y < 44
• Consider adding whitespace around operators with lower priority.
Avoid: x = 5 * z+20
Better: x = 5*z + 20
elif
Sometimes, a complicated decision is based on more than a single condition. Ex: A travel planning site reviews
the layovers on an itinerary. If a layover is greater than 24 hours, the site should suggest accommodations.
Else if the layover is less than one hour, the site should alert for a possible missed connection.
108 4 • Decisions
Two separate if statements do not guarantee that only one branch is taken and might result in both branches
being taken. Ex: The program below attempts to add a curve based on the input test score. If the input is 60,
both if statements are incorrectly executed, and the resulting score is 75.
score = int(input())
if score < 70:
score += 10
# Wrong:
if 70 <= score < 85:
score += 5
Chaining decision statements with elif allows the programmer to check for multiple conditions. An elif (short
for else if) statement checks a condition when the prior decision statement's condition is false. An elif
statement is part of a chain and must follow an if (or elif) statement.
# Statements before
if condition:
# Body
elif condition:
# Body
# Statements after
CHECKPOINT
CONCEPTS IN PRACTICE
Using elif
1. Fill in the blank to execute Body 2 when condition_1 is false and condition_2 is true.
if condition_1:
# Body 1
__ condition_2:
# Body 2
a. if
b. elif
c. else
if x > 44:
y += 2
elif x < 50:
y += 5
a. 2
b. 5
c. 7
3. Which conditions complete the code such that if x is less than 0, Body 1 executes, else if x equals 0,
Body 2 executes.
if _________:
# Body 1
elif _________:
# Body 2
a. x < 0
x == 0
b. x == 0
x < 0
c. x <= 0
[no condition]
rooms = 1
if attendees >= 100:
rooms += 3
if attendees <= 200:
rooms += 7
110 4 • Decisions
a. 4
b. 15
c. 18
if-elif-else statements
Elifs can be chained with an if-else statement to create a more complex decision statement. Ex: A program
shows possible chess moves depending on the piece type. If the piece is a pawn, show moving forward one (or
two) places. Else if the piece is a bishop, show diagonal moves. Else if . . . (finish for the rest of the pieces).
CHECKPOINT
CONCEPTS IN PRACTICE
if hour < 8:
print("Too early")
elif hour < 12:
print("Good morning")
elif hour < 13:
print("Lunchtime")
elif hour < 17:
print("Good afternoon")
else:
print("Too late")
a. Good morning
b. Lunchtime
c. Good afternoon
d. Too late
_1_
if condition:
# Body
_2_
elif condition:
# Body
_3_
else:
# Body
_4_
a. 1
b. 2
c. 3
d. 4
a. 10
b. 20
c. 30
TRY IT
• B : 2.25
• C : 2.75
• D : 3.25
• E : 3.5
• F : 3.75
• G : 4.0
TRY IT
Color wavelengths
Write a program that reads in an integer representing a visible light wavelength in nanometers. Print the
corresponding color using the following inclusive ranges:
• Violet: 380–449
• Blue: 450–484
• Cyan: 485–499
• Green: 500–564
• Yellow: 565–589
• Orange: 590–624
• Red: 625–750
Assume the input is within the visible light spectrum, 380-750 inclusive.
Given input:
550
Green
The programmer realizes the code is redundant. What if the programmer could decide the game ID first and
then make a decision about players? Nesting allows a decision statement to be inside another decision
statement, and is indicated by an indentation level.
An improved program:
if game == 1:
if players < 2:
print("Not enough players")
elif players > 4:
print("Too many players")
else:
print("Ready to start")
if game == 2:
114 4 • Decisions
if players < 3:
print("Not enough players")
elif players > 6:
print("Too many players")
else:
print("Ready to start")
# Test game IDs 3-end
CHECKPOINT
CONCEPTS IN PRACTICE
if num_dancers < 0:
print("Error: num_dancers is negative")
else:
if num_dancers % 2 == 1:
print("Error: num_dancers is odd")
print(num_dancers, "dancers")
3. Given x = 256, y = 513, and max = 512, which of the following will execute?
if x == y:
# Body 1
elif x < y:
# Body 2
if y >= max:
# Body 3
else:
# Body 4
else:
# Body 5
a. Body 2
b. Body 2, Body 3
c. Body 2, Body 5
4. Given x =118, y = 300, and max = 512, which of the following will execute?
if x == y:
# Body 1
elif x < y:
# Body 2
if y >= max:
# Body 3
else:
# Body 4
else:
# Body 5
a. Body 2
b. Body 3
c. Error
TRY IT
Meal orders
Write a program that reads in a string, "lunch" or "dinner", representing the menu choice, and an
integer, 1, 2, or 3, representing the user's meal choice. The program then prints the user's meal choice.
• 1: Caesar salad
• 2: Spicy chicken wrap
• 3: Butternut squash soup
• 1: Baked salmon
• 2: Turkey burger
• 3: Mushroom risotto
Conditional expressions
A conditional expression (also known as a "ternary operator") is a simplified, single-line version of an
if-else statement.
A variable can be assigned with a conditional expression. Ex: Finding the max of two numbers can be
calculated with max_num = y if x < y else x
Note: Conditional expressions have the lowest precedence of all Python operations.
CHECKPOINT
CONCEPTS IN PRACTICE
if x%2 == 0:
response = 'even'
else:
response = 'odd'
a. 90
b. 100
c. 110
a. min_num = x
b. x < y
c. min_num = y
if x < 50:
result = True
else:
result = False
a. 10, 2
b. fee + 10, 2
c. fee + 10, fee + 2
TRY IT
Ping values
Write a program that reads in an integer, ping, and prints ping_report, a string indicating whether the
ping is low to average or too high. ping values under 150 have a ping_report of "low to average".
ping values of 150 and higher have a ping_report of "too high". Use a conditional expression to assign
ping_report.
At this point, you should be able to write programs that evaluate conditions and execute code statements
accordingly with the correct order of operations. The programming practice below ties together most topics
presented in the chapter.
Function Description
Operator Description
x == y Compares the values of x and y and returns True if the values are equal and False
(Equality) otherwise. Ex: 10 == 10 is True.
x != y Compares the values of x and y and returns True if the values are inequal and False
(Inequality) otherwise. Ex: 7 != 4 is True.
x > y Compares the values of x and y and returns True if the x is greater than y and False
(Greater than) otherwise. Ex: 9 > 3 is True.
x < y Compares the values of x and y and returns True if the x is less than y and False
(Less than) otherwise. Ex: 9 < 8 is False.
x >= y
Compares the values of x and y and returns True if the x is greater than or equal to y
(Greater than
and False otherwise. Ex: 2 >= 2 is True.
or equal)
x <= y
Compares the values of x and y and returns True if the x is less than or equal to y and
(Less than or
False otherwise. Ex: 8 <= 7 is False.
equal)
Function Description
x and y Evaluates the Boolean values of x and y and returns True if both are true. Ex: True and
(Logical) False is False.
x or y Evaluates the Boolean values of x and y and returns True if either is true. Ex: True or
(Logical) False is True.
not x Evaluates the Boolean value of x and returns True if the value is false and False if the
(Logical) value is true. Ex: not True is False.
Decision
Description
statement
# Statements before
if statement if condition:
# Body
# Statements after
# Statements before
if condition:
else statement # Body
else:
# Body
# Statements after
Function Description
# Statements before
if condition:
# Body
elif statement elif condition:
# Body
else:
# Body
# Statements after
# Statements before
if condition:
if condition:
# Body
else:
Nested if # Body
statement else:
if condition:
# Body
else:
# Body
# Statements after
Figure 5.1 credit: modification of work "Quantum Computing", by Kevin Dooley/Flickr, CC BY 2.0
Chapter Outline
5.1 While loop
5.2 For loop
5.3 Nested loops
5.4 Break and continue
5.5 Loop else
5.6 Chapter summary
Introduction
A loop is a code block that runs a set of statements while a given condition is true. A loop is often used for
performing a repeating task. Ex: The software on a phone repeatedly checks to see if the phone is idle. Once
the time set by a user is reached, the phone is locked. Loops can also be used for iterating over lists like
student names in a roster, and printing the names one at a time.
In this chapter, two types of loops, for loop and while loop, are introduced. This chapter also introduces
break and continue statements for controlling a loop's execution.
While loop
A while loop is a code construct that runs a set of statements, known as the loop body, while a given
condition, known as the loop expression, is true. At each iteration, once the loop statement is executed, the
122 5 • Loops
• If true, the loop body will execute at least one more time (also called looping or iterating one more time).
• If false, the loop's execution will terminate and the next statement after the loop body will execute.
CHECKPOINT
While loop
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
5-1-while-loop)
CONCEPTS IN PRACTICE
# Running the loop while the last Fibonacci number is less than 20
while g < 20:
print(g, end = ' ')
# Calculating the next Fibonacci number and updating the last two sequence
numbers
temp = f
f = g
g = temp + g
2. What is the variable g's value when the while loop condition evaluates to False?
a. 13
b. 20
c. 21
EXAMPLE 5.1
# Initialization
counter = 1
CHECKPOINT
CONCEPTS IN PRACTICE
n = 4
while n > 0:
print(n)
n = n - 1
n = 4
while n > 0:
print(n)
# Modified line
n = n + 1
TRY IT
Enter different input words to see when the while loop condition is met.
TRY IT
For loop
In Python, a container can be a range of numbers, a string of characters, or a list of values. To access objects
within a container, an iterative loop can be designed to retrieve objects one at a time. A for loop iterates over
all elements in a container. Ex: Iterating over a class roster and printing students' names.
CHECKPOINT
CONCEPTS IN PRACTICE
count = 0
for c in str_var:
count += 1
print(count)
2. What's the code's output if the line count += 1 is replaced with count *= 2?
a. 0
b. 16
c. 28
count = 0
for c in str_var:
count += 1
# New line
print(c, end = '*')
print(count)
a. A string*
b. A*s*t*r*i*n*g*
c. A* *s*t*r*i*n*g*
range(0, 3) 0, 1, 2
range(0, 4,
0, 1, 2, 3
• Generates a sequence beginning at 1)
range(start, end,
start until end.
step)
• Step size: step range(1, 7,
1, 3, 5
2)
range(3, 3, 2, 1, 0,
-2, -1) -1
range(10,
10, 6, 2
0, -4)
EXAMPLE 5.2
Two programs printing all integer multiples of 5 less than 50 (Notice the compactness of the for
construction compared to the while)
Table 5.2
CONCEPTS IN PRACTICE
5. What are the arguments to the range() function for the decreasing sequence of every integer from 5
to 1 (inclusive of both ends)?
a. range(5, 1, 1)
b. range(5, 1, -1)
c. range(5, 0, -1)
128 5 • Loops
TRY IT
Counting spaces
Write a program using a for loop that takes in a string as input and counts the number of spaces in the
provided string. The program must print the number of spaces counted. Ex: If the input is "Hi everyone",
the program outputs 1.
TRY IT
Sequences
Write a program that reads two integer values, n1 and n2, with n1 < n2, and performs the following tasks:
1. Prints all even numbers between the two provided numbers (inclusive of both), in ascending order.
2. Prints all odd numbers between the two provided numbers (exclusive of both), in descending order.
Input: 2 8
prints
2 4 6 8
7 5 3
Note: the program should return an error message if the second number is smaller than the first.
Nested loops
A nested loop has one or more loops within the body of another loop. The two loops are referred to as outer
loop and inner loop. The outer loop controls the number of the inner loop's full execution. More than one
inner loop can exist in a nested loop.
CHECKPOINT
EXAMPLE 5.3
Available appointments
Consider a doctor's office schedule. Each appointment is 30 minutes long. A program to print available
appointments can use a nested for loop where the outer loop iterates over the hours, and the inner loop
iterates over the minutes. This example prints time in hours and minutes in the range between 8:00am and
10:00am. In this example, the outer loop iterates over the time's hour portion between 8 and 9, and the
inner loop iterates over the time's minute portion between 0 and 59.
hour = 8
minute = 0
while hour <= 9:
while minute <= 59:
print(hour, ":", minute)
minute += 30
hour += 1
minute = 0
8 : 0
8 : 30
9 : 0
9 : 30
130 5 • Loops
CONCEPTS IN PRACTICE
i = 1
while i <= 5:
j = 1
while i + j <= 5:
print(i, j)
j += 1
i += 1
a. 5
b. 10
c. 25
i = 1
while i <= 2:
j = 1
while j <= 3:
print('*', end = '')
j += 1
print()
i += 1
a. ******
b. ***
***
c. **
**
**
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
a. i = 1
while i <= 4:
while j <= 4:
print(i * j, end = ' ')
j += 1
print()
i += 1
b. i = 1
while i <= 4:
j = 1
while j <= 4:
print(i * j, end = ' ')
j += 1
print()
i += 1
c. i = 1
while i <= 4:
j = 1
while j <= 4:
print(i * j, end = ' ')
j += 1
i += 1
CHECKPOINT
CONCEPTS IN PRACTICE
for i in range(3):
for j in range(4):
print(i, ' ', j)
a. 3
b. 4
c. 12
5. Given the following code, how many times does the inner loop execute?
for i in range(3):
132 5 • Loops
for j in range(4):
print(i, ' ', j)
a. 4
b. 12
c. 20
0 1 2 3
0 2 4 6
0 3 6 9
a. for i in range(4):
for j in range(4):
print(i * j, end = ' ')
print()
b. for i in range(1, 4):
for j in range(4):
print(i * j)
c. for i in range(1, 4):
for j in range(4):
print(i * j, end = ' ')
print()
MIXED LOOPS
The two for and while loop constructs can also be mixed in a nested loop construct. Ex: Printing even
numbers less than a given number in a list. The outer loop can be implemented using a for loop iterating
over the provided list, and the inner loop iterates over all even numbers less than a given number from the
list using a while loop.
numbers = [12, 5, 3]
i = 0
for n in numbers:
while i < n:
print (i, end = " ")
i += 2
i = 0
print()
TRY IT
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Finish!
TRY IT
****
***
**
*
++++
+++
++
+
Break
A break statement is used within a for or a while loop to allow the program execution to exit the loop once a
given condition is triggered. A break statement can be used to improve runtime efficiency when further loop
execution is not required.
Ex: A loop that looks for the character "a" in a given string called user_string. The loop below is a regular
for loop for going through all the characters of user_string. If the character is found, the break statement
takes execution out of the for loop. Since the task has been accomplished, the rest of the for loop execution
is bypassed.
CHECKPOINT
INFINITE LOOP
A break statement is an essential part of a loop that does not have a termination condition. A loop without
a termination condition is known as an infinite loop. Ex: An infinite loop that counts up starting from 1 and
prints the counter's value while the counter's value is less than 10. A break condition is triggered when the
counter's value is equal to 10, and hence the program execution exits.
counter = 1
while True:
if counter >= 10:
break
print(counter)
counter += 1
CONCEPTS IN PRACTICE
a. Hello
b. Hello World
c. H
e
l
l
o
2. Given the following code, how many times does the print statement get executed?
i = 1
while True:
if i%3 == 0 and i%5 == 0:
print(i)
break
i += 1
a. 0
b. 1
c. 15
i = 1
count = 0
while True:
if i%2 == 0 or i%3 == 0:
count += 1
if count >= 5:
print(i)
break
i += 1
a. 5
b. 8
c. 30
Continue
A continue statement allows for skipping the execution of the remainder of the loop without exiting the loop
entirely. A continue statement can be used in a for or a while loop. After the continue statement's
execution, the loop expression will be evaluated again and the loop will continue from the loop's expression. A
continue statement facilitates the loop's control and readability.
136 5 • Loops
CHECKPOINT
CONCEPTS IN PRACTICE
i = 10
while i >= 0:
i -= 1
if i%3 != 0:
continue
print(i)
a. 3
b. 4
c. 11
a. h
i
A
l
i
b. h
i
A
l
i
c. hi
Ali
TRY IT
TRY IT
• Use loop else statement to identify when the loop execution is interrupted using a break statement.
• Implement a loop else statement with a for or a while loop.
Loop else
A loop else statement runs after the loop's execution is completed without being interrupted by a break
statement. A loop else is used to identify if the loop is terminated normally or the execution is interrupted by
a break statement.
Ex: A for loop that iterates over a list of numbers to find if the value 10 is in the list. In each iteration, if 10 is
observed, the statement "Found 10!" is printed and the execution can terminate using a break statement. If
10 is not in the list, the loop terminates when all integers in the list are evaluated, and hence the else
statement will run and print "10 is not in the list." Alternatively, a Boolean variable can be used to
track whether number 10 is found after loop's execution terminates.
EXAMPLE 5.4
Table 5.3
CHECKPOINT
CONCEPTS IN PRACTICE
n = 16
exp = 0
i = n
while i > 1:
if n%2 == 0:
i = i/2
exp += 1
else:
break
else:
print(n,"is 2 to the", exp)
a. 16 is 2 to the 3
b. 16 is 2 to the 4
c. no output
n = 7
exp = 0
i = n
while i > 1:
if n%2 == 0:
i = i//2
exp += 1
else:
break
else:
print(n,"is 2 to the", exp)
a. no output
b. 7 is 2 to the 3
c. 7 is 2 to the 2
numbers = [1, 2, 2, 6]
for i in numbers:
if i >= 5:
print("Not all numbers are less than 5.")
break
else:
print(i)
continue
else:
print("all numbers are less than 5.")
a. 1
2
2
6
Not all numbers are less than 5.
b. 1
2
2
Not all numbers are less than 5.
c. 1
2
2
6
all numbers are less than 5.
140 5 • Loops
TRY IT
• A while loop runs a set of statements, known as the loop body, while a given condition, known as the
loop expression, is true.
• A for loop can be used to iterate over elements of a container object.
• A range() function generates a sequence of integers between the two numbers given a step size.
• A nested loop has one or more loops within the body of another loop.
• A break statement is used within a for or a while loop to allow the program execution to exit the loop
once a given condition is triggered.
• A continue statement allows for skipping the execution of the remainder of the loop without exiting the
loop entirely.
• A loop else statement runs after the loop's execution is completed without being interrupted by a break
statement.
At this point, you should be able to write programs with loop constructs. The programming practice below ties
together most topics presented in the chapter.
Function Description
range(start, end) Generates a sequence beginning at start until end with step size of 1.
range(start, end, s) Generates a sequence beginning at start until end with the step size of s.
Function Description
# initialization
while expression:
while loop # loop body
# initialization
for loop_variable in container:
for loop # loop body
while outer_loop_expression:
# outer loop body (1)
while inner_loop_expression:
Nested while loop # inner loop body
# outer loop body (2)
# initialization
while loop_expression:
# loop body
break statement if break_condition:
break
# remaining body of loop
Function Description
# initialization
while loop_expression:
# loop body
continue statement if continue_condition:
continue
# remaining body of loop
# initialization
for loop_expression:
# loop body
if break_condition:
Loop else statement break
# remaining body of loop
else:
# loop else statement
TRY IT
Prime numbers
Write a program that takes in a positive integer number (N) and prints out the first N prime numbers on
separate lines.
Note: A prime number is a number that is not divisible by any positive number larger than 1. To check
whether a number is prime, the condition of number % i != 0 can be checked for i greater than 1 and
less than number.
2
3
5
7
11
13
Figure 6.1 credit: modification of work "IMG_3037", by Jay Roc/Flickr, Public Domain
Chapter Outline
6.1 Defining functions
6.2 Control flow
6.3 Variable scope
6.4 Parameters
6.5 Return values
6.6 Keyword arguments
6.7 Chapter summary
Introduction
Functions are the next step toward creating optimized code as a software developer. If the same block of code
is reused repeatedly, a function allows the programmer to write the block of code once, name the block, and
use the code as many times as needed by calling the block by name. Functions can read in values and return
values to perform tasks, including complex calculations.
Like branching statements discussed in the Decisions chapter, functions allow different paths of execution
through a program, and this chapter discusses control flow and the scope of variables in more detail.
Calling a function
Throughout the book, functions have been called to perform tasks. Ex: print() prints values, and sqrt()
calculates the square root. A function is a named, reusable block of code that performs a task when called.
CHECKPOINT
CONCEPTS IN PRACTICE
1 input_num = 14
2 offset_num = input_num - 10
3 print(offset_num)
a. line 1
b. line 2
c. line 3
a. 1
b. 3
c. 5
a. 3
b. 5
c. 6
Defining a function
A function is defined using the def keyword. The first line contains def followed by the function name (in
snake case), parentheses (with any parameters—discussed later), and a colon. The indented body begins with
a documentation string describing the function's task and contains the function statements. A function must
be defined before the function is called.
CHECKPOINT
CONCEPTS IN PRACTICE
Defining functions
4. What's wrong with the first line of the function definition?
def water_plant:
def print_phone_num():
print("Phone: (", 864, ")", 555, "-", 0199)
print("User info:")
print_phone_num()
Benefits of functions
A function promotes modularity by putting code statements related to a single task in a separate group. The
body of a function can be executed repeatedly with multiple function calls, so a function promotes reusability.
Modular, reusable code is easier to modify and is shareable among programmers to avoid reinventing the
wheel.
CHECKPOINT
CONCEPTS IN PRACTICE
8. If the points were changed from floats to integers, how many statements would need to be changed in
the original and revised programs respectively?
a. 3, 1
b. 4, 4
c. 12, 4
TRY IT
Given:
concessions()
Food/Drink Options:
Popcorn: $8-10
Candy: $3-5
Soft drink: $5-7
TRY IT
When execution reaches a function call, control flow moves to where the function is defined and executes the
function statements. Then, control flow moves back to where the function was called and continues the
sequence.
150 6 • Functions
CHECKPOINT
CONCEPTS IN PRACTICE
1 def park_greet():
2 """Output greeting."""
3 print("Welcome. Open sunrise to sunset.")
4
5 car_count = 1
6 park_greet()
7 if car_count > 50:
8 # Direct to extra parking lot
a. 1
b. 3
c. 5
2. Control flow moves to line 9, and park_greet() is called. Which line does control flow move to next?
1 def extra_lot():
2 # Function definition
3
4 def park_greet():
5 """Output greeting."""
6 print("Welcome. Open sunrise to sunset.")
7
8 car_count = 1
9 park_greet()
10 if car_count > 50:
11 extra_lot()
a. 1
b. 4
c. 10
3. Control flow moves to line 12, and extra_lot() is called. Which line does control flow move to after
line 3 is executed?
1 def extra_lot():
2 """Output extra parking lot info."""
a. 5
b. 8
c. 12
def park_greet():
"""Output greeting."""
print("Welcome to the park")
print("Open sunrise to sunset")
park_greet()
CHECKPOINT
CONCEPTS IN PRACTICE
5. How many function calls occur during the execution of the program?
a. 2
b. 3
c. 6
6. When line 3 is reached and executed, which line does control flow return to?
a. 1
b. 11
c. 16
TRY IT
Given a function call to terms() and input "N", the output is:
TRY IT
Laundromat information
Write a program that uses three functions to print information about a laundromat, Liam's Laundry:
• laundromat_info(): Prints the name, Liam's Laundry, and hours of operation, 7a - 11p, and calls
washers_open() and dryers_open()
• washers_open(): Reads an integer, assigns washer_count with the value, and prints washer_count
• dryers_open(): Reads an integer, assigns dryer_count with the value, and prints dryer_count
Liam's Laundry
7a - 11p
Open washers: 50
Open dryers: 40
Global scope
A variable's scope is the part of a program where the variable can be accessed. A variable created outside of a
function has global scope and can be accessed anywhere in the program. A Python program begins in global
scope, and the global scope lasts for the entire program execution.
CHECKPOINT
CONCEPTS IN PRACTICE
Global variables
1. Which variables are global?
num = float(input())
num_sq = num * num
print(num, "squared is", num_sq)
a. num only
b. num_sq only
c. num and num_sq
def print_square():
154 6 • Functions
num = float(input())
print_square()
a. num only
b. num_sq only
c. num and num_sq
def print_double():
num_d = num * 2
print(num, "doubled is", num_d)
def print_square():
num_sq = num * num
print(num, "squared is", num_sq)
num = float(input())
print_double()
print_square()
a. print_double()
b. print_square()
c. print_double() and print_square()
Local scope
A variable created within a function has local scope and only exists within the function. A local variable cannot
be accessed outside of the function in which the variable was created. After a function finishes executing, the
function's local variables no longer exist.
CHECKPOINT
CONCEPTS IN PRACTICE
Local variables
4. Which variables are local?
def print_time():
out_str = "Time is " + str(hour) + ":" + str(min)
print(out_str)
hour = int(input())
min = int(input())
print_time()
def print_greeting():
print(out_str)
hour = int(input())
min = int(input())
if hour < 12:
out_str = "Good morning"
else:
out_str = "Good day"
print_greeting()
def print_greeting():
print("Good day,")
print_time()
def print_time():
out_str = "Time is " + str(hour) + ":" + str(min)
print(out_str)
hour = int(input())
min = int(input())
print_greeting()
a. print_greeting()
b. print_time()
c. print_greeting() and print_time()
156 6 • Functions
CHECKPOINT
CONCEPTS IN PRACTICE
def update_hour():
tmp = hour
if is_dst:
tmp += 1
else:
tmp -= 1
is_dst = True
hour = int(input("Enter hour: "))
update_hour()
print("New hour:", hour)
a. New hour: 9
b. New hour: 10
c. Error
def update_hour():
new_hour = hour
if is_dst:
new_hour += 1
else:
new_hour -= 1
is_dst = True
hour = int(input("Enter hour: "))
update_hour()
print("New hour:", new_hour)
a. New hour: 9
b. New hour: 10
c. Error
is_dst = True
hour = int(input("Enter hour: "))
update_hour()
print("New hour:", new_hour)
a. New hour: 9
b. New hour: 10
c. Error
A programmer might ask, "Why not just make all variables global variables to avoid access errors?" Making
every variable global can make a program messy. Ex: A programmer debugging a large program discovers
a variable has the wrong value. If the whole program can modify the variable, then the bug could be
anywhere in the large program. Limiting a variable's scope to only what's necessary and restricting global
variable use make a program easier to debug, maintain, and update.
TRY IT
battle_royale():
practice():
Given input:
br
1
Finding 2 players...
Match starting...
Given input:
p
Queen's Canyon
6.4 Parameters
Learning objectives
By the end of this section you should be able to
CHECKPOINT
CONCEPTS IN PRACTICE
1 def print_welcome(name):
2 print(f"Welcome {name}!")
3
4 username = int(input("Enter new username: "))
5 print_welcome(username)
1. Which is an argument?
a. name
b. username
c. name and username
2. Which is a parameter?
a. name
b. username
c. name and username
EXAMPLE 6.1
num_1 = 6
num_2 = 3
print_div(num_1, num_2) # Prints "6/3 = 2.0"
print_div(num_2, num_1) # Prints "3/6 = 0.5"
print_div(num_1) # Error: Missing argument: op_2
CONCEPTS IN PRACTICE
5. Which is an argument?
a. p1_x
b. x1
6. Which is a parameter?
a. p1_y
b. y1
7. What would be the value of x2 for the function call, calc_distance(2, 4, 3, 6)?
a. 2
b. 4
c. 3
d. 6
e. Error
uses a pass-by-object-reference system. If an argument is changed in a function, the changes are kept or lost
depending on the object's mutability. A mutable object can be modified after creation. A function's changes to
the object then appear outside the function. An immutable object cannot be modified after creation. So a
function must make a local copy to modify, and the local copy's changes don't appear outside the function.
Programmers should be cautious of modifying function arguments as these side effects can make programs
difficult to debug and maintain.
EXAMPLE 6.2
Converting temperatures
What are the values of weekend_temps and type after convert_temps() finishes?
The output is 9.44°F 10.56°F 6.67°F. type was changed to "C" in the function but didn't keep the
change outside of the function. Why is the list argument change kept and not the string argument change?
(Hint: A list is mutable. A string is immutable.)
CHECKPOINT
CONCEPTS IN PRACTICE
9. After unit is assigned with "C", metric and unit refer to ________ in memory.
a. the same object
b. different objects
10. deg_sign is a string whose value cannot change once created. deg_sign is ________.
a. immutable
b. mutable
TRY IT
Given input:
3
4
TRY IT
Curving scores
Write a function, print_scores(), that takes in a list of test scores and a number representing how many
points to add. For each score, print the original score and the sum of the score and bonus. Make sure not to
change the list.
67 would be updated to 77
68 would be updated to 78
72 would be updated to 82
71 would be updated to 81
69 would be updated to 79
CHECKPOINT
CONCEPTS IN PRACTICE
mpg = miles/gallons
return mpg
a. mpg
b. None
c. Error
a. sqft
b. None
c. Error
def hw_1():
print("Hello world!")
return
def hw_2():
print("Hello world!")
In the table below, calc_mpg() takes in miles driven and gallons of gas used and calculates a car's miles per
gallon. calc_mpg() checks if gallons is 0 (to avoid division by 0), and if so, returns -1, a value often used to
indicate a problem.
CONCEPTS IN PRACTICE
def yarn_weight(num):
if num == 0:
return "lace"
elif num == 1:
return "sock"
elif num == 2:
return "sport"
elif num == 3:
return "dk"
elif num == 4:
return "worsted"
elif num == 5:
return "bulky"
else:
return "super bulky"
a. "lace"
b. "dk"
c. "super bulky"
a. 9
10
b. 10
10
c. 10
11
CHECKPOINT
CONCEPTS IN PRACTICE
def tax(total):
return .06 * total
def auto_tip(total):
return .2 * total
bill = 100.0
bill += tax(bill) + auto_tip(bill)
a. 26.0
b. 126.0
def sq(num):
return num * num
def offset(num):
return num - 2
val = 5
val2 = sq(offset(val))
a. 9
b. 23
TRY IT
Then write a main program that reads in a user's age and outputs the result of days_alive().
Given input:
21
TRY IT
Averaging lists
Write a function, avg_list(), that takes in a list and returns the average of the list values.
Keyword arguments
So far, functions have been called using positional arguments, which are arguments that are assigned to
parameters in order. Python also allows keyword arguments, which are arguments that use parameter names
to assign values rather than order. When mixing positional and keyword arguments, positional arguments
must come first in the correct order, before any keyword arguments.
CHECKPOINT
CONCEPTS IN PRACTICE
c. name="Anita"
The physics example below calculates weight as a force in newtons given mass in kilograms and acceleration
in . Gravity on Earth is 9.8 , and gravity on Mars is 3.7 .
CHECKPOINT
CONCEPTS IN PRACTICE
a. Hello Friend
b. nothing
c. Error
The PEP 8 style guide recommends no spaces around = when indicating keyword arguments and default
parameter values.
TRY IT
Stream donations
Write a function, donate(), that lets an online viewer send a donation to a streamer. donate() has three
parameters:
Given:
donate(10, "gg")
Write function calls that use the default values along with positional and keyword arguments.
• Functions are named blocks of code that perform tasks when called and make programs more organized
and optimized.
• Control flow is the sequence of program execution. Control flow moves between calling code and function
code when a function is called.
• Variable scope refers to where a variable can be accessed. Global variables can be accessed anywhere in a
program. Local variables are limited in scope, such as to a function.
• Parameters are function inputs defined with the function. Arguments are values passed to the function as
input by the calling code. Parameters are assigned with the arguments' values.
• Function calls can use positional arguments to map values to parameters in order.
• Function calls can use keyword arguments to map values using parameter names in any order.
• Functions can define default parameter values to allow for optional arguments in function calls.
• Python uses a pass-by-object-reference system to assign parameters with the object values referenced by
the arguments.
• Functions can use return statements to return values back to the calling code.
At this point, you should be able to write functions that have any number of parameters and return a value,
and programs that call functions using keyword arguments and optional arguments.
Construct Description
def function_name():
Function definition
"""Docstring"""
# Function body
def function_name(parameter_1):
Argument # Function body
function_name(argument_1)
def function_name():
Return statement
# Function body
return result # Returns the value of result to the caller
def function_name(parameter_1):
# Function body
Variables (scope) local_var = parameter_1 * 5
return local_var
global_var = function_name(arg_1)
Construct Description
function_name(parameter_2 = 5, parameter_1 = 2)
Figure 7.1 credit: modification of work "Lone Pine Sunset", by Romain Guy/Flickr, Public Domain
Chapter Outline
7.1 Module basics
7.2 Importing names
7.3 Top-level code
7.4 The help function
7.5 Finding modules
7.6 Chapter summary
Introduction
As programs get longer and more complex, organizing the code into modules is helpful. This chapter shows
how to define, import, and find new modules. Python's standard library (https://openstax.org/r/100pythlibrary)
provides over 200 built-in modules (https://openstax.org/r/100200modules). Hundreds of thousands of other
modules (https://openstax.org/r/100pypi) are available online.
A module is a .py file containing function definitions and other statements. The module's name is the file's
name without the .py extension at the end. Ex: The following code, written in a file named greetings.py,
defines a module named greetings.
def hello():
print("Hello!")
def bye():
print("Goodbye!")
174 7 • Modules
Technically, every program in this book is a module. But not every module is designed to run like a program.
Running greetings.py as a program would accomplish very little. Two functions would be defined, but the
functions would never be called. These functions are intended to be called in other modules.
Defining a module
Modules are defined by putting code in a .py file. The area module below is in a file named area.py. This
module provides functions for calculating area.
EXAMPLE 7.1
import math
# 2D shapes
def square(side):
"""Gets the area of a square."""
return side**2
def circle(radius):
"""Gets the area of a circle."""
return math.pi * radius**2
# 3D shapes
def cube(side):
"""Gets the surface area of a cube."""
return 6 * side**2
def sphere(radius):
"""Gets the surface area of a sphere."""
return 4 * math.pi * radius**2
CONCEPTS IN PRACTICE
Defining a module
1. How many functions are defined in the area module?
a. 6
b. 10
c. 49
Importing a module
The module defined in area.py can be used in other programs. When importing the area module, the suffix
.py is removed:
176 7 • Modules
import area
CHECKPOINT
CONCEPTS IN PRACTICE
Importing a module
4. What statement would import a module from a file named secret.py?
a. import secret
b. import secret.py
c. import "secret.py"
5. What code would return the area of a circle with a radius of 3 meters?
a. circle(3)
b. area.circle(3)
c. circle.area(3)
6. A programmer would like to write a function that calculates the area of a hexagon. Where should the
function be written?
a. the main program
b. the area module
c. the secret module
TRY IT
Conversion module
Write a module that defines the following functions:
1. cel2fah(c) –
Converts a temperature in Celsius to Fahrenheit.
Each function should include a docstring as the first line. A docstring for the module has been provided for
you.
The module should not do anything except define functions. When you click the "Run" button, the module
should run without error. No output should be displayed.
TRY IT
European vacation
Write a program that uses the conversion module from the previous exercise to complete a short story.
The program's output should match the following example (input in bold):
1. european.py, the main program. Input and output statements are provided as a starting point. Edit the
lines with TODO comments to use the conversion module.
2. conversion.py, the other module. Copy and paste your code from the previous exercise. Import this
module in european.py after the docstring.
print(triangle(1, 2))
print(cylinder(3, 4))
EXPLORING FURTHER
CHECKPOINT
Importing functions
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
7-2-importing-names)
CONCEPTS IN PRACTICE
2. How many variables are defined by the statement from math import sin, cos, tan?
a. 0
b. 3
c. 4
3. What error would occur if attempting to call a function that was not imported?
a. ImportError
b. NameError
c. SyntaxError
Name collisions
Modules written by different programmers might use the same name for a function. A name collision occurs
when a function is defined multiple times. If a function is defined more than once, the most recent definition is
used:
A programmer might not realize the cube function is defined twice because no error occurs when running the
program. Name collisions are not considered errors and often lead to unexpected behavior.
Care should be taken to avoid name collisions. Selecting specific functions from a module to import reduces
the memory footprint; however, importing a complete module can help to avoid collisions because a
module.name format would be used. This is a tradeoff the programmer must consider.
CHECKPOINT
CONCEPTS IN PRACTICE
Name collisions
4. A program begins with from area import square, circle. What code causes a name collision?
a. def area(phone):
"""Gets the area code of a phone number."""
b. def circle(x, y, size):
"""Draws a circle centered at (x, y)."""
c. def is_square(length, width):
"""Returns True if length and width are equal."""
5. A program begins with import area. What code causes a name collision?
a. area = 51
b. import cylinder from volume
c. def cube(size):
"""Generates a "size X size" rubik's cube."""
1 def hello():
2 print("Hello!")
3
4 def hello(name):
5 print("Hello,", name)
180 7 • Modules
6
7 hello()
8 hello("Chris")
a. line 4
b. line 7
c. line 8
EXPLORING FURTHER
If a name is defined, imported, or assigned multiple times, Python uses the most recent definition. Other
languages allow multiple functions to have the same name if the parameters are different. This feature,
known as function overloading, is not part of the Python language.
TRY IT
Missing imports
Add the missing import statements to the top of the file. Do not make any changes to the rest of the code.
In the end, the program should run without errors.
TRY IT
Party favors
The following program does not run correctly because of name collisions. Fix the program by modifying
import statements, function calls, and variable assignments. The output should be:
Side effects
Modules define functions and constants to be used in other programs. When importing a module, all code in
the module is run from top to bottom. If a module is not designed carefully, unintended code might run as a
side effect. The unintended code is generally at the top level, outside of function definitions.
CHECKPOINT
CONCEPTS IN PRACTICE
Side effects
1. Which line would cause a side effect when imported?
1 import math
2
3 print("Defining sphere function")
4
5 def sphere(radius):
6 """Gets the volume of a sphere."""
7 return 4/3 * math.pi * radius**3
a. line 1
b. line 3
c. line 5
import math
def sphere(radius):
"""Gets the volume of a sphere."""
return 4/3 * math.pi * radius**3
for r in range(10000000):
volume = sphere(r)
a. true
b. false
a. true
b. false
Using __name__
Python modules often include the statement if __name__ == "__main__" to prevent side effects. This
statement is true when the module is run as a program and false when the module is imported.
CHECKPOINT
CONCEPTS IN PRACTICE
Using __name__
4. What is the output when running the following test.py module?
import math
print(math.__name__)
print(__name__)
a. >math
test
b. __main__
test
c. math
__main__
import math
print(math.__name__)
print(__name__)
a. math
test
b. __main__
test
c. math
__main__
EXPLORING FURTHER
Variables that begin and end with double underscores have special meaning in Python. Double underscores
are informally called "dunder" or "magic" variables. Other examples include __doc__ (the module's
docstring) and __file__ (the module's filename).
TRY IT
Side effects
This exercise is a continuation of the "Missing imports" exercise. Previously, you added missing import
statements to the top of the program. Now, modify the program to prevent side effects when importing the
program as a module:
The program should run without errors and produce the same output as before.
TRY IT
Conversion test
This exercise is a continuation of the "Conversion module" exercise. Previously, you wrote the functions
cel2fah, fah2cel, km2mi, and mi2km. Write test code at the end of conversion.py (the original file) for
each of these functions. The test code must not run as a side effect when conversion is imported by other
programs. When running conversion.py as the main program, the test output should be:
0 C is 32 F
5 C is 41 F
10 C is 50 F
15 C is 59 F
20 C is 68 F
184 7 • Modules
20 F is -7 C
25 F is -4 C
30 F is -1 C
35 F is 2 C
40 F is 4 C
1 km is 0.6 mi
2 km is 1.2 mi
3 km is 1.9 mi
4 km is 2.5 mi
5 km is 3.1 mi
5 mi is 8.0 km
6 mi is 9.7 km
7 mi is 11.3 km
8 mi is 12.9 km
9 mi is 14.5 km
Colors on websites
This section introduces an example module for working with HTML colors (https://openstax.org/r/
100htmlcolors). HyperText Markup Language (HTML) is used to design websites and graphical applications.
Web browsers like Chrome and Safari read HTML and display the corresponding contents. Ex: The HTML code
<p style="color: Red">Look out!</p> represents a paragraph with red text.
HTML defines 140 standard color names (https://openstax.org/r/100colornames). Additional colors can be
specified using a hexadecimal format: #RRGGBB. The digits RR, GG, and BB represent the red, green, and blue
components of the color. Ex: #DC143C is 220 red + 20 green + 60 blue, which is the color Crimson.
Red, green, and blue values range from 0 to 255 (or 00 to FF in hexadecimal). Lower values specify darker
colors, and higher values specify lighter colors. Ex: #008000 is the color Green, and #00FF00 is the color Lime.
CHECKPOINT
CONCEPTS IN PRACTICE
Note: The tohex() and torgb() functions use Python features (string formatting and slicing) described later
in the book. For now, the documentation and comments are more important than the implementation details.
# Primary colors
RED = "#FF0000"
YELLOW = "#FFFF00"
BLUE = "#0000FF"
# Secondary colors
ORANGE = "#FFA500"
GREEN = "#008000"
VIOLET = "#EE82EE"
# Neutral colors
BLACK = "#000000"
GRAY = "#808080"
WHITE = "#FFFFFF"
def _tohex(value):
186 7 • Modules
def torgb(color):
"""Converts a color in hexadecimal to red, green, and blue integers."""
r = int(color[1:3], 16) # First 2 digits
g = int(color[3:5], 16) # Middle 2 digits
b = int(color[5:7], 16) # Last 2 digits
return r, g, b
def lighten(color):
"""Increases the red, green, and blue values of a color by 32 each."""
r, g, b = torgb(color)
return tohex(r+32, g+32, b+32)
def darken(color):
"""Decreases the red, green, and blue values of a color by 32 each."""
r, g, b = torgb(color)
return tohex(r-32, g-32, b-32)
CONCEPTS IN PRACTICE
6. What symbol indicates that a function is not intended to be called by other modules?
a. underscore (_)
b. number sign (#)
c. colon (:)
Module documentation
The built-in help() function provides a summary of a module's functions and data. Calling
help(module_name) in a shell is a convenient way to learn about a module.
EXAMPLE 7.2
help(colors)
NAME
colors - Functions for working with color names and hex/rgb values.
FUNCTIONS
darken(color)
Decreases the red, green, and blue values of a color by 32 each.
lighten(color)
Increases the red, green, and blue values of a color by 32 each.
tohex(r, g, b)
Formats red, green, and blue integers as a color in hexadecimal.
torgb(color)
Converts a color in hexadecimal to red, green, and blue integers.
DATA
BLACK = '#000000'
BLUE = '#0000FF'
GRAY = '#808080'
GREEN = '#008000'
ORANGE = '#FFA500'
RED = '#FF0000'
VIOLET = '#EE82EE'
WHITE = '#FFFFFF'
YELLOW = '#FFFF00'
FILE
/home/student/Desktop/colors.py
>>> help(colors)
NAME
188 7 • Modules
colors - Functions for working with color names and hex/rgb values.
FUNCTIONS
darken(color)
Decreases the red, green, and blue values of a color by 32 each.
lighten(color)
Increases the red, green, and blue values of a color by 32 each.
tohex(r, g, b)
Formats red, green, and blue integers as a color in hexadecimal.
torgb(color)
Converts a color in hexadecimal to red, green, and blue integers.
DATA
BLACK = '#000000'
BLUE = '#0000FF'
GRAY = '#808080'
GREEN = '#008000'
ORANGE = '#FFA500'
RED = '#FF0000'
VIOLET = '#EE82EE'
WHITE = '#FFFFFF'
YELLOW = '#FFFF00'
FILE
/home/student/Desktop/colors.py
CONCEPTS IN PRACTICE
c. torgb
TRY IT
Help on modules
The random and statistics modules are useful for running scientific experiments. You can become
familiar with these two modules by skimming their documentation.
Open a Python shell on your computer, or use the one at python.org/shell (https://openstax.org/r/
100pythonshell). Type the following lines, one at a time, into the shell.
• import random
• help(random)
• import statistics
• help(statistics)
Many shell environments, including the one on python.org, display the output of help() one page at a
time. Use the navigation keys on the keyboard (up/down arrows, page up/down, home/end) to read the
documentation. When you are finished reading, press the Q key ("quit") to return to the Python shell.
TRY IT
Help on functions
The help() function can be called on specific functions in a module. Open a Python shell on your
computer, or use the one at python.org/shell (https://openstax.org/r/100pythonshell). Type the following
lines, one at a time, into the shell.
• import random
• help(random.randint)
• help(random.choice)
• import statistics
• help(statistics.median)
• help(statistics.mode)
Remember to use the navigation keys on the keyboard, and press the Q key ("quit") to return to the Python
shell.
Built-in modules
The Python Standard Library is a collection of built-in functions and modules that support common
programming tasks. Ex: The math module provides functions like sqrt() and constants like pi. Python's
official documentation includes a library reference (https://openstax.org/r/100pythlibrary) and a module index
(https://openstax.org/r/100200modules) for becoming familiar with the standard library.
Module Description
CONCEPTS IN PRACTICE
Built-in modules
Use the library reference, module index, and documentation links above to answer the questions.
Third-party modules
The Python Package Index (PyPI), available at pypi.org (https://openstax.org/r/100pypi), is the official third-
party software library for Python. The abbreviation "PyPI" is pronounced like pie pea eye (in contrast to PyPy
(https://openstax.org/r/100pypy), a different project).
PyPI allows anyone to develop and share modules with the Python community. Module authors include
individuals, large companies, and non-profit organizations. PyPI helps programmers install modules and
receive updates.
Most software available on PyPI is free and open source. PyPI is supported by the Python Software Foundation
(https://openstax.org/r/100foundation) and is maintained by an independent group of developers.
Module Description
Module Description
CONCEPTS IN PRACTICE
Third-party modules
Use pypi.org and the links in the table above to answer the questions.
6. Search for the webcolors module on PyPI. What function provided by webcolors looks up the color
name for a hex code?
a. hex_to_name
b. name_to_hex
c. normalize_hex
EXPLORING FURTHER
Programming blogs often highlight PyPI modules to demonstrate the usefulness of Python. The following
examples provide more background information about the modules listed above.
TRY IT
Happy birthday
Module documentation pages often include examples to help programmers become familiar with the
module. For this exercise, refer to the following examples from the datetime module documentation:
Write a program that creates a date object representing your birthday. Then get a date object
representing today's date (the date the program is run). Calculate the difference between the two dates,
and output the results in the following format:
TRY IT
Notice how leap years are included in the calculation. February 29th occurs four times between birth and
today. Therefore, the user is not only 15 years old, but 15 years and 4 days old.
Many commonly used modules from PyPI, including arrow, are installed in the Python shell at python.org/
shell (https://openstax.org/r/100pythonshell). Open the Python shell and type the following lines:
import arrow
birth = arrow.get(2005, 3, 14)
birth.humanize()
As time permits, experiment with other functions provided by the arrow module.
• Programs can be organized into multiple .py files (modules). The import keyword allows a program to use
functions defined in another .py file.
• The from keyword can be used to import specific functions from a module. However, programs should
avoid importing (or defining) multiple functions with the same name.
• Modules often include the line if __name__ == "__main__" to prevent code from running as a side
effect when the module is imported by other programs.
• When working in a shell, the help() function can be used to look up the documentation for a module.
The documentation is generated from the docstrings.
• Python comes with over 200 built-in modules and hundreds of thousands of third-party modules.
Programmers can search for modules on docs.python.org (https://openstax.org/r/100docstrings) and
pypi.org (https://openstax.org/r/100pypi).
Statement Description
if __name__ == A line of code found at the end of many modules. This statement indicates what
"__main__": code to run if the module is executed as a program (in other words, what code
not to run if this module is imported by another program).
Statement Description
Shows the documentation for the given module. The documentation includes
help(module_name) the module's docstring, followed by a list of functions defined in the module,
followed by a list of global variables assigned in the module, followed by the
module's file name.
Figure 8.1 credit: modification of work "Project 366 #65: 050316 A Night On The Tiles", by Pete/Flickr, CC BY 2.0
Chapter Outline
8.1 String operations
8.2 String slicing
8.3 Searching/testing strings
8.4 String formatting
8.5 Splitting/joining strings
8.6 Chapter summary
Introduction
A string is a sequence of characters. Python provides useful methods for processing string values. In this
chapter, string methods will be demonstrated including comparing string values, string slicing, searching,
testing, formatting, and modifying.
String comparison
String values can be compared using logical operators (<, <=, >, >=, ==, !=) and membership operators (in and
not in). When comparing two string values, the matching characters in two string values are compared
sequentially until a decision is reached. For comparing two characters, ASCII values are used to apply logical
operators.
198 8 • Strings
Checks whether the Since string "bc" does not contain string
second operand "a" in
in False "a", the output of "a" in "bc" evaluates
contains the first "bc"
to False.
operand.
Checks whether the Since string "bc" does not contain string
second operand does "a" not
not in True "a", the output of "a" not in "bc"
not contain the first in "bc"
evaluates to True.
operand.
CONCEPTS IN PRACTICE
EXAMPLE 8.1
x = "Apples"
apples
APPLES
CONCEPTS IN PRACTICE
b. ABBA
c. ABbA
d. aBBA
TRY IT
TRY IT
What is my character?
Given the string, s_input, which is a one-character string object, if the character is between "a" and "t"
or "A" and "T", print True. Otherwise, print False.
Hint: You can convert s_input to lowercase and check if s_input is between "a" and "t".
String indexing
A string is a type of sequence. A string is made up of a sequence of characters indexed from left to right,
starting at 0. For a string variable s, the left-most character is indexed 0 and the right-most character is
indexed len(s) - 1. Ex: The length of the string "Cloud" is 5, so the last index is 4.
Negative indexing can also be used to refer to characters from right to left starting at -1. For a string variable
s, the left-most character is indexed -len(s) and the right-most character is indexed -1. Ex: The length of the
string "flower" is 6, so the index of the first character with negative indexing is -6.
CHECKPOINT
String indexing
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
8-2-string-slicing)
CONCEPTS IN PRACTICE
word = "chance"
print(word[-1] == word[5])
a. True
b. False
String slicing
String slicing is used when a programmer must get access to a sequence of characters. Here, a string slicing
operator can be used. When [a:b] is used with the name of a string variable, a sequence of characters
starting from index a (inclusive) up to index b (exclusive) is returned. Both a and b are optional. If a or b are not
provided, the default values are 0 and len(string), respectively.
EXAMPLE 8.2
time_string = "13:46"
minutes = time_string[3:5]
print(minutes)
202 8 • Strings
46
EXAMPLE 8.3
time_string = "14:50"
hour = time_string[:2]
print(hour)
14
CONCEPTS IN PRACTICE
a. "el"
b. "ll"
c. "llo"
location = "classroom"
print(location[-3:-1])
a. "ro"
b. "oo"
c. "oom"
a. " Leila"
b. "Leila"
c. "ila"
String immutability
String objects are immutable meaning that string objects cannot be modified or changed once created. Once
a string object is created, the string's contents cannot be altered by directly modifying individual characters or
elements within the string. Instead, to make changes to a string, a new string object with the desired changes
is created, leaving the original string unchanged.
CHECKPOINT
CONCEPTS IN PRACTICE
string_variable = "example"
string_variable[-1] = ""
a. TypeError
b. IndexError
c. NameError
str = "morning"
str = str[1]
print(str)
204 8 • Strings
a. TypeError
b. m
c. o
TRY IT
TRY IT
Input:
string_variable = "great"
indices = [0, 1]
prints eat
in operator
The in Boolean operator can be used to check if a string contains another string. in returns True if the first
string exists in the second string, False otherwise.
CHECKPOINT
CONCEPTS IN PRACTICE
CHECKPOINT
CONCEPTS IN PRACTICE
for c in "string":
print(c, end = "")
a. string
b. s
t
r
i
n
g
c. s t r i n g
count = 0
for c in "abca":
if c == "a":
count += 1
print(count)
a. 0
b. 1
c. 2
word = "cab"
for i in word:
if i == "a":
print("A", end = "")
if i == "b":
print("B", end = "")
if i == "c":
print("C", end = "")
a. cab
b. abc
c. CAB
d. ABC
count()
The count() method counts the number of occurrences of a substring in a given string. If the given substring
does not exist in the given string, the value 0 is returned.
CHECKPOINT
CONCEPTS IN PRACTICE
find()
The find() method returns the index of the first occurrence of a substring in a given string. If the substring
does not exist in the given string, the value of -1 is returned.
CHECKPOINT
CONCEPTS IN PRACTICE
a. 0
b. -1
c. ValueError
index()
The index() method performs similarly to the find() method in which the method returns the index of the
first occurrence of a substring in a given string. The index() method assumes that the substring exists in the
given string; otherwise, throws a ValueError.
EXAMPLE 8.4
50
CONCEPTS IN PRACTICE
c. ValueError
a. "This"
b. "This "
c. "sentence"
TRY IT
prints:
2
Thisisgreat
format. The example below shows two string values that use the same template for making requests to
different individuals for taking different courses.
EXAMPLE 8.5
Dear John, I'd like to take a programming course with Prof. Potter.
Dear Kishwar, I'd like to take a math course with Prof. Robinson.
In the example above, replacement fields are 1) the name of the individual the request is being made to, 2)
title of the course, and 3) the name of the instructor. To create a template, replacement fields can be added
with {} to show a placeholder for user input. The format() method is used to pass inputs for replacement
fields in a string template.
EXAMPLE 8.6
print(s)
print(s.format("John", "programming", "Potter"))
print(s.format("Kishwar", "math", "Robinson"))
CONCEPTS IN PRACTICE
b. Hello Ana
c. Hello Ana!
EXAMPLE 8.7
print(s)
print(s.format(season = "summer", temperature = "hot"))
print(s.format(season = "winter", temperature = "cold"))
Since named replacement fields are referred to using a name key, a named replacement field can appear
and be used more than once in the template. Also, positional ordering is not necessary when named
replacement fields are used.
212 8 • Strings
print(s)
print(s.format(season = "summer", temperature = "hot"))
print(s.format(temperature = "cold", season = "winter"))
CONCEPTS IN PRACTICE
greeting = "Hi"
name = "Jess"
print("{greeting} {name}".format(greeting = greeting, name = name))
a. greeting name
b. Hi Jess
c. Jess Hi
Home".
Numbered replacement fields can use argument's values for multiple replacement fields by using the same
argument index. The example below illustrates how an argument is used for more than one numbered
replacement field.
EXAMPLE 8.8
print(template1.format("very", "cold"))
print(template2.format("very", "hot"))
EXAMPLE 8.9
In the example above, the table is formatted into three columns. The first column takes up 15 characters and is
left-aligned. The second column uses 25 characters and is center-aligned, and the last column uses two
characters and is right aligned. Alignment and length format specifications controls are used to create the
214 8 • Strings
formatted table.
The field width in string format specification is used to specify the minimum length of the given string. If the
string is shorter than the given minimum length, the string will be padded by space characters. A field width
is included in the format specification field using an integer after a colon. Ex: {name:15} specifies that the
minimum length of the string values that are passed to the name field is 15.
Since the field width can be used to specify the minimum length of a string, the string can be padded with
space characters from right, left, or both to be left-aligned, right-aligned, and centered, respectively. The
string alignment type is specified using <, >, or ^characters after the colon when field length is specified. Ex:
{name:^20} specifies a named replacement field with the minimum length of 20 characters that is center-
aligned.
Alignment
Symbol Example Output
Type
template = "{hex:<7}{name:<10}"
Left- print(template.format(hex = "#FF0000", #FF0000Red
<
aligned name = "Red")) print(template.format(hex #00FF00green
= "#00FF00", name = "green"))
template = "{hex:>7}{name:>10}"
Right- print(template.format(hex = "#FF0000", #FF0000 Red
>
aligned name = "Red")) print(template.format(hex #00FF00 green
= "#00FF00", name = "green"))
template = "{hex:^7}{name:^10}"
print(template.format(hex = "#FF0000", #FF0000 Red
Centered ^ name = "Red")) print(template.format(hex #00FF00 green
= "#00FF00", name = "green"))
CONCEPTS IN PRACTICE
template = "{name:12}"
formatted_name = template.format(name = "Alice")
print(len(formatted_name))
a. 5
b. 12
c. "Alice"
template = "{greeting:>6}"
formatted_greeting = template.format(greeting = "Hello")
print(formatted_greeting[0])
a. H
b. " Hello"
c. Space character
template = "{:5}"
print(template.format("123456789"))
a. 56789
b. 123456
c. 123456789
Formatting numbers
The format() method can be used to format numerical values. Numerical values can be padded to have a
given minimum length, precision, and sign character. The syntax for modifying numeric values follows the
{[index]:[width][.precision][type]} structure. In the given syntax,
The table below summarizes formatting options for modifying numeric values.
CONCEPTS IN PRACTICE
TRY IT
Input: [12.5, 2]
Prints: 012.50
002:00
split()
A string in Python can be broken into substrings given a delimiter. A delimiter is also referred to as a
separator. The split() method, when applied to a string, splits the string into substrings by using the given
argument as a delimiter. Ex: "1-2".split('-') returns a list of substrings ["1", "2"]. When no arguments
are given to the split() method, blank space characters are used as delimiters. Ex: "1\t2\n3 4".split()
returns ["1", "2", "3", "4"].
CHECKPOINT
CONCEPTS IN PRACTICE
s = """This is a test"""
out = s.split()
print(out)
a. Error
b. ['This', 'is', 'a', 'test']
c. >['This', 'is a', 'test']
join()
The join() method is the inverse of the split() method: a list of string values are concatenated together to
form one output string. When joining string elements in the list, the delimiter is added in-between elements.
Ex: ','.join(["this", "is", "great"]) returns "this,is,great".
CHECKPOINT
CONCEPTS IN PRACTICE
print(",".join(elements))
b. 5
c. 8
s = ["1", "2"]
out = "".join(s)
a. 12
b. "12"
c. "1 2"
TRY IT
happy
smiling
face
TRY IT
Lunch order
Use the join() method to repeat back a user's order at a restaurant, separated by commas. The user will
input each food item on a separate line. When finished ordering, the user will enter a blank line. The output
depends on how many items the user orders:
In the general case with three or more items, each item should be separated by a comma and a space. The
word "and" should be added before the last item.
8-5-splittingjoining-strings)
At this point, you should be able to write programs dealing with string values.
Method Description
find() Returns the index of the first occurrence of a given substring in a string. If the substring
does not exist in the string, -1 is returned.
Returns the index of the first occurrence of a given substring in a string. If the substring
index()
does not exist in the string, a ValueError is returned.
join() Takes a list of string values and combines string values into one string by placing a given
separator between values.
split() Separates a string into tokens based on a given separator string. If no separator string is
provided, blank space characters are used as separators.
Operator Description
Method Description
Figure 9.1 credit: modification of work "Budget and Bills" by Alabama Extension/Flickr, Public Domain
Chapter Outline
9.1 Modifying and iterating lists
9.2 Sorting and reversing lists
9.3 Common list operations
9.4 Nested lists
9.5 List comprehensions
9.6 Chapter summary
Introduction
Programmers often work on collections of data. Lists are a useful way of collecting data elements. Python lists
are extremely flexible, and, unlike strings, a list's contents can be changed.
The Objects chapter introduced lists. This chapter explores operations that can be performed on lists.
EXAMPLE 9.1
Line 8 shows the append() operation, line 12 shows the remove() operation, and line 17 shows the pop()
operation. Since the pop() operation removes the last element, no parameter is needed.
CONCEPTS IN PRACTICE
Modifying lists
1. Which operation can be used to add an element to the end of a list?
a. add()
b. append()
c. pop()
2. What is the correct syntax to remove the element 23 from a list called number_list?
a. remove()
b. number_list.remove()
c. number_list.remove(23)
3. Which operation can be used to remove an element from the end of a list?
a. only pop()
b. only remove()
c. either pop() or remove()
Iterating lists
An iterative for loop can be used to iterate through a list. Alternatively, lists can be iterated using list indexes
with a counting for loop. The animation below shows both ways of iterating a list.
CHECKPOINT
CONCEPTS IN PRACTICE
Iterating lists
For the following questions, consider the list:
my_list = [2, 3, 5, 7, 9]
5. What is the final value of i for the following counting for loop?
for i in range(0, len(my_list)):
a. 9
b. 4
c. 5
a. 2 5 9
b. 2 3 5 7 9
226 9 • Lists
c. 2
5
9
TRY IT
Sports list
Create a list of sports played on a college campus. The sports to be included are baseball, football, tennis,
and table tennis.
Next, remove "football" from the list and add "soccer" to the list.
TRY IT
Simple Searching
Write a program that prints "found!" if "soccer" is found in the given list.
Sorting
Ordering elements in a sequence is often useful. Sorting is the task of arranging elements in a sequence in
ascending or descending order.
Sorting can work on numerical or non-numerical data. When ordering text, dictionary order is used. Ex: "bat"
comes before "cat" because "b" comes before "c".
CHECKPOINT
Sorting
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
9-2-sorting-and-reversing-lists)
CONCEPTS IN PRACTICE
Sorting
1. What would be the last element of the following list if it is sorted in descending order?
[12, 3, 19, 25, 16, -3, 5]
a. 25
b. -3
c. 5
• The sort() method arranges the elements of a list in ascending order. For strings, ASCII values are used
and uppercase characters come before lowercase characters, leading to unexpected results. Ex: "A" is
ordered before "a" in ascending order but so is "G"; thus, "Gail" comes before "apple".
• The reverse() method reverses the elements in a list.
EXAMPLE 9.2
CONCEPTS IN PRACTICE
4. What is the correct way to sort the list board_games in ascending order?
a. sort(board_games)
b. board_games.sort()
c. board_games.sort('ascending')
6. What would be the last element of board_games after the reverse() method has been applied?
a. 'go'
b. 'checkers'
c. 'scrabble'
TRY IT
The sum() function called on a list of numbers returns the sum of all elements in the list.
EXAMPLE 9.3
print(min(city_list))
999
Sacramento
-5
Austin
1135
CONCEPTS IN PRACTICE
List operations
1. What is the correct way to get the minimum of a list named nums_list?
a. min(nums_list)
b. nums_list.min()
c. minimum(nums_list)
Copying a list
The copy() method is used to create a copy of a list.
CHECKPOINT
Copying a list
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
9-3-common-list-operations)
CONCEPTS IN PRACTICE
Copying a list
4. What is the output of the following code?
my_list = [1, 2, 3]
list2 = my_list
list2[0] = 13
print(sum(my_list))
a. 6
b. 13
c. 18
my_list = [1, 2, 3]
list2 = my_list.copy()
list2[0] = 13
print(max(my_list))
a. 3
b. 13
c. 18
a. CatDogPigeon
b. Error
TRY IT
Copy
Make a copy of word_list called wisdom. Sort the list called wisdom. Create a sentence using the words in
each list and print those sentences (no need to add periods at the end of the sentences).
List-of-lists
Lists can be made of any type of element. A list element can also be a list. Ex: [2, [3, 5], 17] is a valid list
with the list [3, 5] being the element at index 1.
When a list is an element inside a larger list, it is called a nested list. Nested lists are useful for expressing
multidimensional data. When each of the elements of a larger list is a smaller list, the larger list is called a list-
of-lists.
Ex: A table can be stored as a two-dimensional list-of-lists, where each row of data is a list in the list-of-lists.
CHECKPOINT
List-of-lists
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
9-4-nested-lists)
CONCEPTS IN PRACTICE
Lists
For each of the questions below, consider the following matrix:
2. What would be the correct index for the number 6 in the above list?
a. [5]
b. [2][1]
c. [1][2]
print(matA[0])
a. Error
b. 7
c. [7, 4, 5]
EXAMPLE 9.4
Iterating a list-of-lists
The code below demonstrates how to iterate a list-of-lists.
The outer loop on line 9 goes element by element for the larger list. Each element in the larger list is a list.
The inner loop on line 10 iterates through each element in each nested list.
1 """Iterating a list-of-lists."""
2
3 # Create a list of numbers
4 list1 = [[1, 2, 3],
5 [1, 4, 9],
6 [1, 8, 27]]
7
8 # Iterating the list-of-lists
9 for row in list1:
10 for num in row:
11 print(num, end=" ")
12 print()
1 2 3
1 4 9
1 8 27
CONCEPTS IN PRACTICE
Iterating a list-of-lists
For each question below, consider the following list:
4. Which code prints each number in my_list starting from 7, then 4, and so on ending with -5?
a. for row in my_list:
for elem in row:
print(elem)
b. for elem in my_list:
print(elem)
5. The range() function can also be used to iterate a list-of-lists. Which code prints each number in
my_list starting from 7, then 4, and so on, ending with -5, using counting for loops?
234 9 • Lists
TRY IT
Matrix multiplication
Write a program that calculates the matrix multiplication product of the matrices matW and matZ below
and prints the result. The expected result is shown.
In the result matrix, each element is calculated according to the position of the element. The result at
position [i][j] is calculated using row i from the first matrix, W, and column j from the second matrix, Z.
Ex:
List comprehensions
A list comprehension is a Python statement to compactly create a new list using a pattern.
list_name refers to the name of a new list, which can be anything, and the for is the for loop keyword. An
expression defines what will become part of the new list. loop_variable is an iterator, and iterable is an
object that can be iterated, such as a list or string.
EXAMPLE 9.5
EXAMPLE 9.6
one fish
two fish
red fish
blue fish
CONCEPTS IN PRACTICE
List comprehensions
1. The component of a list comprehension defining an element of the new list is the _____.
a. expression
b. loop_variable
c. container
2. What would be the contents of b_list after executing the code below?
a_list = [1, 2, 3, 4, 5]
b_list = [i+2 for i in a_list]
a. [1, 2, 3, 4, 5]
b. [0, 1, 2, 3, 4]
c. [3, 4, 5, 6, 7]
In a filter list comprehension, an element is added into list_name only if the condition is met.
CHECKPOINT
Filtering a list
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/9-5-list-
comprehensions)
CONCEPTS IN PRACTICE
a. [i, i, a, o, e]
b. ['i', 'i'', 'a', 'o', 'e']
c. Error
a. []
b. [21]
c. [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
TRY IT
TRY IT
• Lists are mutable and can be easily modified by using append(), remove(), and pop() operations.
• Lists are iterable and can be iterated using an iterator or element indexes.
• The sort() operation arranges the elements of a list in ascending order if all elements of the list are of
the same type.
• The reverse() operation reverses a list.
• The copy() method is used to create a copy of a list.
• Lists have built-in functions for finding the maximum, minimum, and summation of a list for lists with only
numeric values.
• Lists can be nested to represent multidimensional data.
• A list comprehension is a compact way of creating a new list, which can be used to filter items from an
existing list.
Function Description
remove(element) Removes the specified element from the list if the element exists.
Function Description
Chapter Outline
10.1 Dictionary basics
10.2 Dictionary creation
10.3 Dictionary operations
10.4 Conditionals and looping in dictionaries
10.5 Nested dictionaries and dictionary comprehension
10.6 Chapter summary
Introduction
A Python dictionary is a data type for storing the data in a key-value pair format. In this chapter, the dictionary
data type is introduced. This chapter also introduces ways to use dictionaries, including looping over dictionary
items and performing conditional statements on a dictionary.
Dictionaries
A dictionary in Python is a container object including key-value pairs. An example of a key-value item is a
word in an English dictionary with its corresponding definition.
242 10 • Dictionaries
CHECKPOINT
Dictionaries
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
10-1-dictionary-basics)
CONCEPTS IN PRACTICE
Dictionary basics
Given the dictionary object days = {"Sunday": 1, "Monday": 2, "Tuesday": 3}, answer the
following questions.
CHECKPOINT
Dictionary properties
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
10-1-dictionary-basics)
CONCEPTS IN PRACTICE
a. dictionary
b. dict
c. str
5. The encoding dictionary contains duplicate keys since it has both "a" and "A" as keys.
a. true
b. false
TRY IT
Dictionary creation
Two methods exist for creating an empty dictionary:
A dictionary object can also be created with initial key-value pairs enclosed in curly braces. Ex: my_dict =
{"pizza": 2, "pasta": 3, "drink": 4} creates a dictionary object my_dict. A key and associated value
are separated by a colon, and key-value pairs are separated by commas.
CHECKPOINT
CONCEPTS IN PRACTICE
2. What is the correct syntax to create a dictionary with one item "one": 1?
a. my_dict = ("one": 1)
b. my_dict = ("one", 1)
c. my_dict = {"one": 1}
CHECKPOINT
CONCEPTS IN PRACTICE
a. {"a": 1}
b. <class, 'dict'>
c. "a"
1
6. Which option creates a dictionary with two key-value pairs, "a": "A" and "b": "B"?
a. dict(a: A, b: B)
b. dict([("a", "A"), ("b", "B")])
c. dict({"a" = "A", "b" = "B"})
TRY IT
• Square bracket notation: Square brackets [] with the key inside access the value associated with that key.
If the key is not found, an exception will be thrown.
• get() method: The get() method is called with the key as an argument to access the value associated
with that key. If the key is not found, the method returns None by default, or a default value specified as
the second argument.
Ex: In the code below, a dictionary object my_dict is initialized with items {"apple": 2, "banana": 3,
"orange": 4}. The square bracket notation and get() method are used to access values associated with the
keys "banana" and "apple", respectively. When accessing the dictionary to obtain the key "pineapple", -1
is returned since the key does not exist in the dictionary.
CHECKPOINT
CONCEPTS IN PRACTICE
Dictionary items
Given the dictionary members = {"Jaya": "Student", "John": "TA", "Ksenia": "Staff"},
answer the following questions.
function calls, respectively. The return type of keys(), values(), and items() are dict_keys,
dict_values, and dict_items, which can be converted to a list object using the list constructor list().
EXAMPLE 10.1
print(dictionary_object.keys())
print(list(dictionary_object.keys()))
print(dictionary_object.values())
print(dictionary_object.items())
CONCEPTS IN PRACTICE
Dictionary mutability
In Python, a dictionary is a mutable data type, which means that a dictionary's content can be modified after
creation. Dictionary items can be added, updated, or deleted from a dictionary after a dictionary object is
created.
To add an item to a dictionary, either the square bracket notation or update() function can be used.
• Square bracket notation: When using square brackets to create a new key object and assign a value to the
key, the new key-value pair will be added to the dictionary.
• update() method: the update() method can be called with additional key-value pairs to update the
dictionary content.
To modify a dictionary item, the two approaches above can be used on an existing dictionary key along with
the updated value. Ex:
• update() method:
Items can be deleted from a dictionary using the del keyword or the pop() method.
• del keyword:
• pop() method:
CHECKPOINT
CONCEPTS IN PRACTICE
Modifying a dictionary
Given the dictionary food = {"Coconut soup": "$15", "Butter Chicken": "$18", "Kabob":
"$20"}, answer the following questions.
7. Which option modifies the value for the key "Coconut soup" to "$11" while keeping other items the
same?
a. food = {""Coconut soup": "$15""}
b. food["Coconut soup"] = 11
c. food["Coconut soup"] = "$11"
8. Which option removes the item "Butter Chicken": "$18" from the food dictionary?
a. food.remove("Butter Chicken")
b. del food["Butter Chicken"]
c. del food.del("Butter Chicken")
9. What is the content of the food dictionary after calling food.update({"Kabob": "$22", "Sushi":
"$16"})?
a. {"Coconut soup": "$15", "Butter Chicken": "$18", "Kabob": "$22", "Sushi":
"$16"}
b. {"Coconut soup": "$15", "Butter Chicken": "$18", "Kabob": "$20", "Sushi":
"$16"}
c. { "Sushi": "$16", "Coconut soup": "$15", "Butter Chicken": "$18", "Kabob":
"$20"}
TRY IT
Prints {"Mustang": 2}
TRY IT
Input:
string_value = "This is a string"
Prints 10
EXAMPLE 10.2
dict.items()
True
Table 10.1
True
Table 10.2
252 10 • Dictionaries
dict.keys()
# key in dictionary.keys()
print("Interstellar" in movies.keys())
True
Table 10.3
dict.values()
# value in dictionary.values()
print(2014 in movies.values())
True
Table 10.4
CONCEPTS IN PRACTICE
Conditionals on dictionaries
Given the dictionary fruit_count = {"apple": 2, "orange": 5, "pomegranate": 1}, answer the
following questions.
3. Which conditional statement checks if the value associated with the key "pomegranate" is greater
than 0?
a. fruit_count("pomegranate") > 0
b. fruit_count["pomegranate"] > 0
c. fruit_count.get("pomegranate" > 0)
Looping on a dictionary
Looping over a Python dictionary is a way to iterate through key-value pairs in the dictionary. Looping in a
dictionary can be done by iterating over keys or items. When looping using keys, keys are obtained using the
keys() function and are passed to the loop variable one at a time. When looping over items using the
items() function, both the key and value for each item are passed to the loop variable.
EXAMPLE 10.3
dict.items()
Berkeley 94709
Santa Cruz 95064
Mountain View 94030
Table 10.5
254 10 • Dictionaries
dict.keys()
Berkeley
Santa Cruz
Mountain View
Table 10.6
dict.values()
94709
95064
94030
Table 10.7
CONCEPTS IN PRACTICE
Loops on dictionaries
4. Which method is used to loop over the values in a Python dictionary?
a. keys()
b. values()
c. items()
TRY IT
Input:
string_value = "This is a string"
Prints {"T": 1, "h": 1, "i": 3, "s": 3, " ": 3, "a": 1, "t": 1, "r": 1, "n": 1,
"g": 1}
TRY IT
Input:
fruit_count = {"banana": 2, "orange": 5, "peach": 5}
256 10 • Dictionaries
Prints 12
Nested dictionaries
As described before, Python dictionaries are a type of data structure that allows for storing data in key-value
pairs. Nested dictionaries are dictionaries that are stored as values within another dictionary. Ex: An
organizational chart with keys being different departments and values being dictionaries of employees in a
given department. For storing employee information in a department, a dictionary can be used with keys
being employee IDs and values being employee names. The tables below outline the structure of such nested
dictionaries and how nested values can be accessed.
EXAMPLE 10.4
company_org_chart = {
"Marketing": {
"ID234": "Jane Smith"
},
"Sales": {
"ID123": "Bob Johnson",
"ID122": "David Lee"
},
"Engineering": {
"ID303": "Radhika Potlapally",
"ID321": "Maryam Samimi"
}
}
Table 10.8
print(company_org_chart["Sales"]["ID122"])
print(company_org_chart["Engineering"]["ID321"])
David Lee
Maryam Samimi
Table 10.9
CONCEPTS IN PRACTICE
Dictionary comprehension
Dictionary comprehension is a concise and efficient way to create a dictionary in Python. With dictionary
comprehension, elements of an iterable object are transformed into key-value pairs. The syntax of dictionary
comprehension is similar to list comprehension, but instead of using square brackets, curly braces are used to
define a dictionary.
CHECKPOINT
Squares of numbers
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
10-5-nested-dictionaries-and-dictionary-comprehension)
CONCEPTS IN PRACTICE
Dictionary comprehension
4. What is the output of the following code?
numbers = [1, 2, 3, 4, 5]
my_dict = {x: x**2 for x in numbers if x % 2 == 0}
print(my_dict)
a. {1: 1, 3: 9, 5: 25}
b. {2: 4, 4: 16}
c. {1: 2, 2: 4, 3: 6, 4: 8, 5: 10}
a. {0: 0, 1: 1, 2: 4, 3: 9}
b. {0: 0, 1: 1, 2: 4}
c. {1: 1, 2: 4, 3: 9}
TRY IT
Product prices
Suppose you have a dictionary of product prices, where the keys are product names and the values are
their respective prices in dollars. Write a Python program that uses dictionary comprehension to create a
new dictionary that has the same keys as the original dictionary, but the values are the prices in euros.
Assume that the exchange rate is 1 dollar = 0.85 euros.
TRY IT
Input:
employees = {
1001: {"name": "Alice", "department": "Engineering", "salary": 75000},
1002: {"name": "Bob", "department": "Sales", "salary": 50000},
260 10 • Dictionaries
Resulting dictionary:
{
"Engineering": {1001: {"name": "Alice", "salary": 75000}, 1003: {"name":
"Charlie", "salary": 80000}},
"Sales": {1002: {"name": "Bob", "salary": 50000}, 1005: {"name": "Eve",
"salary": 55000}},
"Marketing": {1004: {"name": "Dave", "salary": 60000}}
}
At this point, you should be able to use dictionaries in your programs. The programming practice below ties
together most topics presented in the chapter.
Concept Description
# Using a list
my_list = [(key1, value1), (key2, value2)]
my_dict = dict(my_list)
Concept Description
Concept Description
{
Nested dictionaries key_1:{key11:value11, key12:value12},
key_2:{key21:value21, key22:value22}
}
TRY IT
Input:
input_list = [3, 5, 6, 1]
Figure 11.1 credit: modification of work "Fresh, bright apples newly picked", by Colorado State University Extension/Flickr, Public
Domain
Chapter Outline
11.1 Object-oriented programming basics
11.2 Classes and instances
11.3 Instance methods
11.4 Overloading operators
11.5 Using modules with classes
11.6 Chapter summary
Introduction
A programmer can model real-world entities as objects for better program design and organization. A class
defines a type of object with attributes and methods. Many instances of a class type can be created to
represent multiple objects in a program.
Classes promote reusability. Classes add benefits like data abstraction and encapsulation, which organize code
for better usability and extensibility.
and procedures into objects. Real-world entities are modeled as individual objects that interact with each
other. Ex: A social media account can follow other accounts, and accounts can send messages to each other. An
account can be modeled as an object in a program.
CHECKPOINT
CONCEPTS IN PRACTICE
OOP
1. Consider the example above. Which is a field in Ellis's 12/18/23 post?
a. Ellis's followers list
b. Ellis's username
Encapsulation
Encapsulation is a key concept in OOP that involves wrapping data and procedures that operate on that data
into a single unit. Access to the unit's data is restricted to prevent other units from directly modifying the data.
Ex: A ticket website manages all transactions for a concert, keeping track of tickets sold and tickets still
available to avoid accidental overbooking.
CHECKPOINT
CONCEPTS IN PRACTICE
Encapsulation
3. Consider the example above. Suppose the venue has to be changed due to inclement weather. How
should a programmer change the website object to allow changes to the Venue field?
a. Add a procedure to change the field.
b. Allow users direct access to the field.
c. No valid way to allow changes to the field.
a. All objects can easily access and modify each other's data.
b. Each object's data is restricted for intentional access.
Abstraction
Abstraction is a key concept in OOP in which a unit's inner workings are hidden from users and other units
that don't need to know the inner workings. Ex: A driver doesn't usually need to know their car engine's exact,
numerical temperature. So the car has a gauge to display whether the engine temperature is within an
appropriate range.
CHECKPOINT
CONCEPTS IN PRACTICE
Abstraction
5. Consider the car example in the paragraph above. Suppose the designer decided to remove the engine
temperature indicator. Would this be a good use of abstraction?
a. yes
b. no
• Create a class with instance attributes, class attributes, and the __init__() method.
• Use a class definition to create class instances to represent objects.
In a Python program, a class defines a type of object with attributes (fields) and methods (procedures). A class
is a blueprint for creating objects. Individual objects created of the class type are called instances.
268 11 • Classes
CHECKPOINT
CONCEPTS IN PRACTICE
class Cat:
def __init__(self):
self.name = 'Kitty'
self.breed = 'domestic short hair'
self.age = 1
def print_info(self):
print(self.name, 'is a ', self.age, 'yr old', self.breed)
pet_1 = Cat()
pet_2 = Cat()
4. Suppose the programmer wanted to change the class to represent a pet cat. Which is the appropriate
name that follows PEP 8 recommendations?
a. petcat
b. pet_cat
c. PetCat
CHECKPOINT
CONCEPTS IN PRACTICE
1 class Rectangle:
2 def __init__(self):
3 self.length = 1
4 self.width = 1
5 def area(self):
6 return self.length * self.width
7
8 room_1 = Rectangle()
9 room_1.length = 10
10 room_1.width = 15
11 print("Room 1's area:", room_1.area())
12 room_3 = Rectangle()
13 room_3.length = 12
14 room_3.width = 14
15 print("Room 3's area:", room_3.area())
6. When line 11 executes, execution flow moves to line 5. What does self represent on line 5?
a. the area of room_1
b. the instance room_1
c. the Rectangle class
8. Suppose line 2 is changed to def __init__():. What would room_1's attributes be initialized to?
a. length = 0, width = 0
b. length = 1, width = 1
c. Error
CHECKPOINT
CONCEPTS IN PRACTICE
10. Suppose the line order_1.cup_size = 8 is added before order_3.print_order(). What is the
new output?
a. Cafe Coffee Order 3 : 8 oz
b. Cafe Coffee Order 3 : 16 oz
c. Error
12. Suppose the line self.cls_id = 5 is added to the end of __init__()'s definition. What is the new
output?
a. Cafe Coffee Order 5 : 16 oz
b. Cafe Coffee Order 3 : 16 oz
c. Error
TRY IT
Instance attributes:
• flight_num: 1
• airport: JFK
• gate: T1-1
• time: 8:00
• seat: 1A
• passenger: unknown
Class attributes:
Method:
Given input:
2121
KEF
D22B
11:45
12B
Jules Laurent
Passenger Jules Laurent departs on flight # 2121 at 11:45 from KEF D22B in seat
12B
TRY IT
Instance attributes:
• title: ''
• author: ''
• year: 0
• pages: 0
Class attribute:
Method:
Given input:
• Create and implement __init__() with multiple parameters including default parameter values.
• Describe what information an instance method has access to and can modify.
CHECKPOINT
CONCEPTS IN PRACTICE
1. Suppose the programmer wanted to make blood pressure a required parameter in __init__(). Which
is the correct __init__() method header?
a. def __init__(p_id, bp, self, tmp=-1.0, hr=-1, rr=-1):
b. def __init__(self, p_id, bp, tmp=-1.0, hr=-1, rr=-1):
c. def __init__(self, p_id, bp=[-1,-1], tmp=-1.0, hr=-1, rr=-1):
3. Suppose another __init__() definition is added after the first with the header as follows:
Instance methods
An instance method is used to access and modify instance attributes as well as class attributes. All methods
shown so far, and most methods defined in a class definition, are instance methods.
EXAMPLE 11.1
Instance methods are often used to get and set instance information
class ProductionCar:
def __init__(self, make, model, year, max_mph = 0.0):
self.make = make
self.model = model
self.year = year
274 11 • Classes
self.max_mph = max_mph
def max_kmh(self):
return self.max_mph * 1.609344
CONCEPTS IN PRACTICE
class CoffeeOrder:
loc = 'Cafe Coffee'
cls_id = 1
def print_order(self):
print(CoffeeOrder.loc,'Order', self.order_id, ':', self.cup_size, 'oz')
if self.with_milk:
print('\twith milk')
if self.with_sugar:
print('\twith sugar')
order_1 = CoffeeOrder(8)
order_2 = CoffeeOrder(8, True, False)
order_1.change(False, True)
TRY IT
Instance attributes:
• count: 0
• max: 0
Methods:
Given input:
100
25
Refilled
Current stock: 100
CHECKPOINT
CONCEPTS IN PRACTICE
Magic methods
1. Which of the following is a magic method?
a. add()
b. _add_()
c. __add__()
3. Consider the example above, and identify the magic method(s) in the updated program.
a. __init__()
b. __str__()
c. __init__(), __str__()
CHECKPOINT
CONCEPTS IN PRACTICE
5. Given the code below, which argument maps to the parameter other in the call to
Account.__add__()?
a. acct_a
b. acct_b
c. acct_ab
278 11 • Classes
6. Which __sub__() definition overloads the - operator for the code below to work?
class Pair:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
# Define __sub__()
p1 = Pair(10, 2)
p2 = Pair(8, 4)
p3 = p1 - p2
print(p3.x, p3.y)
a. def __sub__(self):
return Pair(self.x - other.x, self.y - other.y)
b. def __sub__(self, other):
return self.x - other.x, self.y - other.y
c. def __sub__(self, other):
return Pair(self.x - other.x, self.y - other.y)
EXAMPLE 11.2
Code Output
class Account:
def __init__(self, name="", amount=0):
self.name = name
self.amount = amount
def __str__(self):
return f"{self.name}: ${self.amount}"
Table 11.3
CONCEPTS IN PRACTICE
a. def __le__(other):
return self.amount <= other.amount
b. def __le__(self, other):
return self.amount <= other.amount
c. def __le__(self, other):
return other.amount <= self.amount
TRY IT
• If the exercise types are different, combine them with " and " in between. Else, use the same type
and don't duplicate.
• Add durations together.
Given input:
walk
5
run
30
TRY IT
Expand the existing Account example so that the addition operator can also be used to add an integer
value to an Account's amount attribute.
Multiple classes can be grouped in a module. For good organization, classes should be grouped in a module
only if the grouping enables module reuse, as a key benefit of modules is reusability.
CHECKPOINT
CONCEPTS IN PRACTICE
Importing classes
1. Consider the example above. How many classes are imported in the second main.py?
a. 1
b. 2
c. 3
Using aliases
Aliasing allows the programmer to use an alternative name for imported items. Ex: import triangle as
tri allows the program to refer to the triangle module as tri. Aliasing is useful to avoid name collisions but
should be used carefully to avoid confusion.
EXAMPLE 11.3
class Level:
def __init__(self, level=1):
self.level = level
...
def level_up(self):
...
class XP:
def __init__(self, XP=0):
self.XP = XP
...
def add_xp(self, num):
...
main.py
import character as c
bard_level = c.Level(1)
bard_XP = c.XP(0)
bard_XP.add_xp(300)
bard_level.level_up()
...
CONCEPTS IN PRACTICE
Using an alias
Consider the example above. Suppose a program imports the character module using import character
as game_char.
c. rogue_level = character.game_char.Level()
TRY IT
TRY IT
• Object-oriented programming involves grouping related fields and procedures into objects using data
abstraction and encapsulation.
• Classes define a type of object with attributes and methods.
• Instances of a class type represent objects and are created using __init__().
• Attributes may belong to the instance (unique for each instance) or the class (shared by all instances).
• Instance methods have the first parameter self to access the specific instance.
• Python uses magic methods to perform "under-the-hood" actions for users. Magic methods always start
and end with double underscores.
• Python allows overloading of existing operators for user-defined classes.
• Classes can be imported from modules by name or can be renamed using aliases.
At this point, you should be able to write classes that have instance attributes, class attributes, and methods,
and import classes from modules. You should also be able to overload operators when defining a class.
284 11 • Classes
Construct Description
class ClassName:
__init__()
def __init__(self):
# Initialize attributes
class ClassName:
class_attr_1 = [value]
class_attr_2 = [value]
Attributes
def __init__(self):
instance_attr_1 = [value]
instance_attr_2 = [value]
instance_attr_3 = [value]
class ClassName:
def __init__(self):
instance_attr_1 = [value]
instance_attr_2 = [value]
Methods (instance) instance_attr_3 = [value]
def method_2(self):
# Access/change attributes
instance_1 = ClassName()
Instances instance_1.instance_attr_1 = [new value]
instance_2 = ClassName()
instance_2.method_2()
Construct Description
class ClassName:
# Other methods
Overloaded multiplication
operator
def __mul__(self, other):
return ClassName(self.instance_attr_3 *
other.instance_attr_3)
Figure 12.1 credit: modification of work "Rings from the Ocean", by Sue Corbisez/Flickr, CC BY 2.0
Chapter Outline
12.1 Recursion basics
12.2 Simple math recursion
12.3 Recursion with strings and lists
12.4 More math recursion
12.5 Using recursion to solve problems
12.6 Chapter summary
Introduction
Recursion is a powerful programming technique to write solutions compactly. Recursion simplifies programs
by calling smaller versions of a given problem that are used to generate the solution to the overall problem.
In advanced programming, some programs are difficult to write without the use of recursion. So recursion is
also a style of programming that enables us to simplify programs.
Recursion enables a piece of code, a function, to call the same piece of code, the same function, with different
parameters.
Recursion
Recursion is a problem solving technique that uses the solution to a simpler version of the problem to solve
the bigger problem. In turn, the same technique can be applied to the simpler version.
288 12 • Recursion
CHECKPOINT
Three Towers
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
12-1-recursion-basics)
CONCEPTS IN PRACTICE
3. How many times is the two-ring solution used with three rings?
a. 1
b. 2
c. 0
CHECKPOINT
CONCEPTS IN PRACTICE
c. 3
Calculating a factorial
The factorial of a positive integer is defined as the product of the integer and the positive integers less than
the integer.
Ex: 5! = 5 * 4 * 3 * 2 * 1
The above formula for the factorial of n results in a recursive formula: n! = n * (n - 1)!
Thus, the factorial of n depends upon the value of the factorial at n - 1. The factorial of n can be found by
repeating the factorial of n - 1 until (n - 1)! = 1! (we know that 1! = 1). This result can be used to build
the overall solution as seen in the animation below.
CHECKPOINT
CONCEPTS IN PRACTICE
Recognizing recursion
Can the following algorithms be written recursively?
2. Listing the odd numbers greater than 0 and less than a given number n.
290 12 • Recursion
A value for which the recursion applies is called the recursive case. In the recursive case, the function calls
itself with a smaller portion of the input parameter. Ex: In the recursive function factorial(), the initial
parameter is an integer n. In the function's recursive case, the argument passed to factorial() is n - 1,
which is smaller than n.
A value of n for which the solution is known is called the base case. The base case stops the recursion. A
recursive algorithm must include a base case; otherwise, the algorithm may result in an infinite computation.
To calculate a factorial, a recursive function, factorial() is defined with an integer input parameter, n. When
n > 1, the recursive case applies. The factorial() calls itself with a smaller argument, n - 1. When n ==
1, the solution is known because 1! is 1; therefore, n == 1 is a base case.
Note: 0! is defined to be 1; therefore, n == 0 is a second base case for factorial(). When n < 1, an error
is returned.
CHECKPOINT
CONCEPTS IN PRACTICE
Programming recursion
For the questions below, the function rec_fact() is another recursive function that calculates a factorial.
What is the result of each definition of rec_fact() if n = 17 is the initial input parameter?
5. def rec_fact(n):
return n * rec_fact(n - 1)
a. 355687428096000
b. 0
c. no result / infinite computation
6. def rec_fact(n):
if n < 0:
print("error")
elif n == 0:
return n
else:
return n * rec_fact(n - 1)
a. 355687428096000
b. 0
c. no result / infinite computation
7. def rec_fact(n):
if n < 0:
print("error")
elif n == 0:
return 1
else:
return n * rec_fact(n - 1)
a. 355687428096000
b. 0
c. no result / infinite computation
8. def rec_fact(n):
if n < 0:
return -1
else:
return n * rec_fact(n - 1)
a. 355687428096000
b. 0
c. no result / infinite computation
TRY IT
Recursive summation
Write a program that uses a recursive function to calculate the summation of numbers from 0 to a user
specified positive integer n.
TRY IT
Digits
Write a program that computes the sum of the digits of a positive integer using recursion.
Hint: There are 10 base cases, which can be checked easily with the right condition.
CHECKPOINT
Identifying a palindrome
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
12-3-recursion-with-strings-and-lists)
CONCEPTS IN PRACTICE
palindrome(word.strip(word[0])).
a. Nothing. The function would work the same: the first and last letter would be removed.
b. Some words, such as "madamm", would be incorrectly recognized as a palindrome.
The count() function returns a count of the number of items in a list that match the given item, and returns 0
otherwise. Ex: For list_num = [1, 3, 3, 4], list_num.count(3) returns 2.
CHECKPOINT
CONCEPTS IN PRACTICE
List permutations
Refer to the above animation. What would permu_check() return for each pair of lists below?
a. True
b. False
a. True
b. False
a. True
b. False
a. True
b. False
8. list_1 = [1, 2, 3]
list_2 = [1, 2, 3]
a. True
b. False
TRY IT
Remove duplicates
Write a recursive rem_dup() function that removes duplicates from a list.
Recursion can be used to calculate the nth Fibonacci number. The base cases are 0 and 1. Thereafter, the nth
number is given by adding the (n - 1)th and (n - 2)th number. Thus, fib(n) = fib(n - 1) + fib(n - 2),
which is a recursive definition ending with base cases f(0) = 0, and f(1) = 1.
For a recursion that calls multiple functions in the recursive case, tracing how the recursion proceeds is useful.
A structure used to trace the calls made by a recursive function is called a recursion tree. The animation below
traces the recursion tree for a call to a recursive function to calculate the Fibonacci number for n = 5.
EXAMPLE 12.1
def fib(n):
# Base case
if n == 0 or n == 1:
return n
# Recursive case
elif n > 1:
return fib(n - 1) + fib(n - 2)
# Error case
else:
print("Fibonacci numbers begin at 0.")
return
# Test code
print(fib(7))
13
CHECKPOINT
CONCEPTS IN PRACTICE
Fibonacci numbers
1. fib(9) results in _____.
a. 13
b. 21
c. 34
2. How many calls to the base cases are made as a result of calling fib(5)?
a. 2
b. 5
c. 8
3. A structure tracing the function calls made as a result of a complex recursive function call is called a
_____.
296 12 • Recursion
a. tree
b. recursion tree
c. iteration tree
The GCD is found easily using Euclid's method. Euclid's method recursively subtracts the smaller integer from
the larger integer until a base case with equal integers is reached. The greatest common divisor is the integer
value when the base case is reached.
EXAMPLE 12.2
# Test code
print(gcd(24, 30))
CONCEPTS IN PRACTICE
GCD
4. What is the GCD of 15 and 35?
a. 15
b. 20
c. 5
TRY IT
Recursive power
Write a recursive power() function, such that given an integer x and a positive integer y, power(x, y)
returns x raised to y.
TRY IT
(Note: this is an extension of the above problem but now works for any integer value of y, positive or
negative. How should the recursive function change to deal with a negative value of y?)
Binary search
Searching a sorted list usually involves looking at each item. If the item being searched is not found, then the
search can take a long time.
A binary search is a recursive algorithm used to efficiently search sorted lists. In each recursive step, about half
the items are discarded as not being potential matches, so the search proceeds much faster.
A binary search begins by checking the middle element of the list. If the search key is found, the algorithm
returns the matching location (base case). Otherwise, the search is repeated on approximately half the list. If
the key is greater than the middle element, then the key must be on the right half, and vice versa. The process
continues by checking the middle element of the remaining half of the list.
EXAMPLE 12.3
Binary search
# Left subarray
else:
return binary_search(search_list, low, mid - 1, key)
else:
# Key not found (other base case)
return "Not found"
# Test list
in_list = [1, 3, 13, 16, 19, 22, 27, 32, 48, 66, 78, 99, 111, 122]
8
Not found
CHECKPOINT
Binary search
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
12-5-using-recursion-to-solve-problems)
CONCEPTS IN PRACTICE
Binary search
1. Which list can be searched with the binary search algorithm?
a. [5, 2, 7, 1, 8]
b. [9, 8, 7, 6, 5]
c. [4, 5, 6, 7, 8]
2. If the binary_search() function is called with low = 4, and high = 7, mid computes to _____.
a. 5
b. 6
c. 11
4. How many calls to the binary_search() function occur in the code example when searching 86?
a. 4
b. 14
EXAMPLE 12.4
Solving N towers
The solution to Three Towers is simple with recursion. In the code below, rings are numbered from the top
down. The smallest ring is 1, the next ring is 2, and when solving for three rings, the bottom ring is 3.
# Recursive case
# Call the smaller version of the problem:
# to move the N-1 stack to the middle tower
three_towers(N-1, source_tower, temp_tower, dest_tower)
# Test code
print("Solution for 3 rings:")
three_towers(3, 't1', 't3', 't2') # t1, t2, t3 are the towers
CONCEPTS IN PRACTICE
6. Would an iterative solution to the Three Towers problem be more complex or less complex?
a. more complex
b. less complex
TRY IT
Coin combinations
Write a recursive function print_H_T() that produces all possible combinations of heads ("H") and tails
("T") for a given number of coin tosses.
Ex: For three coins, the program should print the output shown below.
HHH
HHT
HTH
HTT
THH
THT
TTH
TTT
Function Description
Returns the number of times the element exists on the list on which the count()
count(element)
function is called.
Figure 13.1 credit: modification of work "Don't stare...only sheep dogs stare...", by Bernard Spragg. NZ/Flickr, Public Domain
Chapter Outline
13.1 Inheritance basics
13.2 Attribute access
13.3 Methods
13.4 Hierarchical inheritance
13.5 Multiple inheritance and mixin classes
13.6 Chapter summary
Introduction
Real-world entities are often described in relation to other entities. Ex: A finch is a type of bird. Similarly,
classes, which represent types of real-world entities, can be related to each other.
Inheritance describes the relationship in which one class is a type of another class. Classes within inheritance
relationships can inherit attributes and methods from other classes without needing to redefine everything.
Thus, inheritance in object-oriented programming reduces redundancy and promotes modularity.
Is-a relationships can be confused with has-a relationships. A has-a relationship exists between a class that
contains another class. Ex: An employee has a company-issued laptop. Note: The laptop is not an employee.
CHECKPOINT
CONCEPTS IN PRACTICE
Inheritance in Python
Inheritance uses an is-a relationship to inherit a class from a superclass. The subclass inherits all the
superclass's attributes and methods, and extends the superclass's functionality.
In Python, a subclass is created by including the superclass name in parentheses at the top of the subclass's
definition:
class SuperClass:
# SuperClass attributes and methods
class SubClass(SuperClass):
# SubClass attributes and methods
CHECKPOINT
CONCEPTS IN PRACTICE
Creating subclasses
4. How is a Daisy class that inherits from the Plant class defined?
a. class Plant(Daisy):
b. class Daisy(Plant):
c. class Daisy:
class Plant:
5. Suppose a CarryOn class is inherited from a Luggage class. How is a CarryOn instance created?
a. small_bag = CarryOn()
b. small_bag = Luggage(CarryOn)
c. small_bag = CarryOn(Luggage)
6. Given the following SuperClass and SubClass, which of the following can an instance of SubClass
access?
class SuperClass():
def func_1(self):
print('Superclass function')
class SubClass(SuperClass):
def func_2(self):
print('Subclass function')
a. func_1() only
b. func_2() only
c. func_1() and func_2()
7. Given the following SuperClass and SubClass, which of the following can an instance of SuperClass
access?
class SuperClass():
def func_1(self):
print('Superclass function')
class SubClass(SuperClass):
def func_2(self):
print('Subclass function')
a. func_1() only
b. func_2() only
c. func_1() and func_2()
Python documentation for inheritance uses multiple terms to refer to the class that is inherited from and
306 13 • Inheritance
the class that inherits. This book uses superclass/subclass throughout for consistency.
superclass subclass
Table 13.1
TRY IT
TRY IT
Polygon classes
Define three classes: Polygon, Rectangle, and Square:
Create an instance of each class. Then, for each instance, call all the methods the instance has access to.
CHECKPOINT
CONCEPTS IN PRACTICE
2. Consider the following example. Line 13 executes and SubClass() is called. Which line does control
flow move to?
1 class SuperClass:
2 def __init__(self):
3 self.feat_1 = 1
4 self.feat_2 = ""
5
6 def bc_display(self):
7 print(f"Superclass: {self.feat_2}")
8
9 class SubClass(SuperClass):
10 def dc_display(self):
11 print(f"Subclass: {self.feat_2}")
12
13 dc_1 = SubClass()
a. 2
b. 10
c. Error
3. Consider the following example. Which instance attribute(s) does dc_1 have?
class SuperClass:
def __init__(self):
self.feat_1 = 1
self.feat_2 = ""
308 13 • Inheritance
def bc_display(self):
print(f"Superclass: {self.feat_2}")
class SubClass(SuperClass):
def dc_display(self):
print(f"Subclass: {self.feat_2}")
dc_1 = SubClass()
a. feat_2
b. feat_1 and feat_2
c. None
CHECKPOINT
CONCEPTS IN PRACTICE
class Employee:
count = 0
def __init__(self):
Employee.count += 1
self.e_id = Employee.count
self.hire_year = 2023
def emp_display(self):
print(f"Employee {self.e_id} hired in {self.hire_year}")
class Developer(Employee):
def __init__(self):
Employee.count += 1
self.e_id = Employee.count
self.hire_year = 2023
self.lang_xp = ["Python", "C++", "Java"]
def dev_display(self):
print(f"Proficient in {self.lang_xp}")
emp_1 = Employee()
dev_1 = Developer()
6. Suppose dev_display() should be modified to display the developer's ID along with their
proficiencies. Ex: dev_1.dev_display() would output Employee 2 proficient in ['Python',
'C++', 'Java']. Which is the appropriate new print() call in dev_display()?
a. print(f"Employee {self.e_id} proficient in {self.lang_xp}")
b. print(f"Employee {self.Employee.e_id} proficient in {self.lang_xp}")
c. print(f"Employee 2 proficient in {self.lang_xp}")
TRY IT
• __init__(self): initializes inherited instance attribute ingredients with ["butter", "sugar", "eggs",
"flour"], and initializes instance attribute frosting with "buttercream"
• display(self): prints a cupcake's ingredients and frosting
Then call the display() method on a new Cupcake object. The output should match:
Made with ["butter", "sugar", "eggs", "flour"] and topped with buttercream
frosting
13.3 Methods
Learning objectives
By the end of this section you should be able to
Overriding methods
Sometimes a programmer wants to change the functions a subclass inherits. Mint is a subclass that has the
same functionality as Plant, except for one function. A subclass can override a superclass method by defining
a method with the same name as the superclass method.
CHECKPOINT
CONCEPTS IN PRACTICE
Overriding methods
1. Suppose a programmer inherits the ContractTax class from class Tax and wants to override Tax's
calc_tax(). What should the programmer do?
a. Define another calc_tax() method in Tax.
b. Define a calc_tax() method in ContractTax.
c. Define a function that takes a ContractTax instance.
2. Which is the error in the program that attempts to override calc_tax() for ContractTaxDE?
class Tax:
def calc_tax(self):
print('Calculating tax')
class ContractTax(Tax):
def calc_tax(self):
print('Calculating contract tax')
class ContractTaxDE(ContractTax):
def calc_tax():
print('Calculating German contract tax')
my_tax = ContractTaxDE()
my_tax.calc_tax()
class Tax:
def calc_tax(self):
print('Calculating tax')
total = 0 # To replace with calculation
return total
class ContractTax:
def calc_tax(self):
print('Calculating contract tax')
total = 0 # To replace with calculation
return total
my_tax = ContractTax()
my_tax.calc_tax()
super()
super() is a special method that provides a temporary superclass object instance for a subclass to use.
super() is commonly used to call superclass methods from a subclass. super() is commonly used in a
subclass's __init__() to assign inherited instance attributes. Ex: super().__init__().
CHECKPOINT
CONCEPTS IN PRACTICE
Using super()
Consider the following program.
1 class Polygon:
2 def __init__(self, num_sides=3):
3 self.num_sides = num_sides
4
5 class Rectangle(Polygon):
312 13 • Inheritance
4. Line 16 executes and Square's __init__() is called on line 12. Line 13 executes and the superclass's
__init__() is called. Which line does control flow move to next?
a. 2
b. 6
c. 14
5. The next line executes. Which line does control flow move to next?
a. 2
b. 6
c. 14
6. The method call returns. Lines 8 and 9 execute to initialize length and width, and Rectangle's
__init__() returns. Which line does control flow move to next?
a. 12
b. 14
c. 17
7. Square's __init__() returns and control flow moves to line 17. What is the output?
a. 3
b. 4
c. 5
Polymorphism
Polymorphism is the concept of having many forms. In programming, a single name can be used for multiple
functionalities. Within inheritance, polymorphism is the basis of method overriding, as multiple methods have
the same name.
EXAMPLE 13.1
class Plant:
def display(self):
print("I'm a plant")
class Mint(Plant):
def display(self):
print("I'm a mint")
class Lavender(Mint):
def display(self):
print("I'm a lavender")
mint_1 = Mint()
mint_1.display()
lavender_1 = Lavender()
lavender_1.display()
I'm a mint
I'm a lavender
Polymorphism can also be used with methods of unrelated classes. The class type of the calling object
determines the method executed.
EXAMPLE 13.2
class Tax:
def __init__(self, value):
self.value = value
def calc_tax(self):
print("Calculating tax")
total = 0.10 * self.value # To replace with calculation
return total
314 13 • Inheritance
class ContractTax:
def __init__(self, value):
self.value = value
def calc_tax(self):
print("Calculating contracts tax")
total = 0.15 * self.value # To replace with calculation
return total
Polymorphism allows methods to be called with different parameter types. Many built-in operators and
functions have this utility.
EXAMPLE 13.3
4
3
CONCEPTS IN PRACTICE
Polymorphism in practice
8. Consider the example. What is the output?
class Polygon:
def print_type(self):
print("Polygon type")
class Rectangle(Polygon):
def print_type(self):
print("Rectangle type")
class Square(Rectangle):
def print_type(self):
print("Square type")
sq_1 = Square()
sq_1.print_type()
a. Polygon type
b. Rectangle type
c. Square type
TRY IT
Overriding methods
Given the Pet class, create a Bird class inherited from Pet and a Finch class inherited from Bird.
Override the display() method in Bird and Finch such that the program output is:
TRY IT
Using super()
Given the Employee class, create a Developer class inherited from Employee with methods __init__()
and print_info() such that:
__init__()
print_info()
CHECKPOINT
CONCEPTS IN PRACTICE
Hierarchical organization
1. Which is an example of hierarchical inheritance?
a. Class B inherits from Class A
Class C inherits from Class A
b. Class B inherits from Class A
Class C inherits from Class B
c. Class B inherits from Class A
EXAMPLE 13.4
Choir members
class ChoirMember:
def display(self):
print("Current choir member")
class Soprano(ChoirMember):
def display(self):
super().display()
print("Part: Soprano")
class Soprano1(Soprano):
def display(self):
super().display()
print("Division: Soprano 1")
class Alto(ChoirMember):
def display(self):
super().display()
print("Part: Alto")
class Tenor(ChoirMember):
def display(self):
super().display()
print("Part: Tenor")
class Bass(ChoirMember):
def display(self):
super().display()
print("Part: Bass")
mem_10 = Alto()
318 13 • Inheritance
mem_13 = Tenor()
mem_15 = Soprano1()
mem_10.display()
print()
mem_13.display()
print()
mem_15.display()
CONCEPTS IN PRACTICE
class A:
def __init__(self, a_attr=0):
self.a_attr = a_attr
class B(A):
def __init__(self, a_attr=0, b_attr=0):
super().__init__(a_attr)
self.b_attr = b_attr
class C(A):
def __init__(self, a_attr=0, c_attr=0):
super().__init__(a_attr)
self.c_attr = c_attr
class D(B):
def __init__(self, a_attr=0, b_attr=0, d_attr=0):
super().__init__(a_attr, b_attr)
self.d_attr = d_attr
b_inst = B(2)
c_inst = C(c_attr=4)
d_inst = D(6, 7)
TRY IT
Overriding methods
Define three classes: Instrument, Woodwind, and String.
CHECKPOINT
CONCEPTS IN PRACTICE
class A:
def __init__(self, a_attr=2):
self.a_attr = a_attr
class B:
def __init__(self, b_attr=4):
self.b_attr = b_attr
b_inst = B(2)
c_inst = C(1, 2)
c. Error
Figure 13.2 The diamond problem. If both Dessert and BakedGood override a Food method, the overridden Food method that
ApplePie inherits is ambiguous. Thus, diamond shaped inheritance should be avoided.
Mixin classes promote modularity and can remove the diamond problem. A mixin class:
• Has no superclass
• Has attributes and methods to be added to a subclass
• Isn't instantiated (Ex: Given MyMixin class, my_inst = MyMixin() should never be used.)
Mixin classes are used in multiple inheritance to add functionality to a subclass without adding inheritance
concerns.
CONCEPTS IN PRACTICE
A pass statement is used in Python to indicate that code is to be written later and prevents certain errors
that would result if no code was written or a comment was used instead.
322 13 • Inheritance
class Plant:
def photosynth(self):
print("Photosynthesizing")
def eat(self):
print("Eating")
class Rose(Plant):
pass
class Pitcher(Plant):
pass
class VenusFlyTrap(Plant):
pass
5. The programmer edits the code as follows. How can the program be improved?
class Plant:
def photosynth(self):
print("Photosynthesizing")
class Rose(Plant):
pass
class Pitcher(Plant):
def eat(self):
print("Eating")
class VenusFlyTrap(Plant)
def eat(self):
print("Eating")
6. The programmer edits the code to create a mixin class containing eat(). Which class name, replacing
X, is most appropriate for the mixin?
class Plant:
def photosynth(self):
print("Photosynthesizing")
class X:
def eat(self):
print("Eating")
class Rose(Plant):
pass
class VenusFlyTrap(Plant, X)
pass
a. CarnivClass
b. CarnivMixin
TRY IT
• Replaces OnlineShop.
• Is a superclass of FoodFast.
• Has one method, process_online(), which prints "Connecting to server" and is called by
FoodFast's process_order().
• Inheritance describes an is-a relationship between classes. One class, the subclass, inherits from another
class, the superclass.
• Subclasses can access inherited attributes and methods directly.
• Subclasses can override superclass methods to change or add functionality.
• super() allows a subclass to access the methods of the superclass.
324 13 • Inheritance
• Polymorphism describes a single representation for multiple forms and is applied in Python to define
multiple methods with the same name and allow the same method to take different arguments.
• Hierarchical inheritance is a type of inheritance in which multiple classes inherit from a single superclass.
• Multiple inheritance is a type of inheritance in which one class inherits from multiple classes.
• Mixin classes are used in multiple inheritance to add functionality to a subclass without adding inheritance
concerns.
At this point, you should be able to write subclasses that inherit instance attributes and methods, and
subclasses that have unique attributes and overridden methods. You should also be able to create hierarchical
inheritance relationships and multiple inheritance relationships between classes.
Task Example
class SuperClass:
pass
Define a subclass
class SubClass(SuperClass):
pass
class SubClass(SuperClass):
Define a subclass's __init__() using def __init__(self):
super() super().__init__() # Calls superclass
__init__()
# Initialize subclass instance attributes
class SuperClass:
def display(self):
print('Superclass method')
Override a superclass method
class SubClass(SuperClass):
def display(self): # Same name as superclass
method
print('Subclass method')
Task Example
class SuperClass:
def display(self):
print('Superclass method')
class SubClass1(SuperClass):
def display(self):
Implement hierarchical inheritance print('Subclass 1 method ')
class SubClass2(SuperClass):
def display(self):
print('Subclass 2 method')
class SubClass3(SuperClass):
def display(self):
print('Subclass 3 method')
class SuperClass1:
pass
Chapter Outline
14.1 Reading from files
14.2 Writing to files
14.3 Files in different locations and working with CSV files
14.4 Handling exceptions
14.5 Raising exceptions
14.6 Chapter summary
Introduction
Programming often involves reading information from files and writing information into files. This chapter
begins by introducing how to read from and write into files in Python.
Reading and writing files can lead to exceptions when the file specified cannot be found. There are many other
situations in which exceptions can occur. The second half of the chapter introduces exceptions and how to
handle exceptions in Python.
Opening a file
Reading information from and writing information to files is a common task in programming.
CHECKPOINT
Opening a file
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
14-1-reading-from-files)
CONCEPTS IN PRACTICE
Opening files
1. What is the correct way to open a file named firstfile.txt so that the file's contents can be read into a
Python program?
a. open("firstfile")
b. open("firstfile.txt")
c. fileobj = open("firstfile.txt")
2. Suppose that there is no file named input.txt. What is the result of trying to execute
open("input.txt")?
a. Error
b. nothing
c. "input.txt" file is created.
fileobj = open("newfile.txt")
print(fileobj)
a. Error
b. Information about the object fileobj is printed.
c. The contents of newfile.txt are printed.
• The read() function reads the contents of a file and returns a string.
• The readline() function reads the next line in a file and returns a string.
• The readlines() function reads the individual lines of a file and returns a string list containing all the
lines of the file in order.
EXAMPLE 14.1
12
55
5
91
# Using read()
# Open the file and associate with a file object
infile = open("input.txt")
# Print str1
print("Result of using read():")
print(str1)
# Using read()
# Open the file and associate with a file object
infile2 = open("input.txt")
CHECKPOINT
CONCEPTS IN PRACTICE
Reading files
Suppose fileobj = open("input.txt") has already been executed for each question below.
input.txt:
Hello world!
How are you?
4. What is the correct way to use the read() function to read the contents of input.txt into a string
file_str?
a. file_str = read(fileobj)
b. file_str = read("input.txt")
c. file_str = fileobj.read()
TRY IT
TRY IT
input.txt
n: 5
25
13
4
6
19
Ex: open("output.txt", 'w') opens the output.txt file in writing mode. The following table lists common
modes.
332 14 • Files
fileobj =
Read mode: open("input.txt")
Open the specified file for reading. same as:
'r' If the file does not exist, an error occurs. fileobj =
When no mode parameter is used, the default is to open a file open("input.txt",
in read mode. 'r')
Append mode:
Open the specified file for appending, which means adding fileobj =
'a' open("log.txt", 'a')
information to the end of the existing file.
If the file does not exist, then the file is created.
CONCEPTS IN PRACTICE
File modes
Assume that a file named logfile.txt exists.
2. Additions are to be made to logfile.txt. Which of the following lines of code works best?
a. infile = open("logfile.txt")
b. infile = open("logfile.txt", 'w')
c. infile = open("logfile.txt", 'a')
3. A new file called newlog.txt must be created. Which of the following lines of code works best?
a. logfile = open("newlog.txt")
b. logfile = open("newlog.txt", 'w')
c. logfile = open("newlog.txt", 'a')
The write() function does not automatically add a newline character as the print() function does. A
newline must be added explicitly by adding a newline ('\n') character.
The write() function writes automatically to a temporary store called the file buffer. To ensure that the
information is written to a file, the close() function must be used. The close() function finalizes changes
and closes the file.
EXAMPLE 14.2
CHECKPOINT
CONCEPTS IN PRACTICE
Writing to files
resources.txt already exists. What would happen for each of the following snippets of code?
a. nothing
b. The contents of the resources.txt file is overwritten with the line "new content".
c. The resources.txt file is overwritten, but the contents of resources.txt are uncertain.
a. The content of the resources.txt file is overwritten with the line "new content".
b. The line "new content" is added to the end of resources.txt.
a. The content of the resources.txt file is overwritten with the line "new content".
TRY IT
TRY IT
Often a programmer needs to open files from folders other than the one in which the Python file exists. A path
uniquely identifies a folder location on a computer. The path can be used along with the filename to open a file
in any folder location. Ex: To open a file named logfile.log located in /users/turtle/desktop the following
can be used:
fileobj = open("/users/turtle/desktop/logfile.log")
336 14 • Files
fileobj = open("c:/projects/code/output.txt")
c:\projects\ or
Windows code\ fileobj =
open("c:\\projects\\code\\output.txt")
Table 14.2 Opening files on different paths. In each of the following cases, a file called output.txt is located in a different folder
than the Python folder. Windows uses backslash \ characters instead of forward slash / characters for the path. If the backslash is
included directly in open(), then an additional backslash is needed for Python to understand the location correctly.
CONCEPTS IN PRACTICE
Each question indicates the location of out.txt, the type of computer, and the desired mode for opening the
file. Choose which option is best for opening out.txt.
such as text files (.txt), Python code files (.py), and other code files (.c,.java).
Comma separated value (CSV, .csv) files are often used for storing tabular data. These files store cells of
information as Unicode separated by commas. CSV files can be read using methods learned thus far, as seen in
the example below.
Figure 14.2 CSV files. A CSV file is simply a text file with rows separated by newline \n characters and cells separated by commas.
EXAMPLE 14.3
list_csv = []
# Remove \n characters from each row and split by comma and save into a 2D
structure
for row in csv_rows:
338 14 • Files
# Remove \n character
row = row.strip("\n")
# Split using commas
cells = row.split(",")
list_csv.append(cells)
# Print result
print(list_csv)
[['Title', ' Author', ' Pages'], ['1984', ' George Orwell', ' 268'], ['Jane
Eyre', ' Charlotte Bronte', ' 532'], ['Walden', ' Henry David Thoreau', ' 156'],
['Moby Dick', ' Herman Melville', ' 538']]
CONCEPTS IN PRACTICE
6. For the code in the example, what would be the output for the statement print(list_csv[1][2])?
a. 532
b. 268
c. Jane Eyre
7. What is the output of the following code for the books.csv seen above?
file_obj = open("books.csv")
csv_read = file_obj.readline()
print(csv_read)
EXPLORING FURTHER
Files such as Word documents (.docx) and PDF documents (.pdf), image formats such as Portable Network
Graphics (PNG, .png) and Joint Photographic Experts Group (JPEG, .jpeg or .jpg) as well as many other file
types are encoded differently.
Some types of non-Unicode files can be read using specialized libraries that support the reading and writing
of different file types.
BeautifulSoup (https://openstax.org/r/100software) can be used to extract information from XML and HTML
files. XML and HTML files usually contain unicode with structure provided through the use of angled <>
bracket tags.
TRY IT
Runtime errors
Various errors may occur when reading a file:
EXAMPLE 14.4
Typo in a file
A file named food_order.txt has the following contents:
5 sandwiches
4 chips
1 pickle
soft drinks
The following program expects each line of the file to begin with an integer:
Unfortunately, the line "soft drinks" does not begin with an integer. As a result, the program terminates
and displays an error message:
CONCEPTS IN PRACTICE
Common exceptions
What error might occur in each situation?
3. parts = line.split()
qty = parts[0]
item = parts[1]
a. FileNotFoundError
b. IndexError
c. ValueError
EXPLORING FURTHER
The Built-in Exceptions (https://openstax.org/r/100exceptions) page of the Python Standard Library explains
the meaning of each exception.
EXAMPLE 14.5
When running this program with the input try_open.py, the name of the program file, the output is:
If the filename does not exist, a FileNotFoundError is raised on line 3. The program then jumps to the
except clause on line 7 and continues to run. The resulting output is:
342 14 • Files
CONCEPTS IN PRACTICE
4. word = "one"
try:
number = int(word)
print(word, "equals", number)
except ValueError:
print(word, "is not a number")
a. one equals 1
b. one is not a number
c. ValueError: invalid literal for int() with base 10: 'one'
5. word = "one"
try:
number = int(word)
print(word, "equals", number)
except IndexError:
print(word, "is not a number")
a. one equals 1
b. one is not a number
c. ValueError: invalid literal for int() with base 10: 'one'
6. word = "one"
try:
char = word[3]
print("The char is", char)
except:
print("That didn't work")
a. The char is e
b. That didn't work
c. IndexError: string index out of range
TRY IT
Type analyzer
Analysis programs often need to find numbers in large bodies of text. How can a program tell if a string like
"123.45" represents a number? One approach is to use exceptions:
1. Try converting the string to an integer. If no ValueError is raised, then the string represents an
integer.
2. Otherwise, try converting the string to a float. If no ValueError is raised, then the string represents a
float.
3. Otherwise, the string does not represent a number.
Implement the get_type() function using this approach. The provided main block calls get_type() for
each word in a file. get_type() should return either "int", "float", or "str", based on the word. The
output for the provided data.txt is:
str: Hello
int: 100
str: times!
float: 3.14159
str: is
str: pi.
TRY IT
United countries
Write a program that prompts the user to input a word and a filename. The program should print each line
of the file that contains the word. Here is an example run of the program (user input in bold):
United
Enter a filename: countries.csv
United Arab Emirates,9890402,83600,118
United Kingdom,67886011,241930,281
United States of America,331002651,9147420,36
This example uses a file named countries.csv based on the alphabetical list of countries
344 14 • Files
(https://openstax.org/r/100countries) from Worldometer. Each line of the file includes a country's name,
population, land area, and population density, separated by commas.
The user might incorrectly type the filename (Ex: countries.txt instead of countries.csv). Your program
should output an error message if the file is not found, and keep prompting the user to input a filename
until the file is found:
countries
File not found: countries
Enter a filename: countries.txt
File not found: countries.txt
Enter a filename: countries.csv
...
...
Enter a filename: countries
File not found: countries
Enter a filename: countries.txt
File not found: countries.txt
Enter a filename: countries.csv
...
Hint: Try to open the file specified by the user. A FileNotFoundError is raised if the filename is invalid.
EXAMPLE 14.6
1 class Pizza:
2
3 SIZES = {
4 "S": ("Small", 5.99),
5 "M": ("Medium", 7.99),
6 "L": ("Large", 9.99),
7 "XL": ("X-Large", 11.99),
8 }
9
10 def __init__(self, size):
11 if size not in Pizza.SIZES:
12 raise ValueError(f"Unknown size '{size}'")
13 self.size = size
14
15 def __str__(self):
16 name, price = Pizza.SIZES[self.size]
17 return f"{name} pizza for ${price}"
18
19 if __name__ == "__main__":
20 p1 = Pizza("L")
21 print(p1)
22 p2 = Pizza("Z")
23 print(p2)
Raising an exception in a constructor prevents an invalid object from being constructed. In the example, p1
is constructed but p2 is not constructed.
CONCEPTS IN PRACTICE
1. p3 = Pizza("M")
print(p3)
3. raise ValueError
a. ValueError
b. NameError: name 'ValueError' is not defined
c. SyntaxError: Missing parenthesis in call to 'ValueError'
TRY IT
Language objects
The file top27.csv is based on Wikipedia's list of languages by number of native speakers
(https://openstax.org/r/100languages). A linguist would like to work with this data in an object-oriented
way and has already defined a Language class. However, the code is not working correctly because of
errors in the CSV file. (The CSV program cannot be changed because the program's purpose is to detect
these kinds of errors.)
Your task is to modify the constructor to raise a ValueError in the following situations:
• If the line does not have exactly four values (separated by commas).
• If the number of speakers is not a positive floating point number.
Note: The provided top27.csv includes mistakes for English, Korean, and French. Your program should raise
a ValueError only for these languages. No other errors (including IndexError) should be raised.
TRY IT
Looking up keys
When looking up a value in a dictionary, a key has to match exactly. Ex: Given fruit = {"apple": 1,
"banana": 2}, the expression fruit["Apple"] raises a KeyError because "Apple" is not the same as
"apple". Likewise, fruit["NANA"] raises a KeyError because "nana" does not exactly match "banana".
Implement the lookup() function. This function tries to find a key in the dictionary that matches the
search term. A key matches if the search term is a substring of the key, ignoring case. Ex: The key
"banana" matches the search term "NANA".
If no matching keys are found, lookup() should raise a KeyError with the message "search term not
found". If multiple matches are found, lookup() should raise a KeyError with the message "multiple
keys found". Otherwise, lookup() should return both the key and the value for the matching item in the
dictionary.
• Python can read and write data in files using the built-in open() function.
• Due to buffering, changes to a file may not be visible until the file is closed.
• Newline characters need to be removed/added when reading/writing files.
• Comma-separate-value (CSV) files are commonly used to represent data.
• Exceptions that cause a program to terminate can be handled using try and except.
• Raising an exception is an alternative way to return from a function when an error occurs.
Statement Description
file =
open("myfile.txt") Open a file for reading.
file =
open("myfile.txt", Open a file for writing.
'w')
file =
open("myfile.txt", Open a file for appending.
'a')
data = file.read() Read the entire contents of a file. The variable data is a string.
data =
Read the next line of a file. The variable data is a string.
file.readline()
Statement Description
data =
Read all lines of a file. The variable data is a list of strings.
file.readlines()
file.write("Have a Writes a line to a file. In contrast to print(), the write() function does not
nice day!\n") automatically append a newline.
file.write(["Line Writes multiple lines to a file. As with write(), a newline is not automatically
1\n", "Line 2\n"]) added at the end of each line.
try:
Try to run statements that might raise an error. If any error is raised, run other
# Statements
statements.
except:
# Statements
try: Try to run statements that might raise a ValueError. If a ValueError is raised,
# Statements run other statements.
except ValueError:
# Statements
try: Try to run statements that might raise a ValueError. If a ValueError is raised,
# Statements run other statements. The variable err contains more details about the
except ValueError ValueError.
as err:
# Statements
Chapter Outline
15.1 Introduction to data science
15.2 NumPy
15.3 Pandas
15.4 Exploratory data analysis
15.5 Data visualization
15.6 Summary
Introduction
Data Science provides the ability to derive insights and make informed decisions from data. Data science
helps people make decisions in disciplines as diverse as healthcare, business, education, politics,
environmental science, and social sciences.
This chapter aims to provide an introduction to the field of data science and the data science life cycle. The
resources provided in this chapter are meant to guide readers using Python to further explore data science.
data to extract insights and drive informed decision-making. The data science life cycle is the framework
followed by data scientists to complete a data science project. The data science life cycle is an iterative process
that starts with data acquisition, followed by data exploration. The data acquisition stage may involve
obtaining data from a source or collecting data through surveys and other means of data collection that are
domain-specific. During the data exploration stage, data scientists will ensure that the data are in the right
format for the data analysis stage through data cleanup and they may also visualize the data for further
inspection. Once the data are cleaned, data scientists can perform data analysis, which is shared with
stakeholders using reports and presentations. The data analysis stage involves using data to generate
insights or make a predictive model. Data science is increasingly being adopted in many different fields, such
as healthcare, economics, education, and social sciences, to name a few. The animation below demonstrates
different stages of the data science life cycle.
CHECKPOINT
CONCEPTS IN PRACTICE
2. How many stages does the data science life cycle have?
a. 3
b. 4
c. 5
• Python programming language: Python is widely used in data science. It has a large system of libraries
designed for data analysis, machine learning, and visualization. Some popular Python libraries for data
science include NumPy, Pandas, Matplotlib, Seaborn, and scikit-learn. In this chapter, you will explore
some of these libraries.
• R programming language: R is commonly used in statistical computing and data analysis, and it offers a
wide range of packages and libraries tailored for data manipulation, statistical modeling, and visualization.
• Jupyter Notebook/JupyterLab: Jupyter Notebook and JupyterLab are web-based interactive computing
environments that support multiple programming languages, including Python and R. They allow a
programmer to create documents that contain code, visualizations, and text, making them suitable for
data exploration, analysis, and reporting.
• Google Colaboratory: Google Colaboratory is a cloud-based Jupyter Notebook environment that allows a
programmer to write, run, and share Python code online. In this chapter, you will use Google Colaboratory
to practice data science concepts.
• Kaggle Kernels: Kaggle Kernels is an online data science platform that provides a collaborative
environment for building and running code. Kaggle Kernels support Python and R and offers access to
datasets, pre-installed libraries, and computational resources. Kaggle also hosts data science competitions
and provides a platform for sharing and discovering data science projects.
• Excel/Sheets: Microsoft Excel and Google Sheets are widely used spreadsheet applications that offer
basic data analysis and visualization capabilities. They can help beginners get started with data
manipulation, basic statistical calculations, and simple visualizations.
CHECKPOINT
CONCEPTS IN PRACTICE
15.2 NumPy
Learning objectives
By the end of this section you should be able to
NumPy library
NumPy (Numerical Python) is a Python library that provides support for efficient numerical operations on
large, multi-dimensional arrays and serves as a fundamental building block for data analysis in Python. The
conventional alias for importing NumPy is np. In other words, NumPy is imported as import numpy as np.
NumPy implements the ndarray object, which allows the creation of a multi-dimensional array of
homogeneous data types (columns with the same data type) and efficient data processing. An ndarray object
can have any number of dimensions and can store elements of various numeric data types. To create a NumPy
ndarray object, one of the following options can be used:
• Creating an ndarray by converting a Python list or tuple using the np.array() function.
• Using built-in functions like np.zeros() and np.ones() for creating an array of all 0's or all 1's,
respectively.
• Generating an array with random numbers using np.random.rand(n, m), where n and m are the
number of rows and columns, respectively.
• Loading data from a file. Ex: np.genfromtxt('data.csv', delimiter=',').
CHECKPOINT
CONCEPTS IN PRACTICE
NumPy library
1. Which of the following creates an ndarray object with one row and two columns?
a. np.array([2, 3])
b. np.zeros(1, 2)
c. np.zeros((1, 2))
b. array-oriented computing
c. memory efficiency
d. all the above
NumPy operations
In addition to the ndarray data type, NumPy's operations provide optimized performance for large-scale
computation. The key features of NumPy include:
• Mathematical operations: NumPy provides a range of mathematical functions and operations that can be
applied to entire arrays or specific elements. These operations include arithmetic, trigonometric,
exponential, and logarithmic functions.
• Array manipulation: NumPy provides various functions to manipulate the shape, size, and structure of
arrays. These include reshaping, transposing, concatenating, splitting, and adding or removing elements
from arrays.
• Linear algebra operations: NumPy offers a set of linear algebra functions for matrix operations, like matrix
multiplication, matrix inversion, eigenvalues, and eigenvectors.
CHECKPOINT
CONCEPTS IN PRACTICE
NumPy operations
4. What is the output of the following code?
import numpy as np
a. [[2 4]
[8 16]]
b. [[2 4]
[6 8]]
c. [[1 2]
[3 4]]
6. The function np.multiply(arr1, arr2) receives two ndarray objects arr1 and arr2 with the same
dimensions, and performs element-wise multiplication. What is the output of the following code?
import numpy as np
a. [[1 0]
[0 4]]
b. [[1 2]
[3 4]]
c. [[2 2]
[3 5]]
EXPLORING FURTHER
Please refer to the NumPy user guide for more information about the NumPy library.
15.3 Pandas
Learning objectives
By the end of this section you should be able to
Pandas library
Pandas is an open-source Python library used for data cleaning, processing, and analysis. Pandas provides
data structures and data analysis tools to analyze structured data efficiently. The name "Pandas" is derived
from the term "panel data," which refers to multidimensional structured datasets. Key features of Pandas
include:
• Data processing functionality: Pandas provides various functionalities for data processing, such as data
selection, filtering, slicing, sorting, merging, joining, and reshaping.
• Integration with other libraries: Pandas integrates well with other Python libraries, such as NumPy. The
integration capability allows for data exchange between different data analysis and visualization tools.
The conventional alias for importing Pandas is pd. In other words, Pandas is imported as import pandas as
pd. Examples of DataFrame and Series objects are shown below.
Table 15.1
import pandas as pd
# Create a dictionary of
columns The
Name Age City pd.DataFrame()
data = {
"Name": ["Emma", function takes in a
0 Emma 15 Dubai dictionary and
"Gireeja", "Sophia"],
DataFrame "Age": [15, 28, 22], converts it into a
1 Gireeja 28 London DataFrame.
from a "City": ["Dubai",
dictionary "London", "San Jose"] Dictionary keys will
San be column labels
} 2 Sophia 22
Jose and values are
# Create a DataFrame from stored in respective
the dictionary columns.
df = pd.DataFrame(data)
import pandas as pd
import numpy as np
import pandas as pd
CONCEPTS IN PRACTICE
Pandas basics
1. Which of the following is a Pandas data structure?
a. Series
b. dictionary
c. list
import pandas as pd
import numpy as np
Table 15.3
360 15 • Data Science
Provides a
summary of
the
<class 'pandas.core.frame.DataFrame'>
DataFrame,
RangeIndex: 6 entries, 0 to 5
including
Data columns (total 4 columns):
the column
# Column Non-Null Count Dtype
names, data
--- ------ -------------- -----
types, and
info() 0 Season 6 non-
the number df.info()
null object
of non-null
1 Month 6 non-
values. The
null object
function
2 Month-day 6 non-null int64
also returns
3 Year 6 non-null int64
the
dtypes: int64(2), object(2)
DataFrame's
memory usage: 320.0+ bytes
memory
usage.
Month-day Year
Counts the
occurrences
of unique
values in a
column
when a
Season
column is
value_counts() df.value_counts Fall 2
passed as
\('Season') Summer 2
an
Winter 2
argument
dtype: int64
and
presents
them in
descending
order.
Returns an
array of
unique
unique() values in a df['Season']
['Summer' 'Fall' 'Winter']
column \.unique()
when called
on a
column.
CONCEPTS IN PRACTICE
DataFrame operations
4. Which of the following returns the top five rows of a DataFrame?
a. df.head()
b. df.head(5)
c. both
6. Which function generates statistical information of columns with numerical data types?
a. describe()
b. info()
c. unique()
EXPLORING FURTHER
Please refer to the Pandas user guide for more information about the Pandas library.
an individual variable or attribute that is calculated from the raw data in the dataset.
Data indexing can be used to select and access specific rows and columns. Data indexing is essential in
examining a dataset. In Pandas, two types of indexing methods exist:
• Label-based indexing using loc[]: loc[] allows you to access data in a DataFrame using row/column
labels. Ex: df.loc[row_label, column_label] returns specific data at the intersection of row_label
and column_label.
• Integer-based indexing using iloc[]: iloc[] allows you to access data in a DataFrame using integer-based
indexes. Integer indexes can be passed to retrieve specific data. Ex: df.iloc[row_index,
column_index] returns specific data at the index row_index and column_index.
CHECKPOINT
Indexing a DataFrame
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
15-4-exploratory-data-analysis)
CONCEPTS IN PRACTICE
DataFrame indexing
Given the following code, respond to the questions below.
import pandas as pd
df = pd.DataFrame(data)
c. b
Data filtering involves selecting rows or columns based on certain conditions. Ex: In the expression
df[df['column_name'] > threshold], the DataFrame df is filtered using the selection operator ([]) and
the condition(df['column_name'] > threshold) that is passed. All entries in the DataFrame df where the
corresponding value in the DataFrame is True will be returned.
CHECKPOINT
CONCEPTS IN PRACTICE
import pandas as pd
df = pd.DataFrame(data)
7. Which of the following results in selecting the second and fourth rows of the DataFrame?
a. df[df.loc[:, "A"] % 2 == 0]
b. df[df[:, "A"] % 2 == 0]
c. df[df.loc["A"] % 2 == 0]
• isnull(): The isnull() function can be used to identify Null entries in a DataFrame. The return value of
the function is a Boolean DataFrame, with the same dimensions as the original DataFrame with True
values where missing values exist.
• dropna(): The dropna() function can be used to drop rows with Null values.
• fillna(): The fillna() function can be used to replace Null values with a provided substitute value.
Ex: df.fillna(df.mean()) replaces all Null values with the average value of the specific column.
To define a Null value in a DataFrame, you can use the np.nan value from the NumPy library. Functions that
aid in identifying and removing null entries are described in the table below the following code.
import pandas as pd
import numpy as np
df = pd.DataFrame(data)
366 15 • Data Science
0 A NaN True
1 B 200.0 True
2 C 500.0 False
3 D 0.0 NaN
4 E -10.0 NaN
Table 15.5
0 A 172.5 True
Null values in
df["Column 2"] =\ 1 B 200.0 True Column 2 are
df["Column replaced with the
fillna()
2"].fillna(df["Column 2 C 500.0 False mean of non-Null
2"]
values in the column.
.mean()) 3 D 0.0 NaN
4 E -10.0 NaN
CONCEPTS IN PRACTICE
9. Assuming that a DataFrame df is given, which of the following replaces Null values with zeros?
a. df.fillna(0)
b. df.replacena(0)
c. df.fill(0)
10. Assuming that a DataFrame df is given, what does the expression df.isnull().sum() do?
a. Calculates sum of the non-Null values in each column
b. Calculates the number of Null values in the DataFrame
c. Calculates the number of Null values in each column
Why visualization?
Data visualization has a crucial role in data science for understanding the data. Data visualization can be used
in all steps of the data science life cycle to facilitate data exploration, identify anomalies, understand
relationships and trends, and produce reports. Several visualization types are commonly used:
Visualization Benefits/common
Description
type usage
Visualization Benefits/common
Description
type usage
Summarizing the
Rectangular box with whiskers that summarize the
distribution of the data
Box plot distribution of a continuous variable, including the median,
and comparing different
quartiles, and outliers
variables.
CHECKPOINT
Visualization types
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
15-5-data-visualization)
CONCEPTS IN PRACTICE
2. Which of the following plot types is best suited for visualizing outliers and quartiles of a continuous
variable?
a. histogram plot
b. bar plot
c. box plot
3. Which of the following plot types is effective for displaying trends and changes over time?
a. line plot
b. bar plot
c. histogram plot
plot types. Some of the most commonly used frameworks are Matplotlib, Plotly, and Seaborn. Here, some
useful functionalities of Matplotlib are summarized.
Example Output
import
matplotlib.pyplot as
plt
# Data
categories = ["Course
A", "Course B", "Course
C"]
values = [25, 40, 30]
Example Output
import matplotlib.pyplot
as plt
# Data
month = ["Jan", "Feb",
"Mar", "Apr", "May"]
inflation = [6.41, 6.04,
4.99, 4.93, 4.05]
Example Output
import matplotlib.pyplot
as plt
# Data
x = [1, 2, 3, 4, 5, 6, 7,
8, 9, 10]
y = [10, 8, 6, 4, 2, 5, 7,
9, 3, 1]
Example Output
import matplotlib.pyplot
as plt
import numpy as np
Example Output
import
matplotlib.pyplot
as plt
import numpy as np
# Customize the
chart
plt.title("Box Plot
of random values")
plt.xlabel("Data
Distribution")
plt.ylabel("Values")
CONCEPTS IN PRACTICE
Matplotlib methods
4. Given the following code, which of the function calls is appropriate in showing association between x
and y?
# Data
x = [1, 2, 3, 4, 5]
y = [10, 15, 12, 18, 20]
a. plt.boxplot(x)
b. plt.hist(y)
c. plt.scatter(x, y)
# Data
categories = ['A', 'B', 'C', 'D']
values = [10, 15, 12, 18]
plt.bar(categories, values)
a. a histogram plot
b. a bar plot
c. a line plot
6. Which library in Python is more commonly used for creating interactive visualizations?
a. Matplotlib
b. Plotly
c. Pandas
EXPLORING FURTHER
Please refer to the following user guide for more information about the Matplotlib, Plotly, and Seaborn
libraries.
15.6 Summary
Highlights from this chapter include:
• Data science is a multidisciplinary field that combines collection, processing, and analysis of large volumes
of data to extract insights and drive informed decision-making.
• The data science life cycle is the framework followed by data scientists to complete a data science project.
• The data science life cycle includes 1) data acquisition, 2) data exploration, 3) data analysis, and 4)
reporting.
376 15 • Data Science
• Google Colaboratory is a cloud-based Jupyter Notebook environment that allows programmers to write,
run, and share Python code online.
• NumPy (Numerical Python) is a Python library that provides support for efficient numerical operations on
large, multi-dimensional arrays and serves as a fundamental building block for data analysis in Python.
• NumPy implements an ndarray object that allows the creation of multi-dimensional arrays of
homogeneous data types and efficient data processing.
• NumPy provides functionalities for mathematical operations, array manipulation, and linear algebra
operations.
• Pandas is an open-source Python library used for data cleaning, processing, and analysis.
• Pandas provides Series and DataFrame data structures, data processing functionality, and integration with
other libraries.
• Exploratory Data Analysis (EDA) is the task of analyzing data to gain insights, identify patterns, and
understand the underlying structure of the data.
• A feature is an individual variable or attribute that is calculated from raw data in a dataset.
• Data indexing can be used to select and access specific rows and columns.
• Data slicing refers to selecting a subset of rows and/or columns from a DataFrame.
• Data filtering involves selecting rows or columns based on certain conditions.
• Missing values in a dataset can occur when data are not available or were not recorded properly.
• Data visualization has a crucial role in data science for understanding the data.
• Different types of visualizations include bar plot, line plot, scatter plot, histogram plot, and box plot.
• Several Python data visualization libraries exist that offer a range of capabilities and features to create
different plot types. These libraries include Matplotlib, Seaborn, and Plotly.
• The conventional aliases for importing NumPy, Pandas, and Matplotlib.pyplot are np, pd, and plt,
respectively.
At this point, you should be able to write programs to create data structures to store different datasets and
explore and visualize datasets.
Function Description
np.genfromtxt('data.csv',
delimiter=',') Creates an array from a CSV file.
Function Description
df[condition] Selects only the rows that meet the given condition.
df.loc[start_row:end_row,
start_column:end_column] Slices using label ranges.
df.loc[[label1, label2,
Slices rows that are in the list [label1, label2, ...].
...], :]
Takes in two inputs, x and height, and plots bars for each x value
plt.bar(x, height)
with the height given in the height variable.
Takes in two inputs, x and y, and plots lines connecting pairs of (x,
plt.plot(x, y)
y) values.
Function Description
Answer Key
Chapter 1
1.1 Background
1. b. Each image in the animation represents a different type of computer program.
2. a. This type of analysis requires basic knowledge of programming.
3. c. Like mobile apps, wi-fi speakers run small programs that connect to servers.
4. b. This book assumes readers understand the basics of algebra, but nothing too difficult.
5. c. Python's syntax is more concise than Java's.
6. a. Python does not require a semicolon at the end of each line. Braces are also not required.
1.2 Input/output
1. c. The hello world program is just one line of code.
2. c. The end="" option removes the newline character after hello, producing Hello world! on the same
line.
3. c. The default space character that separates multiple items has been replaced with the dash character.
4. c. The user input is stored in memory and can be referenced using today_is.
5. b. The print() function outputs "You entered:" followed by a space character, followed by the input
data, Sophia, which is referenced by the variable name.
6. a. The input is stored exactly as the user enters the input. So, the retrieved value is displayed exactly as
entered by the user.
1.3 Variables
1. c. This would produce the output string, "The city where you live is Chicago" .
2. b. These two lines of code would produce the output string, "Total = 6" .
3. c. The single = character is the assignment operator.
4. a. An assignment statement has the variable name on the left side and the value on the right side of the
assignment statement.
5. a. median is a descriptive name for a variable that stores a median value.
6. b. A variable name can contain, but not start with, a digit.
7. c. The name clearly indicates the value being stored is a zip code.
8. a. Snake case separates each word with an underscore and uses only lowercase letters.
9. c. "A" and "wake" are valid strings, and the + is not enclosed in quotes, indicating a concatenation
operation. Thus the string "Awake" is produced. Note that the concatenation operator does not include
any default separator characters like the space character.
10. c. Note that space characters must be explicitly included when using the concatenation operator.
11. c. A concatenation of the two strings is assigned to the variable holiday. Note that "one-sided" would
assign the same string to holiday.
1.7 Comments
1. b. Comments help explain the purpose of code so that other programmers understand what the code
intends.
2. a. The hash character indicates the beginning of a comment.
3. b. Having one space after the # is the community standard.
4. c. This comment explains the intent of the code. If the code had a mistake, the programmer reading the
comment might notice the mistake more easily.
5. a. A blank line separates the input from the output.
Then again, inserting a blank line in a three-line program is generally unnecessary.
6. c. The # symbol "comments out" the line without removing the line permanently.
7. a. The docstring is primarily written for others who will use the program.
8. c. Docstrings must be a valid string, not a multi-line comment. Docstrings are stored in the program's
memory, but comments are ignored.
9. b. The amount of information in this docstring is just right. In most cases, the docstring should not be
longer than the code.
Chapter 2
2.1 The Python shell
1. a. Most programming languages are high-level and meant to be understood by humans.
2. c. The most basic line of Python code is a single expression, such as the integer 1 .
3. b. After the input is stored in memory, the shell displays a prompt for the next line.
4. c. The up arrow displays the previous line, and the left arrow moves within that line.
5. a. Pressing the up arrow twice moves to the line before the previous line.
Chapter 3
3.1 Strings revisited
1. a. Since index values start at 0, the index 1 refers to the second character of the string.
2. b. The character at index 1 is y, and the character at index -1 is !.
3. c. The result of s[0] is "P" , which is a string of length 1.
4. b. Code points 65 to 90 are used for uppercase letters.
5. b. The code point for the character "0" is 48.
6. b. Tilde (~) is the character for code point 126. The chr() function returns a string of length one.
7. c. An escape sequence is two characters long and begins with a backslash (\).
8. c. The first backslash indicates an escape sequence, and the second backslash indicates the character.
9. c. The escape sequence \n produces a new line. print("Enter\nhere") produces the same result.
Chapter 4
4.1 Boolean values
1. c. "True" uses quotation marks, so the value is a string.
2. b. is_vegetable is an integer and can be used in arithmetic operations.
3. a. A variable assigned with either True or False is a Boolean variable.
4. a. A Boolean variable represents the value True or False .
5. b. True and False are the only two values for a Boolean variable.
6. b. Assigning a bool with an int results in implicit type conversion. The type conversion can cause issues
later in the code if the programmer assumes is_dessert is still a Boolean.
7. b. Any numeric type representing 0 converts to False . Non-zero numbers convert to True .
8. a. A negative number is non-zero and converts to True .
9. b. The empty string is the only string that converts to False .
10. a. "0" is a non-empty string and is converted to True .
11. a. The input is read in as a string, so bool("False") is True .
12. b. True converted to a float is 1.0. False converted to a float is 0.0.
13. b. True converted to a string is "True" . False converted to a string is "False" .
14. b. When a Boolean is converted to a numeric type, True is converted to 1 , and False is converted to 0
.
15. b. 14 is greater than 13, so 14 is not less than or equal to 13.
16. a. 0 is less than 0.4, so 0 is not equal to 0.4.
17. b. 4 is equal to 4.0, so 4 is not less than 4.0.
18. c. Numeric types cannot be compared to strings using the comparison operators: >, <, >=, <=. Using == or
!= will produce a Boolean.
19. b. Strings are compared by comparing each character's Unicode values. Letters have ascending Unicode
values in alphabetical order (and are case sensitive). "c" and "c" are equal, so "i" is compared to "o"
. "i" is not equal to "o" so False is produced, though cilantro and coriander come from the same
plant.
20. b. Letters have ascending Unicode values in alphabetical order (and are case sensitive). "d" is greater
than, not less than, "c" , so False is produced.
4. b. (8 < 10) and (21 > 20) evaluates to True and True, which is True . So the body of the if is
executed, and z is assigned with 5 .
5. a. (21 < 30) or False evaluates to True or False, which is True .
6. c. The discount does not apply for ages between 12 and 65 inclusive.
7. a. (9%2==0 and 10%2 ==1) evaluates to (False and False), which is False . (9%2 == 1 and 10%2
== 0) evaluates to (True and True), which is True . False or True is True . The if statement
checks whether the numbers form an even-odd or odd-even pair.
8. a. not(13 < 10) evaluates to not(False), which is True .
9. b. not(18 > 15 and 18 < 20) evaluates to not(True and True), then not(True), and finally, False
.
10. a. 65 > 60 is True , so is_turn is assigned with not(False), which is True .
Chapter 5
5.1 While loop
1. b. The loop runs six times for values of g = 1 , 2 , 3 , 5 , 8 , and 13 .
2. c. 21 is the first Fibonacci number larger than 20, and for g = 21, the condition will evaluate to False for
the first time.
3. a. The code first prints the variable f's initial value. Then, at each iteration, g's value is printed, and g's
value throughout the code's execution will be 1 , 2 , 3 , 5 , 8 , and 13 . Thus, all printed values are 1 , 1
, 2 , 3 , 5 , 8 , and 13 .
4. b. The loop runs for n = 4, 3 , 2 , 1 .
5. b. The loop's termination condition is n = 0; therefore, the value of n after the loop equals 0 . The last
printed line will be: value of n after the loop is 0.
6. c. Since the initialization value for n equals 4 and the value of n is only increasing, the loop condition will
always remain true. The loop will never terminate.
H
e
l
l
o
2. b. As soon as the print statement is executed, the break statement causes the loop's termination. Also,
for i = 15, the condition (i%3 == 0 and i%5 == 0) is met, and hence the print statement will be
executed exactly once.
3. b. The loop finds the first five numbers that are divisible by 2 or 3 . These numbers are 2 , 3 , 4 , 6 ,
and 8 , and thus 8 is the final i's value.
4. b. The print statement is executed for numbers within the range of 0 and 10 (inclusive) that are divisible by
3. The printed output is:
9
6
3
0
5. b. The loop checks whether the character being evaluated is a space character or not. If so, the remainder
of the loop will be skipped and the space character will not be printed.
execution terminates.
Chapter 6
6.1 Defining functions
1. c. The print() function is called to output the variable, offset_num.
2. b. Lines 1, 4, and 5 each call print() once. Lines 2 and 3 call a different function, input().
3. b. Line 1 is a comment that is not executed. Lines 2 and 3 each call float() and input(). Line 3 calls
print() for a total of five function calls.
4. b. The corrected line is def water_plant(): .
5. c. The main program outputs "User info:" and then calls print_phone_num(), which outputs the
phone number.
6. b. A function call includes the function name and parentheses.
7. a. calc_tax is concise, in snake case, and indicates the function's task.
8. c. Each calculation uses four statements to read in the x,y values of a pair of points. The original program
performs three calculations by repeating code for a total of 12 input statements. The revised program
performs three calculations by executing the four input statements multiple times with function calls.
9. c. The benefits of a function's reusability is highlighted by many function calls that drastically reduce the
amount of cluttered, redundant code. Calling calc_distance() 10 times is equivalent to 60 statements
in the original program.
9. b. The global keyword can be used to create a global variable from within a function. new_hour has
global scope and can be accessed outside update_hour().
6.4 Parameters
1. b. username is the argument passed to print_welcome(). name is print_welcome()'s parameter that
is assigned with username's value.
2. a. name is the only parameter defined with print_welcome(). username is the argument passed on line
5's function call.
3. a. name is a parameter that exists only within a call to print_welcome().
4. a. The program would run correctly. Arguments and parameters can have the same name, and a local
variable called name would still be created in print_welcome(). Scope can be confusing with variables of
the same name.
5. a. p1_x is the first argument used in line 9's function call. x1 is defined as the first parameter of
calc_distance().
6. b. y1 is defined as calc_distance()'s second parameter. p1_y is the second argument on line 9's
function call.
7. c. 3 is a literal passed as the third argument, which corresponds to x2, the third parameter.
8. a. A list is mutable, so the object referred to by wknd_temps and temps is modified without needing a
local copy.
9. b. A string is immutable, so convert_temps() makes a new object for unit to refer to.
10. a. An immutable object's value cannot be changed.
11. b. unit no longer exists after convert_temps() finishes and returns to line 14.
Chapter 7
7.1 Module basics
1. b. The code has ten def statements, each of which defines a function.
2. a. Many modules consist of function definitions only. The functions are intended to be called in other
modules.
3. c. area.cube(5) is 6 * 5**2, which is 6 * 25, which is 150 .
4. a. When importing a module from another file, the suffix .py is removed.
5. b. The circle function must be called using the area variable that refers to the module.
6. b. Adding the function to the area module makes the function easier to reuse in other programs.
Chapter 8
8.1 String operations
1. a. Since a's ASCII value is less than b's ASCII value, the output for "aaa" < "aab" is True.
2. b. Since "a" is shorter than "aa" , and the prefix "aa" with length 1 equals the second operand ( "a"
), the shorter string is considered smaller. As such, "aa" < "a" evaluates to False.
3. c. While all characters in "aples" exist in "apples" , the characters are not sequential, so the string
"aples" is not a substring of "apples" . Therefore, "aples" in "apples" returns False.
4. a. "aBbA".lower() converts all characters to lowercase, and the output is "abba" .
5. b. "aBbA".upper() converts all characters to uppercase, and the output is "ABBA" .
6. a. The upper() converts "a" to uppercase ( "A" ). When checking "a".upper() == "A", the output is
True.
Chapter 9
9.1 Modifying and iterating lists
1. b. The append() operation adds an element to the end of a list.
2. c. The element to be removed is passed as a parameter to the remove operation. The remove is performed
on the specified list.
3. c. Both pop() and remove() can be used to remove the last element of a list. pop() automatically
removes the last element, whereas remove() requires the last element be specified.
4. a. The length of the list is 5, so the iterative for loop executes five times.
5. b. len(my_list) is 5. A counting for loop ends at 1 less than the second value in the range function, so
the final value of i is 4 .
6. a. The for loop steps by two each time, so index 0, 2, and 4 are printed. Due to the "end=" in the print
statement, all elements are printed on one line.
6. a. The for loop ranges from 0 through 20, stepping by two, but each iterated element is even, so nothing
is added to new_list.
Chapter 10
10.1 Dictionary basics
1. a. The first item in each key-value pair is the key. Hence "Sunday", "Monday", "Tuesday" are key
items.
2. b. The second item in each key-value pair is the value. Hence 1, 2, 3 are value items.
3. b. Based on the days dictionary object, the value assigned to "Sunday" is 1 .
4. b. The type of a dictionary object is dict .
5. b. A dictionary cannot have duplicate keys, and "a" and "A" are two different keys because "a" and "A"
are different characters.
6. a. A dictionary can have duplicate values, but not duplicate keys.
Chapter 11
11.1 Object-oriented programming basics
1. b. Ellis's username represents the post creator.
2. b. An object models a real-world entity with fields and procedures.
3. a. A procedure, such as Change venue, can make changes to the field and protect the field from
inappropriate access.
4. b. Limiting data access prevents accidental changes and makes relationships between objects clearer.
5. b. Correct abstraction makes only necessary information visible. An overheated engine can cause
irreparable damage, so an indicator is necessary for the driver.
6. a. Showing only the necessary information reduces clutter and improves the overall experience.
are incorrect.
4. c. order_1 is originally a coffee without milk or sugar, but the call to change() adds sugar to the order by
assigning order_1.with_sugar with True .
5. c. An instance method's first parameter indicates the method can change an instance. change() allows a
change in whether a coffee order has milk or sugar.
6. b. Unindenting print_order() would produce an error. An instance method must be defined in the class.
Chapter 12
12.1 Recursion basics
1. a. The move of the smaller ring, which is a smaller problem, is done twice on the way to solving for the two
rings overall. The task of moving all three rings is broken down into smaller tasks of moving one ring at a
time.
2. b. The two-ring solution from question 1 is used in the solution for three rings.
3. b. Once to move two rings to the middle tower following the rules, then to move two rings to the target
tower.
4. c. The small ring is moved to the middle tower. The bigger ring is moved to the target tower. Then, the
small ring is moved from the middle to the target.
5. c. To solve, the two-ring solution is used twice, and there is one extra step to move the biggest ring from
the source to the target. Each solution of two rings requires three steps each. So 3 + 1 + 3 = 7 steps total.
6. a. The four-ring problem uses the three-ring solution twice. The three smaller rings are moved to the
middle tower, taking seven steps as in question 2. One step is needed to move the biggest ring from the
source to the target. It takes seven more steps move three smaller rings from the middle to the target. The
total is 7 + 1 + 7 = 15 steps.
Chapter 13
13.1 Inheritance basics
1. a. A doughnut is a type of pastry, so Doughnut inherits from Pastry.
2. b. A kitchen has a freezer, so a Kitchen class would contain a Freezer instance. A has-a relationship is
also called a composition.
3. b. The Goalkeeper class inherits from the Player class.
4. b. The subclass name is followed by the superclass name in parentheses.
5. a. Deriving CarryOn from Luggage is specified in CarryOn's class definition. Ex: class
CarryOn(Luggage): . Instances of a subclass, like CarryOn, are created like other classes and do not
require the base class name.
6. c. A SubClass instance has access to func_1(), inherited from SuperClass, as well as to func_2(), a
SubClass method.
7. a. A SuperClass instance has access to SuperClass's func_1() but doesn't have access to anything in
SubClass, including func_2().
13.3 Methods
1. b. Overriding involves defining a subclass method that has the same name as the superclass method.
2. b. The self parameter is missing from the overridden method's definition.
3. c. The class definition doesn't indicate inheritance. The definition should be class ContractTax(Tax):.
class Plant:
def photosynth(self):
print("Photosynthesizing")
class CarnivMixin:
def eat(self):
print("Eating")
class Rose(Plant):
pass
Chapter 14
14.1 Reading from files
1. c. The file is opened and is associated with the fileobj object. Reading from the file will be possible using
fileobj.
2. a. The file must exist in the same folder as the Python file in order for the file to be opened.
400
3. b. Python interprets this command as the programmer trying to print the object details. The object details
are printed. Ex: <_io.TextIOWrapper name='newfile.txt' mode='r' encoding='cp1252'>.
4. c. The read() function is called on fileobj. The file's contents will be read into file_str.
5. b. The readlines() function is called on fileobj. file_lines is the string list ['Hello world!\n',
'How are you?\n'].
6. a. The readline() function reads the next line of a file. Since readline() is called right after opening
the file the first line is read. The resulting string "Hello world!" is printed. Note that readline()
includes the '\n' character so the print will include an additional newline.
Chapter 15
15.1 Introduction to data science
1. c. Data acquisition is the first stage of the data science life cycle. Data can be collected by the data scientist
or gathered previously and provided to the data scientist.
2. b. The data science life cycle has four stages: 1) data acquisition, 2) data exploration, 3) data analysis, and
4) reporting.
3. c. The data exploration stage includes data cleaning and visualization.
4. a. Python is the most popular programming language in data science.
5. b. NumPy is a Python library used in numerical data analysis.
6. a. Google Colaboratory is a Google document that can be shared with other Google accounts for viewing
or editing the content.
15.2 NumPy
1. c. To create an m by n matrix of all zeros, np.zeros((m,n)) can be used.
2. a. ndarray is a data type supported by NumPy. When printing the type of an ndarray object,
<'numpy.ndarray'> is printed.
3. d. NumPy array is optimized for computational and memory efficiency while also offering array-oriented
computation.
4. b. 2 * arr multiplies all elements by 2 resulting in:
[[2 4]
[6 8]]
5. c. np.array([[1, 2], [1, 2], [1, 2]]) creates an ndarray of 3 by 2. The T operator transposes the
array. The transpose operator takes an array of m by n and converts the array to an n by m array. The result
of applying the transpose operator on the given array is a 2 by 3 array.
6. a. The result of element-wise multiplication between arr1 and arr2 is:
[[1 0]
[0 4]]
15.3 Pandas
1. a. Series is a Pandas data structure representing one-dimensional labeled data.
2. a. A DataFrame object can be considered a collection of one-dimensional labeled objects represented by
Series objects.
3. b. The Pandas DataFrame can store columns of varying data types, while NumPy only supports numeric
data types.
4. c. The function head() returns a DataFrame's top rows. If an argument is not specified, the default
number of returned rows is five.
5. c. The unique() function when applied to a column returns the unique values (rows) in the given column.
6. a. When the function describe() is applied to a DataFrame, summary statistics of numerical columns will
be generated.
5. c. loc[1] returns the row with label 1 , which corresponds to the second row of the DataFrame.
6. b. iloc[:, 0] selects all rows corresponding to the column index 0, which equals returning the first
column.
7. a. The condition returns all rows where the value in the column with label A is divisible by 2.
8. b. isnull() returns a Boolean DataFrame representing whether data entries are Null or not.
9. a. fillna() replaces all Null values with the provided value passed as an argument.
10. c. The function sum() is applied to each column separately and sums up values in columns. The result is
the number of Null values in each column.
Index
Symbols Data slicing 364 implicit type conversion 43
!= 94 data type 20 import statement 55
< 94 DataFrame 355 in 205
<= 94 def 147 index 88
= 14 delimiter 217 index() 208
== 94 diamond problem 321 infinite loop 134
> 94 dict 242 inner loop 129
>= 94 dictionary 241, 241 Input 12
Dictionary comprehension 258 input() 12
A docstring 29 instance attribute 270
Abstraction 267 instance method 273
Aliasing 282 E instances 267
and 100 elif 108 int() 43
append 223 else statement 98 interpreter 40
argument 158 Encapsulation 266
Arithmetic operators 21 escape sequence 74 J
ASCII 72 explicit type conversion 43 join() 218
assignment operator 14 Exploratory Data Analysis (EDA) Jupyter Notebook 350
362 JupyterLab 350
B expression 39
base case 290 K
bool 92 F Kaggle Kernels 351
bool() 93 f-string 76 key-value 241
branch 91 feature 362 keyword arguments 168
break 134 field width 214 keywords 15
built-in function 55 find() 207
float() 43 L
C Floor division 51 len() 17
class 267 for loop 125 library 9
class attribute 270 format specifier 77 list comprehension 235
code point 72 format() 210 list-of-lists 232
Comments 27 formatted string literal 76 loc[] 363
Comparison operators 94 Formatting string 209 local scope 154
computer 8 function 146 logical operator 100
condition 97 loop 121
conditional expression 116 G loop else 137
container 71, 125 global scope 153 lower() 199
continue 135 Google Colaboratory 351
Control flow 149 Google Sheets 351 M
count() 206 Magic methods 276
H memory diagram 80
D Hierarchical inheritance 316 Microsoft Excel 351
data analysis 350 Mixin classes 321
Data filtering 364 I mode 331
Data indexing 363 identity 81 module 55, 173
Data science 349 if statement 97 modulo operator 51
Data Science 349 iloc[] 363 modulus 52
data science life cycle 350 immutable 86, 161, 203 Multilevel inheritance 316
404 Index