0% found this document useful (0 votes)
3 views

Python Programs 19.02.2025

The document outlines three programming exercises in Python: creating a menu-driven calculator for basic arithmetic operations, concatenating two data frames using pandas, and sorting a dictionary by value. Each exercise includes an aim, algorithm, source code, output, and result indicating successful execution. The exercises demonstrate fundamental programming concepts such as functions, data manipulation, and sorting.

Uploaded by

shivani745verma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Programs 19.02.2025

The document outlines three programming exercises in Python: creating a menu-driven calculator for basic arithmetic operations, concatenating two data frames using pandas, and sorting a dictionary by value. Each exercise includes an aim, algorithm, source code, output, and result indicating successful execution. The exercises demonstrate fundamental programming concepts such as functions, data manipulation, and sorting.

Uploaded by

shivani745verma
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Ex no -9. A.

Write a program to create a menu with the following


options
1. TO PERFORM ADDITION 2. TO PERFORM SUBTRACTION
3. TO PERFORM MULTIPICATION 4. TO PERFORM DIVISION
Accepts user's input and performs the operation accordingly. Use
functions with arguments.

Aim: To write a program to create a menu with the following options.


1. To perform addition 2. To perform subtraction 3. To perform multiplication
4. To perform division
Accepts user's input and performs the operation accordingly. Use functions with
arguments.

Algorithm:
Step 1: Start
Step 2: Define all the operations with variables and use conditional statements
Step 2: Use print function to obtain the output
Step 3: Execute the output
Step 4: Stop

Source code:
def add(a,d):
return a+b
def sub(c,d):
return c-d
def mul(b,h):
return b*h
def div(g,h):
return g/h
print("=================")
print("1.TO PERFORM ADDITION")
print("2.TO PERFORM SUBTRACTION")
print("3.TO PERFORM MULTIPLICATION")
print("4.TO PERFORM DIVISION")
print("=================")
choice = int(input("Enter Your choice"))
if choice ==1:
a=int(input("Enter the 1st value"))
b=int(input("Enter the 2nd value"))
print(add(a,b))
elif choice ==2:
c=int(input("Enter the 1st value"))
d=int(input("Enter the 2nd value"))
print(sub(c,d))
elif choice ==3:
e=int(input("Enter the 1st value"))
f=int(input("Enter the 2nd value"))
print (mul(e,f))
elif choice ==4:
g=int(input("Enter the 1st value"))
h=int(input("Enter the 2nd value"))
print(areadOfSquare(S))
else:
print("Wrong choice")

Output:
=================
1.TO PERFORM ADDITION
2.TO PERFORM SUBTRACTION
3.TO PERFORM MULTIPLICATION
4.TO PERFORM DIVISION
=================
Enter Your choice 12
Wrong choice

Result:
The output is executed successfully
Ex no- 9.B. write a python program to concatenate the data
frames with two different objects.
Aim: To write a python program to concatenate the data frames with two
different objects.

Algorithm:
Step 1: Start
Step 2: Import pandas to create a data frame
Step 3: Assign the data sets to concatenate and obtain data frame as output
Step 4: Execute the output
Step 5: Stop

Source code:
import pandas as pd

# Create the first DataFrame


data1 = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35]
}
df1 = pd.DataFrame(data1)

# Create the second DataFrame


data2 = {
'Name': ['David', 'Eve', 'Frank'],
'Age': [40, 45, 50]
}
df2 = pd.DataFrame(data2)

# Concatenate the DataFrames


result = pd.concat([df1, df2], ignore_index=True)
# Display the concatenated DataFrame
print(result)

Output

Name Age
0 Alice 25
1 Bob 30
2 Charlie 35
3 David 40
4 Eve 45
5 Frank 50

Result:
The output is executed successfully
Ex No-9. C. Write a Python script to sort (ascending and
descending) a dictionary by value using the item function

Aim: To Write a Python script to sort (ascending and descending) a dictionary


by value using the item function

Algorithm:
Step 1: Star
Step 2: Create a dictionary with appropriate elements
Step 3: Sort the dictionary with key values
Step 4: Execute the output
Step 5: Stop

Source Code :
my_dict={'c':3,'a':1,'d':4,'b':2}
sorted_dict=sorted([(value,key)for(key,value)in my_di
ct.items()])
print("Sorted dictionary is :")
print(sorted_dict)

Output:
Sorted dictionary is :
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]

Result:
The output is executed successfully

You might also like