0% found this document useful (0 votes)
268 views

Introducing Phyton Second Edition

This document introduces the second edition of a book about learning the Python programming language. The preface provides an overview of the book's contents and approach. It will introduce Python to both beginners and experienced programmers from other languages. The book will cover Python's basic data structures and code in alternating chapters, building up to more advanced topics. It emphasizes useful real-world applications over academic or complex topics.

Uploaded by

Montessorispain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
268 views

Introducing Phyton Second Edition

This document introduces the second edition of a book about learning the Python programming language. The preface provides an overview of the book's contents and approach. It will introduce Python to both beginners and experienced programmers from other languages. The book will cover Python's basic data structures and code in alternating chapters, building up to more advanced topics. It emphasizes useful real-world applications over academic or complex topics.

Uploaded by

Montessorispain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

INTRODUCING PYTHON Second Edition

Preface

As the title promises,this book will introduce you toone of the world’s most popular
programming languages —Python.It’s aimed at both beginning programmers,and
programmers who want to add Python to the languagesthey already know.In most cases,it’s
easier to learn a computer languagethan a human language.There’s less ambiguity,and fewer
exceptions to keep in your head.Python is one of the most consistent and clear computer
languages.It balancesease of learning,ease of use,and expressive power.Computer languages
are made ofdata (like nouns in spoken languages)and instructions or code (like verbs).You
need both.In alternating chapters,you’ll be introduced to Python’sbasic code and data
structures,learn how to combine them,and build up to more advanced ones. The programs
that you read and write will get longer and more complex. Using a woodworking analogy, we’ll
start with a hammer,nails, and scraps of wood.Over the first half of this book,we’ll introduce
more specialized components,up to the equivalents of lathes and other power tools. You’ll
learn the language,but also what to do with it.I’ll start with the Python language and its
“batteries included” standard library,but also show you how to find, download, install, and
usesome good third-party packages.My emphasis is on whatever I’ve actually found usefulin
over ten years of production Python development,rather than fringe topics or complex
hacks.Although this is an introduction,some advanced topics are included becauseI wanted to
expose them to you.Areas like databases and the web are still covered,but technology changes
fast.A Python programmer might now be expected to know somethingabout cloud computing,
machine learning,or event streaming.You’ll find something here on all of these.Python has
some special features that work betterthan adapting styles from other languages that you may
know.For example, using for and iterators isa more direct way of making a loop thanmanually
incrementing some counter variable.When you’re learning something new,it’s hard to tell
whichterms are specific instead of colloquial,and which concepts are actually important.In
other words,“Is this on the test?”I’ll highlight terms and ideas that havespecific meaning or
importance in Python,but not too many at once.Real Python code is included early and
often.NoteI’ll include a note such as thiswhen something might be confusing, or if there’sa
more appropriate Pythonic way to do it.Python isn’t perfect.I’ll show you things that seem
odd,things to avoid,and alternatives.Now and then,my opinions on some subjects(such as
object inheritance,or MVC and REST designs for the web)may vary a bit fromthe common
wisdom.See what you think.AudienceThis book is for anybody interested in learningone of the
world’s most popular computing languages,whether ornot you have learned any programming
before.Changes in the Second EditionWhat’s changed since the first edition?About a hundred
more pages, including cat pictures.Twice the chapters, and shorter.An early chapter devoted to
data types, variables, and names.New standard Python features like f-strings.New or improved
third-party packages.New code examples throughout.An appendix on basic hardware and
software, for new programmers.An appendix on asyncio, for not-so-new programmers.“New
stack” coverage: containers, clouds, data science,and machine learning.Hints on getting a job
programming in Python.What hasn’t changed?Examples using bad poetry, and ducks.These are
evergreen.OutlineThe first eleven chapters explain Python’s basics,and you should read them
in order.I work up from the simplest data and code structures,combining them on the way
more detailed and realistic programs. The later chapters
Chapter 1.

A Taste of PyOnly ugly languages become popular. Python is the one exception. Don Knuth

Mysteries

Let’s begin with two mini-mysteries and their solutions. What do you think the following two
lines mean?

(Row 1): (RS) K18,ssk,k1,turn work.

(Row 2): (WS) Sl 1 pwise,p5, p2tog, p1, turn.

It looks technical, like some kind of computer program. Actually, it’s a knitting pattern;
specifically, a fragment describing how to turn the heel of a sock. This makes as much sense to
me as a Sudoku puzzle does to one of my cats, but my wife understands it perfectly. If you’re a
knitter, you do, too.

Let’s try another mysterious text, found on an index card. You’ll figure out its purpose right
away, although you might not know its final product. 1/2 c. butter or margarine 1/2 c. cream
2 1/2 c. flour 1 t. salt 1 T. sugar 4 c. riced potatoes (cold) Be sure all ingredients are cold
before adding flour. Mix all ingredients. Knead thoroughly. Form into 20 balls. Store cold until
the next step. For each ball: Spread flour on cloth. Roll ball into a circle with a grooved rolling
pin. Fry on griddle until brown spots appear. Turnover and fry other side.

