tag: beginner | Python Tutorial

Tag: beginner


What is a boolean?

A boolean is a variable that is either True or False. We say the datatype of a variable can be booelan.

You can think of it like a light switch, its either on or off. Its based on the smallest unit in a computer, a bit. A bit is a value that is either 0 (False) or 1 (True). You can view a bit as synonym of boolean for now.

Related Course:


In Python you do not need to explicitly data types. To use one, assign a boolean value to a variable.

To define a boolean in Python we simply type:

light = False

If you want to set it to on, you would type:

light = True

The value of a variable can be shown like so:


You can also type:

light = 0
which is equivalent to False. Likewise you can write
light = 1

which is the same as True.

Download exercises


Variables in Python can hold text and numbers. A program can have one or more variables. For instance, the program below creates 3 variables:

x = 2
price = 2.5
word = 'Hello'

In the example above, we have three variables: x, price and word.

These are the variable rules:

  • Variables may not contain spaces or special characters (!@#$%^&*).
  • Variable names are on the left and their values on the right
  • Once a variable is assigned, it can be used in other places of the program.

Related Course:

Text in variables

Text variables may be defined in 3 ways:

word = 'Hello'
word = "Hello"
word = '''Hello'''
The type depends on what you prefer.


Once defined variables can be replaced or modified:

x = 2

# increase x by one
x = x + 1

# replace x
x = 5
Python supports the operators +, -, / and * as well as brackets. Variables may be shown on the screen using the print statement.

x = 5

y = 3 * x

# more detailed output
print("x = " + str(x))
print("y = " + str(y))

The first output of the program above is simply the raw value of the variables.

python variables

If you want to print a more detailed message like:
"x = 5", use the line 'print("x = " + str(x))'.

This str() function converts the numeric variable to text.

Download exercises

Global variables

What is the difference between global and local variables?

A global variable can be accessed anywhere in code. A local variable can only be used in a specific code block.

Related course:

global variable example

We say the scope is global, it can be used in any function or location throughout the code.

In the example below we define a global variable, y amount lines of code later we can still use that variable.

x = 3
200 lines of code

Note: In Python a global variable is always limited to the module. Thus, there is no global variable as there is in other programming languages. It’s always within a module.

python global variables scope

local variables

This is contrary to a local variable, which can only be accessed in the local scope.
In the example below, x is a local variable..

def f(x):
.. x can only be used here

If a variable is declared inside a function or loop, it’s a local variable. Global variables are usually defined at the top of the code.

best practice

Global variables are considered a bad practice because functions can have non-obvious behavior. Usually you want to keep functions small, staying within the size of the screen. If variables are declared everywhere throughout the code, it becomes less obvious to what the function does.

Download exercises


A tuple is a collection that cannot be modified.

Variables can be of the datatype tuple. A tuple is defined using parenthesis.

If you want to change the data during program execution, use a list instead of a tuple.

Related Courses

python tuple

A tuple with one item ( a comma is needed in the end ):

x = (1,) 
A tuple with multiple items:
x = (1,2,3,4) 
Note: Even with one element, the comma is needed.

Accessing tuples

To access individual elements, we use square brackets. To print the first element (Python starts counting from zero):

To print the second element:

You can access tuples both in the interpreter and during code execution.

python tuple example

To print the last element, you can count from the back using the minus sign.


Download exercises


A dictionary in Python is a one to one mapping.

Every key points to a value, separated by a colon (:).

A dictionary is defined using curly brackets. The value left of the colon is called the key, the value right of the colon is called the value. Every (key,value) pair is separated by a comma.

Related Courses

python dictionary

The example below creates a dictionary. Keys must be unique values, you can not use the same key twice. Values may or may not be unique.

k = { 'EN':'English', 'FR':'French' }

We defined a dictionary named k and access elements using the square brackets you’ve seen before. We use the key [‘EN’] to print the value ‘English’.

python dictionary key value
Note: A dictionary has no specific order.

python dictionary append

To add a new value to a dictionary you can simply assign a key value pair:

k['DE'] = 'German'

dictionary remove

To remove a key/value pair use the del keyword:

k = { 'EN':'English', 'FR':'French' }

del k['FR']

python dictionary

Download exercises