-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataStructuresDemo.py
More file actions
47 lines (40 loc) · 2.12 KB
/
dataStructuresDemo.py
File metadata and controls
47 lines (40 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Basic Python data structures:
# 1. Lists
# 2. Tuples
# 3. Sets
# 4. Dictionaries
# These data structures are classes defined in python and
# we use them by creating objects of those classes.
def main() -> None:
# For example, myList below is an object of class "List"
# -- Lists --
# A List contains a series of objects of the SAME type separated by commas.
myList = [1, 2, 3] # We initialize a List object with brackets []
myList[1] = 5 # A List object is MUTABLE, which means that we can change
# the values of its elements at any time.
# -- Tuples --
# A Tuple contains a series of objects of DIFFERENT type,separated by commas.
myTuple = (2, "hello", myList) # We initialize a Tuple object with parentheses ()
# A Tuple object is IMMUTABLE, which means that
# we cannot change its elements after initialization
print(myTuple[2])
#myTuple[0] = 1 -> ERROR: 'tuple' object does not support assignment
# -- Sets --
# A Set contains a collection of NO DUPLICATE elements of the SAME type, separated by commas.
mySet = {"orange", "apple", "banana"} # We initialize a Set object with curly brackets {}
# A Set object is IMMUTABLE, which means that
# we cannot change its elements after initialization
print(mySet)
#mySet[0]="pineapple" -> ERROR: 'set' object does not support assignment
setDemo = {1, 1, 2, 2, 3, 3}
print(setDemo)
# --Dictionaries --
# A Dictionary is a set of KEY:VALUE pairs.
# The KEY and the corresponding OBJECT can be of any type.
myDict = {"costas": 4056, "katerina": 5092} # We initialize a Dictionary object with curly
# brackets. Being essentially a Set of KEY:VALUE pairs
# a Dictionary is also IMMUTABLE.
print(myDict) # Prints all key:value pairs
print(myDict["costas"]) # We access the element of a Dictionary through its key
if __name__ == "__main__":
main()