Building Functions
Business Scenario
Welcome!
Today is your 11th day as a Junior Data Analyst at the Indian Railways Reservation Department.
Previously, you optimized the system to generate passenger records and reservation details using shorter and cleaner code.
Today, your manager wants you to improve the system further by developing reusable booking operations to organize reservation tasks for ticket booking, passenger verification, fare calculation, and ticket generation.
To complete this task successfully, you will use Python to:
Organize railway operations into reusable modules
Reduce repetitive code
Improve readability and maintenance
Pre-Lab Preparation
git pull origin branchNameGit Pull
Topic: Develop Booking Functions for Reservation System
1) Creating Functions & Calling Functions
2) Returning Values
A function in Python is a reusable block of code that performs a specific task.
It helps to break a program into smaller, manageable parts.
Functions can take input values (parameters) and may return a result.
They improve code readability, reusability, and organization.
1
Task 1: Create Basic Welcome Function
Define a welcome function
def welcome_message():
print("===================================")
print(" Indian Railways Reservation ")
print("===================================")
print("Welcome to Railway Reservation System")Call the function
2
welcome_message()Output
3
1
Task 2: Create Passenger Booking Function
Define booking function with arguments
def book_ticket(passenger_name, train_name):
print("Passenger Name :", passenger_name)
print("Train Name :", train_name)
print("Reservation Status : Confirmed")Call the booking function
2
book_ticket(
"Rahul Sharma",
"Rajdhani Express")Output
3
1
Task 3: Create Fare Calculation Function
Define fare calculation function
def calculate_fare(ticket_price, passengers):
total_amount = ticket_price * passengers
return total_amountCall the function
2
final_fare = calculate_fare(2450, 2)
print("Final Fare :", final_fare)Output
3
Task 4: Create Passenger Verification Function
Call the function
2
def verify_passenger(age):
if age >= 18:
return "Eligible for Reservation"
else:
return "Not Eligible"result = verify_passenger(25)
print(result)Task 5: Build Ticket Generation Function
1
Create ticket generation function
Output
3
def generate_ticket(
passenger_name,
train_name,
seat_number):
print("===================================")
print(" Railway Reservation Ticket ")
print("===================================")
print("Passenger Name :", passenger_name)
print("Train Name :", train_name)
print("Seat Number :", seat_number)Output
3
Call the function
2
generate_ticket(
"Priya Singh",
"Shatabdi Express",
"B2-21")Task 6: Build Final Booking Function
1
Create final booking function
def final_booking_system():
passenger_name = "Rahul Sharma"
train_name = "Rajdhani Express"
seat_number = "A1-12"
ticket_price = 2450
print("===================================")
print(" Indian Railways Reservation ")
print("===================================")
print("Passenger Name :", passenger_name)
print("Train Name :", train_name)
print("Seat Number :", seat_number)
print("Ticket Price :", ticket_price)
print("Reservation Confirmed")Call the function
2
final_booking_system()Output
3
Save File as : booking_functions.ipynb
4
Great job!
You have successfully learnt how to define and call functions and return statements for booking and fare calculations to build reusable ticket booking and reservation system
Checkpoint
Git Push
git push origin branchNameNext-Lab Preparation
Topic: Implement Advanced Function Techniques
1) Use default arguments
2) Variable length args
3) Recursion