BCS-DS-427B : PYTHON
DAY4
Table of
Contents
Tuple
02
LIST
01
Set
03
04 Dictionary
LIST
A data structure in
which multiple data
elements can be stored.
List are mutable,
meaning, their elements
can be changed.
01
LISTS
 A list is a sequential collection of values, it is a data structure
 Each value has a location (an index)Indexes range from 0 to n-1 (where n
is the length of the list) and from -1 to –n
Example, consider list1=['H','E','L','L','O'],
first element can be referred by list1[0]
Memory representation of List
Index value or location
To add value in list at specific
insert(loc,va
lue)
To add value in list at the end
To delete value from list at pa
loc
To delete list
Methods in list
remove(l
oc)
append()
del()
index(va
lue) To find location of value in list
Operators in
List
Creating a List
• A list is created by placing all the items
(elements) inside a square bracket [ ],
separated by commas.
• It can have any number of items and they may be
of different types (integer, float, string etc.).
Accessing elements of List
Index operator [ ] is used to access
an item in a list. Index starts from 0
• Using Positive Index e.g. marks[0]
• Using Negative Index e.gmarks[-1]
90 80 50 70 60
0 1 2 3 4
-5 -4 -3 -2 -1
LIST OPERATIONS
Change/Add in
List
Delete from list Slicing a list
Various functions associated with list are:
Considering marks list
● Index( ) - Returns the index of the element in list
e.g marks1.index(90) 0
● Sort()- Sort items in a list in ascending order
e.g marks.sort()
marks [40,50,80,90]
Sort items in a list in descending order
e.g marks.sort(reverse=True)
marks [90,80,50,40]
● Reverse()- Reverse the order of items in the list
e.g marks.reverse()
marks [90,80,50,40]
● + operator- To concat lists
e.g list3=list1+list2
● * operator- To repeat list
e.g list4=list1*2
List Methods & Operators
Tuple
A tuple is an immutable data type in python, almost
similar to a list in python in terms of indexing and
having duplicate members. It is a collection data type
that stores python objects separated by commas and
within( ).
02
Operations on Tuple
Join two tuples
T3=t1+t2
Deleting tuple
Create Tuple
Num1=(3.14,9.18)
Num2=3.14, 9.18
Access elements
1. Using positive index
Num1[0]3.14
2. Using Negative Index
Num1[-1] 9.18
Changing
Tuple
They are immutable
e.g Num1[0]=89 will
Raise error
del Num1
Slicing
Marks=(20,40,80,87)
Marks[0:2]
???
Concatenate
BUILT-IN
METHODS ON
TUPLE
Method Description
len() Return the length (the number of items) in the
tuple. e.g. len(t1)
max() Return the largest item in the tuple. e.g. max(t1)
min() Return the smallest item in the tuple e.g. min(t1)
sorted() Take elements in the tuple and return a new
sorted list (does not sort the tuple itself).
e.g. sorted(t1)
sum() Retrun the sum of all elements in the tuple.
e.g. sum(t1)
SET
A set is an unordered collection of items.
Every set element is unique (no duplicates).
We can add or remove items from it.
Sets can also be used to perform mathematical set
operations like union, intersection, symmetric
difference, etc.
03
Operations on Set
Creating or
Adding in Set
Set1={1,2,3}
MixedSet1={3,4,”avi”,(9,0)}
Set1.add(4)
Viewing Elements
of Set
Write Set1 to view full set
We can’t refer a value using
index operator[ ]
Deleting from Set
Set1.remove(3)
Remove element 3 from set1
Set1.clear()
Remove all elements from
set
Operations on Set
Union( | )
Set3=Set1 | Set2
Set3=Set1.union(Set2)
Intersection( &)
Difference(-)
Set3=Set1.intersection(Set2)
Set3=Set1.difference(Set2)
Source:https://images.app.goo.gl/6De398s4QcZFdiAi7
DICTIONARY
“A dictionary is mutable and is another
container type consist of items containing
keys and their corresponding values.
CREATING DICTIONARY
● All items exist in dictionary in { } separated with
commas.
● Each key is separated from its value by a colon (:)
e.g
dict1 = {'Name': ‘Ravi', 'Age': 37, ‘Profile':
‘Manager'};
ACCESSING ELEMENTS IN DICTIONARY
● To access elements ,use key as index
e.g print(dict1[‘Name’] ) Ravi

OPERATIONS ON
DICTIONARY
DELETE FROM DICTIONARY
● To update value refer it by key as index
eg
del dict1['Name']; # remove entry with key 'Name'
dict.clear(); # remove all entries in dict
del dict ; # delete entire dictionary
UPDATING DICTIONARY
● To update value refer it by key as index
eg dict1[‘Name’]=‘Rahi’
THANKS
Does anyone have
any questions?
Feel free to ask….

python ..... _

  • 1.
  • 2.
  • 3.
    LIST A data structurein which multiple data elements can be stored. List are mutable, meaning, their elements can be changed. 01
  • 4.
    LISTS  A listis a sequential collection of values, it is a data structure  Each value has a location (an index)Indexes range from 0 to n-1 (where n is the length of the list) and from -1 to –n Example, consider list1=['H','E','L','L','O'], first element can be referred by list1[0] Memory representation of List Index value or location
  • 5.
    To add valuein list at specific insert(loc,va lue) To add value in list at the end To delete value from list at pa loc To delete list Methods in list remove(l oc) append() del() index(va lue) To find location of value in list
  • 6.
    Operators in List Creating aList • A list is created by placing all the items (elements) inside a square bracket [ ], separated by commas. • It can have any number of items and they may be of different types (integer, float, string etc.). Accessing elements of List Index operator [ ] is used to access an item in a list. Index starts from 0 • Using Positive Index e.g. marks[0] • Using Negative Index e.gmarks[-1] 90 80 50 70 60 0 1 2 3 4 -5 -4 -3 -2 -1
  • 7.
  • 8.
    Various functions associatedwith list are: Considering marks list ● Index( ) - Returns the index of the element in list e.g marks1.index(90) 0 ● Sort()- Sort items in a list in ascending order e.g marks.sort() marks [40,50,80,90] Sort items in a list in descending order e.g marks.sort(reverse=True) marks [90,80,50,40] ● Reverse()- Reverse the order of items in the list e.g marks.reverse() marks [90,80,50,40] ● + operator- To concat lists e.g list3=list1+list2 ● * operator- To repeat list e.g list4=list1*2 List Methods & Operators
  • 9.
    Tuple A tuple isan immutable data type in python, almost similar to a list in python in terms of indexing and having duplicate members. It is a collection data type that stores python objects separated by commas and within( ). 02
  • 10.
    Operations on Tuple Jointwo tuples T3=t1+t2 Deleting tuple Create Tuple Num1=(3.14,9.18) Num2=3.14, 9.18 Access elements 1. Using positive index Num1[0]3.14 2. Using Negative Index Num1[-1] 9.18 Changing Tuple They are immutable e.g Num1[0]=89 will Raise error del Num1 Slicing Marks=(20,40,80,87) Marks[0:2] ??? Concatenate
  • 11.
    BUILT-IN METHODS ON TUPLE Method Description len()Return the length (the number of items) in the tuple. e.g. len(t1) max() Return the largest item in the tuple. e.g. max(t1) min() Return the smallest item in the tuple e.g. min(t1) sorted() Take elements in the tuple and return a new sorted list (does not sort the tuple itself). e.g. sorted(t1) sum() Retrun the sum of all elements in the tuple. e.g. sum(t1)
  • 12.
    SET A set isan unordered collection of items. Every set element is unique (no duplicates). We can add or remove items from it. Sets can also be used to perform mathematical set operations like union, intersection, symmetric difference, etc. 03
  • 13.
    Operations on Set Creatingor Adding in Set Set1={1,2,3} MixedSet1={3,4,”avi”,(9,0)} Set1.add(4) Viewing Elements of Set Write Set1 to view full set We can’t refer a value using index operator[ ] Deleting from Set Set1.remove(3) Remove element 3 from set1 Set1.clear() Remove all elements from set
  • 14.
    Operations on Set Union(| ) Set3=Set1 | Set2 Set3=Set1.union(Set2) Intersection( &) Difference(-) Set3=Set1.intersection(Set2) Set3=Set1.difference(Set2) Source:https://images.app.goo.gl/6De398s4QcZFdiAi7
  • 15.
    DICTIONARY “A dictionary ismutable and is another container type consist of items containing keys and their corresponding values.
  • 16.
    CREATING DICTIONARY ● Allitems exist in dictionary in { } separated with commas. ● Each key is separated from its value by a colon (:) e.g dict1 = {'Name': ‘Ravi', 'Age': 37, ‘Profile': ‘Manager'}; ACCESSING ELEMENTS IN DICTIONARY ● To access elements ,use key as index e.g print(dict1[‘Name’] ) Ravi  OPERATIONS ON DICTIONARY DELETE FROM DICTIONARY ● To update value refer it by key as index eg del dict1['Name']; # remove entry with key 'Name' dict.clear(); # remove all entries in dict del dict ; # delete entire dictionary UPDATING DICTIONARY ● To update value refer it by key as index eg dict1[‘Name’]=‘Rahi’
  • 17.
    THANKS Does anyone have anyquestions? Feel free to ask….