Python Ch3
Python Ch3
Data Structures in
Python
Example 2:
# Creating List of numbers
List = [10, 20, 30]
print(“List of numbers: ", List)
Output:
List of numbers: : [10, 20, 30]
Dept. of Computer/IT Engineering LJ Polytechnic
Data Structure
List
Example 3:
# Creating a List of strings and accessing using index
List = ["Programming", "in", "Python"]
print("List Items: ")
print(List[0])
print(List[2])
Output:
List Items:
Programming
Python
Output:
Multi-Dimensional List:
[['Programming', 'in'], ['Python']]
Output:
List with the use of Numbers:
[1, 2, 4, 4, 3, 3, 3, 6, 5]
Output:
List with the use of Mixed Values:
[1, 2, 'Programming', 4, 'in', 6, 'Python']
Output:
Before: [17, 123]
After: [17, 5]
Output:
3
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c)
Output:
[1, 2, 3, 4, 5, 6]
a = [1]
a = a * 3
print(a)
Output:
[1, 1, 1]
Output:
['b', 'c’]
['a', 'b', 'c', 'd’]
['d', 'e', 'f']
Output:
['a', 'b', 'c', 'd', 'e', 'f']
Output:
['a', 'x', 'y', 'd', 'e', 'f']
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
Output:
['apple', 'banana', 'cherry’, ‘orange’]
Output:
[]
Output:
['apple', 'banana', 'cherry’, ‘orange’]
Output:
1
Output:
['apple', 'banana', 'cherry’, 'Ford', 'BMW', 'Volvo’]
Output:
0
Output:
['apple’, ‘orange’, 'banana', 'cherry’]
Output:
['apple','cherry’]
Output:
['apple', 'cherry’]
Output:
['cherry’, 'banana’, 'apple']
Output:
['apple’, ‘banana’, ‘cherry’]
# deleting a tuple
Method Description
Output:
First tuple length : 3
Second tuple length : 2
# accessing a dictionary
Output:
KeyError Traceback (most
recent call last)<ipython-input-58-d6f7f5c44745> in <module>
1 dict1 ={'Name':'Zara','Age':7,'Class':'First'};---->
2print("dict1['Alice']: ", dict1['Alice'])KeyError: 'Alice'
Output:
dict1['Age']: 8
dict1['School']: LJP
Output:
NameError Traceback (most
recent call last)<ipython-input-65-8a3a75cd05f3> in
<module> 3 dict1.clear();# remove all entries in dict
4dels ;# delete entire dictionary---->
5print("dict1['Age']: ", dict1['Age']) 6
print("dict1['School']: ", dict1['School'])NameError: name
'dict1' is not defined
Method Description
dict.get(key, default=None) Returns the value of the item with the specified key
dict.get() : Returns the value of the item with the specified key
Example:
# get()
dict = {'Name': 'Zara', 'Age': 7}
print("Value : %s" % dict.get('Age'))
print("Value : %s" % dict.get('Education', "Never"))
Output:
Value : 7
Value : Never
Output:
Value :dict_items([('Name', 'Zara'), ('Age', 7)])
Output:
Value :dict_keys(['Name', 'Age'])
Output:
Value : 7
Value : None
Output:
Note: Sets are unordered, so you cannot be sure in which order the items will appear.
Output:
Output:
{True, 'abc', 34, 40, 'male'}
Output:
cherry,banana,apple
Output:
True
Once a set is created, you cannot change its items, but you can
add new items. To add one item to a set use the add() method.
Output:
Output:
{'cherry', 'banana'}
Deleting a Set
There are several ways to join two or more sets in Python. You
can use the union() method that returns a new set containing
all items from both sets.
#union() method returns a new set with all items from both
sets
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
Output:
{1, 'c', 2, 3, 'a', 'b'}
There are several ways to join two or more sets in Python. You
can use the union() method that returns a new set containing
all items from both sets, or the update() method that inserts all
the items from one set into another.
#union() method returns a new set with all items from both
sets
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
Output:
{1, 'c', 2, 3, 'a', 'b'}
The update() method that inserts all the items from one set
into another.
#update() method inserts the items in set2 into set1
Output:
{1, 'c', 2, 3, 'a', 'b'}
Note: Both union() and update() will exclude any duplicate items.