--- title: 0821 Other Data Structures (1) tags: Foundamental Python Tutorials description: View the slide with "Slide Mode". slideOptions: spotlight: enabled: true --- # 0821 Other Data Structures (1) Benson Chiu @ FDCS 8th X 7th <!-- Put the link to this slide here so people can follow --> slide: https://hackmd.io/@benson-elementary-cpp/Bya4UjjlY --- ## What are they? - tuple - set - dict --- ## Tuple - Similar to list, but it's **NOT IMMUTABLE** - covered by "( )" ```python= group = (1,2,3,4,5) group = (1,) ``` --- ## Set - Unordered - covered by "{}" - The elements in the set is NOT REPEATABLE ```python= myset = {1,2,3,4,5} ``` --- ## For methods about Tuple and Set Please visit https://www.w3schools.com/python/ --- ## Dictionary key -> value ```python= my_dict = {"chinese":100, "math":87, "english":90} print(my_dict["chinese"]) print(my_dict["math"]) print(my_dict["english"]) print(my_dict["history"]) ``` **What will happen?** --- ## Dict: avoiding key errors ```python= if "history" in my_dict: print(my_dict["history"]) ``` --- ## Dict: additon and revision ```python= my_dict = {"chinese":100, "math":87, "english":90} my_dict["history"] = 100 #add my_dict["chinese"] = 98 #revise ``` --- ## Dict: traversal (1) by key ```python= for k in my_dict.keys(): print(k) ``` --- ## Dict: traversal (2) by value ```python= for v in my_dict.values(): print(v) ``` --- ## Dict: traversal (2) by both ```python= for k,v in my_dict.items(): print(k,v) ``` --- ## Dict: delete ```python= my_dict.pop("chinese") #remove one element by key my_dict.clear() #remove all ``` --- For other methods about dictionary, please visit https://www.w3schools.com/python/python_ref_dictionary.asp