02_20240927_ProceduralProgramming
02_20240927_ProceduralProgramming
Key Concepts:
• Sequential Execution: Instructions are executed in order.
• Procedures (Functions): Encapsulated code blocks that perform a
specific task.
• Variables: Store data used in procedures.
• Control Structures: Decision-making (if-else) and loops (for, while).
Procedural Programming
Key Concepts:
Sequential Execution: Instructions are executed in order.
def my_function():
x = 5 # Local variable
print("Inside function:", x) # Local x is used
my_function()
print("Outside function:", x) # Global x is used
Procedural Programming
4. Control Structures in Procedural Programming:
Procedural programming heavily relies on control structures
to define the flow of execution.
Conditionals (If-Else Statements):
Allows decision-making in code based on conditions.
check_number(10)
Procedural Programming
4. Control Structures in Procedural Programming:
count = 0
while count < 5:
print("Count:", count)
count += 1
Procedural Programming
5. Example: Simple Python Calculator using Functions:
Procedural programming enables the creation of organized
and reusable code like a calculator with functions.
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b != 0:
return a / b
else:
return "Error: Division by zero"
# Example usage
num1 = 10
num2 = 5
print("Addition:", add(num1, num2))
print("Subtraction:", subtract(num1, num2))
print("Multiplication:", multiply(num1, num2))
print("Division:", divide(num1, num2))
Procedural Programming
6. Real-World Application:
Key Takeaways:
Procedural programming breaks complex problems into smaller,
manageable tasks through functions.
Visual Example:
Imagine each data type as a box that holds values of a specific
kind:
# Valid Identifiers
age = 25
student_name = "Alice"
height_in_meters = 1.75
# Invalid Identifiers
1st_place = "Winner" # Starts with a number
student-name = "Alice" # Contains hyphen (-), which is not allowed
Introduction to Data Types in Python
2. Identifiers and Keywords:
Best Practices:
Use descriptive names: student_name instead of x.
Keywords:
Definition: Keywords are reserved words in Python that have a
special meaning. These cannot be used as identifiers.
Definition:
• The int type in Python represents whole numbers, both positive
and negative, without a decimal point.
Memory Usage:
• Python automatically adjusts the size of integers based on the
magnitude of the number. Unlike other languages, Python does
not restrict integer size.
Key Characteristics:
• No size limit.
• Python supports both positive and negative integers.
Introduction to Data Types in Python
3. Integral Types (int):
print("Addition:", a + b) # Output: 19
print("Subtraction:", a - b) # Output: 11
print("Multiplication:", a * b) # Output: 60
print("Division:", a / b) # Output: 3.75 (Float result)
Introduction to Data Types in Python
3. Integral Types (int):
Integer Division (//):
Integer Division returns the floor value of the division, discarding
the decimal part.
Key Characteristics:
Follows the IEEE 754 standard for floating-point arithmetic.
Can represent very large or small numbers using scientific notation.
x = 10.5
y = 4.2
value = 3.14159
print(round(value, 2)) # Output: 3.14 (rounded to 2 decimal places)
Introduction to Data Types in Python
3. Floating Point Data Types (int):
Scientific Notation with Floats:
Python allows very large or small numbers to be written in scientific notation
using e (exponent) notation.