27
loading...
This website collects cookies to deliver better user experience
List_2 = [ 1, 2, 3, 4, 5, 6 ]
List_3 = [ “a”, “b”, “c”, “d”, “e”, “f” ]
print ("List _1[0]: ", List _1[0])
print ("List _2[1:5]: ", List _2[1:5])
List_1[0] : 1
List_2[1:5] : [“b”, “c”, “d”, “e”, “f” ]
#Empty List
List_1 = [ ]
#Add 3 to the beginning of the empty list .
List_1 [0] = 3
print(List_1)
List_2 = [ 1, 2, 3, 4, 5, 6 ]
print ("Value at index 3 :" , List_2 [ 3])
List_2 [ 3] = "Mathematics"
print ("New value at index 3 :" , List_2 [ 3])
Value at at index 3 : 4
New value at index 3 : Mathematics
List_5 = [1, 3.14, “abc”, 5, 7, 8]
print(List before deleting value at index 2 : ", List_5)
del List_5[2]
print("List after deleting value at index 2 : ", List_5)
List before deleting value at index 2 : [1, 3.14, "abc", 5, 7, 8]
List after deleting value at index 2 : [1, 3.14, 5, 7, 8]
List_2 = [ 1, 2, 3, 4, 5, 6 ]
Print(List_2 + [7,8,9])
Num = [9,8,7]
Print(num * 3)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[9,8,7,9,8,7,9,8,7]
in
operator is used. It returns True if the item exists and False if it doesn’t.Num = [9,8,7]
print(8 in Num)
print(3 in Num)
True
False
not
operator to check if an item is not in a list.Num = [9, 8, 7]
Print( not 4 in Num )
Print( 4 not in Num )
Print( not 8 in Num )
Print( 8 not in Num )
List_5 = [1, 3.14, "abc", 5, 7, 8]
# To print the entire list items
print(List_5[:])
# To print from a given index (say 2 ) to the end.
print(List_5[2:])
# To print from a given index (say 2 ) to another index (say 4).
print(List_5[2:4])
# To print from the beginning to the second last item.
print(List_5[ : -1])
# To print from the beginning to the third last item.
print(List_5[ : -1])
# To print from a given index (say 1) to the second last item.
print(List_5[ 1 : -1])
# To reverse the list
print(List_5[ : : -1])
[1, 3.14, 'abc', 5, 7, 8]
['abc', 5, 7, 8]
['abc', 5]
[1, 3.14, 'abc', 5, 7]
[1, 3.14, 'abc', 5]
[3.14, 'abc', 5, 7]
[8, 7, 5, 'abc', 3.14, 1]
# Empty Dictionary
dict = { }
# Dictionary with data
dict = { ‘food’ : ‘Pilau’ , ‘drink’: ‘Soda’, ‘fruit’: ‘strawberry’}
dict = { ‘food’ : ‘Pilau’ , ‘drink’: ‘Soda’, ‘fruit’: ‘strawberry’}
print ("dict[‘food’]: " , dict[‘food’])
print ("dict[‘fruit’]: " , dict[‘fruit’])
dict[‘food’]: Pilau
dict[‘fruit’]: strawberry
dict = { ‘food’ : ‘Pilau’ , ‘drink’: ‘Soda’, ‘fruit’: ‘strawberry’}
# To add new entry to the dictionary
dict['age'] = 28
# To change `drink` from soda to water use:
dict['drink'] = water
del
statement and specify the index of the element. When del
statement is used on a dictionary without defining the key attribute then the entire dictionary is deleted. However, to only clear the dictionary contents and preserve the dictionary itself us the clear
function. This clears every element and returns an empty dictionary.dict = { ‘food’ : ‘Pilau’ , ‘drink’: ‘Soda’, ‘fruit’: ‘strawberry’}
# Remove entry with key ‘fruit
del dict[‘fruit’]
# Remove all entries in the dictionary
del dict
# Remove all entries of the dictionary.
dict.clear()
copy()
method to copy a dictionary.dict = { ‘food’ : ‘Pilau’ , ‘drink’: ‘Soda’, ‘fruit’: ‘strawberry’}
d = dict.copy()
dict = { ‘food’ : ‘Pilau’ , ‘drink’: ‘Soda’, ‘fruit’: ‘strawberry’}
# Check if a given letter/word is a key in dict
letter = input( ‘Enter a letter / word’)
if letter in dict
print(“The value is: ’ , dict[letter]
else:
print(“Not in dictionary”)
for key in dict
print(key) # Keys
print(dict[key]) # Values
d = {‘A’:1, ‘B’:2}
# To generate a list from the dictionary keys
list(d)
# To generate a list from the dictionary values
list(d.values())
# # To generate a list from the dictionary keys – values pair
list(d.items())
['A', 'B']
[1, 2]
[('A', 1), ('B', 2)]
d.items()
are called tuples.dict()
function can also create a dictionary. It’s almost like the opposite of the items method above.d = dict([(‘z’,26), (‘x’,24)])
Print(d)
{'z': 26, 'x': 24}
d = {s : len(s) for s in words}
b = (1,2,'T',7.9)
c="a",10,20.9
print(b)
print(c)
(1, 2, 'T', 7.9)
("a",10,20.9)
tuple_1 = ()
tuple_1=(103,)
tuple_1=(103,104,105,106)
tuple_2=('Marina', 'Naftal','Joanne','Alex')
print(tuple_1[0])
print(tuple_1[0:2])
print(tuple_2[-3:-1])
print(tuple_1[0:])
print(tuple_2[:2])
103
(103, 104)
('Naftal', 'Joanne')
(103, 104, 105, 106)
('Marina', 'Naftal')
del
statement.tuple_2 = ('Marina', 'Naftal','Joanne','Alex')
del tuple_2
tuple_1=(103,104,105,106)
tuple_2=('Marina', 'Naftal','Joanne','Alex')
tuple_3=tuple_1+tuple_2
print(tuple_3)
(103, 104, 105, 106, 'Marina', 'Naftal', 'Joanne', 'Alex')
tuple_4=(10,20,30);
print(tuple_4*2)
(10, 20, 30, 10, 20, 30)
a = {1,5,'s',1.5}
print(a)
print(b)
{1, 's', 5, 1.5}
f = set([2,3,4])
print(f)
{2, 3, 4}
f = set([2,3,4])
print(len(f))
4
a = {1,5,'s',1.5}
for x in a:
print(x)
add()
function allows the addition of a single element to a set.update()
function allows the addition of more than one element to a set.c = set()
c.add(8)
print(c)
c.update([1,5,'s',1.5])
print(c)
{8}
{1, 1.5, 5, 8, 's'}
remove()
function removes elements from the set specified as a parameter.discard()
function is used to remove an element when it's existence in the set is not assured.pop()
function is used to remove a random element from the set.c = { 1, 1.5, 5, 8, 's'}
c.remove(1.5)
print(c)
{1, 5, 8, 's'}
union
method as shown below.c = { 1, 1.5, 5, 8, 's'}
f = {10, 2, 3, 4}
d = {7,'i','p',45}
print(c|f)
print(c.union(d))
print(c|f|d)
{1, 1.5, 2, 3, 5, 4, 8, 10, 's'}
{1, 1.5, 5, 7, 8, 's', 45, 'i', 'p'}
{1, 1.5, 2, 3, 5, 4, 7, 8, 10, 's', 45, 'i', 'p'}
intersection
method as shown below.c = { 1, 10, 5, 8, 's'}
f = {10, 2, 3, 4,5}
d = {7,'i','p','s'}
print(c&f)
print(c.intersection(d))
# Results into an empty set
print(c.intersection(d,f))
{10, 5}
{'s'}
set()
difference()
method.c = { 1, 10, 5, 8, 's'}
f = {10, 2, 3, 4,5}
d = {7,'i','p','s'}
print(c-f)
print(f-c)
print(c.difference(d))
print(c.difference(d,f))
{8, 1, 's'}
{2, 3, 4}
{8, 1, 10, 5}
{1, 8}
frozenset()
function.a = {1,3,5,7,'y',89}
b = frozenset(a)
print(b)
frozenset({1, 'y', 3, 5, 7, 89})
b.add(8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'