Even if you don’t cook, you probably recognized that it’s arecipe 1:a list of food ingredients
followed by directions for preparation. But what does it make? It’s lefse, a Norwegian delicacy
that resembles a tortilla. Slather on some butter and jam or whatever you like, roll it up, and
enjoy.

The knitting pattern and the recipe share some features: A regular vocabulary of words,
abbreviations, and symbols. Some might be familiar, others mystifying. Rules about what can
be said, and where — syntax. A sequence of operations to be performed in order. Sometimes,
a repetition of some operations (a loop),such as the method for frying each piece of lefse.
Sometimes, a reference to another sequence of operations(in computer terms, a function).In
the recipe, you might need to refer to another recipe for ricing potatoes. Assumed knowledge
about the context. The recipe assumes you know what water is and how to boil it. The knitting
pattern assumes that you can knit and purl without stabbing yourself too often. Some data to
be used, created, or modified —potatoes and yarn. The tools used to work with the data —
pots, mixers, ovens, knitting sticks. An expected result. In our examples, something for your
feet and something for your stomach. Just don’t mix them up. Whatever you call them —
idioms, jargon, little languages —you see examples of them everywhere. The lingo saves time
for people who know it, while mystifying the rest of us. Try deciphering a newspaper column
about bridge if you don’t play the game, or a scientific paper if you’re not a scientist(or even if
you are, but in a different field).

Little Programs
You’ll see all of these ideas in computer programs, which are themselves like little languages,
specialized for humans to tell computers what to do. I used the knitting pattern and recipe to
demonstrate that programming isn’t that mysterious. It’s largely a matter of learning the right
words and the rules. Now, it helps greatly if there aren’t too many words and rules, and if you
don’t have to learn too many of them at once. Our brains can only hold so much at one time.
Let’s finally see a real computer program. What do you think this does?

Example 1-1. countdown.py

for countdown in 5, 4, 3, 2, 1, "hey!":

print(countdown)

If you guessed that it’s a Python program that prints the lines: 5 4 3 2 1 hey! then you know
that Python can be easier to learn than a recipe or knitting pattern. And, you can practice
writing Python programs from the comfort and safety of your desk, far from the hazards of hot
water and pointy sticks.

The Python program has some special words and symbols —for, in, print, commas, colons,
parentheses, and so on —that are important parts of the language’s syntax (rules).The good
news is that Python has a nicer syntax, and less of it to remember, than most computer
languages. It seems more natural —almost like a recipe.

Here’s another tiny Python program that selects one Harry Potter spell from a Python list and
prints it:

Example 1-2. spells.py

