able to program your internet-connected lawn sprinker system). But how do
you learn to code?
First, learn to think computationally. Next, you grab a
programming language so you can speak the same lingo as your computer,mobile device, or anything with a CPU. What’s in it for you? More time,
more power, and more creative possibilities to do the things you really want
to do. Come on, let’s get started...
Breaking it down
The first thing that stands between you and writing your first real piece of
code is learning the skill of breaking problems down into achievable little
actions that a computer can do for you. Of course, you and the computer will
also need to be speaking a common language, but we’ll get to that topic in
just a bit.
You can think of these statements as a nice recipe for fishing. Like any
recipe, this one provides a set of steps that, when followed in order, will
produce some result or outcome (in our case, hopefully, catching some fish).
Notice that most steps consist of a simple instruction, like “cast line into
pond,” or “pull in the fish.” But also notice that other instructions are a bit
different because they depend on a condition, like “is the bobber above or
below water?” Instructions might also direct the flow of the recipe, like “if
you haven’t finished fishing, then cycle back to the beginning and put another
worm on the hook.” Or, how about a condition for stopping, as in “if you’re
done, then go home”?
You’re going to find that these simple statements or instructions are the
foundation of coding. In fact, every app or software program you’ve ever
used has been nothing more than a (sometimes large) set of simple
instructions to the computer that tell it what to do
Actually, a recipe is a perfectly good way to describe a set of instructions to
a computer. You might even run into that term loosely used here and there in
more advanced programming books. Heck, you’ll even find books on
common software development techniques that are called cookbooks. That
said, if you want to get technical we can—a computer scientist or serious
software developer would commonly call a recipe an algorithm. What’s an
algorithm? Well, not much more than a recipe—it’s a sequence of
instructions that solves some problem. Often you’ll find algorithms are first
written in an informal form of code called pseudocode.In this book, you’ll hear us interchange all these terms, where appropriate—
and, oh, in your next job interview you might want to use the term algorithm
or even pseudocode to ensure that larger signing bonus (but there’s still
nothing wrong with the word recipe).
The world of programming languages
If you’re reading this book you may have, in passing, heard about various
programming languages. Just walking through the programming section of
your local bookstore you might encounter Java, C, C++, LISP, Scheme,
Objective-C, Perl, PHP, Swift, Clojure, Haskell, COBOL, Ruby, Fortran,
Smalltalk, BASIC, Algol, JavaScript, and of course Python, to name just a
few. You might also be wondering where all these names came from. The
truth is, programming language names are a lot like the names of rock bands
—they’re names that meant something to the people who created the
language. Take Java, for instance: it was named, not surprisingly, after coffee (the preferred name Oak was already taken). Haskell was named after a
mathematician, and the name C was chosen because C was the successor of
the languages A and B at Bell Labs. But why are there so many languages
and what are they all about? Let’s see what a few folks have to say about the
languages they use:
PYTHON
it's considered one of the best languages for beginners because it’s such a
readable and consistent language. It’s also a powerful language in that no
matter what you want to do with it (now or beyond this book), you can find
support in terms of code extensions (we call them modules or libraries) and a
supportive community of developers to give you a hand. Finally, some
developers will even tell you Python is just more fun than other languages. So
how can we go wrong?
Q: What’s the difference between learning to code and thinking
computationally? Is the latter just a computer science thing?
A: Computational thinking is a way of thinking about problem solving that
grew out of computer science. With computational thinking we learn how to
break problems down, to create algorithms to solve them, and to generalize
those solutions so we can solve even bigger problems. Often, though, we
want to teach a computer to execute those algorithms for us, and that’s where
coding comes in. Coding is the means by which we specify an algorithm to a
computer (or any computational device, like your smartphone). So the two
really go hand in hand—computational thinking gives us a way to create
solutions to problems that we want to code, and coding provides a means of
specifying our solutions to a computer. That said, computational thinking can
be valuable even if you aren’t coding.
PYTHON HISTORY
Over in the Netherlands, at the National Research Institute for Mathematics
and Computer Science, they had a big problem: their scientists found
programming languages difficult to learn. Yes, even to these highly educated,
skilled scientists, the most current programming languages were confusing
and inconsistent. To the rescue, the Institute developed a new language called
“ABC” (you thought we were going to say “Python,” didn’t you?), which
was designed to be much easier to learn. While ABC was somewhat
successful, an enterprising young developer named Guido van Rossum, after
a weekend of binge-watching Monty Python reruns, thought he could take
things further—so, using what he’d learned from ABC, Guido created
Python. And the rest is history.
Good question. And you’re right, there are two versions of Python—to be a
little more specifc, at the time this book was printed, the current versions are
3.6 and 2.7.
Python: One of the reasons newbies and professionals appreciate me is
because my code is quite straightforward and readable. Ever look at a
language like, say, Java? Blech. My gosh, the effort you have to go to just
to say, “Hello World!” That takes a single line of Python code.
Python: Given I mentioned Java, let me just give you a little example.
Let’s say you want to tell your user “Hello!” Here’s how you do it with
Java:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println(“Hello!”);
}
}
That’s a lot to take in. I’d call it totally unreadable, especially to someone
just learning to program. What the heck does all that mean, anyway? Is
all that really necessary? Now let’s look at my version, which I’ve
written in Python of course:
print(‘Hello!’)
I think you’d have to agree that is more straightforward and readable—
anyone can look at that line and have a decent idea of what it does. But
that’s just a simple example. Overall, Python strikes people as clear,
almost English-like, and consistent...
Head First: Consistent? What does that mean?
Python: One way to think about consistency is that there aren’t a lot of
surprises in the language. In other words, once you understand a bit of the
language, other things tend to work as you might guess, or expect. Not all
languages are like that.
Python: The space shuttle? You made that up. For the others, I was
giving you examples of things you might consider serious, given that you
claimed Python was otherwise. Some of the most common uses of Python
are for things like creating websites, writing games, and even creating
desktop apps.
Head First: Can we switch gears? Someone just handed me a note: our
sources tell us that there are actually two versions of Python, and what’s
more, they are actually… gosh, how do I say it, incompatible with each
other. How on earth is that being consistent?
Python: Like anything, languages tend to grow and evolve, and yes,
there are two version of Python, version 2 and version 3. Version 3 has
new things in it that were not part of version 2, but there are ways to
make things backward compatible. Let me walk your readers through…
Head First: ...on that note, we’re out of time. We look forward to our
next ambush, er, I mean opportunity, to speak with you.
Python: Thanks, my pleasure…I think.
Phraseomatic
import random
verbs = ['Leverage', 'Sync', 'Target',
'Gamify', 'Offline', 'Crowd-sourced',
'24/7', 'Lean-in', '30,000 foot']
adjectives = ['A/B Tested', 'Freemium',
'Hyperlocal', 'Siloed', 'B-to-B',
'Oriented', 'Cloud-based',
'API-based']
nouns = ['Early Adopter', 'Low-hanging Fruit',
'Pipeline', 'Splash Page', 'Productivity',
'Process', 'Tipping Point', 'Paradigm']
verb = random.choice(verbs)
adjective = random.choice(adjectives)
noun = random.choice(nouns)
phrase = verb + ' ' + adjective + ' ' + noun
print(phrase)
NOTE
random.choice is another built-in function from Python. We’ll learn more about these
later in the book.
Computers really only do two things well: store values and perform
operations on those values. You might think they’re doing a whole lot more,
as you send texts, shop online, use Photoshop, or rely on your phone to
navigate in your car; however, everything computers do can be broken down DESCARTES
into simple operations that are performed on simple values. Now, part of
computational thinking is learning to use these operations and values to build something that is much more sophisticated, complex, and meaningful—
and we’re going to get to that. First, though, we’re going to take a look at
what these values are, the operations you can perform on them, and just what
role variables play in all this.
*****************+
http://programming.itcarlow.ie/resources.html
Q:But a buddy of mine told me I
should learn Java or C#. Why are you
not using either of these programming
languages in this book?
A: Both Java and C# are great
programming technologies, but they can be
difficult to learn, especially when you are
just starting out. This is not the case with
Python. And, anyway, this is a book that’s
designed to teach you how to program, and
using Python as your first programming
language will help us to do just that.
There seems to be many different
versions of Python. Which should I use?
A: There are two main releases of
Python: 2 and 3. This book is based on
release 3 of the language. Python 3 is the
future of the language; any new features
are guaranteed to be added to release 3
of the language, not release 2. Of course,
like all releases, Python 3 remains a free
download, which makes it a no-brainer
when decidiing if you can afford to use it.
Q: What does int(g) mean?
A: It tells Python to interpret the user’s
input as a number rather than a letter.
Within programming languages, the number
5 is different than the letter ‘5’.
Q: So what if I had not typed a
number when I was asked for a guess?
What if I’d just entered my name or
something?
A: The code would have crashed with an
error. In fact, Python will complain that the
program crashed with a “ValueError” (more
on these error messages later in the book).
No hay comentarios:
Publicar un comentario