Python
Python
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.
Statements
1
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
Online music streaming
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
1-1-background)
CONCEPTS IN PRACTICE
Computers and programs
1. How many types of programs were described in the animation?
a. 3
b. 4
c. 5
2. What type of program will this book explain how to write?
a. a tool that summarizes an individual's music preferences
b. a mobile app for managing and sharing playlists of songs
c. a website that shows the top artists for the past five years
3. Which of the following devices is an example of a computer?
a. wired headphones that plug into a smartphone
b. remote control that pauses or skips the current song
c. wi-fi speaker that streams music from Amazon
4. Reading this book requires a strong background in mathematics.
a. true
b. false
8 1 • Statements
Access for free at openstax.org
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.
The Python language
This book introduces Python (https://openstax.org/r/100python), one of the top programming
languages
today. Leading tech giants like Google, Apple, NASA, Instagram, Pixar, and others use Python
extensively.
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
Hello world in Python and Java
By tradition, Hello World (https://openstax.org/r/100helloworld) is the first program to write when
learning
a new language. This program simply displays the message "Hello, World!" to the user. The
hello world
program is only one line in Python:
print("Hello, World!")
In contrast, the hello world program is five lines in Java (a different language).
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
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
Counting lines in a file
Access multimedia content (https://openstax.org/books/introduction-python-programming/pages/
1-1-background)
1.1 • Background 9
CONCEPTS IN PRACTICE
Python vs Java syntax
5. In general, Python programs are _____ than Java programs.
a. faster
b. longer
c. shorter
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.
Access multimedia content (https://openstax.