EXPERIMENT 1
AIM
Basic data types and operators: Create a program that prompts the user for their name and age
and prints a personalized message.
SOFTWARE USED
VS code , Python 3.14.3
THEORY
In programming, data types define the type of data a variable can store. Common basic data
types include:
String – Used to store text such as a person’s name.
Integer (int) – Used to store whole numbers such as age.
This experiment demonstrates how user input is taken, stored in variables of appropriate data
types, and then processed to generate meaningful output.
Concept Used
1. Variables
Variables are memory locations used to store data values entered by the user.
Example:
name stores the user’s name.
age stores the user’s age.
2. Basic Data Types
String → Stores textual information (e.g., name).
int → Stores numeric values (e.g., age).
3. Input Operation
The program prompts the user to enter their name and age using input statements. This allows
interaction between the user and the system.
4. Output Operation
After receiving input, the program prints a personalized message using output statements.
5. Operators
The concatenation operator (such as + in many languages) is used to combine text and
variables to create the final message.
Vishesh Kr Mahor 01814802723 6th sem CS
Algorithm:
1. Start
2. Input user name
3. Input user age
4. Input user city
5. Input user course
6. Input user hobby
7. Display the personal profile details
8. Check age condition
o If age < 18 → display student life message
o Else if age ≤ 25 → display career-building message
o Else → display growth & achievement message
9. Display final greeting message
10. End
FLOWCHART
Vishesh Kr Mahor 01814802723 6th sem CS
CODE
name = input("Enter your name: ")
age = int(input("Enter your age: "))
city = input("Enter your city: ")
course = input("What are you studying? ")
hobby = input("What is your favorite hobby? ")
Vishesh Kr Mahor 01814802723 6th sem CS
print("\n----- Personal Profile -----")
print(f"Hello {name}!")
print(f"You are {age} years old and live in {city}.")
print(f"You are studying {course}.")
print(f"In your free time, you enjoy {hobby}.")
print("\n----- Special Message -----")
if age < 18:
print(f"{name}, keep learning and enjoying your student life!")
elif age <= 25:
print(f"{name}, this is the perfect time to build your future and skills.")
else:
print(f"{name}, keep growing and achieving new goals!")
print("\nHave a wonderful day! ")
OUTPUT
Vishesh Kr Mahor 01814802723 6th sem CS
EXPERIMENT 2
AIM
Conditional statements: Create a program that prompts the user for their age and tells them if
they can vote in the next election
SOFTWARE USED
VS code , Python 3.14.3
THEORY
Conditional statements are used in programming to make decisions based on certain
conditions. These statements allow the program to execute different instructions depending
on whether a condition is true or false.
In this experiment, the program takes the user’s age as input and determines whether the
person is eligible to vote in the next election.
Concept Used
1. Conditional Statements (if–else)
The if–else statement is used to check conditions and control the flow of execution.
If the condition is true, one block of code is executed.
If the condition is false, another block is executed.
Example logic:
If age is greater than or equal to 18, the user can vote.
Otherwise, the user cannot vote.
2. Relational Operators
Relational operators are used to compare values.
Common operator used in this experiment:
>= (greater than or equal to)
This operator helps check whether the entered age satisfies the voting eligibility condition.
Vishesh Kr Mahor 01814802723 6th sem CS
3. Input and Output
The program takes age as input from the user.
Based on the condition, it displays an appropriate message.
Algorithm:
1. Start
2. Prompt the user to enter age
3. Read the age value
4. Check the condition:
o If age is less than 18
→ display "you can vote"
o Else
→ display "you can not vote"
5. Display "thank you"
6. End
FLOWCHART
Vishesh Kr Mahor 01814802723 6th sem CS
CODE
age = int(input("enter your age"))
if age < 18:
print("you can vote")
else:
print("you can not vote \n")
print("thank you")
OUTPUT
EXPERIMENT 3
Vishesh Kr Mahor 01814802723 6th sem CS
AIM
Loops: Create a program that calculates the factorial of a number entered by the user using a
loop.
SOFTWARE USED
VS code , Python 3.14.3
THEORY
A loop is a control structure that allows a set of instructions to be executed repeatedly until a
specified condition becomes false. Loops help avoid writing repetitive code and are
commonly used for mathematical calculations such as factorial.
The factorial of a number nis the product of all positive integers from 1 to n .
Example:
Factorial of 5 = 5 × 4 ×3 ×2 ×1=120
Concept Used
1. Looping Statement
A loop (such as for or while) is used to repeatedly multiply numbers from 1 up to the given
number.
The loop starts from 1.
It continues until the entered number.
During each iteration, multiplication is performed.
2. Variables
One variable stores the user-entered number.
Another variable stores the factorial result (initially set to 1).
3. Operators
The multiplication operator (*) is used to calculate the factorial by multiplying
successive values.
4. Input and Output
Vishesh Kr Mahor 01814802723 6th sem CS
The program takes a number as input from the user.
After performing repeated multiplication using the loop, the result is displayed as
output.
Algorithm
1. Start
2. Input a number num
3. Set fact = 1
4. Repeat from 1 to num
5. Multiply fact by current number
6. Store result in fact
7. Display factorial
8. End
FLOWCHART
CODE
Vishesh Kr Mahor 01814802723 6th sem CS
num = int(input("enter number to calculate factorial"))
fact = 1
for i in range(1,num+1):
fact *= i
print(f"factorial of {num} is {fact}")
OUTPUT
EXPERIMENT 4
Vishesh Kr Mahor 01814802723 6th sem CS
AIM
Create a program that prompts the user for a list of numbers and then sorts them in ascending
order
SOFTWARE USED
VS code , Python 3.14.3
THEORY
Introduction
Sorting is a fundamental operation in programming used to arrange data in a specific order,
either ascending or descending. It improves data organization and makes searching and
processing more efficient.
In this experiment, the program takes multiple numbers as input from the user, stores them in
a data structure (such as an array or list), and then sorts them from the smallest value to the
largest.
Concept Used
1. Arrays or Lists
An array or list is used to store multiple numbers entered by the user. This allows the program
to handle and manipulate several values together.
2. Sorting Technique
A sorting algorithm such as Bubble Sort, Selection Sort, or any built-in sorting method can
be used.
For example, in Bubble Sort:
Adjacent elements are compared.
If the first element is greater than the next, they are swapped.
The process repeats until all elements are arranged correctly.
3. Comparison Operators
Relational operators like < and > are used to compare numbers during sorting.
Vishesh Kr Mahor 01814802723 6th sem CS
4. Loops
Loops are used to repeatedly compare and rearrange elements until the list becomes sorted.
5. Input and Output Operations
The program accepts multiple numbers from the user.
After sorting, it displays the ordered list.
Algorithm:
1. Start
2. Create an empty list num_array
3. Input the total number of elements n
4. Repeat from 1 to n:
1. Input a number
2. Add it to the list
5. Perform Bubble Sort:
1. Repeat from i = 0 to n−1
2. Repeat from j = 0 to n−i−2
3. Compare adjacent elements
4. If left element > right element → swap them
6. After sorting is complete, display the list
7. End
FLOWCHART
Vishesh Kr Mahor 01814802723 6th sem CS
Vishesh Kr Mahor 01814802723 6th sem CS
CODE
num_arry = []
num = int(input("enter no of number"))
for i in range(0,num):
arr = int(input(f"enter number {i+1}" ))
num_arry.append(arr)
for i in range(0,num):
for j in range(0 ,num -i-1):
if num_arry[j] >num_arry[j+1]:
num_arry[j], num_arry[j+1] = num_arry[j+1] ,num_arry[j]
print(num_arry)
OUTPUT
Vishesh Kr Mahor 01814802723 6th sem CS
EXPERIMENT 5
AIM
Strings and string manipulation: Create a program that prompts the user for a string and then
prints out the string reversed.
SOFTWARE USED
VS code , Python 3.14.3
THEORY
Introduction
A string is a sequence of characters used to store textual data such as names, words, or
sentences. String manipulation refers to operations performed on strings, such as
concatenation, slicing, searching, and reversing.
Reversing a string means changing the order of characters so that the last character becomes
the first and the first becomes the last.
Example:
Input: Python
Output: nohtyP
Concepts Used
1. String Data Type
A string stores a sequence of characters.
It can include letters, numbers, and special symbols.
2. Indexing
Each character in a string has a position called an index.
Indexing generally starts from 0.
This helps in accessing individual characters.
3. String Manipulation Techniques
Reversal can be achieved using loops, slicing, or built-in methods.
Characters are rearranged in reverse order to form a new string.
Vishesh Kr Mahor 01814802723 6th sem CS
4. Input and Output Operations
The program accepts a string from the user.
The processed (reversed) string is displayed as output.
Algorithm:
1. Start
2. Input a string from the user
3. Create an empty stack (list)
4. Traverse the string from beginning to end
5. Push each character onto the stack
6. Traverse the stack in reverse order
7. Print each character
8. End
FLOWCHART
Vishesh Kr Mahor 01814802723 6th sem CS
CODE
word = input("enter the string")
stack =[]
for i in range(0,len(word)):
[Link](word[i])
for i in range(len(stack)-1, -1 , -1):
print(stack[i], end="")
OUTPUT
Vishesh Kr Mahor 01814802723 6th sem CS
EXPERIMENT 6
AIM
Functions: Create a program that defines a function to calculate the area of a circle based on
the radius entered by the user
SOFTWARE USED
VS code , Python 3.14.3
THEORY
A function is a structured block of reusable code designed to perform a specific task. Instead
of writing the same code multiple times, a function allows you to define it once and use it
whenever needed. Functions help make programs modular, organized, and easy to
maintain.
🔹 Why Functions Are Used
Reduce code repetition
Improve readability and structure
Make debugging and testing easier
Allow reuse in large programs
Break complex problems into smaller parts
🔹 Basic Syntax
def function_name(parameters):
# statements
return value
Explanation:
def → keyword used to define a function
function_name → name given to the function
parameters → inputs passed to the function
return → sends result back to the caller (optional)
Vishesh Kr Mahor 01814802723 6th sem CS
Algorithm
1. Start
2. Import the math module.
3. Define a function circle_area(radius):
o Compute area using formula:
area = π × radius²
o Return the calculated area.
4. Prompt the user to enter the radius.
5. Read the radius value.
6. Call the function and pass the radius.
7. Store the returned area.
8. Display the area.
9. End
FLOWCHART
Vishesh Kr Mahor 01814802723 6th sem CS
CODE
import math
def circle_area(radius):
return [Link] * ( radius **2)
radius = float(input("Enter the radius of the cicrle :"))
area = circle_area(radius)
print(f"area of the given circle of {radius} is {area}")
Vishesh Kr Mahor 01814802723 6th sem CS
OUTPUT
EXPERIMENT 7
AIM
Create a program to show different logical operations
SOFTWARE USED
VS code , Python 3.14.3
THEORY
Logical operators are used to combine or modify conditions. They evaluate expressions and
return either True or False, which helps in decision-making and controlling program flow.
Logical operators are commonly used with conditional statements, comparisons, and loops.
🔹 Types of Logical Operators
1. AND Operator (and)
The and operator returns True only when both conditions are true.
Syntax
condition1 and condition2
Vishesh Kr Mahor 01814802723 6th sem CS
Example
5 > 2 and 10 > 3 # True
Used when multiple conditions must be satisfied.
2. OR Operator (or)
The or operator returns True if at least one condition is true.
Syntax
condition1 or condition2
Example
5 > 10 or 10 > 3 # True
Used when any one condition is enough.
3. NOT Operator (not)
The not operator reverses the logical result.
Syntax
not condition
Example
not(5 > 2) # False
Used to invert conditions.
Algorithm
1. Start
2. Declare two variables.
3. Prompt the user to enter two numbers.
4. Read the values.
5. Apply AND operator to check if both numbers are positive.
6. Display the result.
7. Apply OR operator to check if at least one number is positive.
8. Display the result.
Vishesh Kr Mahor 01814802723 6th sem CS
9. Apply NOT operator to reverse the condition of the first number being positive.
10. Display the result.
11. Evaluate a combined logical expression.
12. Display the result.
13. End
FLOWCHART
Vishesh Kr Mahor 01814802723 6th sem CS
Vishesh Kr Mahor 01814802723 6th sem CS
CODE
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("AND result:", a > 0 and b > 0)
print("OR result:", a > 0 or b > 0)
print("NOT result:", not(a > 0))
print("Combined result:", (a > 10 and b < 20) or not(a == b))
OUTPUT
Vishesh Kr Mahor 01814802723 6th sem CS
EXPERIMENT 8
AIM
Create a program to show different bitwise operations
SOFTWARE USED
VS code , Python 3.14.3
THEORY
Bitwise operators perform operations directly on the binary (bit-level) representation of
integers.
Computers store numbers in binary form (0s and 1s), and these operators manipulate those
bits.
For example:
10 → 1010
6 → 0110
Bitwise operations are commonly used in:
low-level programming
networking & cybersecurity
encryption & compression
performance optimization
🔹 Types of Bitwise Operators
Operator Name Description Example
& AND 1 if both bits are 1 10 & 6 = 2
` ` OR 1 if at least one bit is 1
^ XOR 1 if bits differ 10 ^ 6 = 12
~ NOT inverts bits ~10 = -11
<< Left Shift shifts bits left 10 << 1 = 20
>> Right Shift shifts bits right 10 >> 1 = 5
Vishesh Kr Mahor 01814802723 6th sem CS
Algorithm
1. Start
2. Input two integers
3. Perform bitwise AND operation
4. Perform bitwise OR operation
5. Perform bitwise XOR operation
6. Perform bitwise NOT on first number
7. Perform left shift on first number
8. Perform right shift on first number
9. Display all results
10. End
FLOWCHART
Vishesh Kr Mahor 01814802723 6th sem CS
CODE
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("AND:", a & b)
print("OR:", a | b)
print("XOR:", a ^ b)
print("NOT of a:", ~a)
print("Left Shift a:", a << 1)
print("Right Shift a:", a >> 1)
OUTPUT
Vishesh Kr Mahor 01814802723 6th sem CS
EXPERIMENT 9
AIM
Create a program to print current date and time in following formate “day name (Mon) ,date ,
month , year , time”
SOFTWARE USED
VS code , Python 3.14.3
THEORY
omputers maintain the current date and time using the system clock.
In Python, the datetime module is used to retrieve and format date and time values.
The function [Link]() returns the current local date and time.
Using formatting functions, we can display the output in a readable format such as:
Day Name, Date Month Year, Time
Example:
Monday, 16 February 2026, 18:45:30
Why formatting is needed
makes date and time human-readable
useful for logs, reports, and applications
allows customization of display format
Algorithm
1. Start
2. Import the datetime module
3. Get current date and time
4. Extract day name
5. Extract date, month, and year
6. Extract current time
7. Display in required format
8. End
Vishesh Kr Mahor 01814802723 6th sem CS
FLOWCHART
CODE
from datetime import datetime
now = [Link]()
day = [Link]("%A")
date = [Link]("%d")
month = [Link]("%B")
year = [Link]("%Y")
time = [Link]("%H:%M:%S")
print(day + ", " + date + " " + month + " " + year + ", " + time)
OUTPUT
Vishesh Kr Mahor 01814802723 6th sem CS
EXPERIMENT 10
Vishesh Kr Mahor 01814802723 6th sem CS
AIM
Create a program to print prime no less than 20
SOFTWARE USED
VS code , Python 3.14.3
THEORY
A prime number is a natural number greater than 1 that has only two factors:
1 and itself.
Examples of prime numbers:
2, 3, 5, 7, 11, 13, 17, 19
A number is not prime if it is divisible by any number other than 1 and itself.
To find prime numbers less than 20:
check each number from 2 to 19
test divisibility
print the number if it has no divisors
Algorithm
1. Start
2. Set limit = 20
3. For each number from 2 to limit−1
4. Check if the number is divisible by any value from 2 to number−1
5. If divisible → not prime
6. If not divisible → prime → print it
7. Repeat until limit reached
8. End
FLOWCHART
Vishesh Kr Mahor 01814802723 6th sem CS
CODE
for num in range(2, 20):
prime = True
for i in range(2, num):
if num % i == 0:
prime = False
break
if prime:
print(num)
OUTPUT
Vishesh Kr Mahor 01814802723 6th sem CS
EXPERIMENT 11
Vishesh Kr Mahor 01814802723 6th sem CS
AIM
Classes and objects: Create a program that defines a class to represent a car and then creates
an object of that class with specific attributes
SOFTWARE USED
VS code , Python 3.14.3
THEORY
Object-Oriented Programming (OOP) is a programming approach that models real-world
entities using classes and objects.
A class is a blueprint used to create objects. It defines the data members (attributes) and
member functions (methods) that describe the properties and behavior of an object.
An object is an instance of a class. It represents a real-world entity and contains actual values
for the attributes defined in the class.
In this experiment, a Car is represented as a class. The class contains attributes such as brand,
model, and owner, and a method to display the car details. An object of the class is then
created to represent a specific car with real values.
Attributes of Car Class
Attributes describe the properties of a car:
Brand of the car
Model of the car
Owner of the car
Method Used
A method is defined to display the details of the car object.
Object Creation
An object is created from the Car class and assigned specific values.
This object represents a real car.
Advantages of Using Classes and Objects
Vishesh Kr Mahor 01814802723 6th sem CS
Helps model real-world entities
Improves code organization
Promotes reusability
Makes programs easier to maintain
Algorithm:
1. Start the program.
2. Define a class named Car.
3. Create a constructor __init__() to initialize the attributes:
o brand
o model
o owner
4. Assign the received values to the object using self.
5. Define a method showinfo() to display car details.
6. Create an object car1 of the Car class.
7. Pass specific values (“Honda”, “Civic”, “Vishesh”) while creating the object.
8. Call the showinfo() method to display the car information.
9. End the program.
FLOWCHART
Vishesh Kr Mahor 01814802723 6th sem CS
CODE
class car:
def __init__(self , brand:str , model:str , owner:str):
[Link] = brand
[Link] = model
[Link] = owner
def showinfo(self):
print(f"car brand is {[Link]} ")
print(f"car model is {[Link]} ")
print(f"car owner is {[Link]} ")
car1=car("honda", "civic","vishesh")
[Link]();
Vishesh Kr Mahor 01814802723 6th sem CS
OUTPUT
EXPERIMENT 12
Vishesh Kr Mahor 01814802723 6th sem CS
AIM
File input/output: Create a program that reads data from a file and writes it to another file in a
different format.
SOFTWARE USED
VS code , Python 3.14.3
THEORY
File handling in Python allows programs to store and retrieve data from files stored on a
storage device. This enables permanent data storage instead of temporary memory storage.
Python provides built-in functions to create, open, read, write, and close files.
A file must be opened before performing any operation on it. After the operation is
completed, it should be closed to free system resources and ensure data integrity.
Types of Files
1. Text Files
Store data in human-readable form.
Examples: .txt, .csv, .log
2. Binary Files
Store data in binary format (bytes).
Examples: images, audio, video files.
File Opening Syntax
file = open("filename", "mode")
File Modes
Mode Description
r Opens file for reading
w Opens file for writing (creates new or overwrites)
a Opens file for appending data
r+ Opens file for both reading and writing
Vishesh Kr Mahor 01814802723 6th sem CS
Mode Description
b Opens file in binary mode
x Creates a new file
Reading Data from File
read() → reads entire file
readline() → reads one line
readlines() → reads all lines into a list
Writing Data to File
The write() function is used to store data into a file.
Closing a File
After completing file operations, the file should be closed using:
[Link]()
Closing ensures data is saved and system resources are released.
Algorithm:
1 Start the program.
2 Open the file [Link] in read mode.
3 Read the contents of the file and store it in a variable.
4 Close the file after reading.
5 Display the file contents on the screen.
6 Convert the file content into uppercase.
7 Open a new file [Link] in write mode.
8 Write the modified (uppercase) data into the new file.
9 Close the file after writing.
10 End the program.
Vishesh Kr Mahor 01814802723 6th sem CS
FLOWCHART
CODE
file1 = open("[Link]", "r")
data = [Link]()
[Link]()
print(f"File contains data :: {data}")
data = [Link]()
Vishesh Kr Mahor 01814802723 6th sem CS
file2 = open("[Link]", "w")
[Link](data)
[Link]()
OUTPUT
Vishesh Kr Mahor 01814802723 6th sem CS
Vishesh Kr Mahor 01814802723 6th sem CS