You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
max() -> returns the greatest element of the list. How the greatest element is determined depends on what type objects are in the list. The max function is undefined for lists that contain elements from different, incomparable types.
min() -> returns the smallest element in a list.
sorted() -> returns a copy of a list in order from smallest to largest, leaving the list unchanged.
join() -> takes a list of strings as an argument, and returns a string consisting of the list elements joined by a separator string.
append() -> adds an element to the end of a list.
Tuples
Tuples (immutable and ordered)
location= (13.4125, 103.866667)
>>>print("Latitude:", location[0])
>>>print("Longitude:", location[1])
dimensions=52, 40, 100length, width, height=dimensions#tuple unpacking>>>print("The dimensions are {} x {} x {}".format(length, width, height))
empty_tuple= ()
empty_tuple=tuple()
Union, intersection, and difference are easy to perform with sets
Dictionaries and Identity Operators
Dictionaries ((im)mutable, unordered, and mappings of unique keys)
elements= {"hydrogen": 1, "helium": 2, "carbon": 6}
>>>print(elements["helium"])
2elements["lithium"] =3#insert "lithium" with a value of 3 into the dictionary>>>print("carbon"inelements)
True>>>print(elements.get("dilithium"))
None#can crashn=elements.get("dilithium")
>>>print(nisNone)
True>>>print(nisnotNone)
False>>>print(elements.get('kryptonite', 'There\'s no such element!'))
"There's no such element!"empty_dict= {}
empty_dict=dict()
Any immutable object (such as an integer, boolean, string, tuple) is hashable (can be used by dictionaries to track unique keys and sets to track unique values)
Compound Data Structures
elements= {"hydrogen": {"number": 1,
"weight": 1.00794,
"symbol": "H"},
"helium": {"number": 2,
"weight": 4.002602,
"symbol": "He"}}
helium=elements["helium"] # get the helium dictionaryhydrogen_weight=elements["hydrogen"]["weight"] # get hydrogen's weightoxygen= {"number":8,"weight":15.999,"symbol":"O"} # create a new oxygen dictionary elements["oxygen"] =oxygen# assign 'oxygen' as a key to the elements dictionary>>>print('elements = ', elements)
elements= {"hydrogen": {"number": 1,
"weight": 1.00794,
"symbol": 'H'},
"helium": {"number": 2,
"weight": 4.002602,
"symbol": "He"},
"oxygen": {"number": 8,
"weight": 15.999,
"symbol": "O"}}