Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

python_practical_File_com

Uploaded by

ay717509
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)
17 views

python_practical_File_com

Uploaded by

ay717509
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/ 12

INDEX

S/N NAME OF TOPIC DATE SIGN


Write a Python program to create an array
1 contains six integers. Also print all the members
of the array
Given a two list, create a third list by picking an
2 odd-index element from the first list and even
index elements from second

Given an input list, remove the element at index


3 4 and add it to the 2nd position and also, at the
end of the list

Given a list, iterate it and count the occurrence


4 of each element and create a dictionary to show
the count of each element

Given a two list of equal size create a set such


5 that it shows the element from both lists in the
pair

Given a following two sets find the intersection


6
and remove those elements from the first set

Iterate a given list and check if a given element


7 already exists in a dictionary as a key’s value if
not delete it from the list

Remove duplicate from a list and create a tuple


8
and find the minimum and maximum number

9 Write a program for swapping of two tuples.

10 Write a program to find no. of vowels in a string

Write a program to find whether a year is leap


11
year or not
Object 1 - Write a Python program to create an array contains six integers. Also print all the
members of the array

import array

# Create an array with six integers

num = array.array('i', [10, 20, 30, 40, 50, 60])

# Print all the members of the array

for numb in num:

print(numb)

Output –

10

20

30

40

50

60
Object 2 - Given a two list. Create a third list by picking an odd-index element from the first list and
even index elements from second.

# Define the first and second lists


first_list = [1, 2, 3, 4, 5, 6]
second_list = [10, 20, 30, 40, 50, 60]

# Initialize the third list


third_list = []

# Pick odd-index elements from the first list


for i in range(1, len(first_list), 2):
third_list.append(first_list[i])

# Pick even-index elements from the second list


for i in range(0, len(second_list), 2):
third_list.append(second_list[i])

# Print the third list


print("Third list:", third_list)

Output –

Third list: [2, 4, 6, 10, 30, 50]


Object 3 - Given an input list removes the element at index 4 and add it to the 2nd position and also,
at the end of the list.

# Define the input list


input_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

# Remove the element at index 4


element = input_list.pop(4)

# Add the removed element to the 2nd position


input_list.insert(2, element)

# Append the removed element to the end of the list


input_list.append(element)

# Print the modified list


print("Modified list:", input_list)

Output –

Modified list: [1, 2, 5, 3, 4, 6, 7, 8, 9, 5]


Object 4 - Given a list, iterate it and count the occurrence of each element and create a dictionary to
show the count of each element

# Define the input list


input_list = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]

# Initialize an empty dictionary to store the counts


element_count = {}

# Iterate over each element in the list


for element in input_list:
# If the element is already in the dictionary, increment its count
if element in element_count:
element_count[element] += 1
# If the element is not in the dictionary, add it with a count of 1
else:
element_count[element] = 1

# Print the dictionary with the count of each element


print("Element count:", element_count)

Output –

Element count: {1: 1, 2: 2, 3: 3, 4: 4}


Object 5 - Given a two list of equal size create a set such that it shows the element from both lists in
the pair

# Define the two input lists


list1 = [1, 2, 3, 4]
list2 = ['a', 'b', 'c', 'd']

# Create a set of tuples from elements of both lists


paired_set = set(zip(list1, list2))

# Print the resulting set


print("Paired set:", paired_set)

Output –

Paired set: {(4, 'd'), (3, 'c'), (2, 'b'), (1, 'a')}
Object 6 - Given a following two sets find the intersection and remove those elements from the first
set

# Define the two input sets


set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}

# Find the intersection of the two sets


intersection = set1.intersection(set2)

# Remove the intersecting elements from the first set


set1.difference_update(intersection)

# Print the modified first set and the intersection


print("Modified first set:", set1)
print("Intersection:", intersection)

Output –

Modified first set: {1, 2}

Intersection: {3, 4, 5}
Object 7 - Iterate a given list and Check if a given element already exists in a dictionary as a key’s
value if not delete it from the list

# Define the input list and dictionary


input_list = [1, 2, 3, 4, 5]
input_dict = {'a': 1, 'b': 2, 'c': 3}

# Iterate over the list and create a new list with elements that exist in the dictionary's
values
filtered_list = [item for item in input_list if item in input_dict.values()]

# Print the filtered list


print("Filtered list:", filtered_list)

Output –

Filtered list: [1, 2, 3]


Object 8 - Remove duplicate from a list and create a tuple and find the minimum and maximum
number

# Define the input list


input_list = [4, 5, 2, 8, 4, 1, 2, 9, 5, 7]

# Remove duplicates by converting the list to a set, then back to a list to maintain the order
unique_list = list(set(input_list))

# Create a tuple from the unique list


unique_tuple = tuple(unique_list)

# Find the minimum and maximum numbers in the tuple


min_value = min(unique_tuple)
max_value = max(unique_tuple)

# Print the results


print("Unique tuple:", unique_tuple)
print("Minimum value:", min_value)
print("Maximum value:", max_value)

Output –

Unique tuple: (1, 2, 4, 5, 7, 8, 9)

Minimum value: 1

Maximum value: 9
Object 9 – Write a Program for swapping of two tuples.

# Define the two tuples


tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)

# Print the original tuples


print("Original tuple1:", tuple1)
print("Original tuple2:", tuple2)

# Swap the tuples


tuple1, tuple2 = tuple2, tuple1

# Print the swapped tuples


print("Swapped tuple1:", tuple1)
print("Swapped tuple2:", tuple2)

Output –

Original tuple1: (1, 2, 3)

Original tuple2: (4, 5, 6)

Swapped tuple1: (4, 5, 6)

Swapped tuple2: (1, 2, 3)


Object 10 – Write a program to find no. of vowels in a string.

def count_vowels(input_string):
# Define the vowels as a string (both uppercase and lowercase)

# Initialize a counter for the number of vowels


count = 0

# Loop through each character in the string using indexing


for i in range(len(input_string)):
# Check if the current character is in the vowels string
if input_string[i]=='a' or input_string[i]=='e' or input_string[i]=='o' or input_string[i]=='i' or
input_string[i]=='u' or input_string[i]=='A' or input_string[i]=='E' or input_string[i]=='I' or
input_string[i]=='O' or input_string[i]=='U' :
count += 1 # Increment the count if it's a vowel

return count

# Taking user input


input_string = input("Enter a string: ")
vowel_count = count_vowels(input_string)
print(f"Number of vowels in the string: {vowel_count}")

Output –

Enter a string: Hello Good morning

Number of vowels in the string: 6


Object 11 – Write a program to find whether a year is leap year or not.

def check_leap(year):
# Check if the year is a leap year
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
return True
else:
return False

# Taking user input


year = int(input("Enter a year: "))

# Checking if the year is a leap year


if check_leap(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")

Output –

Enter a year: 2024

2024 is a leap year.

Enter a year: 2026

2026 is not a leap year.

You might also like