Enrollment No.
230210107079
Experiment No: 2
Develop a program to learn different types of structures (list, dictionary,
tuples) in python.
Date:31/07/2025
Competency and Practical Skills:
Competency skills:
• Basic knowledge of computer systems, operating systems, and file systems.
• Familiarity with command-line interfaces (CLI) and graphical user interfaces (GUI).
• Understanding of programming languages, syntax, and logic.
Practical skills:
• Basic programming concepts: You should have a good grasp of basic programming
concepts such as variables, data types, conditional statements, loops, and functions.
• Python programming language: You should have a good understanding of Python
syntax, data structures, and standard library functions.
• Sequences: Sequences are ordered collections of elements that can be accessed by their
index or key. You should have a good understanding of the different types of sequences
such as string, tuple, list, dictionary, and set, and their respective properties.
• String manipulation: You should know how to manipulate them using methods such as
slicing, concatenation, and formatting.
• Collection manipulation: Collections such as lists, tuples, dictionaries, and sets can be
manipulated using methods such as append, insert, remove, pop, and sort.
• Iteration: You should know how to use for loops and list comprehensions to iterate over
sequences.
• Conditional statements: You should know how to use conditional statements to check
for specific conditions in sequences.
• Functions: You should know how to define functions that operate on sequences and
return values.
Relevant CO: CO1
Objectives: (a) To learn how to manipulate and access their elements, iterate over them,
perform conditional operations on them, and use them in functions.
(b) To learn how to select the appropriate sequence type for a given task based on its properties
and performance characteristics.
Equipment/Instruments: Personal Computer, Internet, Python
Theory:
1. In Python programming language, there are four built-in sequence types: strings, lists,
tuples, and ranges. Additionally, Python includes the set and dictionary data structures,
which are implemented as unordered collections of unique and key-value pairs,
respectively.
2. The string data type in Python represents a sequence of characters and is immutable,
meaning its contents cannot be changed once it is created. Strings can be manipulated
using various methods such as slicing, concatenation, and formatting.
Enrollment No. 230210107079
3. Lists and tuples are similar in many ways, but tuples are immutable, whereas lists are
mutable. Lists and tuples can hold elements of any data type and can be indexed and
sliced like strings. However, lists offer additional methods such as append, insert,
remove, and pop that allow for manipulation of the list's contents.
4. Dictionaries are another important sequence type in Python and are implemented as
unordered collections of key-value pairs. Each element in a dictionary consists of a key
and a corresponding value. Dictionaries can be used to store and retrieve data quickly
based on the key.
5. Sets are collections of unique elements that are unordered and mutable. Sets are often
used to perform set operations such as union, intersection, and difference.
Safety and necessary Precautions:
1. Use of proper data validation.
2. Secure data storage.
3. Proper error handling.
4. Testing and debugging.
5. Keeping software up to date.
6. Proper code formatting and documentation.
Procedure:
1. Create a string variable using single or double quotes. Use string methods like upper(),
lower(), strip(), split(), join(), and replace() to manipulate the string as needed. Use
indexing and slicing to access specific characters or substrings within the string.
CODE:
my_string = " Hello, Python Programming! "
print("Original String:", my_string)
print("Uppercase:", my_string.upper())
print("Lowercase:", my_string.lower())
print("Stripped:", my_string.strip())
print("Split:", my_string.split())
print("Replaced 'Python' with 'Java':", my_string.replace("Python", "Java"))
print("Joined with '-':", "-".join(my_string.strip().split()))
print("First character:", my_string[0])
print("Slice [7:13]:", my_string[7:13])
2. Create a tuple variable using parentheses. Use indexing and slicing to access specific
elements or subsets within the tuple. Tuples are immutable, so you cannot add, remove or
modify elements once created.
CODE:
my_tuple = (10, 20, 30, 40, 50)
print("Tuple:", my_tuple)
print("Element at index 2:", my_tuple[2])
print("Slice [1:4]:", my_tuple[1:4])
Enrollment No. 230210107079
3. Create a list variable using square brackets. Use indexing and slicing to access specific
elements or subsets within the list. Use list methods like append(), insert(), remove(), pop(),
extend(), and sort() to modify the list as needed. Lists are mutable, so you can add, remove
or modify elements once created.
CODE:
my_list = [5, 10, 15, 20]
print("Original List:", my_list)
my_list.append(25)
my_list.insert(2, 12)
my_list.remove(10)
popped = my_list.pop()
my_list.extend([30, 35])
my_list.sort()
print("Modified List:", my_list)
print("Popped Element:", popped)
4. Create a dictionary variable using curly braces or the dict() constructor. Use keys to access
values within the dictionary. Use dictionary methods like keys(), values(), and items() to
access different parts of the dictionary. Use del or pop() to remove elements from the
dictionary. Use assignment to add or modify elements in the dictionary.
CODE:
my_dict = {
"name": "Alice",
"age": 22,
"course": "Python"
}
print("Original Dictionary:", my_dict)
print("Access name:", my_dict["name"])
print("Access age using get():", my_dict.get("age"))
print("Keys:", my_dict.keys())
print("Values:", my_dict.values())
print("Items:", my_dict.items())
my_dict["age"] = 23
my_dict["email"] = "alice@example.com"
del my_dict["course"]
removed = my_dict.pop("email")
print("Modified Dictionary:", my_dict)
print("Removed 'email':", removed)
5. Create a set variable using curly braces or the set() constructor. Use set methods like add(),
remove(), pop(), union(), and intersection() to modify or perform operations on the set. Sets
do not allow duplicate elements, so adding the same element multiple times will only add
it once.
CODE:
Enrollment No. 230210107079
my_set = {1, 2, 3, 4}
print("Original Set:", my_set)
my_set.add(5)
my_set.remove(2)
my_set.add(3)
set_b = {3, 4, 5, 6}
print("Union with set_b:", my_set.union(set_b))
print("Intersection with set_b:", my_set.intersection(set_b))
print("Element removed with pop():", my_set.pop())
print("Final Set:", my_set)
Observations:
Output of 1.
Output of 2.
Output of 3.
Output of 4.
Enrollment No. 230210107079
Output of 5.
Conclusion:
understand and apply basic Python operations on strings, tuples, lists, dictionaries, and sets.
We practiced element access, manipulation, and key methods for each data type. This enhanced
our ability to choose and use the right sequence type effectively in Python programming.
Quiz:
1. What method can you use to convert a string to uppercase in Python?
Ans. Use the .upper() method to convert a string to uppercase.
text = "python"
print(text.upper()) # Output: PYTHON
2. What is the difference between a tuple and a list in Python?
Ans. A list is mutable, meaning its elements can be changed. It is defined using
square brackets [].
A tuple is immutable, meaning its elements cannot be changed. It is defined
using parentheses ().
my_list = [1, 2, 3] # List
my_tuple = (1, 2, 3) # Tuple
3. How do you add an element to a list in Python?
Ans. .append() method to add an element at the end of a list.
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits) # Output: ['apple', 'banana', 'cherry']
4. How do you access a value in a dictionary using its key in Python?
Ans. key inside square brackets or the .get() method to access a value.
info = {"name": "Alice", "age": 22}
print(info["name"]) # Output: Alice
print(info.get("age")) # Output: 22
5. What is a set in Python?
Ans. A set is an unordered collection of unique elements. It is defined using curly
braces {} or the set() function. Duplicate elements are automatically removEXed.
my_set = {1, 2, 3, 2}
print(my_set) # Output: {1, 2, 3}
Enrollment No. 230210107079
Suggested Reference:
1. https://docs.python.org/3/library/
2. https://www.tutorialspoint.com/python/
3. https://www.geeksforgeeks.org/
4. https://realpython.com/
5. https://www.w3schools.com/python/
References used by the students:
1. Python Standard Library - https://docs.python.org/3/library/
2. Geeksforgeeks - https://www.geeksforgeeks.org/
Rubric wise marks obtained:
Rubrics 1 2 3 4 5 Total
Marks
Knowledge of Programming Team work (2) Communication Skill Ethics(2)
subject (2) Skill (2)
Goo Averag Goo Averag Good Satisfactory Good Satisfactory Good Average
d (2) e (1) d (2) e (1) (2) (1) (2) (1) (2) (1)