Preface
As the title promises,this book will introduce you toone of the worlds most popular programming languages: Python.Its aimed at beginning programmers as well as more experiencedprogrammers who want to add Python to the languagesthey already know.
In most cases,its easier to learn a computer languagethan a human language.Theres less ambiguityand 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,youll be introduced to Pythonsbasic 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, well start with a hammer,nails, and scraps of wood.Over the first half of this book,well introduce more specialized components,up to the equivalents of lathes and other power tools.
Youll not only learn the language,but also what to do with it.Well begin with the Python language and itsbatteries included standard library,but Ill also show you how to find, download,install, and usesome good third-party packages.My emphasis is on whatever Ive actually found usefulin more than 10 years of production Python development,rather than fringe topics or complex hacks.
Although this is an introduction,some advanced topics are included becauseI want 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.Youll 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 youre learning something new,its hard to tell whichterms are specific instead of colloquial,and which concepts are actually important.In other words,Is this on the test?Ill 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.
Note
Ill include a note such as thiswhen something might be confusing, or if theresa more appropriate Pythonic way to do it.
Python isnt perfect.Ill show you things that seem odd or that should be avoidedand offer alternatives you can use, instead.
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.
Audience
This book is for anybody interested in learningone of the worlds most popular computing languages,regardless of whether you have previously learned any programming.
Changes in the Second Edition
Whats changed since the first edition?
About a hundred more pages, including cat pictures.
Twice the chapters, each shorter now.
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 hasnt changed?Examples using bad poetry and ducks.These are evergreen.
Outline
(Chapters 1222) shows how Python is used in specific application areas such as the web, databases, networks, and so on; read these chapters in any order you like.
Heres a brief preview of the chapters and appendixes,including some of the terms that youll run into there:
Computer programs arenot that different from directions that you see every day.Some little Python programs give you a glimpseof the languages looks, capabilities,and uses in the real world.Youll see how to run a Python programwithin its interactive interpreter(or shell),or from a text file saved on your computer.
Computer languages mix data and instructions.Different types of dataare stored and treated differently by the computer.They may allow their values to bechanged (mutable)or not (immutable).In a Python program,data can be literal(numbers like 78
,text strings like "waffle"
)or represented by named variables.Python treats variables like names,which is different from many other languagesand has some important consequences.
This chapter shows Pythons simplest data types:booleans, integers, and floating-point numbers.Youll also learn the basic math operations.The examples use Pythons interactive interpreterlike a calculator.
Well bounce between Pythons nouns (data types)and verbs (program structures) for a few chapters.Python code normally runs a line at a time,from the startto the end of a program.The if
code structure lets you run different lines of code,depending on some data comparison.