Computing 1
Algorithm and Program
2024-2025
"Main Objective"
How to write a program to solve a
problem
PROGRAM : ?
PROGRAM: Algorithm
PROGRAM: Definition.
Programming (Coding ) Methodology
Problem
Analysis Algorithm Program
Execution
PROGRAMMING LANGUAGE
VOCABULARY + GRAMATICAL RULES
(Set of reserved words) (grammar + syntax)
Exp: Display instruction:
Python C++ Pascal
print("Hello World") cout << "Hello World" ; write("Hello World");
Reserved word: print Reserved word: cout Reserved word: write
Rule: print is written Rule: the semicolon (;) is Rule: the semicolon (;) is
in lowercase letters. mandatory. mandatory.
"PROGRAMMING LANGUAGE: Python."
• Rank #1 since 2017 (IEEE Spectrum statistics 2021)
• Easy to learn
• Cross-platform (Windows, Linux, iOS, Android, etc.)
• Multidisciplinary (Web applications, Mobile applications,
Scientific computing, Artificial intelligence, etc.)
• Large community (many programmers)
•…
PYTHON
Developed by Guido van Rossum
at CWI, University of Amsterdam
and named after Monty Python’s Flying
Circus (a British television series)
PYTHON : Vocabulary
Reserved word
and del from not while
as elif global or with
assert else if pass yield
break except import print async
class True in raise await
continue finally is return nonlocal
def for lambda try False
Thonny: Integrated Development Environment for
Python
• To Program/Develop with Python, several IDEs exist, and more
particularly, those intended for PCs.
• The IDEs integrate a Text editor , and all the other tools necessary for
easy and intuitive development.
• The Thonny a IDE, which is a free software, is chosen to implement
all the examples of the programs/scripts given in this course.
Note
• For Android devices (mobile), we can use
other IDEs such as Pydroid.
• Pydroid 3 is a popular choice for Python
development on Android due to its user-
friendly interface and support for various
libraries.".
Thonny:
1. Menu bar
2. Toolbar
3. Program (script)
writing area
4. Console (View menu /
Console)
5. Variable inspection
area (View menu /
Variables)
Modes of executing a python code
With Thonny
1. We execute a Python script, which is the source code of a set of
instructions saved in the form of a source file with the .py
extension. (we will see it later.)
Modes of executing a python code
With Thonny
2. We use the Console/Shell to execute commands (instructions)
typed by the user); same use as the scientific calculator.“
The python console (shell):
In the shell we find the prompt represented graphically by the symbol >>>
• An instruction is validated by the Enter key ↵, we can passed more than
one instruction in the same line by separating them with semicolon";".
• You can select/copy from a command already executed, but you cannot
modify it.
• An executed command can be recalled using the directional keys ↑and↓,
modified if necessary and validated by pressing the Enter key ↵.
• You can write a single command in several lines using the backslash
operator (\).
• To clear the Shell window use the command CTL+L or click with the right
button and press clear.
Use of the console :
The console is used in interactive mode, i.e.:
question (command) → response.
Exemple :
-Command
-Answer (Response)
Console (shell), type the new command here
Exercise :
Operation Result in the console Role
<<< 9 + 2 11 Addition
<<< 9 - 2 7 Subtraction
<<< 9 / 2 4.5 Division
<<< 9 // 2 4 quotient
<<< 9 % 2 1 Modulus
<<< 9 ** 2 81 power
<<< 9 == 2 Equality (logical expression).
False
<<< 9 != 2 True different
<<< 9 > 2 False Less-than
Operator Priority
Operator Precedence (from highest to
lowest):
1- Parentheses (()): Highest priority;
expressions within parentheses are
evaluated first.
2- Exponentiation (**): Evaluates powers.
3- Unary Operators: Includes negation (-)
and logical NOT (not).
4- Multiplication, Division, and Modulus
(*, /, %):
5- Addition and Subtraction (+, -):
6- Comparison Operators (<, <=, >, >=, ==,
!=):
7- Logical Operators (and, or):
PYTHON : VARIABLES
RAM Memory
Variable: A variable is a memory space
that contains information manipulated x 1
within a program. It is characterized by
an identifier, a value, and a type.
Create a variable:
x=1 (reads: x is assigned the value 1)
identifier a value Type ?
PYTHON : identifier – (naming)
XXXX….X
alphabet letter Special characters Reserved words
A..Z ou a..z ./§~#{[|`\^ print, input;
_ (underscore) @]}=+° def, in, if, for
except: _
PYTHON : naming convention
Variables Constantes
examen PI = 3.14
(lower case letters) (capital letters)
VITESSE_LUMIERE = 3e5
note_examen (notation SNAKE_CASE)
(snake_case notation)
PYTHON : NAMING CONVENTION
tp ≠ Tp ≠ tP ≠ TP
(4 variables ---<Python is Case Sensitive)
PYTHON : VARIABLES (Types)
Using the values of variables, Python detects
their types
Types of Bases:
int: The integers: Z (1,2, 23, 0, -1, -45 ….)
float: The reals: R (0.23 , 3.0 , - 3.5 ….)
str: Character string ('Ali', "school")
Bool: Boolean (True, False)
Operations on integers (int)
a = 11 " a receives 11" or " a is assigned the value 11 "
b = 4 " b receives 4 " or " b is assigned the value 4 "
Python Calculation performed
reminder Euclidean division of a by b :
a+b 11+4=15 if a є N and b є N*
a-b 11-4=7 a/b = b x q + r with 0 < r < b
a*b 11*4=44 r is the remainder, and q is the quotient
11 = 4x2 +3
a/b 2.75
a // b gives the quotient of the Euclidean
a**b 114=14641 division of a by b
a//b 11//4=2 a % b remainder gives the of the
a%b 11%4=3 Euclidean division of a by b
The float type: Writing format
2.5 2,5
3e4 --< 3x104
str: Represents character strings, which can be
enclosed in single ' … ' or double quotes "…".
Allowed Operations:
a = 'Py'
b = "thon "
- a + b ---< given "Python" (CONCATENATION)
- a * 3 ----< given "PyPyPy" (DUPLICATION)
Operations on (str): TEST
‘5‘ + ‘2‘ = ?
‘5‘ * 2 = ?
Boolean Variable: A Boolean variable can hold one of
two values: "True" or "False."
Operations on (bool)
The comparison operations Logical operations
•9 < 2 —> gives False (Less-than) •True and True —> gives True
•True and False —> gives False
•9 <= 2 —> gives False (Less Than or Equal ) •False and False —> gives False
•9 > 2 —> gives True (Greater-than)
•True or True —> gives True
•9 >= 2 —> gives True (Greater-than or equal) •True or False —> gives True
•9 == 2 —> gives False (Equal) •False or False —> gives False
•9 != 2 —> gives True (Different) •not(True) —> gives False
•not(False) —> gives True
PYTHON : VARIABLES (Types)
With type() we discover the type of the variable.
PYTHON : VARIABLES (Types)
Exercise :
variable Value Command type
a 250 <<< a = 250 int
…………. …………. <<<b =3.1423 ………….
c ' console ' <<<…………. ………….
…………. …………. <<<d = False ………….
TRANSTYPAGE : Implicit Type Conversion in Python
float(R )
int( Z)
Ruler :
type (A + B) --< Python takes largest type
Ex :
2 + 2 --< 4 (int + int given int)
2.0 + 2 --< 4.0 (float + int given float)
TRANSTYPING: Explicit Type Conversion in Python
(programmer)
Transformation Between Types:
• '5' + 2 —> causes an error (TypeError: can only
concatenate str (not "int") to str)
• int('5') + 2 —> gives an integer: 7
• '5' + str(2) —> gives a string: '52'
• float('5') —> gives a float: 5.0
• float(2) —> gives a float: 2.0
• int(2.34) —> gives the integer part: 2
Basic instructions
•Assignment (=)
•Read input(…)
•Display print (…)
Assignment ( = )
Assignment ( = )
ASSIGNMENT: special case
X += 1 X = X + 1
X *= 3 X = X * 3
X **= 3 ……..
X /= X+3 ……..
Programming (Coding ) Methodology
Problem
Analysis Algorithm Program
Execution
PROGRAM : Facebook
The Inputs Processing The Outputs (Results)."
ANALYSIS:
• Inputs (I) ?
• Outputs (O)?
• Processing: How to calculate
outputs from inputs
ANALYSIS: Example1
1- Inputs (I): C
2- Outputs (O): Area
3- Processing: Input C
Ar1=C*C
Ar2=C*C/2
Area =Ar1+Ar2
Display Area
ANALYSIS: Tip
The Outputs will be calculated
come after the verbs: calculate, outputs
find, display)…etc Inputs
The Inputs to be entered (to be
given) come after the verbs: read,
enter, etc.
ANALYSIS Python
analysis Script (python)
The Inputs Object
Problem
(Constants/Variable)
The Outputs _______________
Processing Instructions
print() : syntax
print() : syntax
• The newline character, represented as \n, is used in
Python to move output to the next line.
• To make newline characters visible in the output, you
can use a double backslash like this:
>>> print('Welcom to \n computing1 course')
This will display:
Welcom to
computing1 course
print() : Exo
Ecran ?
X=1
Y = 3.25
print(‘X = ‘, X)
print(X , ‘ + ‘ , Y , ‘ = ‘ , X + Y )
print(‘first Name \n Last name \n Age’)
Read instruction: input()
1
The function input () always returns the value typed on the
keyboard as a string.
Exercise:
Here we want to display 24.
give the solution?
1- Inputs: C, h Script
ANALYSIS 2-Outputs: Surface
--> PROGRAM (Ex 1)
3- Processing: given C C=float(input(‘give a square side C:’))
given h h=float(input("give a height h:"))
Analysis S1=C*C S1=C*C
S2=C*h/2 S2=C*h/2
Surface=S1+S2 Surface=S1+S2
Display Surface print(‘ la surface = ’ , Surface )
Execution
ANALYSIS – < PROGRAM (Ex 5)
R
1- Inputs:
S (surface) ,P (perimeter)
2- Outputs:
give R
3- Processing:
S=R²*π
Analysis
P=2*R* π
Display S et P
Script
Execution R=float(input("given the radius R:"))
S=R**2*3.14
P=2*R*3.14
print(‘ The area of cercle= ’ , S )
print(‘ The Perimeter of cercle = ’ , P )
To be continued……