Tuples and Sets 30/03

Run Settings
LanguagePython
Language Version
Run Command
# Tuples # Similar to lists, but cannot be modified - IMMUTABLE #my_tuple = (1,2,3,4,5) # cannot be sorted or reversed like a list #indexes are still usable for finding and printing etc, changing the values is the only key difference # Tuples can be used over lists to make it simpler and more efficient, for example it points out something cannot be changed #or shouldn't be modified. They are also slightly faster than lists. # a user's destination in a cab ride could be a tuple, and a new one generated each time # but the driver's location may need to be a list as it is constantly changing and updating #print(user.items()) - this will print values from a dictionary as a tuple #new_tuple = my_tuple[1:2] #print(new_tuple) # may print with a comma at end, e.g. (2,) #x,y,z, *other = (1,2,3,4,5) # this assigns each variable to the value, e.g. x=1, y=2 etc #print(x,y,other) #tuple.count(5) - will count how many times that value appears in tuple #tuple.index(5) - will return the value at index of 5 #print(len(my_tuple)) - will print the length of the tuple # Sets # my_set = {1,2,3,4,5} # similar to dictionary in format but no key:value pairs # my_set.add(100) # This will be added, as it is unique # my_set.add(2) # this will not appear in the print, as there is already a 2 # print(my_set) # will only return unique items, so if there are duplicates you will only see 1 instance # my_list = [1,2,3,4,5,5] # print(set(my_list)) # set() will remove all duplicates from the previously created list, here it removes the 5 # my_set = {1,2,3,4,5,5} # new_set = my_set.copy() # #print(1 in my_set) # cannot use standard index checking, have to use in, or len to find length # print(list(my_set)) # print(new_set)
Editor Settings
Theme
Key bindings
Full width
Lines