TA Activity: Innovative Method
Session: 2022-23
Name: Ayushi Gupta
Section: I
Roll number: 01
Subject: Python
Date of Submission: 12.07.2023
Title: Python Program Demonstrating Operations on Various Data Types
Conclusion: This Python program showcases operations on various data types, including
numeric, string, list, tuple, dictionary, and set. It demonstrates common operations like
accessing, updating, and manipulating data, providing a comprehensive overview of their
functionalities.
Program:
# Numeric Operations
# Addition
sum_nums = num_int + num_float
# Subtraction
sub_nums = num_int - num_float
# Multiplication
mul_nums = num_int * num_float
# Division
div_nums = num_int / num_float
# Modulo
mod_nums = num_int % 3
# Exponentiation
exp_nums = num_int ** 2
# Numeric data types
num_int = 10
num_float = 3.14
num_complex = 2 + 3j
# String Operations
# Accessing character
access_char = my_string[0]
# Slicing
slicing = my_string[7:12]
# Indexing
indexing = my_string.index("W")
# Splitting
splitting = my_string.split(",")
# Concatenation
concatenation = my_string + "!!"
# Counting occurrences of a substring
count_substring = my_string.count("l")
# String data type
my_string = "Hello, World!"
# List Operations
# Accessing element
access_element = my_list[2]
# Updating element
my_list[3] = 10
# Appending element
my_list.append(6)
# Inserting element
my_list.insert(1, 7)
# Popping element
popped_element = my_list.pop()
# Pushing element
my_list.append(8)
# Slicing
sliced_list = my_list[1:4]
# Length of list
list_length = len(my_list)
# Sum of list elements
list_sum = sum(my_list)
# Reversed list
list_reverse = list(reversed(my_list))
# List data type
my_list = [1, 2, 3, 4, 5]
# Tuple Operations
# Accessing element
access_element_tuple = my_tuple[2]
# Length of tuple
tuple_length = len(my_tuple)
# Sum of tuple elements
tuple_sum = sum(my_tuple)
# Index of an element in tuple
tuple_index = my_tuple.index(4)
# Tuple data type
my_tuple = (1, 2, 3, 4, 5)
# Dictionary Operations
# Accessing value
access_value = my_dict["name"]
# Updating value
my_dict["age"] = 26
# Adding new key-value pair
my_dict["gender"] = "Male"
# Deleting key-value pair
del my_dict["city"]
# Accessing dictionary keys
dict_keys = my_dict.keys()
# Accessing dictionary values
dict_values = my_dict.values()
# Accessing dictionary items
dict_items = my_dict.items()
# Dictionary data type
my_dict = {"name": "John", "age": 25, "city": "New York"}
# Set Operations
# Checking membership
membership_check = 3 in my_set
# Adding element
my_set.add(6)
# Removing element
my_set.remove(3)
# Intersection of sets
set_intersection = my_set.intersection({2, 4, 6})
# Difference of sets
set_difference = my_set.difference({4, 5, 6})
# Set data type
my_set = {1, 2, 3, 4, 5}
# Tuple to list conversion
tuple_to_list = list(my_tuple)
# List to tuple conversion
list_to_tuple = tuple(my_list)
# Collecting the results in a dictionary
results = {
"Numeric Operations": {
"Sum": sum_nums,
"Difference": sub_nums,
"Product": mul_nums,
"Division": div_nums,
"Modulo": mod_nums,
"Exponentiation": exp_nums
},
"String Operations": {
"Accessing character": access_char,
"Slicing": slicing,
"Indexing": indexing,
"Splitting": splitting,
"Concatenation": concatenation,
"Count substring": count_substring
},
"List Operations": {
"Accessing element": access_element,
"Updating element": my_list,
"Appending element": my_list,
"Inserting element": my_list,
"Popping element": popped_element,
"Pushing element": my_list,
"Slicing": sliced_list,
"Length of list": list_length,
"Sum of list elements": list_sum,
"Reversed list": list_reverse,
"Tuple to list conversion": tuple_to_list
},
"Tuple Operations": {
"Accessing element": access_element_tuple,
"Length of tuple": tuple_length,
"Sum of tuple elements": tuple_sum,
"Index of element": tuple_index,
"List to tuple conversion": list_to_tuple
},
"Dictionary Operations": {
"Accessing value": access_value,
"Updating value": my_dict,
"Adding key-value pair": my_dict,
"Deleting key-value pair": my_dict,
"Dictionary keys": dict_keys,
"Dictionary values": dict_values,
"Dictionary items": dict_items
},
"Set Operations": {
"Membership check": membership_check,
"Adding element": my_set,
"Removing element": my_set,
"Intersection of sets": set_intersection,
"Difference of sets": set_difference
}
}
# Printing the results
for data_type, operations in [Link]():
print(data_type + ":")
for operation, result in [Link]():
print(operation + ":", result)
print()
Output:
Numeric Operations:
Sum: 13.14
Difference: 6.86
Product: 31.400000000000002
Division: 3.1847133757961785
Modulo: 1
Exponentiation: 100
String Operations:
Accessing character: H
Slicing: World
Indexing: 7
Splitting: ['Hello', ' World!']
Concatenation: Hello, World!!
Count substring: 3
List Operations:
Accessing element: 3
Updating element: [1, 2, 3, 10, 5]
Appending element: [1, 2, 3, 10, 5, 6]
Inserting element: [1, 7, 2, 3, 10, 5, 6]
Popping element: 6
Pushing element: [1, 7, 2, 3, 10, 5, 6, 8]
Slicing: [7, 2, 3]
Length of list: 7
Sum of list elements: 36
Reversed list: [8, 6, 5, 10, 3, 2, 1]
Tuple to list conversion: [1, 2, 3, 4, 5]
Tuple Operations:
Accessing element: 3
Length of tuple: 5
Sum of tuple elements: 15
Index of element: 3
List to tuple conversion: (1, 7, 2, 3, 10, 5, 6, 8)
Dictionary Operations:
Accessing value: John
Updating value: {'name': 'John', 'age': 26, 'gender': 'Male'}
Adding key-value pair: {'name': 'John', 'age': 26, 'gender': 'Male', 'city': 'New York'}
Deleting key-value pair: {'name': 'John', 'age': 26, 'gender': 'Male'}
Dictionary keys: dict_keys(['name', 'age', 'gender'])
Dictionary values: dict_values(['John', 26, 'Male'])
Dictionary items: dict_items([('name', 'John'), ('age', 26), ('gender', 'Male')])
Set Operations:
Membership check: False
Adding element: {1, 2, 4, 5, 6}
Removing element: {1, 2, 4, 5, 6}
Intersection of sets: {2, 6, 4}
Difference of sets: {1}