Getting started with Python

An introduction in the form of a webinar, to allow you to discover the importance of the Python language as a programming tool for your company’s internal needs or for the development of an application. This introduction focuses on presenting the basic concepts needed to get you started, such as the use of the Python interpreter and the syntax elements you need for a first program.

1. Introduction to Python

a. What is Python ?

  • An “interpreted” programming language : instructions are transformed into a machine language and executed as they are read.

  • Open source software (since about 1990).

  • Official website: Python.org https://python.org

  • Python can be used for almost any purpose:

    • System administration,

    • Statistical calculation,

    • Web and mobile applications,

    • Data science (analytics, machine learning, computer vision…)

b. Installer Python

  • Recommended to use version 3 (3.6, 3.7, 3.8).
  • Windows: Download and install the executable.
  • Mac: Install with Homebrew
     $ brew install python3
  • Linux :
    • Debian / Ubuntu
      $ apt-get install python3
    • CentOS
      $ yum install python36u

2. Discover Python with the interpreter

Assuming Python is installed, you can run the interpreter to start working.

 $ python3

Or

 > python.exe

You get the interpreter window, which is also called the “Python Shell”, and can be recognized by its command prompt >>>

 >>>

To exit the interpreter, use the quit() statement or do Ctrl-D (or Ctrl-C depending on the operating system)

 >>> quit()

a. Notes on functions

In the above, quit is an example of what we call a function.

Functions are available in the Python language and are used to do processing and possibly return values.

We will see more examples of functions later on.

b. How to display values

The print function is used.

This function is able to display on the screen (the console of the interpreter) one or more values (or values resulting from operations).

>>> print(‘Hello World’)

To display a number :

>>> print(1)

To display multiple values :

>>> print(‘abc’, ‘de’)

abc de

>>> print(‘abc’, ‘de’, sep=’\n’)

abc

de

c. How to enter values

We use the input function.

Allows you to offer a “value input prompt”.

This allows the user of our program to enter information (data) that will be used within the program.

>>> name = input(“Enter your name : “)

To display this name :

>>> print(“This name is :”, name)

d. How to get help

We use the help function.

For example, to display the documentation for the print function, we pass it to help().

>>> help(print)

3. Basics

Let’s look at some basic concepts: Variables, Code Indentation, Conditions, Loops.

a. Variables

Variables help perform operations.

A variable is declared by giving it a value (the assignment of a value to the variable).

>>> i = 5

>>> a = “Boujour”

The assignment is made using the =, called the assignment operator.

Autres exemples :

>>> a = 213
>>> b = 135
>>> c = a + b
>>> print(c)

b. The indentation of the code

In Python, indenting your code is mandatory: you shift (to the right) with 4 spaces.

There will be syntax errors if the code is badly indented.

>>> a = 2
>>> if a = 2:
>>>         print(‘True’)

c. Conditions (if / else)

Several variants are possible in the syntax, depending on the number of conditions.

Syntax for 1 condition :

if condition:

        Instructions…

Syntax for 1 condition + its opposite :

if condition:

        Instructions…

else:

        Instructions…

Syntax for several alternative conditions

if condition1:

        Instructions…

elif condition2:

        Instructions…

else:

        Instructions…

A little example

d. The loops

There are 2 types of loops : for et while.

For this initiation, we only deal with the first type.

With the “for loop“, we loop on a list or a sequence of elements.

Example showing a loop :

Scroll to top