###### tags: Python # Fundamental Data Types int = Interger print(bin(5)) print(int('0b101', 2)) float = Floating Point Number complex = rarely use bool list tuple set dict str * general_string = 'hi hello there 24!' * long_string = ''' ''' ## String Concatenation 'hello' + ' Tihe' ## Classes -> custom types ## Specialized Data Types ## Math Function ## Variables snake_case(using _ not space) case sensitive keywords(highlited in blue) can't be used as variables ## Constants `PI = 3.14` not supposed to change or losing the meaning of constants `__` prevent to use double underscores to name a variable ## Expression & Statement iq = 100 100 is an expression iq = 100 is a statement ## Augmented Assignment Operator ``` some_value = 5 some_value = some_value + 2 some_value += 2(neater) ``` ## Type Conversion ``` a = str(100) b = int(100) c = type(b) print(c) ``` ## Escape Sequence `weather = '\tIt\'s \"kind of\" sunny\nhope you enjoy your day!'` \t add a Tab \n add a Line ## Formatted strings - python3 ``` name = 'Johnny' age = 55 print(f'hi {name}. You are {age} years old') ``` - python2 `print('hi {}. You are {} years old'.format(name, age))` ## String indexs ``` selfish = '01234567' print(selfish[start:stop:stepover]) print(selfish[0]) print(selfish[0:2]) print(selfish[1:]) print(selfish[:5]) print(selfish[::1]) print(selfish[-2]) print(selfish[::-1]) selfish[0] = 'z' - immutable ``` ## List Slicing ``` amazon_cart = [ 'notebooks', 'sunglasses', 'toys', 'grapes' ] amazon_cart[0] = 'laptop' - mutable print(amazon_cart[0::2]) new_cart = amazon_cart (dynamic change) new_cart = amazon_cart[:] (copy the value) ``` ## Matrix - machine learning, image processing matrix = [ [1,2,3], [2,4,6], [7,8,9] ] ## List Method - adding basket = [1,2,3,4,5] new_list = basket.append(100) - only put 100 to the end in memory of basket not creat a new memory append(index) insert() extend() - remove pop(index) remove() clean() ## List Unpacking a,b,c, *other, d=[1,2,3,4,5,6,7,8,9] ## Dictionary (key-value) -not in order ``` dictionary = { 'a': [1,2,3], 'b': 'hello', 'x': True } print(dictionary.get('age', 55)) -if age is none creat 'age': 55 user = dict(name='Jojo') ``` ## Tuple -immutable my_tuple = (1,2,3,4,5) user = { (coordinate):[1,2,3] -(1)is tupple } ## Set-not in order ``` my_set = {1,2,3,4,5,5} print(my_set) result:1,2,3,4,5 print(my_set[0]) result:Error ``` ## Data Structures