Poly
Poly
Python
March 7, 2026
Contents
1 Introduction 3
1.1 Installing Python and Development Environments . . . . . . . . . . . . . . . . . 3
1.1.1 Standard Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.2 Anaconda Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.3 Development Environments . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1
3.1.5 Keyword (Named) Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.1.6 Mixing Positional and Keyword Arguments . . . . . . . . . . . . . . . . . 16
3.2 Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2.1 Importing Standard Modules . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2.2 Using Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2.3 Creating and Importing Custom Modules . . . . . . . . . . . . . . . . . . 17
3.3 Practical Example: Creating a Scientific Module . . . . . . . . . . . . . . . . . . 17
3.3.1 The Module: scientific_tools.py . . . . . . . . . . . . . . . . . . . . . 17
3.3.2 The Main Program: [Link] . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.4 Essential Scientific Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.4.1 The math Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.4.2 The numpy Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.4.3 The scipy Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.4.4 The matplotlib Module . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.5 Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.6 Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.7 Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.8 Exercise 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.9 Exercise 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.10 Exercise 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.11 Exercise 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.12 Exercise 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.13 Exercise 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.14 Exercise 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.15 Solution 1: Factorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.16 Solution 2: Fibonacci Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.17 Solution 3: Mean of a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.18 Solution 4: Prime Check . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.19 Solution 5: Count Vowels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.20 Solution 6: Reverse String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.21 Solution 7: Celsius to Fahrenheit . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.22 Solution 8: Max and Min in a List . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.23 Solution 9: Even Numbers 1 to 100 . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.24 Solution 10: Simple Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2
Chapter 1
Introduction
Python is one of the most widely used programming languages in scientific computing, artificial
intelligence, and data analysis. Its clear syntax and extensive ecosystem of scientific libraries
make it ideal for students beginning programming.
This course introduces the fundamental concepts of programming using Python and demon-
strates how Python can be used to solve mathematical and scientific problems.
Main objectives:
• Python
• NumPy
• SciPy
• Matplotlib
• Jupyter Notebook
3
1.1.3 Development Environments
Several IDEs can be used:
• IDLE
• Spyder
• PyCharm
• Jupyter Notebook
4
Chapter 2
2.2 Variables
Variables are used to store values in memory. You can use them later in calculations or other
operations.
1 # Assign values to variables
2 a = 5 # integer
3 b = 10 # integer
4
5 # Perform arithmetic operation
6 c = a + b # add a and b
7
8 # Display the result
9 print ( c ) # output : 15
2.3.1 Integers
Integers are whole numbers without a fractional part.
1 x = 10
2 print ( x ) # output : 10
3 print ( type ( x ) ) # shows the type of x , output : < class ’ int ’>
5
1 y = 3.14
2 print ( y ) # output : 3.14
3 print ( type ( y ) ) # output : < class ’ float ’>
2.3.3 Strings
Strings are sequences of characters, used to represent text.
1 name = " Alice "
2 print ( name ) # output : Alice
3 print ( type ( name ) ) # output : < class ’ str ’>
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation
% Modulus
Example:
1 a = 5
2 b = 2
3
4 print ( a + b )
5 print ( a * b )
6 print ( a ** b )
6
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 or equal 126 <= 100 False
>= Greater or equal 5.9 >= 5.0 True
and Conjunction 9 != 6 and 2 < 3 True
or Disjunction 2 == 3 or -1 < 5 True
not Negation not 7 > 0 False
• Variables are passed directly without quotes to display their stored value.
By default, the input() function always returns data as a String (str). If you need
to perform mathematical operations on the input, you must convert it using int() or
float().
7
1 # Reading a number and converting it to an integer
2 age = int ( input ( " Enter ␣ your ␣ age : ␣ " ) )
3
4 # Now we can perform calculations
5 next_year = age + 1
6 print ( " Next ␣ year ␣ you ␣ will ␣ be : " , next_year )
General Syntax
if condition:
# block of code executed if True
1 age = int ( input ( " Enter ␣ age : ␣ " ) ) # Let ’s assume the user enters 20
2
3 if age >= 18:
4 # This line only runs if age is 18 or more
5 print ( " You ␣ are ␣ an ␣ Adult " )
6
7 # Output :
8 # You are an Adult
General Syntax
if condition:
# block if True
else:
# block if False
1 # Assuming age is 15
2 if age >= 18:
3 print ( " Status : ␣ Adult " )
4 else :
5 # This runs if age is less than 18
6 print ( " Status : ␣ Minor " )
7
8 # Output :
9 # Status : Minor
8
2.5.3 Multiple Conditions (If-Elif-Else)
When you have more than two possible paths, use the elif keyword. Python checks conditions
from top to bottom.
General Syntax
if condition1:
# block 1
elif condition2:
# block 2
else:
# final fallback block
1 grade = float ( input ( " Enter ␣ your ␣ grade ␣ (0 -20) : ␣ " ) ) # Let ’s assume 14.5
2
3 if grade >= 16:
4 print ( " Result : ␣ Excellent " )
5 elif grade >= 12:
6 print ( " Result : ␣ Good " )
7 elif grade >= 10:
8 print ( " Result : ␣ Pass " )
9 else :
10 print ( " Result : ␣ Fail " )
11
12 # Output :
13 # Result : Good
Syntax Reminder
Don’t forget the colon (:) at the end of every if, elif, and else line. Forgetting this
is a very frequent syntax error for beginners!
1 age = 20
2 has_license = True
3
4 if age >= 18 and has_license :
5 print ( " You ␣ are ␣ allowed ␣ to ␣ drive . " )
6
7 # Output :
8 # You are allowed to drive .
9
2.6.1 The For Loop and the range() Function
The for loop is used to iterate over a sequence.
General Syntax
for variable in sequence:
# block of code to repeat
1 sum_total = 0
2 # Calculating the sum of integers from 0 to 10
3 for i in range (11) :
4 sum_total += i # Equivalent to sum_total = sum_total + i
5
6 print ( " The ␣ final ␣ sum ␣ is : " , sum_total )
7
8 # Output :
9 # The final sum is : 55
Coding Tip
Always initialize your accumulator (sum = 0 or product = 1) outside and before the
loop.
10
Exercise Challenge
General Syntax
while condition:
# block of code to repeat
# (must include a step to change the condition)
1 n = 1
2 # This loop doubles n until it reaches or exceeds 100
3 while n < 100:
4 print ( n )
5 n = n * 2 # Update step
6
7 # Output :
11
8 # 1
9 # 2
10 # 4
11 # 8
12 # 16
13 # 32
14 # 64
In a while loop, you must ensure that the condition eventually becomes False.
• continue: Skips the current iteration and jumps to the next one.
General Syntax
int(value) – Converts to integer
float(value) – Converts to floating point
1 # Conversion examples
2 print ( int (2.5347) ) # Output : 2
3 print ( float (2) ) # Output : 2.0
1 age = 10
2 print ( " Ali ␣ is ␣ % d ␣ years ␣ old " % age )
3 # Output : Ali is 10 years old
4
5 name = " Ali "
6 print ( " Applicant : ␣ % s " % name )
12
7 # Output : Applicant : Ali
8
9 a = 179.49599
10 print ( " %.2 f " % a ) # Output : 179.50
11 print ( " %.4 f " % a ) # Output : 179.4960
13
Chapter 3
3.1 Functions
A function is a reusable block of code designed to perform a specific task. By breaking a complex
program into smaller functions, your code becomes easier to read, test, and maintain.
General Syntax
def function_name(param1, param2, ...):
# block of instructions
return value
9 # Output :
10 # 8
14
3.1.3 Calling Functions
Once defined, a function remains inactive until it is called by its name, followed by parentheses
containing the necessary arguments.
1 def greet ( name ) :
2 print ( f " Hello ␣ { name }! " )
3
4 greet ( " Alice " )
5 greet ( " Bob " )
6
7 # Output :
8 # Hello Alice !
9 # Hello Bob !
You can assign default values to parameters in a function definition. This allows the function
to be called with fewer arguments than it has parameters.
General Syntax
def func(param1=default_value):
# block of code
15
3.1.6 Mixing Positional and Keyword Arguments
When combining both, positional arguments must always come before keyword argu-
ments.
1 def add_numbers (a , b , c =0) :
2 return a + b + c
3
4 print ( add_numbers (5 , 3) ) # c defaults to 0 -> 8
5 print ( add_numbers (5 , 3 , c =2) ) # c is specified -> 10
6 print ( add_numbers ( a =2 , b =3) ) # positional a , b as keywords -> 5
7
8 # Output :
9 # 8
10 # 10
11 # 5
Syntax Rule
3.2 Modules
A module is simply a file containing Python code (definitions and functions) that can be reused
in other scripts. Python’s power lies in its massive library ecosystem, allowing you to extend
the language’s capabilities significantly.
General Syntax
import module_name
from module_name import function_name
1 import math
2
3 print ( math . sqrt (16) ) # Output : 4.0
4 print ( math . pi ) # Output : 3.14 15926 535897 93
5
16
5 print ( arr ) # Output : [1 2 3]
Important Note
For the import to work, [Link] must be in the same directory as your main
script. If you move your module, Python will raise a ModuleNotFoundError.
20 return True
17
21
22 def c e l s i u s _ t o _ f a h r e n h e i t ( c ) :
23 # """ Converts Celsius degrees to Fahrenheit ."""
24 return ( c * 9/5) + 32
• Reusability: You can copy scientific_tools.py into all your future projects.
18
6 print ( arr * 2) # Multiplies all elements -> [2 , 4 , 6 , 8]
7 print ( np . mean ( arr ) ) # Mean -> 2.5
19
The following table summarizes 15 key modules commonly used in academic and practical
projects. Each module is listed with its primary purpose and typical use cases to help students
and practitioners quickly understand their role in Python programming.
20
Exercises
3.5 Exercise 1
Write a program computing the factorial of an integer.
3.6 Exercise 2
Write a program generating the Fibonacci sequence up to a given number of terms.
3.7 Exercise 3
Compute the mean of a list of numbers entered by the user.
3.8 Exercise 4
Write a program that checks if a given number is prime or not.
3.9 Exercise 5
Create a program that counts how many vowels are in a given string.
3.10 Exercise 6
Write a program to reverse a string entered by the user.
3.11 Exercise 7
Write a function that converts temperatures from Celsius to Fahrenheit.
3.12 Exercise 8
Write a program that finds the maximum and minimum values in a list of numbers.
3.13 Exercise 9
Create a program that prints all even numbers between 1 and 100 using a loop.
21
3.14 Exercise 10
Write a program that simulates a simple calculator using functions and ‘if-else‘ statements.
Operations: addition, subtraction, multiplication, division.
22
Solutions
23
3.19 Solution 5: Count Vowels
1 def c e l s i u s _ t o _ f a h r e n h e i t ( c ) :
2 return ( c * 9/5) + 32
3
4 c = float ( input ( " Temperature ␣ in ␣ Celsius : ␣ " ) )
5 f = celsius_to_fahrenheit (c)
6 print ( " Temperature ␣ in ␣ Fahrenheit : " , f )
24
13 print ( sub (a , b ) )
14 elif op == ’* ’:
15 print ( mul (a , b ) )
16 elif op == ’/ ’:
17 print ( div (a , b ) )
18 else :
19 print ( " Invalid ␣ operation " )
25