Part 2: Beginning programming

This section will cover some necessary basics, before looking at what was used to make the “Hello World!” programme from the last section and finishing off with getting input from a keyboard.

Mathematical operations

Like all programming languages Python supports both integer and decimal numbers.

Note: A full stop (.) is used as the decimal mark (separator between the integer and fractional parts of a number) in any decimal numbers.

10
5.39865

It provides the standard mathematical operations with the normal mathematical order of evaluation (calculations in brackets are done before any other calculations, multiplication takes priority over addition, so 1+2*4 is 1+(2*4) and not (1+2)*4).
Open the Python interpreter and execute (excluding >>>):

>>> 1+3

It will output the result of the calculation on the next line (4).
To experiment try:

>>> (1+3/5)*6-5

resulting in:

4.600000000000001

Python has applied the ordinary mathematical evaluation rules to the calculation, using brackets to clarify, you could have executed:

>>> ((1+(3/5))*6)-5

with exactly the same result.
The (standard) mathematical operators in Python are as follows:

Mathematical operation Operator
Addition +
Subtraction
Multiplication *
Division /
Modulus (remainder) %
Brackets ( )

The modulus operator determines the remainder created by dividing a number into a specific number (the line without >>> is the result of the code/command):

>>> 3%2
1

The above results in one, as one is the remainder of 3/2.
To get a feel for how the operations work in Python, try doing some calculations yourself

Strings (of text)

A string is the term for a length of text that is used in a programme. To make a piece of text a string, you enclose it in speech marks (you can use quotes instead if you prefer), demonstrated in the Python interpreter.

>>> "a string"
'a string'

Strings can be ‘added’ (concatenated) together:

>>> "a string" + 'another string'
'a stringanother string'

There are no spaces inserted when concatenating strings, you have to place the space there yourself:

>>> "a string" + " " + "another string"
'a string another string'

Adjacent strings (directly entered as strings), e.g.

"string 1" "another string"

are automatically concatenated (creating a single string):

'string 1another string'

Variables

A variable is a named entity that contains something that can be retrieved by using its name. That something could be a piece of text (like this sentence), a number (5), or another object (covered later).
To create a variable, type the variable’s name, followed by = and then what you wish the variable to contain.

>>> something = 5
>>> str = "Invincitech.com"

You can try this in the Python interpreter.
Variable names can only be made up of the characters A-Z, a-z, 0-9 and _ (upper and lower case letters, digits and the underscore).
To retrieve what was stored, enter the variable’s name.

>>> something
5
>>> str
'Invincitech.com'

The assigned data (contained thing) can be a calculation, which will be evaluated and the result stored in the variable.

>>> calc = (1+3/5)*6-5
>>> calc
4.600000000000001

You can use variable to create a new variable, or you can make use of one in a calculation or operation, and the result of that can create a new variable:

>>> calc + 5
9.600000000000001
>>> calc2 = calc
>>> calc2
4.600000000000001
>>> times5 = calc * 5
>>> times5
23.000000000000007
>>> str + " - Are you ready for the challenge?"
'Invincitech.com - Are you ready for the challenge'
>>> full_url = "www."+str
>>> full_url
'www.Invincitech.com'

Variables in Python have no type (it is the value that has a type instead – it uses a dynamic type system wherein the type of a variable’s content can change). For Python this means that you can assign any value to a variable, regardless of what it already contains:

>>> str = 25%5
>>> str
>>> times5 = "times five"
>>> times5
'times five'

Functions

A function is effectively a named piece of code, that can be run by calling (naming the function followed by ()) it. It can take inputs, and it can output a value.
Example (hypothetical function call):

do()

In the above ‘do’ is the function name (text before the first bracket).
Other functions take inputs (arguments) – if there is more than one, they are separated by commas – their arguments are given between the brackets, e.g. for a hypothetical function add() that adds two numbers (shown in the interpreter).

>>> add(5,6)
11

11 is the output (add outputs/returns a value based on its inputs).
Instead of directly typing numbers or strings as the arguments, you can use a variable as a argument (shown using the len() function which returns the number of characters, letters, numbers or symbols, in a string):

to_count = "my reasonably long string that will be provided to len()"
res = len(to_count)
print(res)

The return value of len is stored in res, and res is then printed using the print function (to try it, save it as a file called counting.py, and then run it as a Python script).

Printing text

The ‘Hello World!’ programme consists of one line:

print("Hello World!")

‘print’ is a function provided by Python for the use of programmers, it outputs its inputs to your terminal. It can take a string or a number (it takes a string in the “Hello World!” programme).
You can provide it with any suitable argument (see Functions). To print several strings on the same line, concatenate them (resulting in print printing the resulting string):

print(str1 + " something more " + str3 + str4)

To concatenate a number to a string so that you can print it on the same line as a string, use the str() function to convert the number to a string. The number will be the argument to the str function and str will return a string created from its argument. Concatenate the result to the string. As a result print will receive the resulting string and print it:

print("The number five as a digit is " + str(5))

Resulting in:

The number five as digit is 5

str(5) is inserted directly into the concatenation, as when a function returns something you can use its result directly, without naming it, just by placing the function call in the place where the result will be used.

Input

Acquiring input from the user of a programme is a necessary part of almost every useful programme, in Python you use the function input(). It returns a string of text that has been entered by the user of the programme.
When using input(), note that the text typed is only given to the programme for use when the user presses [ENTER] (mechanisms for detecting the pressing of individual keys without the need to press [ENTER] are beyond the scope of this tutorial).
For example, a programme that asks for your name, and then greets you:

print("Please enter your name, followed by [ENTER]:")
name = input()
print("Hello "+name+".")

A sample run would be (save the code as a script and then run it):

Please enter your name, followed by [ENTER]:
Matthew
Hello Matthew.

Note: The second line in the run above is the text entered by the user. The input is shown for the sole purpose of easing comprehension of the programme’s behaviour.

As a script

All the code shown here (and all Python code) can be used in a script (excluding any >>> starting any lines). The same code can also be run in the interpreter (entered line by line), with the disadvantage that you lose anything you did when you close the interpreter.
As an exercise, create a script (file with the .py extension containing Python code) that calculates 88/(8*9/(3*3))+5, saves it in a variable (called result) and then prints “The result is: “, followed by what result is, using only one line to output everything on.

Next section:

Part 3: Control Flow
Choosing a path to follow through the code based on inputs provided and loops.