Cours Python
Cours Python
∇ ∂
Py
THON
FUNDAMENTALS OF
PROGRAMMING AND
SCIENTIFIC COMPUTING
WITH PYTHON
Academic Course
MATHEMATICS
f (x) lim C R
2
University of Batna 2
Faculty of Mathematics and Computer Science
ACADEMIC YEAR
2025–2026
···
ACADEMIC EDITION
Contents
THEORETICAL COURSE 1
LABORATORY WORK 18
3
CONTENTS Dr. N. GOLEA
4
THEORETICAL COURSE
Python Programming Fundamentals
Learning Objectives
This theoretical course provides the foundation for understanding
programming concepts
and Python language features. Each chapter builds upon
previous knowledge to develop
a comprehensive understanding of Python for mathematical
applications.
2
Chapter 1
ò INFORMATION
◎ Chapter Objectives
• Understand the fundamentals of Python and its ecosystem
• Install and configure the Anaconda scientific Python distribution
• Master Jupyter Notebook for interactive computing
• Write and execute your first Python program
• Organize your projects with effective file management
3
CHAPTER 1. INTRODUCTION TO PYTHON FOR MATHEMATICS Dr. N. GOLEA
3 Python
Core Language
[ DEFINITION
Anaconda is a comprehensive distribution that includes:
• Ð Python 3.x - The core programming language
• Jupyter Notebook - Interactive computing environment
• ò 250+ Packages - Pre-installed scientific libraries
• Ô Conda - Advanced package and environment manager
4
CHAPTER 1. INTRODUCTION TO PYTHON FOR MATHEMATICS Dr. N. GOLEA
EXAMPLE
Organized Project Structure Setup:
1 # Navigate to Desktop
2 cd Desktop
3
5
CHAPTER 1. INTRODUCTION TO PYTHON FOR MATHEMATICS Dr. N. GOLEA
9 jupyter notebook
Figure 1.2: Jupyter Notebook interface with code execution and results.
6
CHAPTER 1. INTRODUCTION TO PYTHON FOR MATHEMATICS Dr. N. GOLEA
Chapter Summary
. IMPORTANT
c Key Concepts Learned:
• 3 Python provides a powerful yet accessible platform for mathematical computing
• Ô Anaconda simplifies installation and dependency management
• Jupyter Notebook enables interactive exploration and documentation
• i File Management is essential for productive workflow
• y Basic Programs can perform mathematical calculations
End of
Chapter 1
7
CHAPTER 1. INTRODUCTION TO PYTHON FOR MATHEMATICS Dr. N. GOLEA
8
Chapter 2
ò INFORMATION
◎ Chapter Objectives
• Understand variables and data types
• Use arithmetic, comparison, and logical operators
• Write conditional statements (if, elif, else)
• Create loops (for, while) for repetition
9
CHAPTER 2. BASIC PYTHON SYNTAX Dr. N. GOLEA
_ OUTPUT
Output:
Hello, Python!
Name: Ali Age: 20
Student: Ali, Age: 20
[ DEFINITION
Data Types in Python:
• º int: Whole numbers: 5, -3, 0
• ÷ float: Decimals: 3.14, -0.5, 2.0
• k str: Text: "Hello", ’Python’
• § bool: True, False
EXAMPLE
Example: Variable declaration and types
1 # Integer variable
2 students_count = 30
3 print ( " Students : " , students_count )
4 print ( " Type : " , type ( students_count ) )
5
6 # Float variable
7 average_grade = 15.5
8 print ( " Average : " , average_grade )
9 print ( " Type : " , type ( average_grade ) )
10
11 # String variable
12 course_name = " Mathematics "
13 print ( " Course : " , course_name )
14 print ( " Type : " , type ( course_name ) )
15
16 # Boolean variable
17 is_passed = True
18 print ( " Passed : " , is_passed )
19 print ( " Type : " , type ( is_passed ) )
10
CHAPTER 2. BASIC PYTHON SYNTAX Dr. N. GOLEA
_ OUTPUT
Output:
Students: 30
Type: <class 'int'>
Average: 15.5
Type: <class 'float'>
Course: Mathematics
Type: <class 'str'>
Passed: True
Type: <class 'bool'>
EXAMPLE
Example: Arithmetic calculations
1 # Define variables
2 a = 15
3 b = 4
4
5 # Perform calculations
6 addition = a + b
7 subtraction = a - b
8 multiplication = a * b
9 division = a / b
10 integer_division = a // b
11 remainder = a % b
12 power = a ** 2
13
14 # Display results
15 print ( f " { a } + { b } = { addition } " )
11
CHAPTER 2. BASIC PYTHON SYNTAX Dr. N. GOLEA
_ OUTPUT
Output:
15 + 4 = 19
15 - 4 = 11
15 x 4 = 60
15 / 4 = 3.75
15 // 4 = 3
15 % 4 = 3
152 = 225
EXAMPLE
Example: Comparison operations
Compare values and combine conditions with logical operators.
1 # Define variables
2 x = 10
3 y = 5
4 z = 10
5
6 # Comparison operations
7 print ( f " { x } == { y }: { x == y } " )
12
CHAPTER 2. BASIC PYTHON SYNTAX Dr. N. GOLEA
14 # Logical operations
15 print ( f " ({ x } > 5) and ({ y } < 10) : {( x > 5) and ( y < 10) } " )
16 print ( f " ({ x } < 5) or ({ y } > 3) : {( x < 5) or ( y > 3) } " )
17 print ( f " not ({ x } == 10) : { not ( x == 10) } " )
_ OUTPUT
Output:
10 == 5: False
10 != 5: True
10 > 5: True
10 < 5: False
10 >= 10: True
10 <= 10: True
(10 > 5) and (5 < 10): True
(10 < 5) or (5 > 3): True
not (10 == 10): False
13
CHAPTER 2. BASIC PYTHON SYNTAX Dr. N. GOLEA
False
Condition? Condition? End
True False
Loop Body
Continue
Control Structures:
⋄ Condition - Checks a condition
Update
■ Process - Executes code
□ Update - Changes loop variable
Figure 2.1: Control structures in Python: if-else for decision making and while loop
for repetition
1 # Input grade
2 grade = 16.5
3
4 # Classify grade
5 if grade >= 16:
6 category = " Excellent "
7 elif grade >= 14:
8 category = " Very Good "
9 elif grade >= 12:
10 category = " Good "
11 elif grade >= 10:
12 category = " Satisfactory "
13 else :
14 category = " Fail "
14
CHAPTER 2. BASIC PYTHON SYNTAX Dr. N. GOLEA
15
16 # Display result
17 print ( f " Grade : { grade }/20 " )
18 print ( f " Category : { category } " )
_ OUTPUT
Output:
Grade: 16.5/20
Category: Excellent
EXAMPLE
Example: for loop - display numbers
Use a for loop to display numbers from 1 to 5.
_ OUTPUT
Output:
Numbers from 1 to 5:
1
2
3
4
5
EXAMPLE
Example: while loop - countdown
1 count = 5
2 print ( " Countdown : " )
3 while count > 0:
4 print ( count )
5 count = count - 1
6 print ( " Go ! " )
_ OUTPUT
Output:
Countdown:
5
4
3
2
15
CHAPTER 2. BASIC PYTHON SYNTAX Dr. N. GOLEA
1
Go!
EXAMPLE
Example: break and continue
Use break to stop a loop and continue to skip iterations.
1 # Using break
2 print ( " Numbers until 5: " )
3 for i in range (1 , 10) :
4 if i == 6:
5 break
6 print ( i )
7
8 # Using continue
9 print ( " \ nEven numbers from 1 to 10: " )
10 for i in range (1 , 11) :
11 if i % 2 != 0: # Skip odd numbers
12 continue
13 print ( i )
_ OUTPUT
Output:
Numbers until 5:
1
2
3
4
5
16
CHAPTER 2. BASIC PYTHON SYNTAX Dr. N. GOLEA
First number: 10
Second number: 5
Operation (+, -, *, /): *
Result: 10 * 5 = 50
Chapter Summary
. IMPORTANT
c Key Concepts Learned:
• º Variables store data with meaningful names
• ¦ print() displays output to the screen
• ÷ Data types define what kind of data you’re working with
• y Operators perform calculations and comparisons
• Ñ Conditionals (if/elif/else) make decisions
• º Loops (for/while) repeat actions efficiently
End of
Chapter 2
17
LABORATORY WORK
Python Programming Laboratory
Pedagogical Approach
This series of laboratory sessions is designed to develop
computational thinking
through incremental challenges, with each lab building upon
previous concepts.
Learning by doing is our guiding principle.
19
CONTINUOUS EVALUATION
Evaluation System
Continuous Assessment = Attendance (8pts) + Progress
Tracking
Progress Tracking per Lab:
⋆ Participation  Submission 4 Consultation
(star rating) (timely) (resources)
Special Bonus
Top 5 students in each group receive bonus on final exam
Submission Guidelines
Submit all work to your assigned instructor
ò INFORMATION
Lab Objectives
• Install Anaconda and launch Jupyter Notebook
• Understand Jupyter Notebook interface and cells
• Write and execute Python code in Jupyter
• Learn basic Python syntax and operations
4 # Simple calculation
5 result = 5 + 3
6 print ( " 5 + 3 = " , result )
21
TP1: GETTING STARTED WITH PYTHON Dr. N. GOLEA
4 Exercises
X EXERCISE
Exercise 1: Personal Information
Create a Jupyter notebook with:
• Your name
• Your age
• Your university
• Your study program
Display each piece of information using the print() function.
X EXERCISE
Exercise 2: Simple Calculator
Create a notebook that calculates and displays:
• The sum of two numbers
• The difference between two numbers
• The product of two numbers
• The quotient of two numbers
Choose any numbers you like for the calculations.
X EXERCISE
Exercise 3: Temperature Converter
Create a program that converts:
• 25°C to Fahrenheit
• 77°F to Celsius
Use the formulas:
• Fahrenheit = (Celsius × 9/5) + 32
• Celsius = (Fahrenheit - 32) × 5/9
X EXERCISE
Exercise 4: Circle Calculator
Create a program that calculates:
• The area of a circle with radius 5
• The circumference of a circle with radius 5
Use the formulas:
• Area = pi × radius2
• Circumference = 2 × pi × radius
Use [Link] for pi.
22
TP1: GETTING STARTED WITH PYTHON Dr. N. GOLEA
. IMPORTANT
Common Operations:
• Restart Kernel: Kernel → Restart
• Save Notebook: File → Save or Ctrl+S
• Download as PDF: File → Download as → PDF
• Help: Type function_name? then run cell
6 Notebook Organization
EXAMPLE
Suggested Notebook Structure:
# TP1: Getting Started with Python
[Your Name]
[Date]
## 1. Introduction
Brief description of what you will do
## 6. Conclusion
What you learned from this TP
23
TP1: GETTING STARTED WITH PYTHON Dr. N. GOLEA
7 Submission Requirements
. IMPORTANT
Á Submission Instructions:
• g Create folder: TP1_LastName_FirstName
• [ Save your notebook as: TP1_Exercises.ipynb
• P Export notebook as PDF: File → Download as → PDF
• Compress folder: TP1_LastName_FirstName.zip
• # Submit by email to your instructor
8 Troubleshooting
• . Jupyter won’t start: Try jupyter notebook –no-browser
• º Kernel not responding: Kernel → Restart
• Ð Code not executing: Make sure cell type is "Code"
• Import error: Make sure you have installed the package
• Ö Notebook not saving: Check file permissions
9 Additional Resources
• [ Jupyter Documentation: [Link]
• Python Official Tutorial: [Link]
• ÷ Markdown Guide: [Link]
• ® Stack Overflow: [Link]
End of TP1
24
Lab 2: Variables and Data Types
ò INFORMATION
◎ Objectives
• Understand different data types in Python
• Learn to declare and use variables in Jupyter
• Practice type conversion operations
• Solve problems using variables and data types
25
TP2: VARIABLES AND DATA TYPES Dr. N. GOLEA
_ OUTPUT
Expected Output:
_ OUTPUT
Expected Output:
26
TP2: VARIABLES AND DATA TYPES Dr. N. GOLEA
Boolean conversion:
bool(0) = False
bool(1) = True
bool(-1) = True
bool('') = False
bool('Hello') = True
_ OUTPUT
Sample Output:
=== Student Information Form ===
Enter your name: Fatima
Enter your age: 22
Enter your height (m): 1.68
Are you enrolled? (yes/no): yes
27
TP2: VARIABLES AND DATA TYPES Dr. N. GOLEA
5 Practical Exercises
X EXERCISE
Exercise 1: Student Information System
Create a Jupyter notebook that:
1. Asks for student information: name, age, grade average, and major
2. Stores each piece of information in an appropriately typed variable
3. Displays the information in a formatted report
4. Calculates and displays:
• Year of birth
• Years until graduation (assuming 4-year program)
• Grade category (A: >=16, B: 14-15.9, C: 12-13.9, D: 10-11.9, F: <10)
Sample Output:
Calculations:
Year of Birth: 2004
Expected Graduation Year: 2026
X EXERCISE
Exercise 2: Unit Conversion Dashboard
Create a notebook with a unit conversion dashboard:
• Convert kilometers to miles (1 km = 0.621371 miles)
• Convert Celsius to Fahrenheit (F = C × 9/5 + 32)
• Convert kilograms to pounds (1 kg = 2.20462 lbs)
• Convert meters to feet (1 m = 3.28084 ft)
Requirements:
• Use input() to get values from user
• Perform conversions for all four units
• Display results in a table format
• Allow the user to choose which conversion to perform
Sample Output:
28
TP2: VARIABLES AND DATA TYPES Dr. N. GOLEA
3. Kilograms to Pounds
4. Meters to Feet
X EXERCISE
Exercise 4: Statistics Calculator
Create a program that:
1. Asks for 5 numbers
2. Calculates and displays:
• Sum and average
• Minimum and maximum
• Range (max - min)
• Product of all numbers
• Data type statistics (how many integers, floats, etc.)
3. Converts all numbers to different types and displays the results
Advanced Challenge:
• Handle both integers and floats in input
• Detect and handle invalid input (non-numeric)
• Allow the user to choose how many numbers to enter
. IMPORTANT
Jupyter Notebook Tips for TP2:
• Ð Use separate cells for different parts of each exercise
• × Add Markdown cells to explain your code
• O Use f-strings for formatted output
• ¥ Test type conversions in isolated cells
• . Handle potential errors with try-except blocks
29
TP2: VARIABLES AND DATA TYPES Dr. N. GOLEA
6 Notebook Structure
EXAMPLE
Recommended Notebook Structure for TP2:
Organize your TP2 notebook with clear sections
## 5. Conclusion
[Summary and challenges faced]
7 Submission Requirements
. IMPORTANT
Á Submission Instructions:
• g Create folder: TP2_LastName_FirstName
• [ Save your notebook as: TP2_Exercises.ipynb
• P Export notebook as PDF: File → Download as → PDF
• D Include any additional Python files if needed
• Compress folder: TP2_LastName_FirstName.zip
• # Submit by email to your TP instructor
8 Additional Resources
• [ Python Data Types: [Link]
30
TP2: VARIABLES AND DATA TYPES Dr. N. GOLEA
End of TP2
31