Python Coding Book Ch2
Python Coding Book Ch2
Jump to Chapter 1
Why Python?
What matters is that you
learn Programming and not a Programming
Language. This distinction is a subtle but
important one. Programming is the way of
thinking that’s required. It’s the process of
breaking down a problem into logical steps and
then systemically putting them in the right order.
To write code, you need a programming
language. In this book, we are using Python to
introduce you to the coding concepts. But all the
general coding concepts are common across all
programming languages, even if the methods
and tools may vary.
Benefits Of Learning To Code in Python
Unless you have a particular reason to learn
some other programming language, then
Python is the best language to use. Here are
some reasons for this:
It is one of the most popular languages in use
today. So you’ll be learning a very useful
language.
It is a broad language, with applications across
a wide range of uses, from science and finance
to web development and many others along the
way. Other languages can be more narrow in
their scope and therefore only applicable within
a small range of uses.
It is a relatively easy language to learn and
use. I cannot stress enough how important this
point is when learning how to code. As a
beginner and intermediate learner, your main
focus is on learning the fundamental coding
concepts and understanding them thoroughly. A
language like Python allows you to focus on
these key concepts and not on the subtleties
and quirks of the language itself. In Python, it is
possible to explain every single line of code,
even at the very early stages of learning. This is
not possible with many other languages. You’ll
have to accept the “write this code and you’ll
understand it later” concept when learning
other programming languages that are not as
friendly as Python is, and this is not ideal for
beginners.
There are excellent resources online and
superb support from the Python community.
How To Use This Book To Learn To Code
If you’re new to programming, my advice is to
start from the beginning and work your way
through every section in order. Do not jump
across chapters. This book presents the material
in an order carefully planned to make your
journey to learn to code as smooth and efficient
as possible.
The Fundamentals
There are two parts to this book. Chapters 1 to 5
cover the fundamentals of programming. These
are topics you will need whatever you want to
use coding for. There is no way of escaping these
topics. You’ll read about the Climbing a
Tree analogy for learning how to code later on in
the book—these chapters are the main tree
trunk! You have to climb the main trunk before
you can start to climb the smaller branches.
Chapter 1 will guide you from the very
beginning. This chapter is centred around your
first coding project. This project will introduce
several of the key topics in programming and
you’ll get a good understanding of how a
computer program is built, step by step. As you
go through this chapter, we’ll take a few short
detours to introduce these topics.
Chapters 2 to 4 will continue to build on the
fundamentals of coding, and Chapter 5 focuses
on dealing with errors and bugs when coding.
Quantitative Programming
Chapters 6 to 11 form the second part of this
book. They deal with the tools needed for
programming for quantitative applications, such
as science, finance or other data-driven fields. If
you’re interested in these topics, then you’ll find
these chapters interesting and essential. But if
your aims to learn to code are different, then
you can ignore these chapters. You can always
come back to them later.
Object-Oriented Programming and More
Functions
There are two chapters in Part 2 that could also
have fit in the first part. These are Chapters 6
and 7 which expand on functions and introduce
Object-Oriented Programming. These are not
must-know topics when you’re getting started
with coding. However, you’ll soon find you’ll need
these as you work on more complex projects.
Both of these topics, functions and Object-
Oriented Programming, are important parts of
modern computer programming. You’ll need to
know about Object-Oriented Programming as
you move to more intermediate areas of coding.
There is also a bonus chapter: The White Room.
This is the name of an analogy I use throughout
this book to help explain what’s really
happening inside a computer program which
will help you understand how best to write
computer programs too.
Working Through The Examples In The Book |
Practise, Practise, Practise
All the coding concepts are presented through
examples. Some are short programs, others are
slightly longer projects.
This book is not a novel. The only way to read
this book (or any book that teaches coding) is
to write the code and experiment with it as
you go along. Yes, you can copy the code and
run it, but you can also go off-piste and try
things out, experiment, do things your own way.
This is a very important part of the learning
process with programming and it’s the only way
you’ll build your confidence with coding. You can
try to have a go at writing the code before you
read the sections of the book that will go
through the code.
The more mistakes you make, the better it is!
Before You Start | Downloading Python And An
IDE
If this is your first attempt to learn how to code,
you’ll need to set up your computer. You’ll need
to download the Python language and an editor
with which to code.
Downloading And Installing Python
Getting the Python language is the easy part. Go
to python.org and choose the ‘Downloads’ tab.
You will see a button to download the latest
version of Python for your operating system.
You can download and install this on your
computer. If you’re presented with any options
during installation, I would recommend you
choose the default options for now.
You now have Python installed on your
computer. But how can you use it? This is the
part where it can get confusing as there is too
much choice available. There is no best option
here. There are many options because it
depends on preference and, in part, on what you
want to use coding for.
In the same way that you need a word processor
to write in English (or any other language), you
will need an editor to write in Python. Some of
these are very basic text editors, requiring you
to run your code via the command line. If you’re
new to programming, I would recommend
avoiding this option.
Instead, you’re probably better off choosing a
more complete editor. These editors are often
called Integrated Development Environments, or
IDEs for short.
Downloading And Installing PyCharm
If you’ve done your research already and have a
preferred one, then go ahead and use it. You can
follow my recommendation if you don’t yet have
a preference and you’d rather spend your time
learning how to code instead of researching
IDEs: Use PyCharm Community Edition. You
can download PyCharm here. Choose the free
Community edition and not the Professional
one. The Professional edition is not free, and you
won’t need any extra features it offers for the
time being. Once again, the easiest option is to
go along with default options during installation.
Opening Your First Python File
Once you’ve installed PyCharm, you can create
a New Project from the File menu. A project is a
folder that contains all the code that belongs to
the same, er, project! My advice is to have a
single project while you’re learning and not
create a new project for each program you write.
Think of this as your ‘learning project’ if you like.
Later on, you’ll know when it’s time to start a
new project for something new you’ll start
working on.
Once you’re in the project, Choose New… from
the File menu and then choose Python File from
the menu that pops up (not the option that just
says File.) You may wonder why an IDE like
PyCharm designed for coding in Python, as
the Py- prefix in its name shows, also gives you
options for other file types. The reason is
the I in IDE, which stands for integrated. These
editors allow you to work on all the files you’ll
need in a project. For the time being, these will
all be Python files, but you’ll need other file
types as well in your project in the future.
Once you’ve created a new Python file — note
that Python files will have the extension .py —
you should have a blank screen in front of you.
You’re ready to start to learn to code.
In Chapter 1, you’ll start writing your first
program right away, and I’ll guide you on how to
run your program and view the output once
you’re ready to run your first lines of code.
Next Chapter
Buy Paperback or EBook
Browse Zeroth Edition
say my name
These simple phrases are easy for us humans to
understand and respond to. Two important
aspects make this possible.
Firstly, we recognise the word say as the
command we are asked to do. Secondly, as
you’ve seen above, we understand the context of
the phrase, which is why we would react
differently to the two phrases.
In the first one, we separate the three words
into the action say and the information we are
being asked to say: Happy Birthday.
A computer needs to know what the command
is. In Python, we would use the built-in
function print() for the example above. This is
one of the basic Python words, and it is followed
by parentheses (). The parentheses, or brackets,
and the fact that print is written in lowercase
indicate that this is a function.
Think of a function as the equivalent of a verb. It
represents an action that needs to be done. The
context is not enough, as it is for us humans
reading say Happy Birthday.
Now let us look at the difference between say
Happy Birthday and say my name. The structure
of these phrases is very similar but, unless
someone is being deliberately funny, the answer
to the second instruction will be Stephen, and
not the words my name. Again, we humans
understand the context easily, but a computer
needs help to distinguish between the two.
In Python, you can represent the first phrase
above with the line of code:
print("Happy Birthday")
You can write the second pair of phrases as:
my_name = "Stephen"
print(my_name)
The speech marks tell the computer that
whatever is inside them is just a string of
characters that has no meaning for the
computer but that a human will be able to
understand. We call this type of information
a string in programming, short for a string of
characters.
In the second example, you’re first asking
Python to create something you
called my_name and to store the
string "Stephen" within it. Next, you ask the
computer to print out whatever is stored
in my_name. This storage box you
called my_name is what’s referred to as
a variable in Python. You’ll find out more
about assigning data to variable names in the
following sections.
Python will respond in a different way
to print(my_name) compared
to print("my_name").
To write programs that a computer can
understand, you need to think in a similar way to
how the computer operates. It’s helpful to think
of the computer as a stupid device that requires
you to be very clear in what you say, without
taking anything for granted.
Running Your First Python Program
There are many ways to run a Python program.
If you already have run Python programs before,
you can move straight on to the next section.
In the Preface, you downloaded and
installed PyCharm Community Edition, the IDE I
recommend, unless you already have a
favourite.
Once you’ve opened a project, you can open a
new Python File, and you’re ready to start writing
code. Make sure you choose the Python
File option and not just the first option in the list
that says File. You should see the
extension .py added to the filename you choose.
This is displayed in the name of the tab at the
top of the editor window.
When you want to run your program, you can
choose Run… from the Run menu and then select
the name of your file from the list that comes
up. When you’ve run the program once, you can
simply click the play button (green arrow)
whenever you need to run the program. You
may also want to learn the keyboard shortcut to
run your program, as you’ll need to run your
programs often while coding.
The output of your code will show in the bottom
window on your PyCharm screen. If there’s an
error in your code, then instead of the output,
you’ll see error messages in red. You’ll read
more about how to deal with errors later on in
this book.
Your First Project: The Angry Goblin Hunt
The fundamentals of programming are pretty
much the same whether you are writing a game
or analysing scientific data. In this book, you’ll
find a diverse set of projects and applications
which will give you a broad understanding of the
tools and concepts in programming and how to
use them effectively.
It’s time to start working on your first proper
Python program: The Angry Goblin Hunt game.
In this project, you’ll learn about some of the
fundamental concepts and tools in
programming and how to put things together to
make up a fully functioning program. This first
project aims to introduce several key topics, and
we’ll take a few detours along the way to discuss
these new topics as we write this program
together.
The game is simple. However, the programming
needed to write the game will set the scene for
every program you will write in the future.
The best way to introduce the game is to show
you the output from the program:
Welcome to the Angry Goblin Hunt
An award-winning game full of adventure and
excitement (!)
Next Chapter
Buy Paperback or EBook
Browse Zeroth Editio