This post is to cover all the basic concepts in Python. It could be helpful to you if you are here to refresh your knowledge about Python.
This short post covers strings, if else, for loop, while loop, list, list methods, sorting, dictionaries, dictionary methods in Python.
The code below is explained in detail with comments over it. I recommend practicing it on your computer for better understanding.
In case it doesn’t load, please open this gist on Github.
This should be sufficient for you if you are someone who wants to start solving coding problems on site’s like LeetCode.
| # —– | |
| # IF ELSE statements | |
| def donuts(count): | |
| if count>=10: | |
| return 'Number of donuts: many' | |
| else: | |
| return 'Number of donuts: ' + str(count) | |
| # —– | |
| # Mix first two and last two chars of a string | |
| # eg: spring > spng | |
| def both_ends(s): | |
| n = len(s) | |
| if n>2: | |
| return s[0:2]+s[n-2:n] | |
| else: | |
| return '' | |
| # —– | |
| # Replace all occurrences of first char with * except first char | |
| # eg: babble > ba**le | |
| def fix_start(s): | |
| n = len(s) | |
| return s[0] + s[1:n].replace(s[0], "*") | |
| # —– | |
| # Swap first two chars of given strings | |
| # eg: mix, pod > pox, mid | |
| def mix_up(a, b): | |
| n1 = len(a) | |
| n2 = len(b) | |
| return b[0:2] + a[2:n1] + ' ' + a[0:2] + b[2:n2] | |
| # —– | |
| jobs = ['google', 'apple', 'microsoft'] | |
| print jobs[0] ## google | |
| print jobs[2] ## microsoft | |
| print len(jobs) ## 3 | |
| # An interesting thing about lists is that – | |
| # assigning a list to a new variable does not create a new list, | |
| # instead the new variable points to the same list in memory. | |
| # It’s interesting because, changing the first item in jobs | |
| # will also change the first item in companies. | |
| companies = jobs | |
| print companies # ['google', 'apple', 'microsoft'] | |
| jobs[0] = "doodle" | |
| print companies # ['doodle', 'apple', 'microsoft'] | |
| # —– | |
| # FOR AND IN | |
| evens = [0, 2, 4, 8] | |
| sum = 0 | |
| for num in evens: | |
| sum += num | |
| print sum ## 14 | |
| if 4 in evens: | |
| print "4 in list" | |
| for i in range(100): | |
| print i | |
| # —– | |
| # WHILE LOOP | |
| a = [0,1,2,3,4,5,6,7,8,9] | |
| ## Access every 2nd element in a list | |
| i = 0 | |
| while i < len(a): | |
| print a[i] | |
| i = i + 2 | |
| # prints 0 2 4 6 8 | |
| # —– | |
| # LIST methods | |
| names = ['batman', 'superman', 'spiderman'] | |
| print names.append('wonderwoman') #prints none, adds ww at the end | |
| print names.insert(0,'aquaman') #prints none, adds aquaman in the front | |
| print names #prints ['aquaman', 'batman', 'superman', 'spiderman', 'wonderwoman'] | |
| print names.remove('spiderman') #prints none, removes spiderman | |
| print names.pop() #removes last item wonderwoman and prints/returns it | |
| # —– | |
| # LIST sorting | |
| names = ['batman', 'atman', 'datman', 'catman'] | |
| print names.sort() #sorts but returns none | |
| print names #prints sorted list | |
| names = ['batman', 'atman', 'datman', 'catman'] | |
| print sorted(names) #returns sorted list | |
| print sorted(names, reverse=True) #returns reverse sorted list | |
| names = ['aaa', 'a', 'aaaabbbb', 'aaaaa'] | |
| print sorted(names, key=len) #sort using length of each item | |
| #prints ['a', 'aaa', 'aaaaa', 'aaaabbbb'] | |
| ## Say we have a list of strings we want to sort by the last letter of the string. | |
| strs = ['xc', 'zb', 'yd' ,'wa'] | |
| ## Write a little function that takes a string, and returns its last letter. | |
| ## This will be the key function (takes in 1 value, returns 1 value). | |
| def MyFn(s): | |
| return s[-1] | |
| ## Now pass key=MyFn to sorted() to sort by the last letter: | |
| print sorted(strs, key=MyFn) ## ['wa', 'zb', 'xc', 'yd'] | |
| # —– | |
| # Python Dictionary | |
| letters = { 'a':'apple', 'b':'banana', 'c':'cat'} #defining a dictionary | |
| print letters['a'] #prints apple | |
| letters ['d'] = 'dog' | |
| letters ['e'] = 'elephant' | |
| print letters ['d'] #prints dog | |
| print letters #prints the dictionary | |
| # {'a': 'apple', 'c': 'cat', 'b': 'banana', 'e': 'elephant', 'd': 'dog'} | |
| letters = { 'c':'cat', 'b':'ball', 'a':'apple'} #defining a dictionary | |
| #1 | |
| for key in letters: | |
| print key #prints c, b, a | |
| #2 | |
| for key in letters.keys(): | |
| print key #prints c, b, a | |
| #3 | |
| print letters.keys() #prints ['c','b','a'] | |
| #4 | |
| print letters.values() #prints ['cat','ball','apple'] | |
| #5 | |
| for key in sorted(letters.keys()): | |
| print key, letters[key] #prints a apple b ball c cat | |
| #6 | |
| print letters.items() # [('a', 'apple'), ('c', 'cat'), ('b', 'ball')] | |
| #7 | |
| for k, v in letters.items(): | |
| print k, '>', v | |
| #prints a > apple b > ball c > cat | |
| hash = {} | |
| hash['word'] = 'watermelons' | |
| hash['count'] = 500 | |
| s = 'I want %(count)d of those %(word)s' % hash # %d for int, %s for string | |
| print s # 'I want 500 of those watermelons |
I personally refer this gist to refresh my python knowledge whenever I start solving LeetCode problems. I hope you too will find this helpful. If you do, let me know by leaving a comment. Thanks.