0% found this document useful (0 votes)
63 views5 pages

Learn Enough Python To Be Dangerous

Python is a great first language to learn, but you don't need to learn 'everything' to get started, just how to use it efficiently to solve real problems. In Learn Enough Python to Be Dangerous, you will learn the specific concepts, skills, and approaches you need to be professionally productive.

Uploaded by

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

Learn Enough Python To Be Dangerous

Python is a great first language to learn, but you don't need to learn 'everything' to get started, just how to use it efficiently to solve real problems. In Learn Enough Python to Be Dangerous, you will learn the specific concepts, skills, and approaches you need to be professionally productive.

Uploaded by

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

CHAPTER1

Hello, World!

Welcome to Learn Enough Python to Be Dangerous!


This tutorial is designed to get you started writing practical and modern Python
programs as fast as possible, with a focus on the real tools used every day by software
developers. You’ll see how everything fits together by learning skills like testing and
test-driven development, publishing packages, beginning web development, and data
science. As a result, Learn Enough Python to Be Dangerous can serve either as a standalone
introduction or as an excellent prerequisite for longer and more syntax-heavy Python
tutorials, of which there are many excellent ones.
Python is one of the world’s most popular programming languages, and for good
reason. Python has a clean syntax, flexible data types, a wealth of useful libraries,
and a powerful and elegant design that supports multiple styles of programming.
Python has seen particularly robust adoption for command-line programs (also known
as scripting, as discussed in Chapter 9), web development (via frameworks like Flask
(Chapter 10) and Django), and data science (especially data analysis using pandas and
machine learning with libraries like scikit-learn (Chapter 11)).
Just about the only things Python isn’t good for are running inside a web browser
(for which JavaScript (https://www.learnenough.com/javascript-tutorial) is neces-
sary) and writing programs where speed is of the essence. And even in the latter case,
specialized libraries like NumPy (Section 11.2) can give us the speed of a lower-level
language like C with the power and flexibility of a higher-level language like Python.1

1. “Higher-level” languages like Python, JavaScript, and Ruby generally have greater support for abstraction
and perform automatic memory management.

1
Humble Bundle Pearson Learn to Program Ð © Pearson. Do Not Distribute.
2 Chapter 1: Hello, World!

Learn Enough Python to Be Dangerous broadly follows the same structure as


Learn Enough JavaScript to Be Dangerous (https://www.learnenough.com/javascript)
and Learn Enough Ruby to Be Dangerous (https://www.learnenough.com/ruby), either
of which can be studied either before or after this tutorial. Because many of the exam-
ples are the same, the tutorials reinforce each other nicely—there are few things more
instructive in computer programming than seeing the same basic problems solved in
two or more different languages.2 As noted in Box 1.1, though, we’ll definitely be
writing Python, not JavaScript or Ruby translated into Python.

Box 1.1: Pythonic programming

More so even than users of other languages, Python programmers—sometimes


known as Pythonistas—tend to have strongly held opinions on what constitutes
proper programming style. For example, as noted by Python contributor Tim Peters
in “The Zen of Python” (Section 1.2.1): “There should be one—and preferably only
one—obvious way to do it.” (This stands in contrast to a famous principle associated
with the Perl programming language known as “TMTOWTDI”: There’s More Than
One Way To Do It.)
Code that adheres to good programming practices (as judged by Pythonistas) is
known as Pythonic code. This includes proper code formatting (especially the prac-
tices in PEP 8 – Style Guide for Python Code (https://peps.python.org/pep-0008/)),
using built-in Python facilities like enumerate() (Section 3.5) and items() (Sec-
tion 4.4.1), and using characteristic idioms like list and dictionary comprehensions
(Chapter 6). (As noted in the official documentation (https://peps.python.org/pep-
0001/), “PEP stands for Python Enhancement Proposal. A PEP is a design document
providing information to the Python community, or describing a new feature for
Python or its processes or environment.” PEP 8 is the PEP specifically concerned
with Python code style and formatting.)
The code in this tutorial generally strives to be as Pythonic as possible given
the material introduced at the given point in the exposition. In addition, we will
often begin by introducing a series of intentionally unPythonic examples, culminating
in a fully Pythonic version. In such cases, the distinction between unPythonic and
Pythonic code will be carefully noted.
Pythonistas have been known to be a bit harsh in their judgment of unPythonic
code, which can lead beginners to become overly concerned about programming

2. See Rosetta Code (https://rosettacode.org/wiki/Rosetta_Code) for a huge compilation of such examples.

Humble Bundle Pearson Learn to Program Ð © Pearson. Do Not Distribute.


Hello, World! 3

Pythonically. But “Pythonic” is a sliding scale, depending on how much experience


you have in the language. Moreover, programming is fundamentally about solving
problems, so don’t let worries about programming Pythonically stop you from
solving the problems you face in your role as a Python programmer and software
developer.

There are no programming prerequisites for Learn Enough Python to Be


Dangerous, although it certainly won’t hurt if you’ve programmed before.
What is important is that you’ve started developing your technical sophistication
(Box 1.2), either on your own or using the preceding Learn Enough tutori-
als (https://www.learnenough.com/courses). These tutorials include the following,
which together make a good list of prerequisites for this book:

1. Learn Enough Command Line to Be Dangerous (https://www.learnenough.com/


command-line)
2. Learn Enough Text Editor to Be Dangerous (https://www.learnenough.com/text-
editor)
3. Learn Enough Git to Be Dangerous (https://www.learnenough.com/git)

All of these tutorials are available as print or digital books or online for individual
purchase, and we offer a subscription service—the Learn Enough All Access subscrip-
tion (https://www.learnenough.com/all-access)—with access to all the corresponding
online courses.

Box 1.2: Technical sophistication

An essential aspect of using computers is the ability to figure things out and trou-
bleshoot on your own, a skill we at Learn Enough (https://www.learnenough.com/)
call technical sophistication.
Developing technical sophistication means not only following systematic tutori-
als like Learn Enough Python to Be Dangerous, but also knowing when it’s time to break
free of a structured presentation and just start Googling around for a solution.
Learn Enough Python to Be Dangerous will give us ample opportunity to practice
this essential technical skill.
In particular, as alluded to above, there is a wealth of Python reference mate-
rial on the Web, but it can be hard to use unless you already basically know

Humble Bundle Pearson Learn to Program Ð © Pearson. Do Not Distribute.


4 Chapter 1: Hello, World!

what you’re doing. One goal of this tutorial is to be the key that unlocks the
documentation. This will include lots of pointers to the official Python site.
Especially as the exposition gets more advanced, I’ll also sometimes include the
web searches you could use to figure out how to accomplish the particular task at
hand. For example, how do you use Python to manipulate a Document Object
Model (DOM)? Like this: python dom manipulation.

You won’t learn everything there is to know about Python in this tutorial—that
would take thousands of pages and centuries of effort—but you will learn enough
Python to be dangerous (Figure 1.13 ). Let’s take a look at what that means.
In Chapter 1, we’ll begin at the beginning with a series of simple “hello, world”
programs using several different techniques, including an introduction to an inter-
active command-line program for evaluating Python code. In line with the Learn
Enough philosophy of always doing things “for real”, even as early as the first chapter
we’ll deploy a (very simple) dynamic Python application to the live Web. You’ll

Figure 1.1: Python knowledge, like Rome, wasn’t built in a day.

3. Image courtesy of Kirk Fisher/Shutterstock.

Humble Bundle Pearson Learn to Program Ð © Pearson. Do Not Distribute.


Hello, World! 5

also get pointers to the latest setup and installation instructions via Learn Enough
Dev Environment to Be Dangerous (https://www.learnenough.com/dev-environment),
which is available for free online and as a free downloadable ebook.
After mastering “hello, world”, we’ll take a tour of some Python objects, including
strings (Chapter 2), arrays (Chapter 3), and other native objects (Chapter 4). Taken
together, these chapters constitute a gentle introduction to object-oriented programming
with Python.
In Chapter 5, we’ll learn the basics of functions, an essential subject for virtu-
ally every programming language. We’ll then apply this knowledge to an elegant and
powerful style of coding called functional programming (Chapter 6).
Having covered the basics of built-in Python objects, in Chapter 7 we’ll learn
how to make objects of our own. In particular, we’ll define an object for a phrase, and
then develop a method for determining whether or not the phrase is a palindrome (the
same read forward and backward).
Our initial palindrome implementation will be rather rudimentary, but we’ll
extend it in Chapter 8 using a powerful technique called test-driven development (TDD).
In the process, we’ll learn more about testing generally, as well as how to create and
publish a self-contained Python package.
In Chapter 9, we’ll learn how to write nontrivial shell scripts, one of Python’s
biggest strengths. Examples include reading from both files and URLs, with a final
example showing how to manipulate a downloaded file as if it were an HTML web
page.
In Chapter 10, we’ll develop our first full Python web application: a site for detect-
ing palindromes. This will give us a chance to learn about routes, layouts, embedded
Python, and form handling. As a capstone to our work, we’ll deploy our palindrome
detector to the live Web.
Finally, Chapter 11 introduces several core libraries for doing data science in
Python, including NumPy, Matplotlib, pandas, and scikit-learn.
By the way, experienced developers can largely skip the first four chapters of Learn
Enough Python to Be Dangerous, as described in Box 1.3.

Box 1.3: For experienced developers

By keeping a few diffs in mind, experienced developers can skip Chapters 1–4 of
this tutorial and start with functions in Chapter 5. They can then move quickly on

Humble Bundle Pearson Learn to Program Ð © Pearson. Do Not Distribute.

You might also like