CODE WITH MARTIN

3. Python Overview


Code File Structure

Before we get in to writing more code, lets explore the code file we wrote and how Python executes the code. Here it is:

import os
os.system('cls')
print("Hello World")

In the 3 lines of code we have above, each line is called a 'Statement'. There are 3 statements in this code file, each separated by a new line. Each statement is a task that Python executes, making your computer do work.

When you run your program, Python starts at the top of the file on line 1 and executes the statement on that line. It then moves on to line 2, executes the statement on line 2, and moves on to line 3 and finally executes the last statement on line 3.

Python will keep moving down the lines of a code file until there are no more statements. When it runs out of statements, your program ends.

Python allows you to put any number of empty lines in between statements. It doesn't treat them as statements and simply skips over them. For example, if our code file looked like this:

import os

os.system('cls')

print("Hello World")

There is no difference to how this program runs at all compared with the code file having 3 lines.

Using empty lines in your code can help make it more readable.

It is possible to have multiple statements on one line. We would use a semi-colon character ';' to separate the statements. For example, our program could all be placed on a single line, like so:

import os;os.system('cls');print("Hello World")

You can try this yourself and see that it works perfectly. Having multiple statements on one line will cause Python to start with the statement on the left and execute the statements as it reads them going right.

It's not very common to use this style of writing code and can harm how easy it is to read Python, but it's something to be aware of. You might one day read some Python code that looks like this, which would look confusing if you didn't understand what the semi-colon character is doing.

Comments

An extremely useful feature of Python is writing comments in your code file. Comments are not statements in any way and will simply cause Python to ignore them. They have no impact on the behaviour or performance of your program.

To write a comment, you simply start a line with the character '#'. The whole line is then considered to be a comment. Here's an example of some comments that we could add in our 3 line program:

# This code file was written by Martin Blore. It simply prints "Hello World" in the terminal.
import os

# The next line is a statement that clears the terminal.
os.system('cls')

# The next line outputs text to the terminal.
print("Hello World")

Try adding some comments yourself and see how your program behaves exactly the same when you run it.

If you ever need to stop a statement from running and you're not sure you want to remove the code, you can simply place a '#' at the start of the line to make it a comment. Like this:

# This code file was written by Martin Blore. It simply prints "Hello World" in the terminal.
import os

# The next line is a statement that clears the terminal.
os.system('cls')

# The next line outputs text to the terminal.
#print("Hello World")

Comments are extremely useful to use when writing code and I highly encourage you to use them as often and freely as you like.

When working in software teams, where multiple developers might work on code that you wrote, comments can really help people understand the what and why the code is doing what it does without reading through every single statement.

There is sometimes an occurrence of comments being pointless if all they do is explain the obvious. For example, in the code above, line 7 is a comment that explains what the print statement is doing on line 8. Its becomes extremely obvious to developers what the print statement does after a short while programming with Python so we don't really need that comment.

What We Learned

Let's recap what we've learned.