A Concise Introduction to Programming in Python
eBook - ePub

A Concise Introduction to Programming in Python

Mark J. Johnson

Share book
  1. 209 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

A Concise Introduction to Programming in Python

Mark J. Johnson

Book details
Book preview
Table of contents
Citations

About This Book

A Concise Introduction to Programming in Python, Second Edition provides a hands-on and accessible introduction to writing software in Python, with no prior programming experience required.

The Second Edition was thoroughly reorganized and rewritten based on classroom experience to incorporate:

  • A spiral approach, starting with turtle graphics, and then revisiting concepts in greater depth using numeric, textual, and image data


  • Clear, concise explanations written for beginning students, emphasizing core principles


  • A variety of accessible examples, focusing on key concepts


  • Diagrams to help visualize new concepts


  • New sections on recursion and exception handling, as well as an earlier introduction of lists, based on instructor feedback

The text offers sections designed for approximately one class period each, and proceeds gradually from procedural to object-oriented design. Examples, exercises, and projects are included from diverse application domains, including finance, biology, image processing, and textual analysis. It also includes a brief "How-To" sections that introduce optional topics students may be interested in exploring.

The text is written to be read, making it a good fit in flipped classrooms. Designed for either classroom use or self-study, all example programs and solutions to odd-numbered exercises (except for projects) are available at: http://www.central.edu/go/conciseintro/.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is A Concise Introduction to Programming in Python an online PDF/ePUB?
Yes, you can access A Concise Introduction to Programming in Python by Mark J. Johnson in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming Games. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781351621984
Edition
2

CHAPTER 1

Turtle Graphics

This chapter introduces many of the fundamental concepts of computing using examples from turtle graphics.

1.1 GETTING STARTED

Programmable software is what makes a computer a powerful tool. Each different program essentially “rewires” the computer to allow it to perform a different task. By following this text, you will learn basic principles of writing software in the Python programming language.
Python is a popular scripting language available as a free download from www.python.org. Follow the instructions given there to install the latest production version of Python 3 on your system. All examples in this text were written with Python 3.6.
The CPU and RAM
In order to write software, it will be helpful to imagine what happens inside the computer when a program runs. We begin with a rough picture and gradually fill in details along the way.
When a program is ready to run, it is loaded into RAM, usually from long-term storage such as a network drive or flash drive. RAM is an acronym for random access memory, which is the working memory of a computer. RAM is volatile, meaning that it requires electricity to maintain its contents.
Once a program is loaded into RAM, the CPU, or central processing unit, executes the instructions of the program, one at a time. Each CPU family has its own instruction set, and you might be surprised at how limited these instruction sets are. Most instructions boil down to one of a few simple types: load data, perform arithmetic, make comparisons, and store data. It is amazing that these small steps can be combined in so many different ways to build software that is incredibly diverse and complex.
Computer Languages
CPU instruction sets are also known as machine languages. The key point to remember about machine languages is that in order to be run by a CPU, a program must be written in the machine language of that CPU. Unfortunately, machine languages are not meant to be read or written by humans. They are really just specific sequences of bits in memory. (We will explain bits later if you are not sure what they are.)
Because of this, people usually write software in a higher-level language, in the sense of Table 1.1. This ordering is not meant to be precise, but, for example, most programmers would agree that C and C++ are closer to the machine than Python.
TABLE 1.1 Programming language hierarchy
image
Compilation and Interpretation
Now if CPUs can only run programs written in their own machine language, how do we run programs written in Python, Java, or C++? The answer is that the programs are translated into machine language first.
There are two main types of translation: compilation and interpretation. When a program is compiled, it is completely translated into machine language to produce an executable file. C and C++ programs are usually compiled, and most applications you normally run have been compiled. In fact, many companies only distribute compiled executables: unless a project is open source, you do not have access to the uncompiled source code.
On the other hand, when a program is interpreted, it is translated “on-the-fly.” No separate executable file is created. Instead, the translator program (the interpreter) translates your program so that the CPU can execute it. Python programs are usually interpreted.
The Python Interpreter
When you start Python, you are in immediate contact with a Python interpreter. If you provide it with valid Python, the interpreter will translate your code so that it can be executed by the CPU. The interpreter displays the version of Python it was written for, and then shows that it is ready for your input with a “>>>” prompt. The interpreter will translate and execute any legal Python code that is typed at this prompt. For example, if we enter the following statement:
image
the interpreter will respond accordingly. Try it and see.
Remember that if you are not sure what something will do in Python, you can always try it out in the interpreter without having to write a complete program. Experiment—the interpreter will not mind.
A Python Program
That said, our focus will be on writ...

Table of contents