Notes
Parts of a Procedure
- A group of programming instructions, also known as methods or functions.
- Parameters are input values of a procedure, and arguments are the values of the parameters when the procedure is called.
- A procedure call interrupts the sequential execution of statements, executes the procedure’s statements, and then returns control to the point immediately following the call.
Procedure Example:
- Procedures allow you to reuse sets of instructions, enhancing code reusability.
- A procedure in Python is defined using the def keyword and can take parameters.
- Parameters are the input values to a procedure, and when you call a procedure, you provide arguments.
- A procedure can return a value using the return statement. The returned value can be assigned to a variable and manipulated.
Modularity and Code Simplification:
- Modularity involves breaking a program into separate sub-programs.
- Procedures help simplify code, improve readability, and allow code reuse.
Reusability and Flexibility:
- Procedures can be reused with different arguments, making code flexible.
- Changing a procedure’s code only affects the procedure, not the entire program.
Procedure Abstraction:
- Procedures are an example of abstraction, allowing you to call a procedure without knowing how it works.
- Abstraction helps solve complex problems based on solutions to smaller sub-problems.
Anatomy of Python Classes:
- Class definition starts with the class keyword and is followed by a colon.
- Attributes are variables that belong to a class and are accessed using the dot notation. Methods are functions defined within a class and operate on objects or modify attributes.
- The “init” method is a constructor called when an object is created.
- “self” represents the instance of the class and is used to access attributes and methods.
Homework
# HW 1: Procedural abstraction
def procedural_abstraction(a, b):
result = a + b
return result
# Example usage of procedural_abstraction
x = 7
y = 5
result = procedural_abstraction(x, y)
print(f"The sum of {x} and {y} is {result}")
# HW 2: Summing machine
def summing_machine(first_number, second_number):
sum_result = first_number + second_number
return sum_result
# Calculate the sum of 7 and 5 using the summing_machine function
num1 = 7
num2 = 5
total = summing_machine(num1, num2)
print(f"The sum of {num1} and {num2} is {total}")