Skip to content

Latest commit

History

21 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Python Cheat Sheet

A Cheat Sheet 📜 to revise Python syntax in less time. Particularly useful for solving Data Structure and Algorithmic problems or a quick overview before an interview.

Click here for similar Java Resource (not made by me)
Get a PDF of this sheet at the end.
Leave a ⭐ if you like the cheat sheet (contributions welcome!)

Basics

  • Data Types

    Untitled

  • Operators and it’s precedence

    Untitled

Data Structures

Important data structures for LeetCode

Lists

Lists are used to store multiple items in a single variable

  • Operations Time Complexities

    Untitled

nums= [1,2,3]
nums.index(1) # returns indexnums.append(1) # appends 1nums.insert(0,10) # inserts 10 at 0th indexnums.remove(3) # removes all instances of 3nums.copy(1) # returns copy of the listnums.count(1) # returns no.of times '1' is present in the listnums.extend(someOtherList) # ...nums.pop() # pops last element [which element to pop can also be given as optional argument]nums.reverse() # reverses original list (nums in this case)nums.sort() # sorts list [does NOT return sorted list]#Python's default sort uses Tim Sort, which is a combination of both merge sort and insertion sort.

List or String slicing in Python

It'sprettysimplereally:
a[start:stop] # items start through stop-1a[start:] # items start through the rest of the arraya[:stop] # items from the beginning through stop-1a[:] # a copy of the whole arrayThereisalsothestepvalue, whichcanbeusedwithanyoftheabove:
a[start:stop:step] # start through not past stop, by stepThekeypointtorememberisthatthe :stopvaluerepresentsthefirstvaluethatisnotintheselectedslice. So, thedifferencebetweenstopandstartisthenumberofelementsselected (ifstepis1, thedefault).
Theotherfeatureisthatstartorstopmaybeanegativenumber, whichmeansitcountsfromtheendofthearrayinsteadofthebeginning. So:
a[-1] # last item in the arraya[-2:] # last two items in the arraya[:-2] # everything except the last two itemsSimilarly, stepmaybeanegativenumber:
a[::-1] # all items in the array, reverseda[1::-1] # the first two items, reverseda[:-3:-1] # the last two items, reverseda[-3::-1] # everything except the last two items, reversedPythoniskindtotheprogrammeriftherearefeweritemsthanyouaskfor. Forexample, ifyouaskfora[:-2] andaonlycontainsoneelement, yougetanemptylistinsteadofanerror. Sometimesyouwouldprefertheerror, soyouhavetobeawarethatthismayhappen.
Relationtoslice() objectTheslicingoperator [] isactuallybeingusedintheabovecodewithaslice()
objectusingthe : notation (whichisonlyvalidwithin []), i.e.:
a[start:stop:step]
isequivalentto:
a[slice(start, stop, step)]
Sliceobjectsalsobehaveslightlydifferentlydependingonthenumberofarguments, similarlytorange(), i.e. bothslice(stop) andslice(start, stop[,
step]) aresupported. Toskipspecifyingagivenargument, onemightuseNone,
sothate.g. a[start:] isequivalenttoa[slice(start, None)] ora[::-1] isequivalenttoa[slice(None, None, -1)].
Whilethe :-basednotationisveryhelpfulforsimpleslicing, theexplicituseofslice() objectssimplifiestheprogrammaticgenerationofslicing.

Dictionary

Dictionaries are used to store data values in key:value pairs. Info about collections.Counter() available below.

  • Operations Time Complexities

    Untitled

