0% found this document useful (0 votes)
33 views13 pages

Pda Lab 2

DATA SCIENCE METHODS

Uploaded by

aryankothambia
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)
33 views13 pages

Pda Lab 2

DATA SCIENCE METHODS

Uploaded by

aryankothambia
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
You are on page 1/ 13

Python for Data science

SUBJECT : Python for Data science (102045603)


By Professor : Mr. Aryan Ketankumar Kothambia
G H Patel College of Engineering & Technology
D: 16/07/2024
Content
Create a list named "Subjects" by inserting 10 subjects into it through any loop and create a list
"Elective Subjects" with 5 subjects through direct initialization. Extend the "Subjects" list by
appending the "Elective Subjects" list. Append 3 duplicate subjects into the "Subjects" list. Find the
index of the first occurrence of that duplicate value and then remove all occurrences of that specific
subject through a loop. Define a function remove_range(i1, i2) to remove a range of elements
from index i1 to i2 using the del keyword and return the resultant list. Pop the 5th element after
reversing and sorting your list. Count the total number of elements in your list and finally clear the
list.

Which of the above operations can be performed directly? Which of the above operations cannot be
performed directly on a Tuple and why? Update and remove a specific item from the tuple by
converting it into a list.
Create a list named "Subjects" and insert 10 subjects into it using a for loop

Subjects = []

# Number of new subjects to add


add_subjects = int(input("How many subjects do you want to add? "))

# Loop to add new subjects


for _ in range(add_subjects):
new_subject = input("Enter the subject: ")
Subjects.append(new_subject)

# Print the updated list


print("Updated list of subjects:")
for subject in Subjects:
print(Subjects)
Create a list “Elective Subjects” with 5 subjects through direct initialization.

# Directly initializing the list with 5 subjects

Elective_Subjects = ["Data Science", "Artificial Intelligence", "Cyber Security",


"Robotics", "Cloud Computing"]

# Print the list to verify

print(Elective_Subjects)
Extend list “Subject” by another list “Elective Subjects”.

# Extend the student subjects list with the elective subjects list

Subjects.extend(Elective_Subjects)

# Print the final combined list to verify

print("Final list of subjects:")

print(Subjects)
Append 3 duplicate subjects into “Subject” list.

# Append 3 duplicate subjects into the Subjects list

duplicate_subjects = ["Math", "Science", "English"]

Subjects.extend(duplicate_subjects)

# Print the updated list after adding duplicates

print("List of subjects after adding duplicates:")

print(Subjects)
Find the index of first occurrence of that duplicate value and then remove all
the occurrences of that specific subject through loop.
# Identify and remove duplicates, keeping the first occurrence
seen = set()
result = []
for subject in Subjects:
if subject not in seen:
seen.add(subject)
result.append(subject)
elif subject in duplicate_subjects:
duplicate_subjects.remove(subject)

print("Subjects list after removing duplicates, keeping the first occurrence:")


print(result)
Define function remove_range(i1,i2) to remove range of element from i1 to i2
through del keyword and return the resultant list.

# Define function remove_range(i1, i2) to remove range of elements from i1 to i2


def remove_range(i1, i2):
del Subjects[i1:i2]
return Subjects

# Test the function


print("Removing elements from index 3 to 7:")
print(remove_range(3, 7))
Pop 5th element after reversing and sorting your list. Count total elements in
your list and finally clear the list.
# Reverse and sort the list
Subjects.reverse()
Subjects.sort()

# Pop the 5th element (index 4)


popped_element = Subjects.pop(4)
print(f"Popped element after reversing and sorting: {popped_element}")

print("Subjects list after popping 5th element:")


print(Subjects)
Count total elements in your list and finally clear the list.

# Count total elements in the list


total_elements = len(Subjects)
print(f"Total elements in Subjects list: {total_elements}")

# Clear the list


Subjects.clear()
print("Subjects list after clearing:")
print(Subjects)
Which of the above operations can be performed directly?

Operations that can be performed directly on tuples


● Accessing elements
● Iterating through elements
● Counting occurrences
● Finding the index of an element
Operations that cannot be performed directly on tuples
● Updating an element
● Removing an element
● Adding an element
These operations cannot be performed because tuples are immutable
Update and remove specific item from the tuple by converting it into list.

# Operations on tuples
tuple_subjects = ("Math", "Science", "English", "History", "Geography")
# Convert tuple to list to update and remove specific items
list_subjects = list(tuple_subjects)
list_subjects[2] = "Physics" # Update specific item
list_subjects.remove("History") # Remove specific item
tuple_subjects = tuple(list_subjects)

print("Updated tuple after converting to list and modifying:")


print(tuple_subjects)
Congratulations

Practical 2 (Part - 1) Completed

You might also like