python programs lab manual, Exercises of Computer Science

lab manual of python programs from exercise 1 to exercise 5 for ug programme for the year 2025 -2026 prepared by anitha

Typology: Exercises

2023/2024

Uploaded on 11/06/2025

vanitha-7
vanitha-7 🇮🇳

1 document

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PRACTICAL VII PYTHON PROGRAMMING LAB- 21UCSP07
LIST OF PROGRAMS:
1. Create a simple calculator to do all the arithmetic operations.
2. Write a program to use control flow tools like if.
3. Write a program to use for loop.
4. Data structures a. use list as stack. b. use list as queue. c. tuple, sequence.
5. Create new module for mathematical operations and use in your program.
6. Write a program to read and write files, create and delete directories.
7. Write a program with exception handling.
8. Write a program using classes.
9. Connect with MySQL and create address book.
10. Write a program using string handling and regular expressions.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download python programs lab manual and more Exercises Computer Science in PDF only on Docsity!

PRACTICAL – VII – PYTHON PROGRAMMING LAB- 21UCSP

LIST OF PROGRAMS:

1. Create a simple calculator to do all the arithmetic operations.

2. Write a program to use control flow tools like if.

3. Write a program to use for loop.

4. Data structures a. use list as stack. b. use list as queue. c. tuple, sequence.

5. Create new module for mathematical operations and use in your program.

6. Write a program to read and write files, create and delete directories.

7. Write a program with exception handling.

8. Write a program using classes.

9. Connect with MySQL and create address book.

10. Write a program using string handling and regular expressions.

1. SIMPLE CALCULATOR

AIM:

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

Function to add two numbers

def add(x, y): return x + y

Function to subtract two numbers

def subtract(x, y): return x - y

Function to multiply two numbers

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:

  1. Add
  2. Subtract
  3. Multiply
  4. Divide
  5. Exit Enter choice (1/2/3/4/5): 1 Enter first number: 10 Enter second number: 5 Result: 10.0 + 5.0 = 15. Select operation:
  6. Add
  7. Subtract
  8. Multiply
  9. Divide
  10. Exit Enter choice (1/2/3/4/5): 2 Enter first number: 10 Enter second number: 5 Result: 10.0 - 5.0 = 5.

Select operation:

  1. Add
  2. Subtract
  3. Multiply
  4. Divide
  5. Exit Enter choice (1/2/3/4/5): 3 Enter first number: 10 Enter second number: 5 Result: 10.0 * 5.0 = 5 0. Select operation:
  6. Add
  7. Subtract
  8. Multiply
  9. Divide
  10. Exit Enter choice (1/2/3/4/5): 4 Enter first number: 8 Enter second number: 2 Result: 8.0 / 2.0 = 4. Select operation:
  11. Add
  12. Subtract
  13. Multiply
  14. Divide

PROGRAM

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}")

ALGORITHM:

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

use list as stack

stack = [3, 4, 5] stack.append(6) stack.append(7) print("Stack:", stack) popped = stack.pop() print("Popped:", popped) print("Modified Stack:", stack)

use list as queue

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, tuple in python

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:&gt;pg5.py Mathematical operations using modulePlease select an operation:

  1. Add
  2. Subtract
  1. Multiply
  2. Divide Enter the number of the operation you want to perform: 1 Enter the first number: 3 Enter the second number: 8 The sum of 3.0 and 8.0 is

C:&gt;pg5.py Mathematical operations using modulePlease select an operation:

  1. Add
  2. Subtract
  3. Multiply
  4. Divide Enter the number of the operation you want to perform: 2 Enter the first number: 10 Enter the second number: 6 The difference of 10.0 and 6.0 is 4. C:&gt;pg5.py Mathematical operations using modulePlease select an operation:
  5. Add
  6. Subtract
  7. Multiply
  8. Divide Enter the number of the operation you want to perform: 3 Enter the first number: 4 Enter the second number: 6 The product of 4.0 and 6.0 is 24.