What are the main data str u ct u res in P y thon ? P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON Kirill Smirno v Data Science Cons u ltant , Altran
Data Str u ct u re Data Str u ct u re - a speciali z ed format to organi z e and store data . Main Data Str u ct u res in P y thon : list t u ple set dictionar y PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
List list - an ordered m u table seq u ence of items ( e . g . n u mbers , strings etc . ) my_list = [1, 2, 3, 4, 5] print(my_list) [1, 2, 3, 4, 5] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
List : accessing items my_list = [1, 2, 3, 4, 5] print(my_list[1:4]) [2, 3, 4] print(my_list[2]) print(my_list[2:]) 3 [3, 4, 5] print(my_list[-1]) 5 PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
List : modif y ing items my_list = [1, 2, 3, 4, 5] my_list[2] = 30 print(my_list) [1, 2, 30, 4, 5] my_list[:2] = [10, 20] print(my_list) [10, 20, 30, 4, 5] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
List : methods my_list.append(60) my_list = [10, 20, 30, 40, 50] print(my_list) [10, 20, 30, 40, 50, 60] my_list.remove(60) print(my_list) [10, 20, 30, 40, 50] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
List : methods my_list = [10, 20, 30, 40, 50] my_list.pop() 50 print(my_list) [10, 20, 30, 40] my_list.count(40) 1 PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
T u ple t u ple - an ordered im m u table seq u ence of items ( e . g . n u mbers , strings etc . ) my_tuple = (1, 'apple', 2, 'banana') print(my_tuple) (1, 'apple', 2, 'banana') my_tuple = 1, 'apple', 2, 'banana' print(my_tuple) (1, 'apple', 2, 'banana') PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
T u ple : modif y ing v al u es Modif y ing items in a t u ple is not possible . my_tuple[0] = 10 TypeError PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Set set - an u n ordered collection w ith no d u plicate items ( e . g . n u mbers , strings etc . ) my_set = set([1, 2, 3, 4, 5]) print(my_set) {1, 2, 3, 4, 5} my_set = set([1, 1, 1, 2, 3, 4, 5, 5, 5]) print(my_set) {1, 2, 3, 4, 5} PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Set : methods my_set1 = set([1, 2, 3, 4, 5]) my_set1.union(my_set2) my_set2 = set([3, 4, 5, 6, 7]) {1, 2, 3, 4, 5, 6, 7} my_set1.add(6) print(my_set1) my_set1.intersection(my_set2) {1, 2, 3, 4, 5, 6} {3, 4, 5} my_set1.remove(6) my_set1.difference(my_set2) print(my_set1) {1, 2} {1, 2, 3, 4, 5} PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Dictionar y dictionar y - a collection of ke y-v al u e pairs w here ke y s are u niq u e and imm u table key → value fruits = {'apple': 10, 'orange': 6, 'banana': 9} print(fruits) {'apple': 10, 'banana': 9, 'orange': 6} fruits = dict([('apple', 1), ('orange', 6), ('banana', 9)]) print(fruits) {'apple': 10, 'banana': 9, 'orange': 6} PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Dictionar y: accessing v al u es Accessing a v al u e for a ke y: fruits = {'apple': 10, 'orange': 6, 'banana': 9} fruits['apple'] 10 fruits['grapefruit'] KeyError: 'grapefruit' PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Dictionar y: modif y ing v al u es fruits['apple'] = 20 print(fruits) {'apple': 20, 'orange': 6, 'banana': 9} fruits['grapefruit'] = 11 print(fruits) {'apple': 20, 'orange': 6, 'banana': 9, 'grapefruit': 11} PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Dictionar y: methods fruits = {'apple': 10, 'orange': 6, 'banana': 9} fruits.items() dict_items([('apple', 10), ('orange', 6), ('banana', 9)]) PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Dictionar y: methods fruits = {'apple': 10, 'orange': 6, 'banana': 9} list(fruits.items())) [('apple', 10), ('orange', 6), ('banana', 9)] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Dictionar y: methods fruits = {'apple': 10, 'orange': 6, 'banana': 9} fruits.keys() dict_keys(['apple', 'orange', 'banana']) fruits.values() dict_values([10, 6, 9]) PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Dictionar y: methods fruits = {'apple': 10, 'orange': 6, 'banana': 9} list(fruits.keys()) ['apple', 'orange', 'banana'] list(fruits.values()) [10, 6, 9] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Dictionar y: methods fruits = {'apple': 10, 'orange': 6, 'banana': 9} fruits.popitem('banana') 9 print(fruits) {'apple': 10, 'orange': 6} PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Operations on Lists , T u ples , Sets , and Dictionaries my_list = [1, 2, 3, 4, 5] my_set = set([1, 2, 3, 4]) len(my_list) len(my_set) 5 4 my_tuple = (1, 2, 3, 4, 5) my_dict = {'a': 1, 'b': 2, 'c': 3} len(my_tuple) len(my_dict) 5 3 PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Operations on Lists , T u ples , Sets , and Dictionaries my_list = [1, 2, 3, 4, 5] my_set = set([1, 2, 3, 4]) 2 in my_list 5 in my_set True False my_tuple = (1, 2, 3, 4, 5) my_dict = {'a': 1, 'b': 2, 'c': 3} 2 in my_tuple 'b' in my_dict True True PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Let ' s practice ! P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON
What are common w a y s to manip u late strings ? P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON Kirill Smirno v Data Science Cons u ltant , Altran
String Create strings : s = 'hello' print(s) hello s = "hello" print(s) hello PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
String str() constr u ctor : str(11.5) str("hello") '11.5' 'hello' str([1, 2, 3]) '[1, 2, 3]' PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
str () contr u ctor class NewClass: def __init__(self, num): self.num = num nc = NewClass(2) print(nc.num) 2 str(nc) '<__main__.NewClass instance at 0x105cdabd8>' PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
str () constr u ctor class NewClass: def __init__(self, num): self.num = num def __str__(self): return str(self.num) nc = NewClass(3) str(nc) '3' PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Accessing characters in a string s = "interview" s[1:4] 'nte' s[1] s[2:] 'n' 'terview' s[-2] s[:3] 'e' 'int' PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
The . inde x() method s = "interview" s.index('n') 1 s.index('i') 0 PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Strings are imm u table s[0] = 'a' TypeError .capitalize() .lower() .upper() .replace() Methods ret u rn a ne w string object PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Modif y ing methods 1 # String concatencation # Replace a substring s1 = "worm" s1 = 'a dog ate my food' s2 = s1 + "hole" s2 = s1.replace('dog', 'cat') print(s2) print(s2) wormhole a cat ate my food PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Modif y ing methods 2 # Upper case # Capitalization s3 = s2.upper() s5 = s4.capitalize() print(s3) print(s5) A CAT ATE MY FOOD A cat ate my food # Lower case s4 = s3.lower() print(s4) a cat ate my food PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Relation to lists Create a string from a list of strings : Breaking a string into a list of strings : l = ['I', 'like', 'to', 'study'] l = s.split(' ') s = ' '.join(l) print(l) print(s) ['I', 'like', 'to', 'study'] I like to study PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
String methods w ith DataFrames import pandas as pd d = {'name': ['john', 'amanda', 'rick'], 'age': [35, 29, 19]} D = pd.DataFrame(d) print(D) name age 0 john 35 1 amanda 29 2 rick 19 PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
String methods w ith DataFrames D['name'] = # we will modify this column PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
String methods w ith DataFrames D['name'] = D['name'] PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
String methods w ith DataFrames D['name'] = D['name'].str PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
String methods w ith DataFrames D['name'] = D['name'].str.capitalize() print(D) name age 0 John 35 1 Amanda 29 2 Rick 19 PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Let ' s practice ! P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON
Ho w to w rite reg u lar e x pressions in P y thon ? P R AC TIC IN G C OD IN G IN TE R VIE W QU E STION S IN P YTH ON Kirill Smirno v Data Science Cons u ltant , Altran
Definition Reg u lar e x pression - a seq u ence of special characters ( metacharacters ) de � ning a pa � ern to search in a te x t . cat " I ha v e a cat . M y cat likes to eat a lot . It also catches mice ." PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Definition Reg u lar e x pression - a seq u ence of special characters ( metacharacters ) de � ning a pa � ern to search in a te x t . cat " I ha v e a cat . M y cat likes to eat a lot . It also cat ches mice ." PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Comple x patterns E x ample : john . smith @ mailbo x. com is the e - mail of John . He o � en w rites to his boss at boss @ big - compan y. com . B u t the messages get for w arded to his secretar y at info @ big - compan y. com . PRACTICING CODING INTERVIEW QUESTIONS IN PYTHON
Recommend
More recommend