Cp-i Lab Manual Python Final (1)
Cp-i Lab Manual Python Final (1)
Semester: I
First Year
Alard University
Mini Tic Tac Toe game using GUI for two players 28
Project - c
Apparatus/Software Required:
● A computer with Python 3 installed.
● Python IDE (like PyCharm, Jupyter Notebook, or any text editor with Python
support).
Problem Statement:
Write a program that takes the basic pay of an employee as input and calculates the
Gross Salary and Net Salary. The calculations include:
Apparatus/Software Required:
● A computer with Python 3 installed.
● Python IDE (like PyCharm, Jupyter Notebook, or any text editor with Python
support).
Algorithm:
1. Start the program.
2. Prompt the user to input the basic pay of the employee.
3. Calculate HRA as 10% of the basic pay.
4. Calculate TA as 5% of the basic pay.
5. Calculate the Gross Salary as the sum of basic pay, HRA, and TA.
6. Calculate Professional Tax as 2% of the Gross Salary.
7. Calculate the Net Salary by subtracting the Professional Tax from the Gross
Salary.
8. Display the Gross Salary and Net Salary.
9. End the program.
Source Code:
Conclusion:
The program successfully calculates the Gross and Net Salary of an employee based
on their basic pay, including allowances and deductions.
III. Algorithm:
1. Start the program.
2. Prompt the user to input the number of elements (N).
3. Initialize an empty list to store the numbers.
4. Using a loop, accept N numbers from the user and store each number in the
list.
5. Calculate the maximum value in the list using the max() function.
6. Calculate the minimum value in the list using the min() function.
7. Calculate the sum of all elements in the list using the sum() function.
8. Calculate the average of the list by dividing the sum by N.
9. Display the maximum, minimum, sum, and average of the list.
10. End the program.
VII. Conclusion:
The program successfully accepts a list of numbers from the user, then computes and
displays the maximum, minimum, sum, and average of those numbers.
Problem Statement:
Write a program that:
Apparatus/Software Required:
● A computer with Python 3 installed.
● Python IDE (like PyCharm, Jupyter Notebook, or any text editor with Python
support).
Algorithm:
1. Start the program.
2. Prompt the user to enter the marks for five courses.
3. Check if the student has scored at least 40 marks in each course:
○ If any mark is less than 40, the student fails.
4. Calculate the aggregate percentage as the average of the five course marks.
5. Determine the grade based on the aggregate percentage:
○ If aggregate ≥ 75%, assign “Distinction.”
○ If 60% ≤ aggregate < 75%, assign “First Division.”
○ If 50% ≤ aggregate < 60%, assign “Second Division.”
○ If 40% ≤ aggregate < 50%, assign “Third Division.”
6. Display the pass/fail status, aggregate percentage, and grade.
7. End the program.
Source Code:
Observations:
1. The program accurately identifies if the student has passed or failed based on
the marks of each course.
2. The program calculates the aggregate percentage and assigns the correct grade
based on the specified criteria.
Conclusion:
The program successfully calculates the aggregate percentage of a student's marks,
checks pass/fail status for individual courses, and assigns an appropriate grade based
on the aggregate.
Problem Statement:
Write a program that allows the user to:
Algorithm:
1. Start the program.
2. Display a menu for the user to select an operation:
○ 1: Addition
○ 2: Subtraction
○ 3: Multiplication
○ 4: Division
○ 5: Exponentiation (xyx^yxy)
○ 6: Factorial (x!x!x!)
3. Take the user’s choice as input.
4. Based on the chosen operation, do the following:
○ If the operation is addition, subtraction, multiplication, or division,
take two numbers as input and perform the operation.
○ If the operation is exponentiation, take two numbers as input (base and
exponent) and calculate the power.
○ If the operation is factorial, take one number as input and calculate its
factorial.
5. Display the result of the operation.
6. End the program.
Source Code:
Observations:
1. The program successfully performs basic arithmetic operations and special
operations as selected by the user.
2. The program handles division by zero and factorial for negative numbers,
displaying appropriate error messages.
Conclusion:
The program simulates a simple calculator that performs addition, subtraction,
multiplication, division, exponentiation, and factorial calculations based on the user's
choice.
Problem Statement:
Write a program that:
Algorithm:
1. Start the program.
2. Accept a number from the user.
3. Calculate the square root of the number using the math.sqrt() function.
4. Calculate the square of the number by multiplying it by itself.
5. Calculate the cube of the number by raising it to the power of 3.
6. Check if the number is prime by checking divisibility from 2 up to the square
root of the number.
7. Calculate the factorial of the number using the math.factorial()
function.
8. Find the prime factors of the number by:
○ Dividing by 2 until it is no longer divisible by 2.
○ Checking odd numbers from 3 upwards and dividing until no longer
divisible.
9. Display all the results.
Flowchart:
Source Code:
Observations:
1. The program correctly calculates and displays the square root, square, and
cube of the input number.
2. It accurately checks if the number is prime and calculates its factorial.
3. It correctly determines the prime factors of the input number.
Conclusion:
The program successfully performs the required mathematical operations on a
user-given number, including calculating the square root, square, cube, checking for
primality, calculating the factorial, and determining the prime factors.
Problem Statement:
Write a program that:
Algorithm:
1. Start the program.
2. Take a three-digit number as input from the user.
3. Extract the digits of the number:
○ Divide the number by 100 to get the hundreds place digit.
○ Use modulus and division to get the tens place digit.
○ Use modulus to get the units place digit.
4. Calculate the sum of the cubes of the digits.
5. Check if the sum of the cubes of the digits is equal to the original number:
○ If they are equal, the number is an Armstrong number.
○ If they are not equal, the number is not an Armstrong number.
6. Display the result.
7. End the program.
Flowchart:
Source Code:
Observations:
1. The program successfully identifies Armstrong numbers.
2. The program correctly calculates the sum of the cubes of the digits and checks
if it matches the input number.
3. Edge cases such as numbers with zero digits (e.g., 370) are handled properly.
Conclusion:
The program effectively checks if a given three-digit number is an Armstrong number
by comparing the sum of the cubes of its digits with the original number.
Problem Statement:
Write a Python program that:
Algorithm:
1. Start the program.
2. Input a string from the user.
3. Calculate the length of the string using the len() function and display it.
4. Reverse the string using slicing and display it.
5. Check equality:
○ Ask for a second string from the user.
○ Compare the original string with the second string and display whether
they are equal.
6. Check if the original string is a palindrome:
○ Compare the original string with its reverse.
○ If they are the same, display that the string is a palindrome; otherwise,
it is not.
7. Check if a substring exists within the original string:
○ Accept a substring from the user.
○ Use the in keyword to check if the substring exists in the string and
display the result.
8. End the program.
Source Code:
Observations:
1. The program successfully calculates the length of the string and displays it.
2. The program correctly reverses the string and checks for palindrome
properties.
3. The program accurately checks for equality between two strings.
4. The program correctly identifies whether a substring exists within the input
string.
Conclusion:
The program effectively performs various operations on the user-given string,
including calculating its length, reversing it, checking equality with another string,
verifying if it is a palindrome, and finding a substring within it.
Problem Statement:
Write a Python program that defines an Employee class to store and manipulate
details of employees. Define methods to:
Algorithm:
1. Define the Employee class with the following attributes:
○ name: Employee's name
○ designation: Employee's designation
○ gender: Employee's gender
○ date_of_joining: Employee's joining date
○ salary: Employee's salary
2. Implement a class variable employee_count to keep track of the total
number of employees.
3. Define the following methods in the Employee class:
○ __init__: Initialize each employee object with the above attributes
and increment employee_count.
○ display_details: Display the details of the employee.
4. Define the following standalone functions:
Flowchart:
Source Code:
Observations:
1. The program accurately maintains and displays the total number of employees.
2. It correctly counts and displays the number of male and female employees.
3. It successfully identifies employees earning above a certain salary threshold.
4. It correctly filters employees by their designation.
Conclusion:
The program effectively demonstrates the use of classes and functions to manage
employee records, including counting, filtering, and displaying information based on
various criteria.
Problem Statement:
Write a Python program that:
Algorithm:
1. Start the program.
2. Open the source file in read mode.
3. Read the content of the source file.
4. Process the content:
○ Replace all full stops (.) with commas (,).
○ Convert lowercase letters to uppercase.
○ Convert uppercase letters to lowercase.
5. Open the destination file in write mode.
6. Write the modified content to the destination file.
7. Close both files.
8. Display a message indicating that the file has been copied and modified
successfully.
9. End the program.
Source Code:
Observations:
1. The program successfully reads content from the source file and applies the
required modifications.
2. The modified content is written correctly to the destination file.
3. The program handles cases where the source file may not exist.
Conclusion:
The program effectively copies the contents of a file to another with specified
character modifications, demonstrating file handling and string manipulation in
Python.
Objective:
To develop a Python program that uses a class-based approach to manage a store
inventory system. The program should:
Problem Statement:
Write a Python program that defines a Product class to store and display product
details. Create a Store class that manages the inventory and provides a menu of
products to the user. Allow the user to place an order, specify the quantity of each
product, and generate a bill.
Algorithm:
1. Define the Product class with the following attributes:
○ product_code: Unique identifier for the product.
○ name: Name of the product.
○ price: Price of the product.
2. Define the Store class with the following attributes and methods:
○ Attributes:
■ A list of Product objects to store the inventory.
○ Methods:
■ add_product: Adds a product to the store.
■ display_menu: Displays the list of products with their
details.
■ generate_bill: Calculates the total bill based on the user’s
selected products and quantities.
3. Implement the main program:
○ Create instances of Product and add them to the Store.
○ Display the product menu.
○ Accept user input for products and quantities.
○ Generate and display the bill.
Source Code:
Observations:
1. The program correctly displays the list of available products with their prices.
2. The program accepts multiple products and quantities from the user.
3. The program accurately calculates the total bill based on user-selected
products and quantities.
Conclusion:
The program successfully demonstrates the use of classes to manage product data and
generate a bill, illustrating object-oriented programming concepts in Python.
Objective:
To design and develop a GUI-based calculator using Python that performs basic
arithmetic operations (addition, subtraction, multiplication, division) and complex
functions (power, square root, factorial, trigonometric operations).
Problem Statement:
Create a calculator program with a Graphical User Interface (GUI) in Python. The
calculator should support:
Apparatus/Software Required:
● A computer with Python 3 installed.
● Python libraries: tkinter (for GUI), math (for complex functions).
● Python IDE (like PyCharm, Jupyter Notebook, or any text editor with Python
support).
Algorithm:
1. Import Required Libraries:
○ Import tkinter for creating the GUI.
○ Import math for complex functions.
2. Create the Main Calculator Window:
○ Initialize the main application window.
○ Set window title and size.
3. Design the Input and Display Area:
○ Use an Entry widget for the calculator display to show entered
numbers and results.
4. Define Button Layout:
○ Create buttons for numbers (0-9), basic operations (+, -, *, /), and
complex operations (sqrt, x^y, sin, cos, tan, factorial).
○ Arrange buttons in a grid layout.
5. Implement Event Handlers for Buttons:
Flowchart:
Source Code:
Observations:
1. The calculator performs basic arithmetic accurately.
2. It handles complex functions like square root, factorial, and trigonometric
functions.
3. The GUI is interactive and user-friendly.
Conclusion:
The program successfully implements a GUI-based calculator using Python
tkinter, which supports both basic and advanced mathematical operations. This
project demonstrates effective use of GUI programming and math functions in
Python.
Problem Statement:
Create a Python program with a graphical interface that simulates rolling a pair of
dice. The dice roll should randomly display numbers from 1 to 6 each time the user
clicks a button. The program should show the results for both dice and allow repeated
rolls.
Apparatus/Software Required:
● A computer with Python 3 installed.
● Python libraries: tkinter (for GUI) and random (for generating random
numbers).
● Python IDE (like PyCharm, Jupyter Notebook, or any text editor with Python
support).
Algorithm:
1. Import Required Libraries:
○ Import tkinter for creating the GUI.
○ Import random for generating random numbers.
2. Create the Main Application Window:
○ Initialize the main window.
○ Set window title and size.
3. Design the Display Area for Dice:
○ Use Labels to show the result of each dice roll.
4. Create a Button to Roll the Dice:
○ Define a button that triggers the dice roll.
5. Define a Function to Roll Dice:
○ Generate two random numbers between 1 and 6.
○ Display the random numbers on the dice display area.
6. Run the Application Loop:
○ Start the main event loop to make the dice roll simulator interactive.
Source Code:
Observations:
1. Each click on the "Roll Dice" button generates a new pair of random numbers.
2. The simulator correctly displays the outcome of the dice rolls.
3. The GUI is interactive and allows continuous rolling of dice.
Conclusion:
The program successfully simulates a dice roll using Python’s tkinter and
random libraries, providing a simple and effective interactive GUI. This project
demonstrates basic GUI programming and random number generation in Python.
Problem Statement:
Create a Python program with a GUI to simulate the classic Tic Tac Toe game. The
game should:
1. Allow two players to take turns placing their marks (X and O) on a 3x3 grid.
2. Detect if a player has won by aligning three of their marks horizontally,
vertically, or diagonally.
3. Announce the winner or declare a draw if all spaces are filled without a
winner.
Apparatus/Software Required:
● A computer with Python 3 installed.
● Python libraries: tkinter for creating the GUI.
● Python IDE (like PyCharm, Jupyter Notebook, or any text editor with Python
support).
Algorithm:
1. Import Required Libraries:
○ Import tkinter for GUI creation.
2. Initialize Main Application Window:
○ Set up the main window with a title and dimensions.
3. Create a 3x3 Grid of Buttons:
○ Use Button widgets for each cell in the Tic Tac Toe grid.
○ Arrange buttons in a 3x3 grid layout.
4. Define Game Logic:
○ Use a 2D list to represent the board state.
○ Define a function to handle button clicks, updating the board with the
current player’s mark (X or O).
○ Alternate turns between Player X and Player O after each click.
5. Check for Win or Draw:
Flowchart:
Source Code:
Observations:
1. The game alternates turns between Player X and Player O.
2. The program correctly detects wins across rows, columns, and diagonals.
3. A message box displays the result, either announcing the winner or declaring a
draw.
Conclusion:
The program successfully implements a Tic Tac Toe game with a GUI, allowing two
players to play interactively. It demonstrates effective use of GUI programming and
basic game logic with win and draw conditions.