الجمهورية الجزائرية الديمقراطية الشعبية
العلم
ي العال والبحث
ي وزارة التعليم
PEOPLE’S DEMOCRATIC REPUBLIC OF ALGERIA
Ministry of Higher Education and Scientific Research
CHAPTER 1
Basic Programming Concepts in Python
Prepared by: BELLAOUER Mohammed Cherif
Academic Year 2025/2026
What is programming?
Preparing (writing) programs and run them on computers
What is an algorithm?
A step-by-step procedure to solve a problem.
Python programming language
Python is a versatile (polyvalent) interpreted programming
language, easy to learn and widely used in various fields.
Variable and Expression
Variable: A named piece of memory that can store a value.
Usage:
▪ Compute an expression’s result. A: int X: float S: string
▪ Store this result into variable
▪ Use this variable later in the program
5 3.625 "Hello"
Expression: A data value or set of operations to compute a value.
[2]: 3*2+5*4-1
25
[3] a=5; b=2
: c=a*b+b*5
print(c)
20
Write your first Python code
[4]: 5+8
13
[5]: 5*3+4*10-8
47
[6]: a=" Hello "
b="World !"
a+b
Hello World !
Data Types
Integer (int)
[7]: a=128
type (a) #Display the type of the variable a
int
Floating point numbers (Float)
[8]: a=14.5
type (a)
float
Data Types
String (str)
[9]: a="Bonjour"
type (a)
str
Boolean numbers (bool)
[10]: a=True
type (a)
bool
Data Types
Complex numbers (complex)
[12]: a=2+3j
type (a)
complex
a
2+3j
List type (list)
[11]: Jour=["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday"]
type (Jour) #Display the type of the variable Jour
list
Arithmetic Operators
Operator Meaning Example Result
+ Addition a = 5+3 8
- Substraction b = 10-4 6
* Multiplication c=5*2 10
/ Division d=10/2 5
% Modulus e=12%5 2
** Exponentiation f=4**2 16
Operator’s Precedence
*, /, %, **: have a higher precedence than + -
Example: 1 + 3 * 4 ==> 13
(1 + 3) * 4 ==> 16
Logic Operators
Operator Meaning Example Result
== Equals 5+3 == 8 True
!= Does not equal 3.2 != 2.5 True
< Less than 10 < 5 False
> Greater than 10 > 5 True
<= Less than or equal to 126 <= 100 False
>= Greater than or equal 5.9 >= 5.0 True
and Conjuction 9 != 6 and 2 < 3 True
or Disjunction 2 == 3 or -1 < 5 True
not Negation not 7 > 0 False
Frequent Math function
Function Description Example
abs(value) Absolute value Abs(-5)== 5
ceil(value) Rounds up (Lib: Math) [Link](3.2)== 4
floor(value) Rounds down (Lib: Math) [Link](3.2)== 3
cos(value) Cosine in radians (Lib: Math) [Link]([Link]/6) == 0.866
sin(value) Sine in radians (Lib: Math) [Link]([Link]/6) == 0.4999
Log(value) Nipearian logarithm (Lib: Math) [Link](2) ==0.6931
Log(value, base) Logarithm, base b (Lib: Math) [Link](14, 5) ==1.6397
Max(value1, value2) Greater of two values Max(25, 12) == 25
Min(value1, value2) Smaller of two values min(7.5, 1.25) == 1.25
Sqrt(value) Square root (Lib: Math) [Link](49)==7
Output function print()
print(): produces output on the console
Syntaxe:
print ("Message") ====> Ex: print("Hello World")
print (constant) ====> Ex: print(10)
print (Variable) ====> Ex: a=12 print(a)
print (expression) ===> Ex: a=1 b=3 print(a*2+b*5-1)
print (item1, item2, …, itemn)
===> Ex: a=12 b=6 print("Results: ", a, b, a+b, a-b, a*b/2)
Input() function
Input: Reads a number from user input. Then he can store it into a variable
Syntax:
input (prompt)
Example:
n=int(input ("Introduce an integer please: ")) #Waiting for an integer value of n
Introduce an integer please: 10
n #Display the introduced value
10
price = float(input("Price of each rose: ")) #Waiting for a float value of price
Price of each rose: 15.50
print(price)
15.50
Input() function
Example:
name = input("Please Enter Your Name: ")
Please Enter Your Name: Ali
age = int(input("Please Enter Your Age: "))
Please Enter Your Age: 25
print("Name & Age: ", name, age)
Name & Age: Ali 25
a = int(input("Please Enter the First Number: "))
Please Enter the First Number: 8
b = int(input("Please Enter the Second Number: "))
Please Enter the Second Number: 7
print("The Addition Result is :", a+b)
The Addition Result is : 15
IF Statement
Executes a group of instructions only if a certain condition is true.
Otherwise, the instructions are skipped.
Syntaxe:
if <Condition>:
Instruction
Examples:
note = 7
if note >= 10:
print("you are accepted") #This message will not be displayed because note=7 is
less than 10
OR
import math
note=float(input ("Give your note: "))
if note >= 10:
print("you are accepted")
IF-Else Statement
Executes one block of instructions if a certain condition is True, and a
second block of instructions if it is False.
Syntaxe:
IF <Condition>:
Instructions
Else:
Instructions
Example:
n = int(input("Introduce an integer : "))
if n >= 0:
print(" You have introduced a positive number")
else:
print(" You have introduced a negative number")
Application:
if we introduce n=5 ===> we will have the message: You have introduced a positive number
if we introduce n=-8 ===> we will have the message: You have introduced a negative number
Multiple Conditions Statement
Can be chained with elif ("else if"):.
Syntaxe:
if <condition>:
Instructions
elif <condition>:
Instructions
else:
Instructions
Example:
import math
x = int(input("Introduce an integer : "))
if x <= 15 :
y = x + 15
elif x <= 30 :
y = x + 30
else :
y=x
print ("y = ", y)
Repetition Statement
for loop: Repeats a set of statements over a group of values.
Syntaxe:
for <Variable> in <groupe of values>:
Instructions
- We indent the statements to be repeated with tabs or spaces.
- VariableName: gives a name to each value, so you can refer to it in the statements.
- GroupOfValues: can be a range of integers, specified with the range function.
Example:
import math
for x in range(1, 6):
print(x, "squared is", x * x)
Output:
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
The range Function
The range function specifies a range of integers:
Syntaxe:
range(start, stop) - the integers between start (inclusive)
and stop (exclusive)
Example:
for x in range(0,5):
print(" The Value of x =" , x)
Output:
La valeur de x = 0
La valeur de x = 1
La valeur de x = 2
La valeur de x = 3
La valeur de x = 4
Cumulative loops using the range Function
Some loops incrementally compute a value that is initialized outside the
loop. This is sometimes called a cumulative loop.
Example:
sum = 0
for i in range(0, 10):
sum = sum + i * i
print("sum of first 10 squares is: ", sum)
Output:
sum of first 10 squares is 285
While loop
While loop: Executes a group of statements as long as a condition is True.
Good for indefinite loops (repeat an unknown number of times)
Syntaxe:
while <condition>:
Instructions
Example:
number = 1
while number < 200:
print(number)
number = number * 2
Output:
1 2 4 8 16 32 64 128
Converting Data type
Int(x): converts x to an integer
Float(x): converts x to a floating point
Example:
int(2.5347)
2
float(2)
2.0
0o12 #Displays the equivalent of the number 12 in byte ====> 10
0x12 #Displays the equivalent of the number 12 in hexadecimal ====> 18
Converting Data type
type(int("16") #Converting a string to int
<class int>
5e3 #Displays the number 5 followed by three 0, a decimal point, one digit after
the decimal point
str(359) #Convert an int to a string
Output: '359‘
complex(7.5) #Convert a float to a complex
Output: (7.5+0j)
More Data Types
Lists: Ordered collection of data.
- Data can be of different types
- Lists are mutable
Example:
x = [1, Hello , (3+2j)]
x
Output: [1, Hello , (3+2j)]
x[2]
Output: (3+2j)
x[0:2]
Output: [1, ‘Hello’]
List Functions
len(list): Return the number of items in a list.
[Link](x): Add item at the end of the list.
[Link](i, x): Insert an item at a given position.
[Link](x): Removes the first item from the list with value x
[Link](i): Remove item at position I and return it. If no index I is given then
remove the first item in the list.
[Link](x): Return the index in the list of the first item with value x.
[Link](x): Return the number of time x appears in the list
[Link](): Sorts items in the list in ascending order
[Link](): Reverses items in the list
Example:
lis = [1, 2, -5, ' Hello' , (3+2j), 3.75] #Define a list lis
len(lis) # return number of items in the list lis
Output: 6
[Link](50) #Add the item 50 to the list lis
[Link](1, -1) #Insert the item -1 of position 1 to the list lis
[Link](-1) #Remove the item -1 from the list lis
Lis ===> Output: [1, 2, -5, Hello , (3+2j), 3.75, 50]
[Link](5) #Remove the item at position 5 from the list and return its value
Output: 3.75
[Link](-5) #return the index in the list of the first value -5
Output: 2
[Link](1) #Return the number of occurrences of the item having the value 1
Output: 1
[Link]() #Sort the list lis
Output: error
[Link]() #reveverse the list lis
lis
Output: [50, (3+2j), Hello , 2, 1, -5]
Lists
Example:
lis = [1, 2, -5, Hello , (3+2j), 3.75]
lis
Output: [1, 2, -5, Hello , (3+2j), 3.75]
new_lis=lis #lis and new_lis point to the same list object
lis[1]=15 #Reassign the element number 1 to the value 15
lis
Output: [1, 15, -5, Hello , (3+2j), 3.75]
new_lis
Output: [1, 15, -5, Hello , (3+2j), 3.75]
#Since lis and new_lis point to the same list object
[Link](50) #Add new element at the end of the list lis
new_lis
Output: [1, 15, -5, Hello , (3+2j), 3.75, 50]
lis = lis + [80, 90]
new_lis
Output: [1, 15, -5, Hello , (3+2j), 3.75, 50, 80, 90]