(Ebook PDF) Building Python Programs 1St Edition
(Ebook PDF) Building Python Programs 1St Edition
(Ebook PDF) Building Python Programs 1St Edition
com
https://ebooksecure.com/product/ebook-pdf-building-python-
programs-1st-edition/
OR CLICK BUTTON
DOWLOAD EBOOK
http://ebooksecure.com/product/ebook-pdf-fundamentals-of-python-
first-programs-2nd-edition/
https://ebooksecure.com/download/fundamentals-of-python-first-
programs-2nd-edition-ebook-pdf/
http://ebooksecure.com/product/ebook-pdf-building-java-programs-
a-back-to-basics-approach-5th-edition/
http://ebooksecure.com/product/building-java-programs-a-back-to-
basics-approach-4th-edition-ebook-pdf/
Building Java Programs: A Back to Basics Approach 4th
Edition Stuart Reges - eBook PDF
https://ebooksecure.com/download/building-java-programs-a-back-
to-basics-approach-ebook-pdf/
https://ebooksecure.com/download/building-java-programs-a-back-
to-basics-approach-ebook-pdf-2/
https://ebooksecure.com/download/progress-in-heterocyclic-
chemistry-ebook-pdf/
https://ebooksecure.com/download/python-distilled-ebook-pdf/
http://ebooksecure.com/product/ebook-pdf-building-java-programs-
a-back-to-basics-approach-4th-edition-by-stuart-reges/
Preface
The Python programming language has become enormously popular in recent years.
Many people are impressed with how quickly you can learn Python’s simple and intui-
tive syntax and that has led many users to create popular libraries. Python was designed
by Guido van Rossum who has been affectionaly dubbed “Benevolent Dictator For Life
(BDFL)” by the Python community. He has said that he chose the name Python because
he was “in a slightly irreverent mood” and that he is “a big fan of Monty Python’s
Flying Circus” (a British comedy show). Who wouldn’t want to learn a programming
language named after a group of comedians?
Our new Building Python Programs text is designed for use in a first course in com-
puter science. We have class-tested it with hundreds of undergraduates at the University
of Arizona, most of whom were not computer science majors. This textbook is based
on our previous text, Building Java Programs, now in its fourth edition. The Java text
has proven effective in our class testing with thousands of students including our own
at the University of Washington since 2007.
Introductory computer science courses have a long history at many universities
of being “killer” courses with high failure rates. But as Douglas Adams says in The
Hitchhiker’s Guide to the Galaxy, “Don’t panic.” Students can master this material if
they can learn it gradually.
Python has many attributes that make it an appealing language for a first computer
science course. It has a simple and concise yet powerful syntax that makes it pleasant
to learn and great for writing many common programs. A student can write their first
Python program with only a single line of code, as opposed to several lines in most other
languages such as Java or C++. Python includes a built-in interpreter and read-evaluate-
print loop (REPL) for quickly running and testing code, encouraging students to test and
explore the language. Python also offers a rich set of libraries that students can use for
graphics, animation, math, scientific computing, games, and much more. This text has
been built from the start for Python 3, the most modern version of the language as of this
writing, and it embraces the modern features and idioms of that version of the language.
Our teaching materials are based on a “back to basics” approach that focuses on
procedural programming and program decomposition. This is also called the “objects
later” approach, as opposed to the “objects early” approach taught in some schools. We
know from years of experience that a broad range of scientists, engineers, and others
can learn how to program in a procedural manner. Once we have built a solid founda-
tion of procedural techniques, we turn to object-oriented programming. By the end of
the text, students will have learned about both styles of programming.
iii
Brief Contents
xi
Contents
Arithmetic Operators 62
Precedence 66
Mixing and Converting Types 69
2.2 Variables 70
A Program with Variables 74
Increment/Decrement Operators 79
Printing Multiple Values 80
2.3 The for Loop 83
Using a Loop Variable 87
Details about Ranges 90
String Multiplication and Printing Partial Lines 94
Nested for Loops 98
2.4 Managing Complexity 101
Scope 101
Pseudocode 103
Constants 108
2.5 Case Study: Hourglass Figure 111
Problem Decomposition and Pseudocode 112
Initial Structured Version 114
Adding a Constant 115
Images 188
Procedural Decomposition with Graphics 189
3.5 Case Study: Projectile Trajectory 191
Unstructured Solution 195
Structured Solution 196
Graphical Version 199
Introduction
1.1 Basic Computing Concepts
■■ Why Programming?
This chapter begins with a review of some basic terminology about comput- ■■ Hardware and Software
ers and computer programming. Many of these concepts will come up in later ■■ The Digital Realm
chapters, so it will be useful to review them before we start delving into the
■■ The Process of Programming
■■ Why Python?
details of how to program in Python. ■■ The Python Programming
Environment
We will begin our exploration of Python by looking at simple programs
1.2 And Now: Python
that produce text output to a console window. This discussion will allow us
■■ Printing Output
to explore many elements that are common to all Python programs, while ■■ String Literals (Strings)
working with programs that are fairly simple in structure. ■■ Escape Sequences
■■ Printing a Complex Figure
After we have reviewed the basic elements of Python programs, we will ■■ Comments, Whitespace, and
explore the technique of procedural decomposition by learning how to break Readability
up a Python program into several functions. Using this technique, we can 1.3 Program Errors
break up complex tasks into smaller subtasks that are easier to manage and ■■ Syntax Errors
■■ Logic Errors (Bugs)
we can avoid redundancy in our program solutions.
1.4 Procedural Decomposition
■■ Functions
■■ Flow of Control
■■ Identifiers and Keywords
■■ Functions That Call Other
Functions
■■ An Example Runtime Error
Chapter Summary
Self-Check Problems
Exercises
Programming Projects
1
2 Chapter 1 Introduction to Python Programming
Why Programming?
At most universities, the first course in computer science is a programming course.
Many computer scientists are bothered by this because it leaves people with the impres-
sion that computer science is programming. While it is true that many trained computer
scientists spend time programming, there is a lot more to the discipline. So why do we
study programming first?
A Stanford computer scientist named Don Knuth answers this question by saying
that the common thread for most computer scientists is that we all in some way work
with algorithms.
Algorithm
A step-by-step description of how to accomplish a task.
It has often been said that a person does not really understand something until
after teaching it to someone else. Actually a person does not really understand
something until after teaching it to a computer, i.e., expressing it as an
algorithm.1
1
Knuth, Don. Selected Papers on Computer Science. Stanford. CA: Center for the Study of Language and
Information, 1996.
1.1 Basic Computing Concepts 3
Program
A list of instructions to be carried out by a computer.
One key feature that differentiates a computer from a simpler machine like a calculator
is its versatility. The same computer can perform many different tasks (playing games,
computing income taxes, connecting to other computers around the world), depending
on what program it is running at a given moment. A computer can run not only the pro-
grams that exist on it currently, but also new programs that haven’t even been written yet.
The physical components that make up a computer are collectively called hardware.
One of the most important pieces of hardware is the central processing unit, or CPU.
The CPU is the “brain” of the computer: It is what executes the instructions. Also
important is the computer’s memory (often called random access memory, or RAM,
because the computer can access any part of that memory at any time). The computer
uses its memory to store programs that are being executed, along with their data. RAM
is limited in size and does not retain its contents when the computer is turned off.
Therefore, computers generally also use a hard disk as a larger permanent storage area.
Computer programs are collectively called software. The primary piece of soft-
ware running on a computer is its operating system. An operating system provides an
environment in which many programs may be run at the same time; it also provides
4 Chapter 1 Introduction to Python Programming
a bridge among those programs, the hardware, and the user (the person using the
computer). The programs that run inside the operating system are often called
applications or apps.
When the user selects a program for the operating system to run (e.g., by double-
clicking the program’s icon on the desktop), several things happen: The instructions for
that program are loaded into the computer’s memory from the hard disk, the operating
system allocates memory for that program to use, and the instructions to run the pro-
gram are fed from memory to the CPU and executed sequentially.
Digital
Based on numbers that increase in discrete increments, such as the integers
0, 1, 2, 3, etc.
Binary Number
A number composed of just 0s and 1s, also known as a base-2 number.
Humans generally work with decimal or base-10 numbers, which match our physiol-
ogy (10 fingers and 10 toes). However, when we were designing the first computers,
we wanted systems that would be easy to create and very reliable. It turned out to be
simpler to build these systems on top of binary phenomena (e.g., a circuit being open or
closed) rather than having 10 different states that would have to be distinguished from
one another (e.g., 10 different voltage levels).
From a mathematical point of view, you can store things just as easily using binary
numbers as you can using base-10 numbers. But since it is easier to construct a physical
device that uses binary numbers, that’s what computers use.
This does mean, however, that people who aren’t used to computers find their
conventions unfamiliar. As a result, it is worth spending a little time reviewing how
Another random document with
no related content on Scribd:
»Jo kuuluu kummia», hykerteli Aapeli eikä muistanut
vesihernettäkään nenän päästä pyyhkäistä. »Piispalta on minulle
tullut preivi… sitä on Vaanperi minulle suomentanut…»
*****
Oli sittenkin onni, ettei hän ollut ketään ottanut uskotukseen, ettei
kenellekään ollut aikeestaan puhunut. Ei kukaan osannut
aavistaakaan, mitä hän mietti poikansa onnen ja elämän vuoksi.
Yksin vain hän sen tiesi, eivätkä sitä muut koskaan saisi tietääkään.
Hän oli niin rakas, niin rakas! Hän oli niin hyvä ja lempeä!
»En, en koskaan.»
Sitten hän lähti. Kerran kirjoitti. Ei silloin tiennyt, milloin tulisi. Hän
kirjoitti vastaan, kertoi kuinka laitansa oli. Siihen ei vastausta tullut.
Silloin hän teki epätoivoisen tekonsa. Mitä hän silloin taisi? Oma ja
koko suvun häpeä vartoi. Ja hän päätti kärsiä ja ottaa miehekseen
Portaankorvan Aapelin…
Ainainen pelko oli lisäksi vaivannut, että Aapeli joskus saa kuulla
— ellei itse viimein ymmärrä — ettei poika olekaan hänen, ainainen
pelko, että silloin se pojan ruhjoo… Ja vaikka hän sydämensä
syvyydessä tunsi, että kerran se saapi tietää, kerran hän katkaisee
kärsimystensä kahleet, ja silloin on kaikki sileää… Mutta poika piti
ensin saada turvaan, varmaan turvaan…
Ja nyt se oli hänelle selvinnyt. Vielä nyt kun jaksaisi jonkun viikon
viettää tätä valhe-elämää! Kun vielä kaikki onnistuisi eikä Aapelin
epäluulo enenisi! Kohta, kohta tulee otollinen hetki!
— Niin! Niin!
Hän sai siitä vahvistusta, uusia voimia siihen, jonka oli päättänyt
tehdä.
Tätä aikaa hän oli koko kesän odottanut — tätä viikkoa, jolloin
tiesi muun väen ja isännänkin menevän viikon kestävälle
niittymatkalle. Nyt se oli tullut, ja emäntä oli sitä aikaa hyväksensä
käyttänyt.
Oskari oli saanut tietää koko elämänsä salaisuuden, äiti oli itkenyt
ja poika oli itkenyt, mutta äidin päätös aiottiin toteuttaa.
*****
Niin tulee hän kuin tietämättään taloon, nousee kuistille, lyö ovet
lukkoon ja menee sisälle. Hän saa pirtissä kynttilän palamaan ja nyt
hän taas jaksaa ajatella ja tuntee tointuvansa.
»Missä poika on?» karjaisi hän uudelleen, niin että pirtti jymisi.
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.