Path: blob/master/Python Essentials/2 Python Data Containers (Lists, Tuples, Dict, strings).ipynb
3603 views
Python Data Containers
Lists
Lists are ordered collections of items, defined using square brackets [].
You can access, modify, and perform operations on lists.
Tuples
Tuples are ordered, immutable collections of items, defined using parentheses ()
. Once created, the elements of a tuple cannot be changed.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[24], line 1
----> 1 m.append(9)
AttributeError: 'tuple' object has no attribute 'append'
Dictionaries
Dictionaries store data in key-value pairs, defined using curly braces {}
. They are unordered, mutable, and indexed by keys.
Sets
Sets are unordered collections of unique elements, defined using curly braces {}
. They are useful for mathematical operations like union, intersection, and difference.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[41], line 1
----> 1 set1[0]
TypeError: 'set' object is not subscriptable
Strings
Strings are sequences of characters enclosed in single or double quotes. They support slicing, concatenation, and many built-in methods.
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[39], line 4
1 #please note: strings are immutable
2 #s.append("456")
3 #del s[2]
----> 4 del s
5 s
NameError: name 's' is not defined
Hands-on Quick Practice
Try solving the following problems:
Create a tuple of 5 numbers and print the second and fourth element.
Make a dictionary for storing student info (name, age, grade) and print values.
Use sets to find common elements between two lists.
Write a program to check if a word exists in a given string.
Convert a string to uppercase and count how many vowels it has.