Title: 3
Course: Start programming with Python in 10 steps

Write and run your first programs

Now that you have Python and Jupyter installed and under control, you can use them to start writing and running your first programs. You certainly already know that Python is a programming language, but now it's time to learn some of its words and grammar. In other words, you learn how to use Python to tell the computer what you want it to do for you. This step is a lot like learning the rules of a new game. At this point, you get to know the core rules of Python and how not to fear mistakes.

👉    7 things to know at this step

1. Variables are temporary "boxes" where we keep all the information needed for our program to work. Sometimes you need to create a box before you can put stuff in it (this is called declare a variable) but Python usually creates them at the same time when you put something in them.
2. Stuff that you can put in a variable can be pretty much anything. To start with, we have literals, which is a term that stands for single numbers (integers or floats) and single words (strings of characters). A more complex variable is a list of either numbers or strings, often called an array, but also simply a list. There are other things that can go in there, but they are coming in the next steps.
3. To put something in a variable, you first need to think of a convenient name, something like my_first_var, then you just start a code cell in your notebook and type my_first_var = 1 in it.
4. To see what is in a variable, you need to send its content to an output device (e.g. the output cell of your Jupyter notebook). This is what you do when you print things.
5. The content of variables can be updated and this is very simple to do, you just type my_first_var = 2 (you can do this on a new line in the same cell or start a new cell). When it sees this new assignment, Python will drop the number 1 and the number 2 will be now the only content of your variable. We update variables all the time in programming, but watch out -- things can get out of control really quickly!
6. Flow control is actually where you take control of what the program does and when. If-statements and loops that you saw in the code demo are part of flow control. An important thing about flow control specifically in Python is that indentation is used to show what belongs where.
7. Python executes all your commands and assignments (e.g. printing, variable updates) in the order in which they are written in your code. If it cannot execute something, it shows you an error. When you see an error, you are likely to feel frustrated or even scared. You will probably want to look away and seek help, but it is really important that you read and understand the error. This is often hard, but it is extremely useful, probably one of the key steps in becoming a skilled programmer. Python doesn't have the habit of praising you for doing things right. If there are no errors, all is good!

🧑‍🎓    Learn actively

▶️   Open the notebook that you downloaded in the previous step and try to see if you find any examples of the things you need to know at this step.  How many variables are there in the whole notebook? Any control flow? Try to edit the code again, see if you get some more ideas what makes sense to change and what output you get.

▶️   Get familiar with 4️⃣ by reading points 1 to 3 of the notebook.
❗  Keep in mind that print() is what we call a built-in function -- there are many of them in Python, but we’ll come back to that in the next step.

▶️   Now complete points 4 to 20 to get a better sense of 6️⃣. At one point, you will be asked to refer to the following animated demonstration: python_example_16.pptx. For loops allow you to make your computer repeat the same actions until a certain condition is met, but they aren’t the only type of loops. Here is a video demo by Martin Volk of how while loops work:

📹  While loops by Martin Volk

❗  This video, like most other Martin Volk videos in this guide, uses something called the command line to run Python code. We will see how to do that in Step 5: for now, you only need to know that everything that you see can also be written in the exact same way in your notebook.
❓ Can you spot the differences between the two types of loops? Try turning some of the for loops in the notebook into while loops.
❗  We’ll come back to this in a second, but in case something bad happens, remember you can always make use of the ⏹ button in the upper part of the screen.

▶️   Now, let’s tackle the other part of 6️⃣ by completing points 21 to 25. Unlike loops, if-statements allow you to set conditions to make your computer choose between several alternatives. See it in action in this video:

📹  If statements by Martin Volk

▶️   Complete points 26 to 30 to get started with variables and better understand 1️⃣, 2️⃣, 3️⃣ and 5️⃣. Then, watch the following video demos to see in more details how storing and updating information in variables works:

📹  Storing information in variables by Martin Volk

📹  Storing words in variables by Martin Volk

❗  These videos also use the command line, but with interactive Python, which allows for the Python code to be written directly in the command line. Once again, at this point, the only thing you need to know is that you can use the exact same instructions in your notebook.

📝   Get some more explanations for variables and control flow from the DigiLing materials. These explanations are a little different in style, but time to get used to it.

▶️   A few more basic tricks it’s better to keep in mind when writing Python code: watch the videos below to see how tricky indentation can really be and learn about comments.

📹  Indentation by Martin Volk

❗  Remember when we mentioned the ⏹ button of Jupyter Notebook? If you create an infinite loop by mistake, that’s when it comes in handy.

📹  Commenting the code by Martin Volk

▶️ Finally, read the HolyPython lesson on Error Handling. This will give you an overview and examples of the most common cases of 7️⃣. Then, try to create (on purpose!) a few similar errors in your notebook.
❗  These errors are actually sent to you by the Python interpreter. This is why the line where it says the problem is located is not always accurate: the interpreter lets you know when it stopped understanding your code, but often the mistake occured earlier than that.

📝   The examples are enough to get an idea of how statements and loops work, but some formal definitions might be useful too. It's now time to get to know the Python official documentation, here is a useful page for this step, sections 4.1 to 4.5. You'll certainly get this right, but just in case, what comes after ">>>" is code (to type in a code cell in your notebook) and what comes after "#" are comments (to type in a text cell in your notebook).
❗  For beginners, it's better to look up everything in Python docs. Googling and StackOverflow will be useful only after you become confident enough.

📝   If there is any term or word in this section that you don't understand, hit Wikipedia.

❓  Create a new notebook and try a few things

1. Print the same sentence 24 times (you decide which one)

2. Print all numbers (integers) up to 24
3. Print all even numbers up to 24  (hint: you need to update a variable)
4. Print all the numbers up to 24 that can be divided by 5

5. Print all the numbers between 16 and 24
6. Print three times each number up to 24

Each item is your own little program! It might take some time to get these right. It is important to take this time, think, try out your ideas step-by-step. Go back to the readings and examples as many times as needed until things are perfectly logical and clear.

Remember: