0% found this document useful (0 votes)
4 views

Notes_Class_10_Recap_Python_List_Tuples_Dictionary_set

The document provides an overview of Python's compound data types: lists, tuples, dictionaries, and sets. It explains the characteristics and functionalities of each type, highlighting differences such as mutability in lists and tuples, and the unique properties of sets and dictionaries. Additionally, it includes examples of how to create and manipulate these data types in Python.

Uploaded by

Rishabh Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Notes_Class_10_Recap_Python_List_Tuples_Dictionary_set

The document provides an overview of Python's compound data types: lists, tuples, dictionaries, and sets. It explains the characteristics and functionalities of each type, highlighting differences such as mutability in lists and tuples, and the unique properties of sets and dictionaries. Additionally, it includes examples of how to create and manipulate these data types in Python.

Uploaded by

Rishabh Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

LIST, TUPLE,

DICTIONARY & SET iN


PYTHON
Lists
The lists and tuples are Python’s compound data types. They are basically the same
types with one difference. List can be changed or modified i.e. mutable but tuples
cannot be changed or modified i.e. immutable.
A list in Python represents a list of comma separated values of any datatypes
between square brackets.

List items are ordered, changeable, and allow duplicate values.


List items are indexed, the first item has index [0], the second item has index [1]
etc.
When we say that lists are ordered, it means that the items have a defined order,
and that order will not change.

If you add new items to a list, the new items will be placed at the end of the list.

Note: There are some list methods that will change the order, but in general: the
order of the items will not change.
Since lists are indexed, lists can have items with the same value. Lists allow
Lists
To assign a list to a variable.
list1=[1,2,3,4,5]
list2=[‘a’, ‘e’, ‘i’, ‘o’, ‘u’]
list3=[‘DPS’, 12, ‘Bopal’]
List4=[] #empty list
list5=list() #creating an empty list using list
function
list6=[3,4,[5,6],7,8] #nested list
print(type(list1)) #will display <class list>
print(list1[1]) #will print 2
print(list2[3]) #will print ‘o’
print(list2[-2]) #will print ‘o’
Lists
list7=list(‘DPS’)
print(list7) #will print [‘D’, ‘P’, ‘S’]
t1=(‘w’, ‘o’, ‘r’, ‘k’) # creates tuple t1
print(list(t1)) #will print [‘w’, ‘o’, ‘r’, ‘k’]

To get list from user


list7=list(input(“Enter a list:”))
Enter a list: 23456
print(list7) #will print [‘2’, ‘3’, ‘4’, ‘5’]
Refer list6
print(list6[1:3]) #will print [2,3]
print(list6[:3]) #will print [3,4,[5,6]]
print(list6[3:]) #will print [7,8]
print(list6[3:3]) []
Lists
print(3 in list6) # will print True
print(list6[-4:-1]) #will print [4,[5,6],7]
list6[-3]=[5,6,7] # lists are mutable
print(list6) #will print [3,4,[5,6,7],7,8]
list6[0:1]=[1,2]
print(list6) #will print [1, 2, 4, [5, 6], 7, 8]
list6[1:3]=[10]
print(list6) #will print [3,10,7,8]
Lists
list6=[3,4,[5,6],7,8] (consider this list as reference for the
following)
insert(): To insert a new list item, without replacing any of the
existing values at the specified index.
print(list6.insert(2,6)) # will print [3,4,2,[5,6],7,8]

Append(): To add an item to the end of the list.


print(list6.append(10)) #will print[3,4,[5,6],7,8,10]

extend(): To append more than one elements or append from


another list to the current list, use the extend() method.
print(list6.extend(11,12)) #will print[3,4,[5,6],7,8,11,12]
Lists
list6=[3,4,[5,6],7,8] (consider this list as reference for the following)
Note:
pop(): Removes the element at the specified position In python del is a keyword
print(list6.pop(1)) # will print [3,[5,6],7,8] and remove(), pop() are in-
print(list6.pop()) # will print [3,[5,6],7] built methods. The purpose
of these three are same but
remove(): Removes the first item with the specified value
the behavior is different
print(list6.remove(7)) # will print [3,[5,6],8] remove() method delete
values or object from the list
using value and del and
clear(): Removes all the elements from the list pop() deletes values or
print(list6.clear()) # will print [] object from the list using an
index.

del: Python del function is used to delete an item from a list at a user-specified
index. The index position of the List del function starts from 0 and ends at n-1
del list6[0] #will print[4,[5,6],7,8]
del list6 # will delete the entire list
print(list 6) # will show error ‘list6 not defined’
Lists
list6=[3,4,[5,6],7,8] (consider this list as reference for the
following)

