If you are reading this book, you may be interested in programming for the first time. Maybe it isnt your first time trying to learn to program either? This book is very different than other books that claim to teach you programming. It eliminates a large part of Python that you dont need.
Even books that have learn in the title introduce readers to hopelessly complex topics like object-oriented programming or concurrency. It turns out YAGNI (You Aint Gonna Need It). Why teach students subjects they wont use either ever, or not for a few years?
Dont worry if you failed before; you may have read a book written by an expert for an expert. Lets fix that and teach you just what you need to get started in a career in Data Science, Data Engineering, Software Engineering, or Cloud Computing.
Chapter 1: Learn to execute commands in Python
Noah Gift
Execute Commands in Colab Notebook
Another common failure point in learning Python is that the installation may be tricky. One of the ways around this is to use a hosted environment to learn Python. An excellent environment for learning is Google Colab
The examples in this chapter can run from this Google Colab notebook.
The first thing to be aware of in starting to program is to learn about executing statements. You can run commands by typing them in. Here is a print
function.
print
(
"hi"
)
The output is:
hi
The big takeaway is your already a programmer. If you can type commands in, you solved 80% of the challenge of writing code. Even further, you can program like this for the REST OF YOUR LIFE! If you are a data scientist, you may never type more than a statement.
Ok, now that you passed that hurdle and youre a programmer, lets step it up a bit and use Python as a calculator. Here is a simple 1+1
example.
1
+
1
The output is
Here is your first challenge of the book. A challenge ensures you have grasped each concept.
Challenge: Do another calculation using addition or subtraction
Ok, so what did you try? Did it work? If not, no big deal. Mistakes are a necessary part of being a programmer. The sooner you accept that mistakes are a great thing, the quicker you learn.
In regular life, you dont normally encounter things that are in a continuous state of brokenness. In programming, everything is in a state of brokenness. The secret to getting good very quick is to accept this and make it create a smile on your face. Mistakes mean you are learning quickly. The more mistakes you make, the more you learn.
Write procedural code
Procedural Statements
Procedural statements are literal statements that can be issued one line at a time. Below are the types of procedural statements. These statements can run in:
* Jupyter Notebook
* IPython shell
* Python interpreter
* Python scripts
three_type_of_energy
=
[
"protein"
,
"carbohydrates"
,
"fat"
]
Multiple procedural statements
You can also chain multiple procedural statements together by executing them one by one. In writing a script or doing data science this is very common. You can think of it like to-do list you write first thing in the morning.
protein
,
carbohydrate
,
fat
=
three_type_of_energy
print
(
f
"{carbohydrate} sure taste good"
)
print
(
f
"{fat} isn't bad for you anymore?"
)
carbohydrates sure taste good fat isn't bad for you anymore?
Adding Numbers
Python also works identical to the way a desktop calculator works. If you type in numbers and add them together you get a sum. The only difference in this example is that the numbers are assigned variables.
protein
=
4
fat
=
9
carbohydrate
=
4
carbohydrate
+
protein
Adding Phrases
Python can also add together strings by using the + operator. This is similar to putting words on post-its then assembling them into funny phrases. If you have word magnets on your fridge and mix them together to create a sentence than you are essentially doing the same thing.
"a carbohydrate "
+
"has "
+
str
(
carbohydrate
)
+
" calories"
'a carbohydrate has 4 calories'
Complex statements
More complex statements use loops. This is a for loop. The main reason for using a loop is to do something to a group of items. In this example there are several items in the three_type_of_energy
list.
What is the analogy for the real world? Imagine a shopping list. You walk into a store and loop to each aisle of the store and pick up each item in the list. This is a for loop in the real world.
for
energy_type
in
three_type_of_energy
:
print
(
energy_type
)
protein carbohydrates fat
Use simple expressions and variables
There are other expressions that are helpful in programming. Here are some examples.
assert
In the real-world we use the concept of assert
often. You may ask which gas station has the lowest prices from a friend. To double check you may go to their website and assert
whether your friend is correct. You can do the same thing in Python. This example asks Python to assert whether the variable carbohydrate
is equal to 9
.
assert
carbohydrate
==
9
------------------------------------------------------------------- AssertionError Traceback (most recent call last) in () ----> 1 assert carbohydrate == 9 2 AssertionError:
Python throws an AssertionError
which allows a programmer to later catch this exception and do somethign with it. Perhaps this could be part of a test suite. Many popular test frameworks use assert
to test statements or modules.
Notice this statement works.
assert
carbohydrate
==
4
pass
Another handy tool is pass
. It can be used to fill in a statement and do nothing. The following statement shows that