Module 0.5.0 - Dictionaries, Tuples, Sets

  • more data structures

0.5.1 - Dictionaries

  • an array type that can list multiple keys [items] and assign each key a value [definition]

  • dictionaries have no order

Initialization

  • created using curly braces "{}"

  • keys in between the curly braces are seperated by a comma ","

  • each key is assigned a value by using a colon ":"

my_dict = {"name":"cosmo", "age":100, "occupation":"hacker"}

dictionaries can be indexed

print(my_dict["name"])
>>> "cosmo"

values can be added or changed through the assignmetn operator

nums = {"one":1, "two":2, "three":5}
nums["four"] = 4
nums["three"] = 3
print(nums)
>>> {"one":1, "two":2, "three":3, "four":4}

"in" and "not" boolean operators in work with dictionary keys

"get" method, returns the key's value, can return specified value if key is not found

  • if no return value is specified, it will return "None"

0.5.2 - Tuples

  • lists that can not be edited, immutable

  • iterate faster than lists

  • created using parenthesis "()" or just seperating values with commas ","

    • values are seperated by commas "."

tuples can be indexed

list slicing can also be done with tuples

0.5.3 - Sets

  • assign multiple values to one variable

  • sets are not ordered, they can not be indexed

  • they can not contain duplicate items

  • can use most of the list methods

  • created using curly braces "{}" or the "set" function, takes list as argument

  • each item is seperated by a comma ","

sets can use "in" and "not" boolean operators

"add" method, add item to end of set

  • this replaces "append" method

0.5.4 - Set Operators

sets have unuiqe operators for comining with other sets

"union" operator, combines two sets into one [with no duplicate items]

  • created using the pipe "|"

"intersection" operator, combines two sets into one [with items only in both sets]

  • created using the ampersand "&"

"difference" operator, combines two sets into one [with items in the first, but not in the second]

  • created using the minus "-"

"symmetric difference" operator, combines two sets into one [with items in either set]

  • created using carrot "^"

0.5.5 - Iterable Unpacking

assign each item in an iterable [lists, dictionaries, tuples, sets] to a variable

allows for easy value swapping for variables

if there is an asterisk * placed before the variable, it will assign itself to the remaining values