Python Programming : Advanced (Leaving Cert Computer Science): Revision Notes
Python Programming : Advanced
Nested Conditionals
Nested conditionals involve placing one conditional statement inside another. This is useful when you need to check multiple conditions in a hierarchical manner.
For example, an e-commerce platform wants to determine the discount to offer to different customers based on age and membership
- If the person's age is below 18 or 65 and above, they get a "Senior / Child Discount."
- If the person's age is 18 or above but below 65:
- If they are a member, they get a "Member Discount."
- If they are not a member, they get "No Discount."
age = 45
is_member = True
if age < 18 or age >= 65:
discount = "Senior / Child Discount"
elif 18 <= age < 65:
if is_member:
discount = "Member Discount"
else:
discount = "No Discount"
print("Discount : " + discount)
Nested Loops
Nested loops involve placing one loop inside another loop.
list1 = ['a', 'b', 'c']
list2 = [1, 2, 3]
for item1 in list1:
for item2 in list2:
print(f"({item1}, {item2})")
"""
(a, 1)
(a, 2)
(a, 3)
(b, 1)
(b, 2)
(b, 3)
(c, 1)
(c, 2)
(c, 3)
"""
- The outer loop (for item1 in list1) iterates over each element in list1.
- The inner loop (for item2 in list2) iterates over each element in list2 for each value of item1.
- Each pair (item1, item2) is printed.
Functions
Functions are a fundamental aspect of Python programming. They allow you to encapsulate code into reusable blocks, making your code more modular, readable, and easier to maintain.
We've been using functions this entire time such as print and range. Now we will write our own functions.
The basic syntax of a function is :
def function_name(parameters):
# Function body
# Code to execute
return result # Optional
Let's write a function that takes in a number, and return the square of that number.
def square(x):
return x * x
print(square(5)) # Output: 25
print(square(3)) # Output: 9
print(square(2)) # Output: 4
Recursion
Recursion is a programming technique where a function calls itself in order to solve smaller instances of the same problem.
It is often used to solve problems that can be broken down into smaller, repetitive tasks. Recursion is a powerful tool, but it must be used with care to avoid infinite loops and stack overflow errors.
A recursive function typically has two main components:
- Base Case: The condition under which the function stops calling itself, preventing infinite recursion.
- Recursive Case: The part of the function where it calls itself with a smaller or simpler input.
For example, in mathematics, the factorial of a non-negative integer is the product of all positive integers less than or equal to .
For instance, . Let's analyse the factorial of 5 closer.
We can observe how recursion is used to break down the factorial function into smaller factorials.
- is broken down into
- is broken down into and so on.
- Eventually, we reach a base case, which is 1.
def factorial(n):
# Base case: if n is 0, return 1
if n == 0:
return 1
else:
# Recursive case: return n * factorial(n - 1)
return n * factorial(n - 1)
Here is a visual representation of the recursive calls :
factorial(4)
-> 4 * factorial(3)
-> 3 * factorial(2)
-> 2 * factorial(1)
-> 1 * factorial(0)
-> 1