Extensions
List Slicing, Robust typecasting via Error Handling
Last updated
my_numbers = [5,-2,71,-438,9]
print(max(my_numbers)) # 71
print(min(my_numbers)) # -438# Example of sorting
# NOTE: don't mix strings and numbers if you want to sort!
my_list = [4, 7, -20, 32, 9.3]
my_list.sort() # This sorts the list
print(my_list) # [-20, 4, 7, 9.3, 32]# Example of split()
my_sentence = "I am too lazy to make sentences into lists"
my_list = my_sentence.split(" ") # Split up string by empty spaces
v
print(my_list)# Example of join()
# NOTE: all items in the list must be strings - unless you do something clever! ;)
my_list = ["I'm", "12", "now", "mum", "I", "can", "do", "what", "I", "want"]
my_sentence = " ".join(my_list) # Join items in list with an empty space
print(my_sentence)try:
# do stuff here which might throw an error
weird_function_does_bad_stuff()
except: # this code runs if an error was thrown above
print("Oops - I encountered an error!")