Skip to content

Python basics

20+ helpful Python syntax patterns for coding interviews

Integers

Integers in python3 are unbounded

Examples

  • Happy Number

Power

x = -2

print(x**2) # 4
print(x*x) # 4

Square root *

import math

math.sqrt(49)
# 7

Modulus(%) vs Floor(//)

Modulus: The modulus-function computes the remainder of a division, which is the "leftover" of an integral division.

Floor: The floor-function provides the lower-bound of an integral division. The upper-bound is computed by the ceil function. (Basically speaking, the floor-function cuts off all decimals).

# modulus
print(324 % 10) # 4

# floor
print(324 // 10) # 32

math.ceil() & math.floor()

import math

math.ceil(2.17) # 3
math.floor(2.17) # 2

Infinity (can be used for math.max)

float("inf")
float("-inf")

.isalpha() .isnumeric() & .isalnum()

chr() & ord()

Other

Lambda functions

>>> (lambda x, y: x + y)(2, 3)
5
>>> double = lambda x: x * 2
>>> double(4)

Example usage

# Program to double each item in a list using map()

my_list = [1, 5, 4, 6, 8, 11, 3, 12]

new_list = list(map(lambda x: x * 2 , my_list))

print(new_list) # [2, 10, 8, 12, 16, 22, 6, 24]

Sort


Random

random.random()

Return the next random floating point number in the range [0.0, 1.0]

import random

>>> random.random()
0.6280590213451548
>>> random.random()
0.1623939995862359
>>> random.random()
0.6704949828925247
>>> random.random()
0.6838627536588555

random.randrange(start,stop,step)

Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn’t actually build a range object.

import random

>>> random.randrange(20)
7
>>> random.randrange(10,20,2)
16
>>> random.randrange(start=10,stop=20,step=2)
18
>>> random.randrange(start=10,stop=20,step=2)
14
>>> random.randrange(start=10,stop=20,step=2)
12
>>> random.randrange(start=10,stop=20,step=2)
14
>>> random.randrange(start=10,stop=20,step=2)
16
>>> random.randrange(start=10,stop=20,step=2)
18
>>> random.randrange(start=10,stop=20,step=2)
18
>>> random.randrange(start=10,stop=20,step=2)
18
>>> random.randrange(start=10,stop=20,step=2)
16
>>> random.randrange(start=10,stop=20,step=2)
16
>>> random.randrange(start=10,stop=20,step=2)

random.randint(a,b)

Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1)

import random

>>> random.randint(10,20)
19
>>> random.randint(10,20)
16
>>> random.randint(10,20)
15
>>> random.randint(10,20)
20
>>> random.randint(10,20)
11
>>> random.randint(10,20)
17
>>> random.randint(10,20)
20
>>> random.randint(10,20)

random.choice(seq)

Return a random element from the non-empty sequence seq

import random

>>> random.choice(['win', 'lose', 'draw'])
'lose'
>>> random.choice(['win', 'lose', 'draw'])
'win'
>>> random.choice(['win', 'lose', 'draw'])
'win'
>>> random.choice(['win', 'lose', 'draw'])
'win'
>>> random.choice(['win', 'lose', 'draw'])
'lose'
>>> random.choice(['win', 'lose', 'draw'])
'lose'
>>> random.choice(['win', 'lose', 'draw'])
'draw'

heapq

Heaps & Priority Queues

collections

collections.deque() *

collections.defaultdict

collections.OrderedDict

sortedcontainers.SortedDict



Find the original version of this page (with additional content) on Notion here.



Last update: December 13, 2021 16:05:48
Created: December 13, 2021 16:05:48
Authors: paulonteri (96.73%), Not Committed Yet (3.27%)