0% found this document useful (0 votes)
2 views17 pages

Python Programming and Data Structures Lab Manual

Uploaded by

jpinneti
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

Python Programming and Data Structures Lab Manual

Uploaded by

jpinneti
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON FOR DATA SCIENCE

LAB MANUAL
5 Aim

To write a Python program to perform list operations such as insertion, deletion, searching,
sorting, and list comprehension.

Procedure
1. Start the program
2. Create a list of numbers
3. Insert an element into the list using append()
4. Delete an element using remove()
5. Search for an element using in operator
6. Sort the list using sort()
7. Create a new list using list comprehension
8. Display all results
9. Stop the program
Program:-

# Create a list of numbers


numbers = [10, 20, 30, 40, 50]

print("Original List:", numbers)

# Insertion (Adding element)


[Link](60)
print("After Insertion:", numbers)

# Deletion (Removing element)


[Link](30)
print("After Deletion:", numbers)

# Searching (Check if element exists)


search = 40
if search in numbers:
print(search, "found in the list")
else:
print(search, "not found in the list")

# Sorting the list


[Link]()
print("Sorted List:", numbers)

Output:-

Original List: [10, 20, 30, 40, 50]

After Insertion: [10, 20, 30, 40, 50, 60]

After Deletion: [10, 20, 40, 50, 60]

100 not found in the list

Sorted List: [10, 20, 40, 50, 60]


6. Aim

To write a Python program to perform set operations such as union, difference,


subset, and superset.

Procedure:-
Algorithm

1. Start the program


2. Create two sets
3. Perform union using union() method
4. Perform difference using difference() method
5. Check subset using issubset()
6. Check superset using issuperset()
7. Display the results
8. Stop the program

Program:-
# Create two sets

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
print("Set 1:", set1)
print("Set 2:", set2)
# Union
union_set = [Link](set2)
print("Union:", union_set)
# Difference
difference_set = [Link](set2)
print("Difference (set1 - set2):", difference_set)
# Subset
if [Link](set2):
print("set1 is subset of set2")
else:
print("set1 is not a subset of set2")

# Superset
if [Link](set2):
print("set1 is superset of set2")
else:
print("set1 is not a superset of set2")

Output:-
Set 1: {1, 2, 3, 4}

Set 2: {3, 4, 5, 6}

Union: {1, 2, 3, 4, 5, 6}

Difference (set1 - set2): {1, 2}

set1 is not a subset of set2

set1 is not a superset of set2


AIM:-7

To Write a program using functions to swap two numbers.

Procedure:-
Algorithm

1. Start the program.


2. Define a function swap_numbers(a, b).
3. Store the value of a in a temporary variable.
4. Assign the value of b to a.
5. Assign the temporary value to b.
6. Return the swapped values.
7. Read two numbers from the user.
8. Display the numbers before swapping.
9. Call the function.
[Link] the numbers after swapping.
[Link] the program.

Program:-

# Function to swap two numbers

def swap_numbers(a, b):


temp = a
a=b
b = temp
return a, b
# Input from user

num1 = int(input("Enter first number: "))

num2 = int(input("Enter second number: "))


print("Before swapping:")

print("First number =", num1)

print("Second number =", num2)

# Function call

num1, num2 = swap_numbers(num1, num2)

print("After swapping:")

print("First number =", num1)

print("Second number =", num2)

Output:-

Enter first number: 10

Enter second number: 20

Before swapping:

First number = 10

Second number = 20

After swapping:

First number = 20

Second number = 10
AIM:-8

Create a list and perform different operations on it.

Procedure:-
Algorithm

1. Start the program.


2. Create a list with some elements.
3. Display the original list.
4. Add a new element using append().
5. Insert an element using insert().
6. Remove an element using remove().
7. Delete the last element using pop().
8. Access elements using indexing.
9. Display a sliced portion of the list.
[Link] the list using sort().
[Link] the list using reverse().
[Link] the length of the list using len().
[Link] the program.

Program:-

# Creating a list

numbers = [10, 20, 30, 40, 50]

print("Original List:")

print(numbers)

# Adding an element

[Link](60)
print("\nAfter Adding 60:")

print(numbers)

# Inserting an element

[Link](2, 25)

print("\nAfter Inserting 25 at index 2:")

print(numbers)

# Removing an element

[Link](40)

print("\nAfter Removing 40:")

print(numbers)

# Popping an element

[Link]()

print("\nAfter Popping Last Element:")

print(numbers)

# Accessing elements

print("\nFirst Element:", numbers[0])

print("Last Element:", numbers[-1])

# List slicing

print("\nSliced List (index 1 to 3):")

print(numbers[1:4])

# Sorting the list

[Link]()

