ch01
ch01
murach’s
Python
programming
2nd Edition
(Chapter 1)
Thanks for downloading this chapter from Murach’s Python Programming (2nd
Edition). We hope it will show you how easy it is to learn from any Murach book, with
its paired-pages presentation, its “how-to” headings, its practical coding examples, and
its clear, concise style.
To view the full table of contents for this book, you can go to our website. From there,
you can read more about this book, you can find out about any additional downloads that
are available, and you can review our other books on related topics.
Thanks for your interest in our books!
“This is not only a book to learn Python but also a guide to refer to whenever
I face a Python problem. What I really like is that it is very well-organized
and easy to follow, and it does not make you bored by providing unnecessary
details.”
Posted at an online bookseller
“The material in the book itself is worth 6 stars [out of 5]. It’s second to none
in its quality.”
Posted at an online bookseller
“This is now my third text book for Python, and it is the ONLY one that
has made me feel comfortable solving problems and reading code. The
paired-pages approach is fantastic, and it makes learning the syntax, rules,
and conventions understandable for me.”
Posted at an online bookseller
“[Look at the contents] and see where they put testing/debug: Chapter 5. I
love the placement! Folks going through the book get Python installed, receive
positive returns with some basic code, and then take a breather with testing/
debugging before more complex things come their way.”
Jeremy Johnson, DreamInCode.net
“Murach’s Rocks: Murach’s usual excellent coverage with examples that are
actually practical.”
Posted at an online bookseller
Section 1
Essential concepts
and skills
The eight chapters in this section get you off to a fast start by presenting
a complete subset of the essential concepts and skills that you need for
Python programming. First, chapter 1 introduces you to Python and shows
you how to use an integrated development environment (IDE) called IDLE
to develop and run programs. Then, chapters 2, 3, and 4 present skills that
let you develop substantial programs of your own.
At that point, you’re going to need to improve your testing and
debugging skills, so that’s what chapter 5 shows you how to do. Then,
chapters 6, 7, and 8 continue your development. When you complete this
section, you’ll be able to design, code, test, and debug Python programs
that can work with data that’s stored in files.
1
An introduction to Python
programming
This chapter starts by showing why Python is considered by many to be the
best language for teaching beginners how to program. Next, this chapter
presents the concepts and terms that you need to know before you start
programming. Then, it shows how to use an integrated development
environment (IDE) called IDLE to develop and test Python programs.
Introduction to Python............................................................4
Why Python works so well as your first programming language....................4
Three types of Python applications..................................................................6
The source code for a console application.......................................................8
How Python compiles and runs source code.................................................10
How disk storage and main memory work together.......................................12
How to use IDLE to develop programs...............................14
How to use the interactive shell......................................................................14
How to work with source files........................................................................16
How to compile and run a program................................................................18
How to fix syntax and runtime errors.............................................................20
Perspective............................................................................22
4 Section 1 Essential concepts and skills
Introduction to Python
The Python programming language was developed in the 1990s by Guido
van Rossum of the Netherlands. It was intended to be a simple, intuitive
language that is as powerful as traditional languages, and it succeeded at that.
Guido named the language Python because he was a big fan of “Monty Python’s
Flying Circus,” and he continues to be involved in the development of Python. In
fact, some developers in the Python community refer to him as the “Benevolent
Dictator for Life (BDFL)”, although he officially stepped down from that role in
2018.
Figure 1-1 Why Python works so well as your first programming language
6 Section 1 Essential concepts and skills
A console application
A GUI application
A web application
Description
•• A console application is a desktop application that uses the console to interact with
the user.
•• A GUI application is an application that uses a graphical user interface (GUI) to
interact with the user.
•• A web application typically gets requests from a web browser, processes them on a
web server, and returns the responses to the web browser.
import locale
choice = "y"
while choice.lower() == "y":
print("Bye!")
Discussion
•• This application gets three values from the user: monthly investment amount,
yearly interest rate, and number of years. Then, it calculates and displays the future
value of the investments at the specified interest rate for the specified number of
years.
•• The first line in this program is called the shebang line, or shebang. It specifies
the interpreter that should be used to run this program. In this book, all programs
should be run by Python 3.
Procedure
Step 1 The programmer uses a text editor or IDE to enter and edit the source
code. Then, the programmer saves the source code to a file with a .py
extension.
Step 2 The source code is compiled by the Python interpreter into bytecode.
Step 3 The bytecode is translated by the Python virtual machine into instructions
that can interact with the operating system of the computer.
Description
•• Although this procedure is complicated, it runs behind the scenes when you’re
developing Python programs. As a result, it’s mostly invisible to the developer.
•• Python bytecode can be run on any operating system that has a Python virtual
machine. That’s why Python is said to be platform-independent.
•• Most programs import one or more modules, which are files that contain reusable
code. The Python interpreter saves the compiled bytecode for those modules in files
that have .pyc or .pyo extensions. This is an import optimization that can improve
the startup time for a program.
Description
•• The systems software for a computer provides all of the software that is needed for
running the applications, including the operating system.
•• The applications software consists of the applications that are available on the
system.
•• A computer consists of a CPU (Central Processing Unit) and main memory, also
known as RAM (Random Access Memory).
•• The data in main memory is lost when an application ends. Disk storage is used for
persistent data storage, which remains after an application ends.
•• Main memory and disk storage are commonly measured in megabytes (MB) and
gigabytes (GB), and a byte is roughly equivalent to one character of data.
Figure 1-5 How disk storage and main memory work together
14 Section 1 Essential concepts and skills
Description
•• The interactive shell makes it easy to experiment with Python code and view the
results right away. This is another feature that makes Python a good first language
for beginning programmers.
How to switch between a source file window and its shell window
•• Use the Window menu. Or, arrange the windows on your screen so both are visible,
and then click on the window you want to use.
Description
•• Like other programs that you’ve used, IDLE provides menus for working with files,
editing and formatting selected text, and switching from one window to another.
Description
•• When you run a program, Python attempts to compile it, as shown in figure 1-4. If
it doesn’t detect any errors, it executes the code for the program.
•• However, if Python detects any errors, it usually alerts you as shown in the next
figure, and it doesn’t execute the code.
•• When you use IDLE to run a console application, it uses the shell as the console.
•• On most platforms, IDLE’s shell automatically becomes active when you run a
console application. If it doesn’t, you have to switch to the shell after you run the
application.
Description
•• After you write the code for a Python program, you test the program to make sure it
works. As part of that process, you will usually find errors (bugs) so you will have
to debug the program.
•• A syntax error violates one of the rules of Python coding so the source code can’t
be compiled. By contrast, a runtime error occurs when a Python statement can’t be
executed as the program is running.
•• When you try to run a program, IDLE detects one syntax error at a time. For each
error, a dialog box is displayed and the point at which the error was detected is
highlighted. Then, you close the dialog box, fix the error, and try again.
•• When all of the errors have been fixed, the program will run. But here again, only
one runtime error is detected at a time. So you have to fix the error, run the program
again, and keep going until all of runtime errors have been debugged.
•• A runtime error is also known as an exception, and how to handle exceptions is one
of the critical skills that you’ll learn in chapter 8 of this book.
Perspective
Now that you’ve finished this chapter, you should understand the concepts
and terms that have been presented. You should also be familiar with the way
IDLE works. At this point, you’re ready to start learning how to program. And
that’s what you’re going to do in the next chapter.
Terms
Python CPU (Central Processing Unit)
open source main memory
backward compatible RAM (Random Access Memory)
forward compatible disk storage
application persistent data storage
app systems software
program operating system
console application application software
console byte
command prompt bit
GUI application megabyte (MB)
web application gigabyte (GB)
desktop application integrated development
scripting language environment (IDE)
shebang line IDLE
source code interactive shell
compile testing
interpreter debugging
bytecode syntax error
virtual machine runtime error
platform-independent exception
module exception handling
Chapter 1 An introduction to Python programming 23
Summary
•• Python is a powerful programming language with a simple syntax that’s
easy to read and understand. That’s why it’s such a good first language for
beginners.
•• Python can be used to develop desktop applications such as console
applications and GUI applications. Python can also be used to develop web
applications. And Python can be used as a scripting language for purposes
such as system administration.
•• Before the source code for a Python program can be run, it is compiled by
the Python interpreter into bytecode that can be run by the Python virtual
machine for a computer. The bytecode and virtual machine are what makes
Python platform-independent.
•• When a program is being run, the operating system, the program, and the
program’s data are all stored in the main memory of the computer.
•• Disk storage provides persistent data storage for the data that’s operated
upon by a program. Disk storage also stores all of the systems software and
applications software of the system.
•• An integrated development environment (IDE) like IDLE makes it easier to
develop Python programs.
•• You can use IDLE’s interactive shell to enter and test Python code such as
expressions, functions, and statements. You use its editor to enter and edit
Python source code.
•• To run a program with IDLE, you can press the F5 key. If the program uses
the console, the IDLE shell is used as the console.
•• The goal of testing is to find all of the errors in a program. The goal of
debugging is to fix all the errors.
•• If a program has syntax errors, you need to correct them before the program
can be compiled. If a program has runtime errors, you need to debug each
one until the program runs correctly.
•• A runtime error is also known as an exception. To make sure that a program
doesn’t crash, you must learn how to handle the exceptions that occur.
24 Section 1 Essential concepts and skills
>>> 30 + 12
42
>>> 30-12
18
>>> 3 * 4
12
>>> 12/3
4.0
>>> 1 / 3
0.3333333333333333
>>> 3 * 4 + 30
42
>>> 3 * (4 + 30)
102
Note that (1) the print() function prints a blank line if you don’t code anything
between the parentheses, (2) the * is used for multiplication in an arithmetic
expression, (3) the spaces before and after arithmetic operators aren’t required,
and (4) parentheses are used just as they are in algebraic expressions. If you
make entry errors, the shell should display appropriate error messages.
3. Try entering these statements to see how they work:
var1 = 30
var2 = 25
var1 + 10
var1
var1 + var2
var_2
The last one is deliberately incorrect, so it should display an error message.
4. Continue experimenting if you like. However, this should make more sense
after you read the next chapter and learn how to start writing Python code.
Chapter 1 An introduction to Python programming 25
100% Guarantee
When you buy directly from us, you must be
satisfied. Try our books for 30 days or our eBooks
for 14 days. They must outperform any competing
book or course you’ve ever tried, or return your
purchase for a prompt refund.…no questions asked.