A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. Creating a tuple is as simple as putting different comma-separated values.¶
t = () #empty tuple
type(t)
te = (1,2,"three")
te
te2 = (4, "five", 6)
te + te2 #tuple addition
te[1] #get value based on index
te[1] = 4 #fails because remeber tuples are immutable
te[1:2] #Notice the comma after 2 which tells it is a tuple not any single element
x= (5)
y= (7)
(x,y) = (y,x)
x
y
def quotient_and_reminder(x,y):
q = x//y
r = x% y
return (q,r)
(quo, rem) = quotient_and_reminder(4,5)
print(quo)
print(rem)
t = (1, "two", 3 , 5, "onezerozero")
for i in t:
print(i)
List is one of the simplest and most important data structures in Python. Lists are enclosed in square brackets [ ] and each item is separated by a comma. Lists are collections of items where each item in the list has an assigned index value. A list is mutable, meaning you can change its contents.¶
a_list =[] #empty list
L = [1,2,3,"four"]
L
type(L)
L[1]
L[2]+1
L[3] + "-4-2"
L
L[2] = 7
L # the value changes
L = [1,2,3,4]
total=0
for i in L:
total += i
print(total)
L = [1,2,3,4]
L.append(5) # adds 5 at the end
L
L2 = [6,7,8]
L2
L + L2 #concatinate
L2.extend([9,10]) #addes elements at the end
L2
del(L2[2]) #remove second element
L2
L2.pop() #remove last element
L2
L2.remove(7) #searchs and removes first occurence , the second 7 still remains if any
L2
s = "abc"
list(s)
s = " I - am mad"
s.split("-") # splits at "-"
L = ['a', 'b', 'c']
''.join(L) # joins all the elements from list
'_'.join(L)
L = [2,7,1,9,0]
L
sorted(L)
L.sort()
L
L.reverse()
L
L.sort
L.insert(2,1)
L.pop()
L
L.pop(2)
L.reverse()
L= [100, 0, 1, 4, 4, 1, 6, 3, 4]
L.reverse()
L.index(1) #index of 1
L
Lists are mutable hence when alias is created and changes are made to the alias same original list is changed.¶
L1 = [1,2,3,4]
L2 = L1
L1
L2
L2.append(5)
L2 # 5 is added to the end of L2 because of previous command
L1 # 5 is added even though no operation was performed on L1, beacause both L1 and L2 points to same lists.
#### TO avoide this its better to assign them sepereatly
L1 = [1,2,3,4]
L2 = [1,2,3,4]
L1
L2
L2.append(5)
L2
L1 # you see L1 remains the same
#### Another way to do this is
L2 = L1[:]
L2
L1
L2.append(5)
L1 # not changed
L1 = [ 6, 1, 8, 0]
L1.sort() # no output
L1 # it sorts L1 , hence L1 is altered
L2 = [8,0,1,4]
sorted(L2) # gives sorted list output
L2 # unchanged L2
L1 = [ 6, 1, 8, 0]
L2 = [8,0,1,4]
L3 = [L1]
L3
L3.append(L2)
L3
L3.append(100)
L3 #notice how the list is changing and the square brackets
### Now suppose i make change to L1
L1.append(999)
L1 #L1 changes , no surpise there
L3 # 999 is added to L3 also , becasue L1 points to location which is inside L3
for i in map(abs,[-1,2,-3,4]): #takes function 'abs' and applies to each number in the list
print(i)
L1 = [65,0,187,24]
L2 = [76,99,27,1983]
for i in map(max, L1, L2): #gives max out of both the list
print(i)
A dictionary is an associative array (also known as hashes). Any key of the dictionary is associated (or mapped) to a value. The values of a dictionary can be any Python data type. So dictionaries are unordered key-value-pairs.¶
dict1 = {}
type(dict1)
dict2 = {'1': 'a', '2':'b', '3':'c'} # key value pair
dict2
dict2['1']
dict2['4'] = 'd' #add elements to dict2
dict2
'1' in dict2
'5' in dict2
'c' in dict2 #checks ony for key not value
del(dict2['2']) #delet entry from dict2
dict2
dict2.keys()
dict2.values()
d = {4:{1:0},(1,3):"number", "const" :[1,3.5,"list"]} # dict with lot of other things list tuple list strings
d
animals = { 'a': ['aardvark'], 'b': ['baboon'], 'c': ['coati']}
animals['d'] = ['donkey']
animals['d'].append('dog')
animals['d'].append('dingo')
def how_many(aDict):
'''
aDict: A dictionary, where all the values are lists.
returns: int, how many values are in the dictionary.
'''
n=0
values = aDict.values()
for i in values:
n += len(i)
return(n)
print(how_many(animals))
animals
Reference