Cours Python V3 en-GB
Cours Python V3 en-GB
Introduction to programming
Python language
The word "algorithm" comes from the name of the 9ème century Uzbek mathematician Al
Khwarizmi.
An algorithm consists of performing a complex task by breaking it down into a succession of
simple tasks: An algorithm is a finite sequence of instructions indicating the precise order in
which a set of operations must be performed to obtain the solution to a given problem.
For each problem, there may of course be several different algorithms that give the solution.
To carry out a task with a computer, it is expressed in the form of an algorithm, using
elementary operations that it knows: a computer program is the translation of an algorithm
into a well-defined language.
The choice of an algorithm is then made according to the simplicity of the operations, the
ease of programming, the number of operations necessary to arrive at the result, the space
occupied in the computer's memory, the time necessary to obtain the result, etc.
These criteria are sometimes difficult to reconcile.
Python is a very popular programming language created in 1991. Its main advantage is a
simple syntax that allows even beginners to write complex code without having to worry
too much about the syntax.
It is an interpreted language, which means that lines of code are read and executed one
after the other when processing a program. (no compilation unlike Java or Turbo Pascal)
Python is used in a wide range of applications (science, of course, but also game creation)
and in all countries, at school or university level.
I started by using PyScripter, installed on my computer, for the corrections of the first
exercises, which explains the slight difference between the display of my first results and
those obtained with Python. But I had difficulties installing this software on my second
computer, so I preferred to go back to the basics.
Launch the IDLE program: the Shell appears Click on New File, the editor appears
You can also, without installing the software on your computer, test your programs directly
online: https://www.onlinegdb.com/online_python_compiler or
https://www.codabrainy.com/python-compiler/ and retrieve your work by email.
All examples in this introduction will be handled with version 3.8.3.
Example:
I am looking to hang a frame on the wall.
1st step: I determine the nature of the wall (concrete, plaster...), the place of the frame, the
tools and objects needed (dowel, drill, meter....). Now that the problem is analysed, I can
move on to the execution.
2nd step: in Bricolo-Python, my algorithm would be written as follows;
program hang_frame ; (the name of the program, to be given before execution)
tools
stepladder, pencil, mechebeton, meter, drill ;
supplies
peg, hook ;
var (Define variables of type int, float, list...)
height = 1.7 (float variable)
start of "operations
measure the height of the hole
mark location with pencil
plug in drill ;
if height>1m70 :
use stepladder (indentation: 4 indents)
drill wall
place ankle
screw on hook
place frame
Run ;
Remarks :
This example assumes, of course, that you do not live under a bridge, but statistically
this is highly unlikely in the world of ESM students.
More seriously, writing a program uses the same approach and is no more
complicated once you have mastered the syntax of the language.
The main feature of Python is the use of indentation (automatic indentation of text, or
shifting to the right), whereas other languages require the use of 'begin' and 'end' keywords
to delimit blocks. This reduces the risk of syntactic errors in Python.
A simple example:
for i in range(6):
Indentation
a = 2 + 3j
The third type of numerical variable is the "complex":
b = 5 - 4j
Booleans: these are logical variables that only take the values "True" and "False".
Indispensable for tests.
Lists. Less useful in Mathematics, but very practical for a "clean" presentation of
results. Letters and numbers are allowed in a list.
Exercise 1:
Analyse the script :
As we will see later, the "math" library, which needs to be called often in our mathematical
programs, is not necessary here for the recognition of these 6 operations.
Example of use: Python console (or Shell) used as a calculator.
Note:
Division always gives a float result, but you can "force" an integer result with "//".
Exercise 2:
Determine the GCD of 654 and 780 (Use the Python console)
2). Assignments
The main assignment is the "=" symbol
Example: x=5; y=3 means that 5 is placed in the x memory box, and 3 in y. You should never
write "5=x".
Exercise 4:
Example 2:
In each example, the entered term needs to be converted to a number (float or int).
2). The simple "for" loop (loop = loop)
The program will return: 7, 14, 21, 28, 35. When i reaches the value 6, the loop is exited. (No
calculation is performed with 6, only the loop exit)
Note that there is no need to define "i" before using it.
The full syntax is: range(start value, end value, step).
You can increment or decrement (decrease) in a loop.
Another example, which gives exactly the same result: 7, 14, 21, 28, 35.
As soon as "i" reaches the value 0, the loop is exited without calculation. The decrement is: -
1.
Exercise 6:
Write a script to determine the divisors of an integer.
Exercise 7:
It is not necessary to know precisely the length of the list to implement the loop. The small
program above will write one after the other the values 12, then 15...
Exercise 8:
Write a program that determines how many times an integer is divisible by 2.
Exercise 9:
Write a program that calculates how many years are needed to double a capital invested
with compound interest at a rate of t %. (input variable: t of type float)
Exercise 10:
Write a program that determines the GCD of two given integers.
X). The functions
Functions are a script that is assigned the task of returning a particular value.
A function is defined using the keyword "def", followed by the name of the function with
the list of arguments or variables in brackets. The two dots are still essential. Be careful of
Example 2:
In Python, the predefined "math" function gcd (a,b) gives the PGCD of two integers a and b.
No function gives the PPCM.
We will therefore use the following mathematical property: PGCD(a,b)xPPCM(a,b)=axb.