spells = [

"Riddikulus!",

"Wingardium Leviosa!",

"Avada Kadavra!",

"Expecto Patronum!",

"Nox!", "Lumos!",

print(spells[3])

The individual spells are Python strings(sequences of text characters, enclosed in


quotes).They’re separated by commas and enclosed in a Python list that’s defined by enclosing
square brackets ([ and ]).The word spells is a variable that gives the list a name so we can do
things with it. In this case, the program would print the fourth spell:

Expecto Patronum! Why did we say 3 if we wanted the fourth?


A Python list such as spells is a sequence of values, accessed by their offset from the beginning
of the list. The first value is at offset 0,and the fourth value is at offset 3.

Note People count from 1,so it might seem weird to count from 0. It helps to think in terms of
offsets instead of positions. Yes, this is an example of how computer programs sometimes
differ from common language usage.

Lists are very common data structures in Python, and Chapter 7 shows how to use them.
Following is another program that prints a quotefrom one of the Three Stooges,but referenced
by who said it rather than its position in a list:

Example 1-3. quotes.py

quotes = {

"Moe": "A wise guy, huh?",

"Larry": "Ow!",

"Curly": "Nyuk nyuk!",

stooge = "Curly"

print (stooge, "says:", quotes[stooge])

If you were to run this little program, it would print the following:

Curly says: Nyuk nyuk!

quotes is a variable that names a Python dictionary —a collection of unique keys(in this
example, the name of the Stooge)and associated values(here, a notable saying of that
Stooge).Using a dictionary, you can store and look up things by name, which is often a useful
alternative to a list.

The spells example used square brackets ([ and ]) to make a Python list ,and the quotes
example uses curly brackets ({ and },which are no relation to Curly), to make a Python
dictionary. Also, a colon (:) is used to associate each key in the dictionary with its value. You
can read much more about dictionaries in Chapter 8.

That wasn’t too much syntax at once, I hope. In the next few chapters, you’ll encounter more
of these little rules, a bit at a time.

A Bigger Program

And now for something completely different: Example 1-4 presents a Python program
performing a more complex series of tasks. Don’t expect to understand how the program
works yet; that’s what this book is for! The intent is to introduce you to the look and feel of
atypical nontrivial Python program. If you know other computer languages, evaluate how
Python compares. Even without knowing Python yet, can you roughly figure out what each line
does before reading the explanation after the program? You’ve already seen examples of a
Python list and a dictionary, and this throws in a few more features.

In earlier printings of this book, the sample program connected to a YouTube website and
retrieved information on its most highly rated videos, like “Charlie Bit My Finger.” It worked
well until shortly after the ink was dry on the second printing. That’s when Google dropped
support for this service and the marquee sample program stopped working. Our new
Example 1-4 goes to another site which should be around longer —the Way back Machine at
the Internet Archive, a free service which has saved billions of web pages(and movies, TV
shows, music, games, and other digital artifacts)over twenty years. You’ll see more example of
such web APIs in [Link to Come].

The program will ask you to type a URL and a date. Then it asks the Wayback Machine if it has
a copy of that website around that date. If it found one, it returns the information to this
Python program, which prints the URL and displays it in your web browser. The point is to
show how Python handles a variety of tasks —get your typed input, talk across the Internet to
a website, get back some content, extract a URL from it, and convince your web browser to
display that URL.

If we got back a normal web page full of HTML-formatted text, we would have to figure out to
display it, which is a lot of work that we happily entrust to web browsers. We could also try to
extract the parts we want(see more details about web scraping in [Link to Come]).Either choice
would be more work and a larger program. Instead, the Wayback Machine returns data in
JSON format. JSON (JavaScript Object Notation)is a human-readable text format that describes
thetypes, values, and order of the data within it. It’s another little language, and has become a
popular way to exchange data among different computer languages and systems. You’ll read
more about JSON in [Link to Come].

Python programs can translate JSON text into Python data structures —the kind you’ll see in
the next few chapters —as though you wrote a program to create them yourself. Our little
program just selects one piece(the URL of the old page from the Archive).Again, this is a
complete Python program that you can run yourself. We’ve included only a little error-
checking, just to keep the example short. The line numbers are not part of the program, but
are included to help you follow the description that follows the program.

Example 1-4. archive.py

1 import webbrowser

2 import json

3 from urllib.request import urlopen

5 print("Let's find an old website.")

6 site = input("Type a website URL: ")


7 era = input("Type a year, month, and day, like 20150613: ")

8 url = "http://archive.org/wayback/available?url=%s&timestamp=%s" % (site, era)

9 response = urlopen(url)

10 contents = response.read()

11 text = contents.decode("utf-8")

12 data = json.loads(text)13 try:

14 old_site = data["archived_snapshots"]["closest"]["url"]

15 print("Found this copy: ", old_site)

16 print("It should appear in your browser now.")

17 webbrowser.open(old_site)

18 except:

19 print("Sorry, no luck finding", site)

This little Python program did a lot in a few fairly readable lines. You don’t know all these
terms yet, butyou will within the next few chapters. Here’s what’s going on in each line:

1. Import (make available to this program) all the codefrom the Python standard library
module called web browser.

2Import all the code from the Python standard library module called json.

3 Import only the url open function from the standard library module urllib.request.

4. A blank line, because we don’t want to feel crowded.

5 Print some initial text to your display.

6 Print a question about a URL, read what you type, and save it in a program variable called
site.

7 Print another question, this time reading a year, month and day, and saving it in a variable
called era.

8 Construct a string variable called url to make theWayback Machine look up its copy of the
site and date that you typed.

9 Connect to the web server at that URL and request a particular web service.

10 Get the response data and assign to the variable contents

11 Decode contents to a text string in JSON format,and assign to the variable text.
12 Convert text to data — Python data structures.

13 Error-checking: try to run the next four lines,and if any fail, run the last line of the
program(after the except).

14 If we got back a match for this site and date, extract its value from a three-level Python
dictionary. Notice that this line and the next two are indented.That’s how Python knows that
they go with the preceding try line.

15 Print the URL that we found.

16 Print what will happen after the next line executes.

17 Display the URL we found in your web browser.

18 If anything failed in the previous four lines, Python jumps down to here.

19 If it failed, print a message and the site that we were looking for. This is indented because it
should be run only ifthe preceding except line runs.

When I ran this in a terminal window, I typed a site URL and a date, and got this text output:

$ python archive.py

Let's find an old website.

Type a website URL: lolcats.com

Type a year, month, and day, like 20150613: 20151022

Found this copy: http://web.archive.org/web/20151102055938/http://www.lolcats.com/

It should appear in your browser now. And this appeared in my browser:

And this appeared in my browser:

You might also like