0% found this document useful (0 votes)
4 views2 pages

Ex1 Python Notes

The document outlines two Python programming lab exercises. The first program performs basic arithmetic operations based on user input, while the second program determines if a person is a senior citizen based on their age. Both programs are designed to be user-friendly and execute successfully, providing appropriate outputs based on the inputs given.

Uploaded by

hrbharthi1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Ex1 Python Notes

The document outlines two Python programming lab exercises. The first program performs basic arithmetic operations based on user input, while the second program determines if a person is a senior citizen based on their age. Both programs are designed to be user-friendly and execute successfully, providing appropriate outputs based on the inputs given.

Uploaded by

hrbharthi1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1BPLC105B/205B PYTHON PROGRAMMING LAB

PROGRAMS
1. a. Develop a python program to read 2 numbers from the keyboard and perform the basic arithmetic
operations based on the choice. (1-Add, 2-Subtract, 3-Multiply, 4-Divide).
Aim
To develop a Python program that reads two numbers from the keyboard and performs basic arithmetic
operations (Addition, Subtraction, Multiplication, Division) based on the user’s choice using a looping
(menu-driven) calculator.
Algorithm
1. Read the first number from the user.
2. Read the second number from the user.
3. Display the menu of operations:
1 → Addi on
2 → Subtrac on
3 → Mul plica on
4 → Division
5 → Exit
4. Read the user’s choice.
5. If choice = 1, compute sum of the two numbers and display the result.
6. Else if choice = 2, compute difference and display the result.
7. Else if choice = 3, compute product and display the result.
8. Else if choice = 4, check if second number ≠ 0
 If yes, compute division and display result
 Else, display “Division by zero not allowed”
9. Else if choice = 5, exit the loop/program.
10. Else display “Invalid choice”.
Program
# Read two numbers from the keyboard
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# Display menu
print("\nSelect operation:")
print("1 - Add")
print("2 - Subtract")
print("3 - Multiply")
print("4 - Divide")

# Read user choice


choice = int(input("Enter your choice (1-4): "))

# Perform operation based on choice


if choice == 1:
print("Result:", num1 + num2)
elif choice == 2:
print("Result:", num1 - num2)
elif choice == 3:
print("Result:", num1 * num2)
elif choice == 4:

Prof. Parthasarathy PV (M) 8951523822 URL: [Link]/vtu/python/ Page -1-


1BPLC105B/205B PYTHON PROGRAMMING LAB

if num2 != 0:
print("Result:", num1 / num2)
else:
print("Error: Division by zero is not allowed.")
else:
print("Invalid choice. Please select between 1 and 4.")
Result
The Python program was executed successfully. It performed Addition, Subtraction, Multiplication, and
Division of two input numbers based on the user’s choice and repeated the operations until the exit option
was selected.

============================================================================
1. b. Develop a program to read the name and year of birth of a person. Display whether the person is a
senior citizen or not.
Aim
To develop a Python program to read the name and year of birth of a person and determine whether the
person is a senior citizen or not.
Algorithm
1. Start
2. Read the name of the person.
3. Read the year of birth of the person.
4. Obtain the current year.
5. Calculate the age of the person:
Age = Current Year − Year of Birth
6. If age ≥ 60
o Display the message “<Name> is a Senior Citizen”.
7. Else
o Display the message “<Name> is not a Senior Citizen”.
Program
from datetime import date
# Read input
name = input("Enter name: ")
year_of_birth = int(input("Enter year of birth: "))

# Get current year


current_year = [Link]().year

# Calculate age
age = current_year - year_of_birth

# Check senior citizen


if age >= 60:
print(name, "is a Senior Citizen.")
else:
print(name, "is not a Senior Citizen.")
Result
The program was executed successfully. It read the name and year of birth of a person, calculated
the age using the current year, and displayed whether the person is a senior citizen (age 60 years or
above) or not.

Prof. Parthasarathy PV (M) 8951523822 URL: [Link]/vtu/python/ Page -2-

You might also like