print("\nAfter Sorting:")
print(numbers)

# Reversing the list

[Link]()

print("\nAfter Reversing:")

print(numbers)

# Finding length of list

print("\nLength of List:", len(numbers))

Output:-
Original List:

[10, 20, 30, 40, 50]

After Adding 60:

[10, 20, 30, 40, 50, 60]

After Inserting 25 at index 2:

[10, 20, 25, 30, 40, 50, 60]

After Removing 40:

[10, 20, 25, 30, 50, 60]

After Popping Last Element:

[10, 20, 25, 30, 50]

First Element: 10

Last Element: 50

Sliced List (index 1 to 3):


[20, 25, 30]

After Sorting:

[10, 20, 25, 30, 50]

After Reversing:

[50, 30, 25, 20, 10]

Length of List: 5
AIM:-9

To create a dictionary and perform different operations on it.

Algorithm

1. Start the program.


2. Create a dictionary with key-value pairs.
3. Display the dictionary.
4. Access values using keys.
5. Add a new element to the dictionary.
6. Update an existing value.
7. Remove an element using pop().
8. Display all keys, values, and items.
9. Find the length of the dictionary.
[Link] the program.

Program:-

# Creating a dictionary

student = {

"name": "Rahul",

"age": 20,

"course": "Python"
}

print("Original Dictionary:")

print(student)

# Accessing values

print("\nStudent Name:", student["name"])

print("Student Age:", student["age"])

# Adding a new key-value pair

student["grade"] = "A"

print("\nAfter Adding Grade:")

print(student)

# Updating a value

student["age"] = 21

print("\nAfter Updating Age:")

print(student)

# Removing an element

[Link]("course")

print("\nAfter Removing Course:")

print(student)

# Displaying keys, values, and items

print("\nKeys:", [Link]())
print("Values:", [Link]())

print("Items:", [Link]())

# Finding length of dictionary

print("\nLength of Dictionary:", len(student))

Output:-
Original Dictionary:

{'name': 'Rahul', 'age': 20, 'course': 'Python'}

Student Name: Rahul

Student Age: 20

After Adding Grade:

{'name': 'Rahul', 'age': 20, 'course': 'Python', 'grade': 'A'}

After Updating Age:

{'name': 'Rahul', 'age': 21, 'course': 'Python', 'grade': 'A'}

After Removing Course:

{'name': 'Rahul', 'age': 21, 'grade': 'A'}

Keys: dict_keys(['name', 'age', 'grade'])

Values: dict_values(['Rahul', 21, 'A'])

Items: dict_items([('name', 'Rahul'), ('age', 21), ('grade', 'A')])

Length of Dictionary: 3
AIM:-10

Import pandas and create a dataframe and perform operations on it.

Process:-

Operations Performed

1. Imported pandas.
2. Created a DataFrame.
3. Displayed the DataFrame.
4. Used head() to view rows.
5. Calculated average using mean().
6. Filtered rows using a condition.
7. Added a new column.
8. Sorted the DataFrame using sort_values().

Programs:-
# Import pandas

import pandas as pd

# Create a DataFrame

data = {

'Name': ['Ravi', 'Sita', 'Kiran', 'Anu'],

'Age': [20, 21, 19, 22],

'Marks': [85, 92, 78, 88]

df = [Link](data)
# Display DataFrame

print("Original DataFrame:")

print(df)

# Display first two rows

print("\nFirst Two Rows:")

print([Link](2))

# Calculate average marks

print("\nAverage Marks:")

print(df['Marks'].mean())

# Filter students with marks greater than 80

print("\nStudents with Marks > 80:")

print(df[df['Marks'] > 80])

# Add a new column

df['Result'] = ['Pass', 'Pass', 'Pass', 'Pass']

print("\nDataFrame After Adding Result Column:")

print(df)
# Sort by Marks in descending order

print("\nSorted by Marks:")

print(df.sort_values(by='Marks', ascending=False))

Output
Original DataFrame:

Name Age Marks

0 Ravi 20 85

1 Sita 21 92

2 Kiran 19 78

3 Anu 22 88

First Two Rows:

Name Age Marks

0 Ravi 20 85

1 Sita 21 92

Average Marks:

85.75

Students with Marks > 80:


Name Age Marks

0 Ravi 20 85

1 Sita 21 92

3 Anu 22 88

DataFrame After Adding Result Column:

Name Age Marks Result

0 Ravi 20 85 Pass

1 Sita 21 92 Pass

2 Kiran 19 78 Pass

3 Anu 22 88 Pass

Sorted by Marks:

Name Age Marks Result

1 Sita 21 92 Pass

3 Anu 22 88 Pass

0 Ravi 20 85 Pass

2 Kiran 19 78 Pass

You might also like