|
| 1 | +my_variable = 'hello' |
| 2 | +my_list_variable = ['hello', 'hi', 'nice to meet you'] |
| 3 | +my_tuple_variable = ('hello', 'hi', 'nice to meet you') |
| 4 | +my_set_variable = {'hello', 'hi', 'nice to meet you'} |
| 5 | + |
| 6 | +print(my_list_variable) |
| 7 | +print(my_tuple_variable) |
| 8 | +print(my_set_variable) |
| 9 | + |
| 10 | +my_short_tuple_variable = ("hello",) |
| 11 | +another_short_tuple_variable = "hello", |
| 12 | + |
| 13 | +print(my_list_variable[0]) |
| 14 | +print(my_tuple_variable[0]) |
| 15 | +print(my_set_variable[0]) # This won't work, because there is no order. Which one is element 0? |
| 16 | + |
| 17 | +my_list_variable.append('another string') |
| 18 | +print(my_list_variable) |
| 19 | + |
| 20 | +my_tuple_variable.append('a string') # This won't work, because a tuple is not a list. |
| 21 | +my_tuple_variable = my_tuple_variable + ("a string",) |
| 22 | +print(my_tuple_variable) |
| 23 | +my_tuple_variable[0] = 'can I change this?' # No, you can't |
| 24 | + |
| 25 | +my_set_variable.add('hello') |
| 26 | +print(my_set_variable) |
| 27 | +my_set_variable.add('hello') |
| 28 | +print(my_set_variable) |
| 29 | + |
| 30 | + |
| 31 | +###### Set Operations |
| 32 | + |
| 33 | +set_one = {1, 2, 3, 4, 5} |
| 34 | +set_two = {1, 3, 5, 7, 9, 11} |
| 35 | + |
| 36 | +print(set_one.intersection(set_two)) # {1, 3, 5} |
| 37 | + |
| 38 | +print({1, 2}.union({2, 3})) # {1, 2, 3} |
| 39 | + |
| 40 | +print({1, 2, 3, 4}.difference({2, 4})) # {1, 3} |
0 commit comments