Implement Advanced Functions
Business Scenario
Welcome!
Today is your twelfth day as a Junior Data Analyst at the Indian Railways Reservation Department.
Previously you created functions for the reservation system to perform multiple operations from reusable functions.
Today, your manager wants you to improve the system further by making the booking functions more flexible and automated. The department now requires the application to handle default booking values, process multiple passengers dynamically and automate repetitive reservation operations.
To complete this task successfully, you must use Python to:
Automate repetitive reservation tasks
Improve function reusability
Pre-Lab Preparation
git pull origin branchNameGit Pull
Topic: Implementing Advanced Functions
1) Use default arguments and variable length argumnets
2) Recursion
In Python, arguments are the values you pass into a function when you call it. They let you send data to the function so it can work with that data.
Example :
def greet(name):
print("Hello,", name)
greet("Alice")name → parameter (the variable in the function definition)"Alice" → argument (the actual value you pass)Types of arguments in Python :
1) Default Arguments
2) Variable - length arguments
a) *args → multiple positional arguments
b) **kwargs → multiple keyword arguments
1
Task 1: Create Function Using Default Arguments
Define reservation function
def reservation_status(
passenger_name,
ticket_status="Confirmed"):
print("Passenger Name :", passenger_name)
print("Ticket Status :", ticket_status)Call the function
2
reservation_status("Rahul Sharma")
reservation_status("Amit Verma", "Waiting")Output
3
1
Task 2: Use *args (Variable Length Arguments)
Create a function to handle multiple passenger names
def passenger_booking(*passengers):
print("Passenger List:")
for name in passengers:
print("Passenger :", name)Call the function with multiple variables
2
passenger_booking(
"Rahul Sharma",
"Priya Singh",
"Amit Verma",
"Neha Kapoor")Output
3
1
Task 3: Use **kwargs (Keyword Variable Arguments)
Create a function to store passenger ticket details dynamically
def ticket_details(**details):
print("Ticket Information:")
for key, value in details.items():
print(key, ":", value)Call the function
2
ticket_details(
passenger_name="Rahul Sharma",
train_name="Rajdhani Express",
seat_number="B1-21",
fare=2500,
status="Confirmed")Task 4: Create Recursive Seat Countdown Function
1
Define recursive function
Output
3
def seat_countdown(seats):
if seats == 0:
print("All Seats Booked")
return
print("Remaining Seats :", seats)
seat_countdown(seats - 1)Call the recursive function
2
seat_countdown(5)Output
3
Task 5: Create Recursive Passenger Verification Function
1
Define recursive verification function
def verify_passenger(passenger_number):
if passenger_number == 0:
print("Verification Completed")
return
print("Verifying Passenger :", passenger_number)
verify_passenger(passenger_number - 1)Call the function
2
verify_passenger(3)Output
3
Task 6: Build Final Advanced Booking Module
1
Create final booking function
def final_booking(
train_name="Rajdhani Express",
*seat_numbers
):
print("===================================")
print(" Railway Reservation ")
print("===================================")
print("Train Name :", train_name)
for seat in seat_numbers:
print("Allocated Seat :", seat)Call the function
2
final_booking(
"Rajdhani Express",
"B1-21",
"B1-22",
"B1-23")Output
3
Save File as : advanced_functions.ipynb
4
Great job!
You have successfully learned how to use default arguments and variable length arguments for automating reservation operations
Checkpoint
Git Push
git push origin branchNameNext-Lab Preparation
Topic: Apply Functional Programming for Data Handling
1) Use lambda
2) Apply map and filter
3) Use zip and enumerate