copy(): Returns a copy of the list


l6=list6.copy()
print(l6) # will print [3,4,[5,6],7,8]
count(): Returns the number of elements with the specified value.
print(list6.count(4) # will display 1
index(): Returns the index of the first element with the specified
value
print(list6.index(3) # will display 0
reverse(): Reverses the order of the list
print(list6.reverse() # will display [8,7,[5,6],4,3]
Tuples
Tuples are used to store multiple items in a single variable.
A tuple is a collection which is ordered and unchangeable and allow
duplicate values.. So tuples are immutable.
Tuples are written with round brackets. Tuple items are indexed, the
first item has index [0], the second item has index [1] etc.
t=(1,2,3,4,5)
When we say that tuples are ordered, it means that the items have a
defined order, and that order will not change.
Tuples are unchangeable, meaning that we cannot change, add or
remove items after the tuple has been created.
Since tuples are indexed, they can have items with the same value.
print(len(t)) # will print 5
print(type(t)) # will print <class typle>
Tuples
While creating tuple using one element, always use comma after
the element. Otherwise Python will treat it as an integer/string
based on a value which you will put in.
tup1=(10)
print(tup1)) # will print <class int>
tup2=(10,)
print(type(tup2)) # will print <class tuple>
tup3=(1,2,3,4,5)
a,b,c,d,e=tup3
print(tup3) # will print (1,2,3,4,5)
print(a,b,c,d,e) # will print 1 2 3 4 5
del tup3 # will delete the tuple
DIFFERENCES BETWEEN LIST AND TUPLE IN PYTHON
Sno LIST TUPLE
1 Lists are mutable Tuples are immutable

The implication of
The implication of iterations is
2 iterations is Time-
comparatively Faster
consuming

Lists consume more Tuple consumes less memory


4
memory as compared to the list

Lists have several built-in Tuple does not have many


5
methods built-in methods.

Unexpected changes and


In a tuple, it is hard to take
6 errors are more likely to
place.
Set
Sets are a type of python collection. They are ordered,
unchangeable and unindexed. They do not have duplicate elements
and are identified by curly bracket. The set() constructor is used to
convert any collection such as list or tuple into a list.
Set is created by enclosing the comma-separated immutable items
with the curly brackets.
set1={10, ‘Vinay’, 19}
print(set1) # will print {10, 19, “Vinay”}
As set is unordered, the order of elements while printing will be
random.
Set does not allow duplicate elements. Set is unindexed, you cannot
access set elements by its index. However you can check if a
specified value is present in a set or not using in operator keyboard.
Set
You cannot change the elements after the creation of set, but
you can add elements using add() and update() functions.
The add() is used to add a single element at the end.
set1={10, ‘Vinay’, 19}
set1.add(‘Anu’)
print(set1) #will {10, ‘Anu’, 19, “Vinay”}

The update() is used to add any literals (list, tuple, set) in a set
set1={10, ‘Vinay’, 19}
set2={30,40}
set1.update(set2)
print(set1) #will {19, 40, 10, 30, “Vinay”}
Set
The remove(), discard() and pop() functions are used to in set to
delete from the set.
set1={10, ‘Vinay’, 19}
set1.remove(19)
print(set1) #will {10, ‘Vinay’}

set1={10, ‘Vinay’, 19}


set1.discard(19)
print(set1) #will {10, ‘Vinay’}

The major difference between remove() and discard() function is


that unlike remove() function, discard() function does not raise
an error when specified element is not present in the set.
Set
The pop() is used to delete the last elements from a list or tuple.
But set is an unordered in nature, so when you pop() you don’t
know which elements will get deleted in the set.
set1={10, ‘Vinay’, 19}
set1.clear()
print(set1) #will set()

clear() function is used to delete all elements in a set & del key
word is used to destroy or delete a set permanently.
set1={10, ‘Vinay’, 19}
del set1
print(set1) #will show an error
Dictionary
Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection which is ordered, changeable and do
not allow duplicates.
As of Python version 3.7, dictionaries are ordered. In Python 3.6
and earlier, dictionaries are unordered.
Dictionaries are written with curly brackets, and have keys and
values.

d= { “Name": “Anu", “Admn": 1234, “Roll": 30 }


print(d) # will display {“Name": “Anu", “Admn": 1234, “Roll":
30 }
Print(d[“Name”]) #will display ‘Anu’

You might also like