dict= {'a':1,'b':2,'c':3}
dict.keys() # returns list of keys of dictionarydict.values() # returns list of values of dictionarydict.get('a') # returns value for any corresponding keydict.items() # returns [('a',1),('b',2),('c',3)]dict.copy() # returns copy of the dictionary# NOTE : items() Returns view object that will be updated with any future# changes to dictdict.pop(KEY) # pops key-value pair with that keydict.popitem() # removes most recent pair addeddict.setDefault(KEY,DEFAULT_VALUE)
# returns value of key, if key exists, else default value returned# If the key exist, this parameter(DEFAULT_VALUE) has no effect.# If the key does not exist, DEFAULT_VALUE becomes the key's value. 2nd# argument's default is None.dict.update({KEY:VALUE})
# inserts pair in dictionary if not present, if present, corresponding value is# overriden (not key)# defaultdict ensures that if any element is accessed that is not present in# the dictionary# it will be created and error will not be thrown (which happens in normal dictionary)# Also, the new element created will be of argument type, for example in the below line# an element of type 'list' will be made for a Key that does not existmyDictionary=defaultdict(list) 

Counter

Python Counter is a container that will hold the count of each of the elements present in the container. The counter is a sub-class available inside the dictionary class. Specifically used for element frequencies

Pretty similar to dictionary, in fact I usedefaultdict(int)most of the time

fromcollectionsimportCounter#(capital 'C')# can also be used as 'collections.Counter()' in codelist1= ['x','y','z','x','x','x','y', 'z']
# InitializationCounter(list1) # => Counter({'x': 4, 'y': 2, 'z': 2})Counter("Welcome to Guru99 Tutorials!") # => Counter({'o': 3, ' ': 3, 'u': 3, 'e': 2.....})# UpdatingcounterObject=collections.Counter(list1)
counterObject.keys() = [ 'x' , 'y' , 'z' ]
most_common_element=counterObject.most_common(1) # [('x', 4)]counterObject.update("some string") # => Counter({'o': 3, 'u': 3, 'e': 2, 's': 2})counterObject['s'] +=1# Increase/Decrease frequency# Accessingfrequency_of_s=counterObject['s']
# DeletingdelcouterObject['s']

Deque

A double-ended queue, or deque, has the feature of adding and removing elements from either end.

  • Operations Time Complexities

    Untitled

#in BFS(Breadth-first search) or other algorithms where we have to pop or add elements to the begining , deque is the best option #we can also use list, but list.pop(0) is O(n) operation where as dequeu.popleft() is O(1)fromcollectionsimportdequequeue=deque(['name','age','DOB'])
queue.append("append_from_right") # Append from rightqueue.pop() # Pop from rightqueue.appendleft("fromLeft") # Append from leftqueue.popleft() # Pop from leftqueue.index(element,begin_index,end_index) # Returns first index of element b/w the 2 indices.queue.insert(index,element)
queue.remove() # removes first occurrancequeue.count() # obviousqueue.reverse() # reverses order of queue elements

Heapq

As we know the Heap Data Structure is used to implement the Priority Queue ADT. In python we can directly access a Priority Queue implemented using a Heap by using the Heapq library/module.

  • Operations Time Complexities

    Untitled

importheapq# (minHeap by Default)nums= [5, 7, 9, 1, 3]
heapq.heapify(nums) # converts list into heap. Can be converted back to list by list(nums).heapq.heappush(nums,element) # Push an element into the heapheapq.heappop(nums) # Pop an element from the heap# heappush(heap, ele) :- This function is used to insert the element mentioned# in its arguments into heap. The order is adjusted, so as heap structure is# maintained.# heappop(heap) :- This function is used to remove and return the smallest# element from heap. The order is adjusted, so as heap structure is maintained.# Other Methods Available in the Library# Used to return the k largest elements from the iterable specified # The key is a function with that accepts single element from iterable,# and the returned value from that function is then used to rank that element in the heapheapq.nlargest(k, iterable, key=fun)
heapq.nsmallest(k, iterable, key=fun)
#Max heap in python #By default heapq in python is min heap, #if we want to use max heap we can simply invert the value of the keys and use heapq. #For example, turn 1000.0 into -1000.0 and 5.0 into -5.0.#The easiest and ideal solution#Multiply the values by -1#All the highest numbers are now the lowest and vice versa.#Just remember that when you pop an element to multiply it with -1 in order to get the original value again.#Example: importheapqheap= []
heapq.heappush(heap, 1*(-1))
heapq.heappush(heap, 10*(-1))
heapq.heappush(heap, 20*(-1))
print(heap)
Theoutputwilllooklike:
[-20, -1, -10]
#when popping element multiply it with -1max_element=-heapq.heappop(heap)
print(max_element)
Outputwillbe:
20

