DataStructure

Set of Data Structures in Python

Set Definition One of the data structure in python. Create set of data Data Structures in Python List: Tuple: ( ) Dictionary: { key : value } set: { } declaration use { and } to declare setA={1,3,4,"test"} # {1, 'test', 3, 4} setB={2,4,"test",5,6} # {2, 'test', 4, 5, 6} Characteristics No duplicated value Do not care about order set algebra(union, intersection, difference, symmetric difference) Set Algebra union Keyword: “|”, “union()”

Dictionary of Data Structures in Python

Dictionary Definition One of the data structure in python. has key and value bined. You can fine value when you know the key very slimier with hashmap Data Structures in Python List Tuple Dictionary set declaration use { and } to declare the value can be list the key can be int or string. But not list. (TypeError: unhashable type: ‘list’) dictionary={"key1":"value1","key2":"value2","key3":"value3"} how to use indexing like List get() method difference between the indexing and get method If key does not exist, the indexing will return error.

Tuple of Data Structures in Python

Tuple Definition One of the data structure in python. Almost same as List But Tuple is not editable. Object. Data Structures in Python List Tuple Dictionary set declaration use “(” and “)” to declear. But, if there is a element in Tuple, you need to add comma tuple1=(1,) tuple2=(1,2,3) notTuple=(0) #wrong, this is int type. comma needed index structure same as List characteristic Tuple is not editable.

List of Data Structures in Python

List Definition One of the data structure in python. Almost same as array in other languages. Object. Data Structures in Python List Tuple Dictionary set declaration temp=[] temp=list() temp=[5,9,44] index structure characteristic List in List is possible. This is same as 2D array in other languages String in python is same as List slicing [starting index : ending index-1 : condition index] [0:8:3] = select index 0 to 7 by adding 3 [:8:] = select index 0 to 7 by adding 1 [7::] = select index 7 to -1 by adding 1 [::-1] = select index 0 to -1 by adding -1 a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(a[0:8:3]) # [0, 3, 6] print(a[:8:]) # [0, 1, 2, 3, 4, 5, 6, 7] print(a[7::]) # [7, 8, 9] print(a[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] Arithmetic Operation a=[1,2,3] b=["apple","house","computer"] print(a+b) # [1, 2, 3, 'apple', 'house', 'computer'] # print(a+3) # error # print(a-b) # error # print(a-3) # error # print(a*b) # error print(b*3) # ['apple', 'house', 'computer', 'apple', 'house', 'computer', 'apple', 'house', 'computer'] print(b*0) # [] # print(a/b) # error # print(a/3) # error add element append() = add a new element behind extend() = add new elements behind insert() = add new elements by index [] = add new elements by index condition a=[1,2,3,4,5,6,7,8,9] b=["apple","house","computer"] a.