tag: beginner | Python Tutorial

Tag: beginner

Booleans

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.

Note: A bit is the smallest unit in a computer. Its either 0 or 1, True or False. Also called a boolean

Related Course:

Boolean

To define a boolean in Python we simply type:

light = FALSE

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

light = TRUE

You can also type:

light = 0

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

which is the same as TRUE.

Variables

Variables in Python can hold text and numbers. For example:

x = 2
price = 2.5
word = 'Hello'

The variable names are on the left and the values on the right. Once a variable is assigned, it can be used in other places of the program.

Related Course:

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

Note: Variables may not contain spaces or special characters.

Text in variables

Text variables may be defined in 3 ways:

word = 'Hello'
word = "Hello"
word = '''Hello'''

The type depends on what you prefer.

Operators

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
print(x)

y = 3 * x
print(y)

# 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
...
print(x)

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.

Tuples

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):

print(x[0]) 
To print the second element:
print(x[1]) 

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

print(x[-1]) 

Dictionary

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' }
print(k['EN'])

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']
print(k)

python dictionary
12348