Sets

A set is a collection which is unordered, immutable, unindexed, No Duplicates.

  • Operations Time Complexities

    Untitled

set= {1,2,3}
set.add(item)
set.remove(item)
set.discard(item) |set.remove(item)
# removes item | remove will throw error if item is not there, discard will notset.pop() # removes random item (since unordered)set.isdisjoint(anotherSet) # returns true if no common elementsset.issubset(anotherSet) # returns true if all elements from anotherSet is present in original setset.issuperset(anotherSet) # returns true if all elements from original set is present in anotherSetset.difference(anotherSet) # returns set containing items ONLY in first setset.difference_update(anotherSet) # removes common elements from first set [no new set is created or returned]set.intersection(anotherSet) # returns new set with common elementsset.intersection_update(anotherSet) # modifies first set keeping only common elementsset.symmetric_difference(anotherSet) # returns set containing all non-common elements of both setsset.symmetric_difference_update(anotherSet) # same as symmetric_difference but changes are made on original setset.union(anotherSet) # ...set.update(anotherSet) # adds anotherSet without duplicate

Tuples

A tuple is a collection which is ordered, unchangeable and can contain duplicate values

  • Operations Time Complexities

    Similar to list

tuple= (1,2,3,1)
tuple.count(1) # returns occurence of an itemtuple.index(1) # returns index of 1 in array

Strings

Python String isnumeric()

# ** split Function **# The split() method breaks up a string at the specified separator and returns# a list of strings.text='Python is a fun programming language'# split the text from spaceprint(text.split(' '))
# Output: ['Python', 'is', 'a', 'fun', 'programming', 'language']# convert string to lists="abcd"s=list(s)
print(s)
# Output: ['a', 'b', 'c', 'd']# ** count Function **# The count() method returns the number of occurrences of a substring in the given string.# Examplemessage='python is popular programming language'# number of occurrence of 'p'print('Number of occurrence of p:', message.count('p')) # Output: Number of occurrence of p: 4# The isnumeric() method returns True if all characters in a string are numeric characters. If not, it returns False.s='1242323'print(s.isnumeric()) #Output: True# The find() method returns the index of first occurrence of the substring (if found). If not found, it returns -1.# check the index of 'fun'print(message.find('fun')) # Output: 12# The isalnum() method returns True if all characters in the string are alphanumeric (either alphabets or numbers). If not, it returns False.name="M3onica Gell22er "print(name.isalnum()) # Output : False# The isalpha() method returns True if all characters in the string are alphabets. If not, it returns Falsename="Monica"print(name.isalpha()) #output true# other important functionsstring.strip([chars]) #The strip() method returns a copy of the string by removing both the leading and the trailing characters (based on the string argument passed).string.upper() # The upper() method converts all lowercase characters in a string into uppercase characters and returns it.string.lower() # The lower() method converts all uppercase characters in a string into lowercase characters and returns it.string.islower() # The islower() method returns True if all cased characters in the string are lowercase and there is at least one cased character, False otherwise.string.isdigit() string.isupper() # The isupper() method returns True if all cased characters in the string are uppercase and there is at least one cased character, False otherwise.

