EXPT02
EXPT02
Experiment No. 2
Aim: Study of Strings, Lists, Tuples, Dictionaries, Sets, Accessing Elements, Properties,
Operations and methods on these data structures.
Problem Statement:
Theory:
1. Explain various Data Types in Python.
String: String is sequence of Unicode characters. We can use single quotes or double quotes to
represent strings. Multi-line strings can be denoted using triple quotes, ''' or """
s = "This is a string”
Declaring a list is pretty straight forward. Items separated by commas are enclosed within brackets
[ ].
We can use the slicing operator [ ] to extract an item or a range of items from a list. Index starts form
0 in Python
Tuple: Tuple is an ordered sequence of items same as list. The only difference is that tuples are
immutable. Tuples once created cannot be modified.
Tuples are used to write-protect data and are usually faster than list as it cannot change dynamically.
We can use the slicing operator [] to extract items but we cannot change its value.
Set: Set is an unordered collection of unique items. Set is defined by values separated by comma
inside braces {}. Items in a set are not ordered.
a = {5,2,3,1,4}
print(type(a))
We can perform set operations like union, intersection on two sets. Set have unique values. They
eliminate duplicates.
Since, set are unordered collection, indexing has no meaning. Hence the slicing operator [] does not
work.
It is generally used when we have a huge amount of data. Dictionaries are optimized for retrieving
data. We must know the key to retrieve the value.
In Python, dictionaries are defined within braces {} with each item being a pair in the form key:
value.
We use key to retrieve the respective value. But not the other way around.
UG Program in Electronics and Computer Science
Academic Year: 2024-2025
Program
UG Program in Electronics and Computer Science
Academic Year: 2024-2025
Output
Conclusion:
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________
________________________________________________________________________________