Introduction To Python Programming 1721886042
Introduction To Python Programming 1721886042
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 support@openstax.org. 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