Professional Documents
Culture Documents
Python Programming 2: Mohamad Sadra Salman Noori
Python Programming 2: Mohamad Sadra Salman Noori
• Debug
– Trace automatically
– Breakpoints
• Code Complexity
– Efficient CPU and RAM usage
James Tam
Mutable / Immutable
• Immutable object can’t be changed after it is created
– int, float, bool, string and tuple
• S = “sadra”
• S[-1] = “i”
• Error!
• L = [6,0]
• L[0] = 4
• No Error
James Tam
Tuple
Ordered, unchangable collection
myTuple = (“1”, 2, 3)
x = ("apple", "banana", "cherry")
y = list(x)
Indexing and looping and checking existence of y[1] = "kiwi"
x = tuple(y)
item is just like list
print(len(x))
Z=(1,)
Add, Remove and change items are not allowed
T3 = T1 + T2
James Tam
Set
x = ("apple", "banana", "cherry")
Unordered, unindexed and unchangeable collection y = list(x)
y[1] = "kiwi"
x = tuple(y)
mySet = {“1”, 2, 3}
print(len(x))
Z=(1,)
looping and checking existence of item is just like list T3 = T1 + T2
mySet = {1,2}
Once a set is created, you cannot change its items, mySet.add(“’3’”)
but you can add new items. mySet.update([“apple”,4])
mySet.remove(3)
mySet.discard(3)
Clear, del, union and etc mySet.pop()
print(len(mySet))
James Tam
Debug
• Debugging is tracing code automatically
– Interactive code
– Complex code
– Error debugging
• Breakpoints
• Watch
James Tam
Homework
• Write a Python program to sort a tuple by its float element
• Write a Python program that calculate requested items with 2 given sets
– Union
– Intersection
– Disjunctive Union
– Check if is subset
• Write a Python program that calculate red parts of three given sets
James Tam