Chapter 1
Chapter 1
Institute of Sciences
Department of science and technology (ST) – 1st Year (LMD)
Course : Computer Science 2 ( S2)
Table of Contents
1. Introduction ........................................................................................................................................... 3
3. Procedures ............................................................................................................................................. 4
4. Functions ............................................................................................................................................... 7
7. Practices........................................................................................................................................... 15
✓ Decomposition: break the problem down into smaller sub-problems or tasks, making it easier
to tackle each component individually.
✓ Identifying patterns: look for recurring patterns or similarities among the sub-problems, which
can help in developing reusable solutions.
3. Procedures
3.1. What is a procedure?
- A procedure is a named block of code that performs a specific task or a set of tasks. It is a
sequence of instructions that can be executed one after another. Procedures help in breaking
down complex problems into smaller, manageable tasks, making the code more organized and
easier to understand.
- procedures simply execute their code and may perform actions or tasks and do not return any
value.
• Procedure Name:
- The procedure name should follow the same rules as variable names in Python.
Dr. Mansouri 2024/2025
- It should be descriptive and indicative of the task the procedure performs.
• Parentheses:
- After the procedure name, a pair of parentheses `()` is used.
- These parentheses may or may not contain parameters (placeholders for data passed into the
procedure).
• Colon:
- Following the parentheses, a colon `:` is used to indicate the start of the procedure's body. -
The colon is a required part of the syntax and signifies the beginning of an indented code block.
• Procedure body:
- The procedure body consists of one or more indented lines of code that define the actions or
tasks the procedure performs.
- All code within the procedure body must be indented to the same level to denote that it belongs
to the procedure.
• Indentation:
- Python uses indentation to define the scope of code blocks, including procedure bodies.
- All code within the procedure must be indented consistently, typically using four spaces for
each level of indentation.
• Whitespace:
- Whitespace (spaces or tabs) before the `def` keyword or after the colon is optional but
recommended for readability.
- Consistent use of whitespace helps improve code readability and maintainability
3.5.2. Calling Procedures
Usage:
- Understanding how to call procedures correctly is crucial for leveraging their functionality
within a program.
- Proper usage ensures that procedures receive the necessary input data to perform their tasks
effectively.
Syntax:
- To call a procedure in Python, you use its name followed by parentheses containing any
required arguments.
- If the procedure has no parameters, you still need to include empty parentheses `()`.
A function is a named block of code that performs a specific task and usually returns a value.
Functions are primarily used for computation and produce an output based on the input
parameters.
Function Name:
Following the `def` keyword, you specify the name of the function.
Function names should be descriptive and follow Python's identifier naming rules (e.g., no
spaces, start with a letter or underscore, etc)
Parameters (optional):
- Inside the parentheses `()`, you can specify parameters that the function accepts.
- Parameters are variables that you define in the function signature to receive input values.
Dr. Mansouri 2024/2025
- Parameters are separated by commas.
Colon `:`:
- After specifying the function name and parameters (if any), you end the function declaration
line with a colon `:`.
- The colon indicates the beginning of the function body.
- Indentation and Whitespace are also the same as procedures
4.3. Calling a Function
Once a function is defined, you can call it from anywhere in your code (after the declaration). To
call a function, simply use its name followed by parentheses `()`, optionally passing arguments if
the function expects them. If there's no return statement, the function returns `None` by default
4.4. Characteristics of Functions
- Return Value: Functions typically return a value.
- Pure Functions: Functions are often pure, meaning they produce the same output for the same
input and do not have side effects.
- Usage: Functions are used when you need to compute a value based on input parameters.
4.5. Documentation String (Docstring)
Is a string literal that occurs as the first statement in a module, function, class, or method definition
in Python. It is used to provide documentation about the purpose and usage of the code element it
precedes. Docstrings are optional but highly recommended as they serve as valuable documentation
for your code.
Example:
2-
3-
4-
2.
4.
6.
7. Practices
• Write a procedure that takes a list as input and prints a new list with duplicates removed.
• Write a procedure that takes two strings as input and prints whether they are anagrams of each
other or not.
• Write a Python function named check_prime_number that takes a number as input and returns
a Boolean value indicating if the number is prime or not.
• Write a Python function named find_common_elements that takes two lists as input and returns
a list containing the common elements between the two lists.