import sys import os def blockPrint(): sys.stdout = open(os.devnull, 'w') def enablePrint(): sys.stdout = sys.__stdout__ blockPrint() # ok so instead of using len - 1, we can just use -1, which is len - 1, if len = 4, then this is 4 - 1 = 3. # alright cool. list = [1,23,4] print(list[-2]) # and i and j is inclusive and exclusive a = '1234' print(a[1:2]) # omitting and :j is start to j, inclusive to exclusive print(a[0:2]) print(a[:2]) # omitting and i: is inclusive to at the end, including all print(a[0:]) # omitting all and just [:] gives a copy returned. print(a[:]) # ok so when doing multiple complehension u must have in correct order from left to right, likst like u normally do list_of_lists = [['4', '8'], ['4', '2', '28'], ['1', '12'], ['3', '6', '2']] print([int(i) for sublist in list_of_lists for i in sublist]) message = 'David is cool' print(message[0:10:2]) print([x for x in range(0, 10, 2)]) # [0, 2, 4, 6, 8] print([x for x in range(10, 0, -1)]) # reverse! # [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] # if index with 3, then start(inclusive), end(exclusive), step added to start print(message[:4:-1]) # start to index 4, 0 -1, 0 -2, which is -1, -2, index max, max - 1, stop count from beginning # 'looc si ', max len 13, this is 8. so 13 - 4 = 8 print(message[::-1]) # this reverse the string! # f'string' with {evaluate math, etc. expression} print(f'hello world') # 'OMG' we can set up the default value of function parameter # def function(order_matter_variable_without_default_go_first!, set_as_default = 1) # if there is specfic value, then no longer default value! # DO NOT DO NOT DO NOT EVER AND EVER! SIMPLY MUTATE! # def add_num(num: int, numbers: list[int] = []) -> list[int]: # """Append num to the given numbers, and return the list. # # If no numbers are given, return a new list containing just num. # # >>> my_numbers = [1, 2, 3] # >>> add_num(10, my_numbers) # [1, 2, 3, 10] # >>> add_num(10) # [10] # """ # numbers.append(num) # return numbers # >>> add_num(10) # [10] # Looks okay... # >>> add_num(20) # [10, 20] # Wait, what? Should be [20]... # >>> add_num(30) # [10, 20, 30] # ??? Should be [30]... # >>> add_num(40) # [10, 20, 30, 40] # I see the pattern, but why?! # alternate method # from typing import Optional # def add_num(num: int, numbers: Optional[list[int]] = None) -> list[int]: # """Append num to the given numbers, and return the list. # # If no numbers are given, return a new list containing just num. # # >>> my_numbers = [1, 2, 3] # >>> add_num(10, my_numbers) # [1, 2, 3, 10] # >>> add_num(10) # [10] # """ # if numbers is None: # return [num] # else: # numbers.append(num) # return numbers