Variables have a certain reach within a program. A global variable can be used anywhere in a program, but a local variable is known only in a certain area (function, loop)

Sometimes the word scope is used in projects: “its outside the scope of the project”, meaning not included. Likewise, a variable can be outside the scope of a function.

Related course: Complete Python Bootcamp: Go from zero to hero in Python


In the program below, balance is a global variable. It can be used anywhere in the code. But the variable x can only be used inside addAmount.

Visually that’s:


We have two variables: balance, a global variable that can be used anywhere. x, that can only be used inside the function (its not known outside, local scope).

Study drill

Try the exercises below:


Time and date

Python can get the system time using the module time.
To use this module type:

Time is not part of the standard library.


In the example below we output the day,month and year followed by the current time.

The first line returns all variables required (year,month,day,hour,minute).

The function time.time() returns ticks. Ticks are system ticks every computer holds.

As humans we don’t read system ticks, this needs to be converted to actual human time.
The function localtime() converts these ticks into the actual human readable values.

Convert with:

Type the program shown below and run it:

Study drill

Try the exercises below

After completing these continue with the next exercise.