Built-in or Library functions

  • Functions to iterate over list / other iterable (tuple, dictionaries)

    **map(fun, iter) **# fun : It is a function to which map passes each element of given iterable.# iter : It is a iterable which is to be mapped.**zip(list,list) **forelem1,elem2inzip(firstList,secondList):
    # will merge both lists and produce tuples with both elements# Tuples will stop at shortest list (in case of both lists having different len)# Example'''a = ("John", "Charles", "Mike")b = ("Jenny", "Christy", "Monica")x = zip(a, b)# use the tuple() function to display a readable version of the result:print(tuple(x))o/p: (('John', 'Jenny'), ('Charles', 'Christy'), ('Mike', 'Monica'))'''**any(list) ** [ OPPOSITEIS=>**all() ** ]
    any(someList) # returns true if ANY element in list is true [any string, all numbers except 0 also count as true]**enumerate(list|tuple) **# [when you need to attach indexes to lists or tuples ]enumerate(anyList) # ['a','b','c'] => [(0, 'a'), (1, 'b'), (2, 'c')]**filter(function|list) **filter(myFunction,list) # returns list with elements that returned true when passed in function*****************importbisect*************************bisect.bisect(list,number,begin,end) **O(log(n))
    # [ returns the index where the element should be inserted # such that sorting order is maintained ]a= [1,2,4]
    bisect.bisect(a,3,0,4) # [1,2,4] => 3 coz '3' should be inserted in 3rd index to maintain sorting order# Other variants of this functions are => bisect.bisect_left() | bisect.bisect_right()# they have same arguments. Suppose the element we want to insert is already present# in the sorting list, the bisect_left() will return index left of the existing number# and the bisect_right() or bisect() will return index right to the existing number# ** bisect.insort(list,number,begin,end) ** O(n) to insert# ** bisect.insort_right(list,number,begin,end) ** # ** bisect.insort_left(list,number,begin,end) ** Theabove3functionsareexactsameofbisect.bisect(), theonlydifferenceisthattheyreturnthesortedlistafterinsertingandnottheindex. Theleft() right() logicisalsosameasabove.
  • Getting ASCII value of a character

    **ord(str) **# returns ascii value of the character , Example ord("a") = 97**chr(int) **# return character of given ascii value , Example chr(97) = "a"

Clean Code Tips

  • Doc Strings - Documentation for your functions in the interview to look slick 😎

    A docstring is short for documentation string.

    Python docstrings (documentation strings) are the string literals that appear right after the definition of a function, method, class, or module.

    Triple quotes are used while writing docstrings. For example:

    def double(num):
    """Function to double the value"""
    return 2*num
    

    Docstrings appear right after the definition of a function, class, or a module. This separates docstrings from multiline comments using triple quotes.

    The docstrings are associated with the object as their __doc__ attribute.

    So, we can access the docstrings of the above function with the following lines of code:

    def double(num):
    """Function to double the value"""
    return 2*num
    print(double.__doc__)
    

    Output

    Function to double the value
    
  • Use Assert keyword in python for testing edge cases. Looks more professional.

    Definition and Usage

    The assert keyword is used when debugging code.

    The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError.

    You can write a message to be written if the code returns False, check the example below.

    x="hello"#if condition returns False, AssertionError is raised:assertx=="goodbye", "x should be 'hello'"
  • ALWAYS be aware of any code snippet that is being REPEATED in your solution. MODULARITY #1 Priority. Refactoring is also an important part of interview.

    • This is usually asked as a follow up after coding the solution. Are there any changes you want to make to this solution?

