0% found this document useful (0 votes)
110 views5 pages

Python Lists, Tuples, Sets, and Dictionaries

The document discusses various data structures in Python - lists, tuples, sets, and dictionaries. It provides examples of creating, accessing, modifying, and iterating over elements in each type of data structure. Key operations demonstrated include inserting and removing list elements, immutable tuples, unordered and unique set elements, nested dictionaries, and merging dictionaries from keys and values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views5 pages

Python Lists, Tuples, Sets, and Dictionaries

The document discusses various data structures in Python - lists, tuples, sets, and dictionaries. It provides examples of creating, accessing, modifying, and iterating over elements in each type of data structure. Key operations demonstrated include inserting and removing list elements, immutable tuples, unordered and unique set elements, nested dictionaries, and merging dictionaries from keys and values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

31 01 2023

LIST IN PYTHON USING STRINGS AND NEW NOTATIONS

/\ We can give data as in the form of list

>>> num = [25, 12, 36, 95, 14]


>>> num
[25, 12, 36, 95, 14]

>>> num [5]


Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
num [5]
IndexError: list index out of range

>>> num [3]


95
>>> num [2:]
[36, 95, 14]
>>> num [0]
25
>>> num [-5]
25

/\ We can give strings as data input they will store we can use multi at a time

>>> names = ['navin', 'kiran', 'john']


>>> values = [9.5, 'navin' ,25]
>>> names
['navin', 'kiran', 'john']

>>> mil = [num, names]


>>>mil
[[25, 12, 36, 95, 14], ['navin', 'kiran', 'john']]

/\ We can add or edit the above in data by

for inserting
>>> num
[25, 12, 36, 95, 14, 45]
>>> num.insert(2,77)
>>> num
[25, 12, 77, 36, 95, 14, 45]

for removing
>>> num.remove(14)
>>> num
[25, 12, 77, 36, 95, 45]

we use pop for going to the last element of the data


>>> num.pop(1)
12
>>> num.pop()
45
>>> num.pop(3)
95

to delete a desired element from 2 to everything


>>> del num[2:]
>>> num
[25, 77]

if we want to add values to the string


>>> num
[25, 77]
>>> num.extend([29,12,14,36])
>>> num
[25, 77, 29, 12, 14, 36]

to find the minimum, maximum, sum, sorting(low to high) values

min
>>> num
[25, 77, 29, 12, 14, 36]
>>> min(num)
12

max
>>> max(num)
77

sum
>>> sum(num)
193

sorting
>>> num.sort()
num
[12, 14, 25, 29, 36, 77]

end = ['thank you']


end
['thank you']

TUPLE AND SET IN PYTHON

/\Tuple is a kind of lists there you cant change like lists


we shoud use square brackets we use tupile to increase speed
of exceution than the lists

tup = (21,36,14,25)
tup
(21, 36, 14, 25)
tup[1]
36
tup [1] = 33
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
tup [1] = 33
TypeError: 'tuple' object does not support item assignment

/\ sets is the collection of elements(unique), only use flower


brackets and it wonts sort

s = {22, 25, 14, 21, 5}


s
{5, 21, 22, 25, 14}
s = {5, 14, 21, 22, 3}
s
{3, 5, 21, 22, 14}

/\ Also the use of set is not sorting for faster excecution


And here desired values cant select beacuse its not in order
s
{3, 5, 21, 22, 14}
s[2]
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
s[2]
TypeError: 'set' object is not subscriptable

DICTIONARY IN PYTHON

/\ Starting while specifing we need to use flower bkts


and later for desired fetching value we use sqr bkts

data = {1:'balaji', 2:'paul', 4:'navin'}


data
{1: 'balaji', 2: 'paul', 4: 'navin'}
data[4]
'navin'
data[1]
'balaji'
data[3]
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
data[3] =====> only stored item will be displayed from that
KeyError: 3

/\ we can use the functions (press shift+space after dot)

print(data.get(4))
navin
data.get(1,'Not Found')
'balaji'
data.get(3, 'Not Found')
'Not Found'

/\ We can merge the two strings using dictionary


>>> keys = ['balaji', 'kiran', 'navin']
>>> values = ['python', 'java', 'js']
>>> data = dict(zip(keys,values))
>>> data
{'balaji': 'python', 'kiran': 'java', 'navin': 'js'}

/\ We can add data to a dictionary

>>> data['harsh'] = 'cs'


>>> data
{'balaji': 'python', 'kiran': 'java', 'navin': 'js', 'harsh': 'cs'}

/\ We can remove a data from the dictionary

>>> del data['navin']


>>>> data
{'balaji': 'python', 'kiran': 'java', 'harsh': 'cs'}

/\ Creating a mini model of dictionary

>>> prog = {'JS':'ATOM', 'CS':'VS', 'PYTHON':['PYCHARM','SUBLIME'], 'JAVA':


{'JSE':'NETBEANS', 'JEE':'ECLIPSE'} }
>>> prog
{'JS': 'ATOM', 'CS': 'VS', 'PYTHON': ['PYCHARM', 'SUBLIME'], 'JAVA': {'JSE':
'NETBEANS', 'JEE': 'ECLIPSE'}}

>>> prog['JS']
'ATOM'

>>> prog['PYTHON']
['PYCHARM', 'SUBLIME']

>>> prog['PYTHON'][1]
'SUBLIME'

>>> prog['JAVA']

{'JSE': 'NETBEANS', 'JEE': 'ECLIPSE'}


prog['JAVA']['JEE']

'ECLIPSE'

You might also like