Tuples
In Python tuples are written with round brackets as a collection of ordered and immutable comma-separated objects.
#tuples
Tpl0 = ()
Tpl1 = ('I', 'love', 'pancakes')
Tpl2 = (1,2,3,8,5)
Tpl3 = 1,"Cats", 4, "Dogs"
TplAll = Tpl2,Tpl1,Tpl3
print(Tpl0, type(Tpl0))
print(Tpl1, Tpl2)
print(Tpl3)
First tuple is empty. Might be strange but this option will be use, f.e. if we want to import data from file into tuple expression. Tuples might be string, numeric or mixed. Might be added, indexed, converted. A tuple is a collection which is ordered and unchangeable.
#operations
print(Tpl1 + Tpl2)
print(Tpl2[3:])
print(Tpl1[:2])
print(list(Tpl1), list(Tpl3))
print(TplAll)
print(sorted(Tpl3))
print(len(Tpl3))
print(max(Tpl2), min(Tpl2))
print(TplAll.count(1)) #output : 2
print(Tpl2.index(8) #output: 4
print("sum values of Tpl2: ", sum(Tpl2))
#to add values to exisitng tuples
tpl = ("apple", "banana", "cherry")
y = ("orange", "raspberry",)
tpl1 +=y
print(tpl1)
#to update values
y = list(Tpl3)
y[2] = "and"
x = tuple(y) #output: 1,"Cats", and, "Dogs"
print(x)

We can also delete tuples:
del Tpl1
print(Tpl1 + Tpl2)
[Un]Pack
# tuple (un)packing
Tpl4 = 5, 6, 7
print(Tpl4)
x1, x2, x3 = Tpl4
print("Val1 = " + str(x1))
print("Val2 = " + repr(x2))
print("Val3 = " + str(x3))
fruits = ("apple", "banana", "cherry")
(green, yellow, red) = fruits
print(green)
print(yellow)
print(red)
fruits = ("apple", "banana", "cherry", "strawberry", "raspberry")
(green, yellow, *red) = fruits
print(green)
print(yellow)
print(red)
fruits = ("apple", "mango", "papaya", "pineapple", "cherry")
(green, *tropic, red) = fruits
print(green)
print(tropic)
print(red)
Loops:
#Loop
Tpl5 = ("one", "two", "three")
for x in Tpl5:
print(x)
LISTS
A list in Python is a collection that can be compared to arrays in other programming languages. An important feature of lists is that they can store different types of data.
#lists
tpl = 1,2,9,7,8
lst = list(tpl)
print(tpl)
print(lst)
print(type(tpl))
print(type(lst))
Lst1 = []
Lst2 = list((1,2,3))
Lst3 = [1,2,0,8,5,3]
Lst4 = ["abc", 5, "Jimmy", 4.8]
print(Lst1)
print(Lst2)
print(Lst3)
print(Lst4)
We can make similar operations on lists like on tuples
#operations
Lst5 = Lst1+Lst2
Lst6 = [Lst3,Lst4]
Lst4.extend(Lst2)
Lst1.insert(1,1)
Lst1.clear
X = [9, 4,]
Lst2 +=X
print(Lst2)
Tpl1 = (33,44,55)
Lst1.extend(Tpl1)
print(Lst1)
We can add, change or remove data inside
Lst7 = [6, 9, 4, 2, 0]
Lst7.clear()
print(Lst7)
del Lst7
Lst7 = [6, 9, 4, 2, 0]
Lst7_sorted = Lst7.sort()
print(Lst7)
print(Lst7_sorted)
print("sum values of Lst3: ", sum(Lst3))
Lst3.append(11)
Lst3.append(99) #cannot add more than one elem with append function
print(Lst3)
Lst3[3] = 12
print(Lst3)
Lst3.pop(3)
print(Lst3)

2. to sort the list we need to declare another var.
3. Changes in list
#removing duplicates
L1 = [1,3,7,4,3]
#check for duplicates
if len(L1) != len(set(L1)):
print("duplicates exist")
else:
print("no duplicates")
#method1
unique = []
for x in L1:
if x not in unique:
unique.append(x)
print(unique)
unique.sort()
print(unique)
#method2
seen = set()
unique = []
for x in L1:
if x not in seen:
seen.add(x)
unique.append(x)
print(unique)
Loops:
#loops
List1 = ["white", "blue", "green"]
for x in List1:
print(x)
List2 = [33,89,45,0]
for y in List1:
print(y)
#join
L1 = [1,2,3]
L2 = [4,5,6]
print(L1,L2)
print(L1+L2)
for x in L2:
L1.append(x)
print(L1)
L2.extend(L1)
print(L2)
Nested lists
#nested list
numbers = [[0,1,2],[3,4,5],[6,7,8]]
print(numbers)
#with loop
for list in numbers:
print(list)
''' output:
[0, 1, 2]
[3, 4, 5]
[6, 7, 8] '''
for list in numbers:
print(numbers)
'''output:
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
[[0, 1, 2], [3, 4, 5], [6, 7, 8]] '''
# with rows and columns
print(" A B C")
counter = 0
for line in numbers:
print(counter, line)
#counter = counter + 1 #method1
counter += 1
''' output:
A B C
0 [0, 1, 2]
1 [3, 4, 5]
2 [6, 7, 8] '''
#with index
numbers[0][1] = 9
numbers[1][1] = 9
numbers[2][1] = 9
print(" A B C")
for counter, list in enumerate(numbers):
print(counter, list)
''' output:
0 [0, 9, 2]
1 [3, 9, 5]
2 [6, 9, 8] '''
#as def
def numbers_board(your_number = 0, row = 0, column = 0):
print(" A B C")
numbers[row][column] = your_number
for count, row in enumerate(numbers):
print(count,row)
numbers_board(your_number=9, row=2, column=1)

#comrehension
S = [[i for i in range(11)] for i in range(3)]
print(S)
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]