First look

An important part of learning any programming languge is to be able to read it. At the beginning, looking at a piece of code will be mostly overwhelming. The following piece of code calculates if a number is a prime number or not (right now it does not matter at all if You don’t understand how it works):

Elements of Python 3

Some example code

► Click on ‘Run’ to run the code and see its output in the area below the buttons. If You want to test a different number, You can change the value 4441 in line 13 to any other positive whole number (= no decimals like e.g. 4441.5).

Though only 14 lines long, this code includes a lot of different elements of the Python programming language. Those elements can be split into 2 groups:

  • Pre-defined elements, which are built into the programming language and must be written exactly the way they are defined (Python is case-sensitive, thus true is not the same as True):

    • Keywords (also known as reserved words): import, def, True, for, in, if, break, return

    • Built-in functions: int(), range(), print(), format()

    • Built in data types: str (the text in single-quotes '), int (the numbers)

    • A module from the built in library: math

    • Delimiters: (, ), :, =, ,, ., [, ], '

    • Operators: +, %, ==

    • Syntax: the ‘grammar’ of the programming language

  • Elements defined by the programmer, which can be written the way how they suit the purpose of the program (following a few rules):

    • Identifiers: whole_number, is_prime_number, start_value, stop_value, i

    • Literals: 4441, '{} is{} a prime number', ' not'

    • A comment: # Change the value of this number

    • A function definition: def is_prime_number(whole_number):

    • A for-loop: for i in range(start_value, stop_value + 1):

    • A condition: if whole_number % i == 0:

    • An expression within the condition: whole_number % i == 0

Hello, World!

Let’s start with a much simpler (and tradional) program.

Hello, World!

Its code consists of one statement using the built-in function print() to output the string literal 'Hello, World!'. String is the programmers word for text (think string = chain of characters) and literal means that it is directly written in the code. Notice that the text is delimited by single-quotes '.

Since we are going to use the built-in print() function quite often, let’s look at a some details.

There is some freedom here to add extra spaces or round brackets (), but that should only be used if it improves readability.

Variations that produce exactly the same output

The text after the hash character # until the end of the line is a comment and will be ignored. Comments are used to add information e.g. about the purpose of the code that cannot easily be concluded by reading the statements.

Variations that throw an error :^(

Each of the statements above throw an error (and stop the program).
► Try to correct all of them, one statment after the other, and watch the error messages. As You can see, the automatic coloring of the code is failing (although not completely), which helps to spot syntax errors.

Variations that showcase some string trickery

It’s recommended to pick one quote variant (single or double) and use it consistently. Switching to the other variants is ok if it improves the readability by e.g. avoiding escape characters.