Miscellaneous

  • How to take multiple line input in python?

    Taking multiple inputs from user in Python - GeeksforGeeks

    • Using split() method
    • Using List comprehension

    Syntax :

    input().split(separator, maxsplit)
    

    Example

    # Python program showing how to# multiple input using split# taking two inputs at a timex, y=input("Enter a two value: ").split()
    print("Number of boys: ", x)
    print("Number of girls: ", y)
    print()
    # taking three inputs at a timex, y, z=input("Enter a three value: ").split()
    print("Total number of students: ", x)
    print("Number of boys is : ", y)
    print("Number of girls is : ", z)
    print()
    # taking two inputs at a timea, b=input("Enter a two value: ").split()
    print("First number is {} and second number is {}".format(a, b))
    print()
    # taking multiple inputs at a time# and type casting using list() functionx=list(map(int, input("Enter a multiple value: ").split()))
    print("List of students: ", x)
    # Python program showing# how to take multiple input# using List comprehension# taking two input at a timex, y= [int(x) forxininput("Enter two value: ").split()]
    print("First Number is: ", x)
    print("Second Number is: ", y)
    print()
    # taking three input at a timex, y, z= [int(x) forxininput("Enter three value: ").split()]
    print("First Number is: ", x)
    print("Second Number is: ", y)
    print("Third Number is: ", z)
    print()
    # taking two inputs at a timex, y= [int(x) forxininput("Enter two value: ").split()]
    print("First number is {} and second number is {}".format(x, y))
    print()
    # taking multiple inputs at a timex= [int(x) forxininput("Enter multiple value: ").split()]
    print("Number of list is: ", x)
    # taking multiple inputs at a time separated by commax= [int(x) forxininput("Enter multiple value: ").split(",")]
    print("Number of list is: ", x)
  • Important Python Math Functions

    Python Math Module - GeeksforGeeks

    • Log Function

    Log functions in Python - GeeksforGeeks

    Syntax :
    math.log(a,Base)
    Parameters :a : The numeric value
    Base : Base to which the logarithm has to be computed.
    Return Value :
    Returns natural log if 1 argument is passed and log with
    specified base if 2 arguments are passed.
    Exceptions :
    Raises ValueError is a negative no. is passed as argument.
    
    importmath# Printing the log base e of 14print ("Natural logarithm of 14 is : ", end="")
    print (math.log(14))
    # Printing the log base 5 of 14print ("Logarithm base 5 of 14 is : ", end="")
    print (math.log(14,5))
    • Finding the ceiling and the floor value
      • Ceil value means the smallest integral value greater than the number and the floor value means the greatest integral value smaller than the number. This can be easily calculated using the ceil() and floor() method respectively.
    # Python code to demonstrate the working of# ceil() and floor()# importing "math" for mathematical operationsimportmatha=2.3# returning the ceil of 2.3 (i.e 3)print ("The ceil of 2.3 is : ", end="")
    print (math.ceil(a))
    # returning the floor of 2.3 (i.e 2)print ("The floor of 2.3 is : ", end="")
    print (math.floor(a))
    • Other Important functions
    # Constants# Print the value of Euler e (2.718281828459045)print (math.e)
    # Print the value of pi (3.141592653589793)print (math.pi)
    print (math.gcd(b, a))
    print (pow(3,4))
    # print the square root of 4print(math.sqrt(4))
    a=math.pi/6b=30# returning the converted value from radians to degreesprint ("The converted value from radians to degrees is : ", end="")
    print (math.degrees(a))
    # returning the converted value from degrees to radiansprint ("The converted value from degrees to radians is : ", end="")
    print (math.radians(b))
    **bin(int) **bin(anyNumber) # Returns binary version of number**divmod(int,int) **divmod(dividend,divisor) # returns tuple like (quotient, remainder)
  • Python cmp_to_key function to sort list with custom compare function

    Sort a list of lists with a custom compare function

    How the custom comparator works

    When providing a custom comparator, it should generally return an integer/float value that follows the following pattern (as with most other programming languages and frameworks):

    • return a negative value (< 0) when the left item should be sorted before the right item
    • return a positive value (> 0) when the left item should be sorted after the right item
    • return 0 when both the left and the right item have the same weight and should be ordered "equally" without precedence
    fromfunctoolsimportcmp_to_keysorted(mylist, key=cmp_to_key(compare))
    # Exampledefcompare(item1, item2):
    iffitness(item1) <fitness(item2):
    return-1eliffitness(item1) >fitness(item2):
    return1else:
    return0

Python integer division acts a bit weird with -ve numbers ex: -3//2 will give -2 answer instead of -1 so always use int(-3/2) for integer division in problems

Resources

The Modulo Operation (%) With Negative Numbers in Python

About

A Cheat Sheet 📜 to revise Python syntax. Particularly useful for solving Data Structure and Algorithmic problems with Python.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors