









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
lab manual of python programs from exercise 1 to exercise 5 for ug programme for the year 2025 -2026 prepared by anitha
Typology: Exercises
1 / 15
This page cannot be seen from the preview
Don't miss anything!










Write a python program to create a simple calculator that performs basic arithmetic operations such as addition, subtraction, multiplication, and division. ALGORITHM: STEP 1 : Define functions for addition, subtraction, multiplication, and division. STEP 2: Display a welcome message to the user. STEP 3: Enter a while loop to keep the calculator running until the user chooses to exit. STEP 4:Display the available operations (addition, subtraction, multiplication, division, or exit). STEP 5: Prompt the user to select an operation by entering a corresponding number (1, 2, 3, 4, or 5). STEP 6: If the user chooses an arithmetic operation (1, 2, 3, or 4): 6.1:Prompt the user to enter two numbers. Perform the selected arithmetic operation on the given numbers using the respective function. Display the result to the user. STEP 7: If the user chooses to exit (option 5) then exit the program. STEP 8 : If the user enters an invalid choice, Display an error message and prompt the user to enter a valid choice. STEP 9: Stop the Program. PROGRAM
def add(x, y): return x + y
def subtract(x, y): return x - y
elif choice == '5': print("Exiting the calculator. Goodbye!") break else: print("Invalid input. Please enter a valid choice.") OUTPUT Welcome to Simple Calculator Select operation:
Select operation:
age = int(input("Enter your age: ")) if age < 0: print("Age cannot be negative. Please enter a valid age.") elif age == 0: print("You are just a baby!") elif 0 < age < 18: print("You are a minor.") elif 18 <= age < 65: print("You are an adult.") else: print("You are a senior citizen.") OUTPUT Case 1: User inputs a negative age Enter your age: - 5 Output: Age cannot be negative. Please enter a valid age. Case 2: User inputs an age of 0 Enter your age: 0 Output: You are just a baby! Case 3: User inputs an age between 1 and 17 Enter your age: 15 Output: You are a minor. Case 4: User inputs an age between 18 and 64 Enter your age: 30 Output: You are an adult.
Case 5: User inputs an age of 65 or older Enter your age: 70 Output: You are a senior citizen. RESULT: Thus, the above program executed successfully. 3 .PROGRAM USING FOR LOOP. AIM: Write the Python program to demonstrate the for loop. ALGORITHM: Step 1: Get the input as number for print the multiplication table. Step 2: The program uses a for loop to iterate from 1 to 10 Step 3: Inside the for loop, It multiplies the entered number (num) with the current value of i (from 1 to 10). It prints the multiplication expression in the format num x i = num * i, Step 4: Stop the Program. PROGRAM num = int(input("Enter a number to print its multiplication table: ")) print(f"Multiplication table for {num}:") for i in range(1, 11): print(f"{num} x {i} = {num * i}")
Step 1: Start the Program. Step2: Using List as a Stack: 2.1: Initialize a list as a stack. 2.2: Push elements onto the stack using append () method. 2.3: Pop elements from the stack using pop () method. Step 3: Using List as a Queue: 3.1: Import deque from the collections module to create a more efficient queue. 3.2: Initialize a list as a queue. 3.3: Enqueue elements into the queue using append () method. 3.4: Dequeue elements from the queue using pop left () method. Step 4: Tuple as a Sequence: 4.1: Create a tuple containing various types of elements. 4.2: Access elements in the tuple using indexing ([]). Step 5: Stop the Program. PROGRAM
stack = [3, 4, 5] stack.append(6) stack.append(7) print("Stack:", stack) popped = stack.pop() print("Popped:", popped) print("Modified Stack:", stack)
queue = [3, 4, 5] queue.append(6) queue.append(7) print("Queue:", queue) popped = queue.pop(0) print("Popped:", popped) print("Modified Queue:", queue)
sequence = [3, 4, 5] tuple_example = (3, 4, 5) print("Sequence:", sequence) print("Tuple:", tuple_example) OUTPUT Stack: [3, 4, 5, 6, 7] Popped: 7 Modified Stack: [3, 4, 5, 6] Queue: [3, 4, 5, 6, 7] Popped: 3 Modified Queue: [4, 5, 6, 7] Sequence: [3, 4, 5] Tuple: (3, 4, 5) RESULT: Thus, the above program executed successfully.
import math _operations print("Mathematical operations using module") print("Please select an operation:") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") operation = input("Enter the number of the operation you want to perform: ") num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: "))if operation == '1': print(f"The sum of {num1} and {num2} is {math_operations.add(num1, num2)}") elif operation == '2': print(f"The difference of {num1} and {num2} is {math_operations.subtract(num1, num2)}") elif operation == '3': print(f"The product of {num1} and {num2} is {math_operations.multiply(num1, num2)}") elif operation == '4': print(f"The quotient of {num1} and {num2} is {math_operations.divide(num1, num2)}") else: print("Invalid input!") OUTPUT: C:>pg5.py Mathematical operations using modulePlease select an operation:
C:>pg5.py Mathematical operations using modulePlease select an operation: