Welcome to the code club!


Code for this session in: https://gitlab.fabcloud.org/barcelonaworkshops/code-club/tree/2020/01_python_basics

According to Codelani’s list, there are about 3,156 computer languages (including dialects of BASIC, esoteric programming languages, and markup languages ).

In Wikipedia you can find a more filtered list by type of language.

For the geeks!

Instructions for the Apollo Guidance Computer https://en.wikipedia.org/wiki/Apollo_Guidance_Computer#Instruction_set

What is python?

Python is an open source and high-level programming language, which means, it is easily understandable and most crucially readable for humans, simultaneously being independent of the platform it is running.

It was designed by this guy (Guido Van Rossum - Monty python fan)

Why python?

List of Python applications:

age = 16
if age >= 18:
    print (“Eligible to Vote”)
    print (“Not Eligible to Vote”)

Python is supported in everywhere. You can even use Python interpreters to run the code on specific platforms and tools. Also, since Python is an interpreted programming language, it allows you to run the same code on multiple platforms without recompilation.

According to the latest TIOBE Programming Community Index, Python is one of the top 3 popular programming languages of 2019.

Its large and robust standard library makes Python score over other programming languages. The standard library allows you to choose from a wide range of modules according to your precise needs. Each module further enables you to add functionality to the Python application without writing additional code.

You can even use several open source Python frameworks, libraries and development tools to curtail development time without increasing development cost.

Where to start?

Python 2 vs 3

Python Environments

A python environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them. This is one of the most important tools that most of the Python developers use.

The official way

Installing packages

A Virtual Environment is a tool to keep the dependencies required by different projects in separate places, by creating virtual Python environments for them. It solves the “Project X depends on version 1.x but, Project Y needs 4.x” dilemma, and keeps your global site-packages directory clean and manageable.

The most popular tools for setting up environments are:

# Create environment with virtualenv
➜ python -m venv environment
# Activate the environment
➜ source ./environment/bin/activate
# List packages
➜ pip list
# Install packages
➜ pip install <package-name>

What’s the best?

It depends on what you do and how you do it. Conda manages environments globally, although it can be slow due to conda forge size. Virtualenvwrapper is very good, and does things globally too. Pip manages globally, but without environment, and pipenv or venv does not install things globally… So it depends!

The official way

Follow this guide. ATM not very well maintained?

Virtualenvwrapper (easy peasy)

The docs.

virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.

To install virtualenvwrapper:

pip install virtualenvwrapper

To create an environment:

mkvirtualenv py3 -p python3

To activate it:

workon py3

To install something in it:

pip install something

Anaconda/Miniconda (heavy but with GUI)

Anaconda is a Python distribution intended for data processing, predictive analysis and scientific programming that simplifies the management of packages. There are other ways to install python and the necessary packages for OpenCv but they are more complex and Anaconda makes it easier for us to work.

Windows users

In this link you can find a nice tutorial to set up your anaconda

There are different versions for different Windows, Mac and Linux operating systems. Download and follow the instructions to install the one that suits you best.

In my case is Ubuntu ( Linux )

You can check that you have installed it by opening a command line and typing the following:

➜ python -V

Use the terminal or an Anaconda Prompt for the following steps.

Create the environment for Python 3.7:

➜ conda create -n yourenvname python=x.x anaconda

And press Yes [y]

To activate your environment:

➜ conda activate nameenv

To deactivate an active environment:

➜ conda deactivate

Manage your environments like a pro

More information about managing environments.

Windows users

Follow this for setting up your PATH

Install a package:

➜ conda install -c conda-forge <name_package>

Working with python

Mainly, one can execute a script in three ways:

➜ python app.py

To check where an application (in UNIX) or program (in Windows) resides in your computer, you can check it in the CL by:

➜ where python

bash, windows and linux users

Instead of where, type in whereis

Then, the very first line of the file should be: #!path/to/python, i.e.: #!/Users/macuser/anaconda2/bin/python. Then, we can make the file executable by (this is called a shebang)

➜ chmod +x app.py

And run it by:

➜ ./app.py

Algorithms and flow charts

Coding control flow structures

While Loop


while (condition) {

    // Do stuff


a = 0
while a < 10:
    a = a + 1  #alternatively a += 1



if (condition) {

    // Do stuff
} else {

    // Do other stuff


a = 1
if a > 5:
    print("This shouldn't happen.")
    print("This should happen.")

For loop


for (# iterations) {
    //Do stuff


for i in range(6):
    print ('Hello')

Boolean Expressions


Hello world

Super basic example:

print ('Hello')
➜ python Example_00.py

A bit more complex, with main structure:

def main():
    print ('Hello')

if __name__ == '__main__':
➜ python Example_01.py

TODAY’s CHALLENGE: Hello world with arguments


Make a python script that you can run either with python script.py or by ./script.py and that prints: Hello <YOUR NAME> <YOUR SURNAME>

➜ python script.py -h
usage: Example_02.py [-h] [--name NAME] [--surname SURNAME]

optional arguments:
  -h, --help            show this help message and exit
  --name NAME, -n NAME  your name
  --surname SURNAME, -s SURNAME  your surname
➜ python script.py -n 'Oscar' -s 'Gonzalez'
Hello Oscar Gonzalez

Where to start?

More info about argparsing here

import argparse

--- Your function here ---

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
    parser.add_argument("--name", "-n", default = "Antonio", help="your name")
    # --- Your argument here ---

    args = parser.parse_args()
    main(args.name, args.surname)