Skip to content

Latest commit

 

History

History
78 lines (58 loc) · 1 KB

File metadata and controls

78 lines (58 loc) · 1 KB

What is the difference between list, tuple, set, dict?

"""
 list:
  - mutuable
  - []
  - indexed
  - ordered

 tuple:
  - immutuable
  - ()
  - indexed
  - ordered

 set:
  - mutuable
  - {}
  - unordered
  - unique

 dictionary:
  - key-value pair
  - {}
  - unordered
  - unique keys
  - mutuable
  - key should be immutable type e.g. String, number, tuples.
"""

How do you slice a list to reverse it? To get every second item?

myList = [1,2,3,4,5,6,7,8,9,0];

new_reversed = myList[::-1][::2];

print(new_reversed);

What does this do: a, b = b, a?

a = 10;
b = 15;

print("a = ", a, " b = ", b);

#SWAP
a,b = b,a;
print("a = ", a, " b = ", b);

What's the difference between is vs ==?

a = [1,2,3];
b = a;

c = [1,2,3];

print(a==b); # True
print(a==c); # True

print(a is b); # True
print(a is c); # False # Cause: 'a' & 'b' are refering to different memory location

"""
 == :
  - this checks content
 is :
  - this checks memory reference
"""