Python syntax, SQL commands, and networking concepts — organised by chapter for quick revision.
raise ExceptionType('error message')
assert condition, 'error message'
try:
# statements that may raise exception
except ExceptionType:
# handler code
try:
# statements
except ValueError:
# handle ValueError
except ZeroDivisionError:
# handle ZeroDivisionError
try:
# statements
except ExceptionType:
# handle exception
else:
# runs only if no exception occurred
try:
# statements
except ExceptionType:
# handle exception
finally:
# always executes (cleanup code)
try:
# risky code
except ExceptionType:
# handle error
else:
# no exception occurred
finally:
# cleanup — always runs
file_object = open(filename, mode)
file_object.close()
with open('filename', 'mode') as file_object:
# file operations
file_object.write(string)
file_object.writelines(list_of_strings)
content = file_object.read([size])
line = file_object.readline()
lines = file_object.readlines()
position = file_object.tell()
file_object.seek(offset, reference_point)
import pickle
pickle.dump(object, file_object)
import pickle
object = pickle.load(file_object)
Step 1: If stack is full, print 'Overflow' and return
Step 2: Increment TOP by 1
Step 3: Set stack[TOP] = element
Step 4: Return
Step 1: If stack is empty, print 'Underflow' and return
Step 2: Set element = stack[TOP]
Step 3: Decrement TOP by 1
Step 4: Return element
stack = [] # empty stack
stack.append(element) # PUSH
element = stack.pop() # POP
top = stack[-1] # PEEK
len(stack) # SIZE
len(stack) == 0 # isEmpty
** (exponentiation) > *, /, //, % > +, -
A + B -> A B +
A + B * C -> A B C * +
(A + B) * C -> A B + C *
A + B * C - D -> A B C * + D -
(A + B) * (C - D) -> A B + C D - *
1. Create an empty stack and output list
2. Scan the infix expression left to right:
a. If operand, add to output
b. If '(', push onto stack
c. If ')', pop and add to output until '(' is found; discard '('
d. If operator, pop and add to output while stack top has >= precedence (for left-assoc) or > precedence (for right-assoc); then push current operator
3. Pop all remaining operators from stack to output
1. Create an empty stack
2. Scan the postfix expression left to right:
a. If operand, push onto stack
b. If operator, pop two operands (op2 = pop, op1 = pop), compute op1 operator op2, push result
3. The final element in the stack is the result
Step 1: If queue is full, print 'Overflow' and return
Step 2: Increment rear by 1
Step 3: Set queue[rear] = element
Step 4: If front is -1, set front = 0
Step 5: Return
Step 1: If queue is empty, print 'Underflow' and return
Step 2: Set element = queue[front]
Step 3: Increment front by 1
Step 4: If front > rear, reset front = rear = -1 (queue empty)
Step 5: Return element
queue = [] # empty queue
queue.append(element) # Enqueue
element = queue.pop(0) # Dequeue
front = queue[0] # Peek
len(queue) # Size
len(queue) == 0 # isEmpty
deque = [] # empty deque
deque.append(element) # Add at rear
deque.insert(0, element) # Add at front
element = deque.pop() # Remove from rear
element = deque.pop(0) # Remove from front
O(n^2)
O(n)
O(1)
(n-1) + (n-2) + ... + 1 = n(n-1)/2
O(n^2)
O(1)
n - 1
O(n^2)
O(n)
O(n^2)
O(1)
O(1)
O(n)
O(n^2)
O(1)
O(n)
O(n)
O(1)
O(1)
O(1)
mid = (low + high) // 2
h(key) = key \% table\_size
O(1)
O(n)
SELECT COUNT(*) FROM table_name;
SELECT SUM(column) FROM table_name;
SELECT AVG(column) FROM table_name;
SELECT MAX(column), MIN(column) FROM table_name;
Number of links = n(n-1)/2, where n is the number of devices
Ports per device = n - 1
2^32 = 4,294,967,296 (approximately 4.3 billion)
2^128 = approximately 3.4 x 10^38
1 Kbps = 1,024 bps (or 10^3 bps); 1 Mbps = 1,024 Kbps (or 10^6 bps); 1 Gbps = 1,024 Mbps (or 10^9 bps); 1 Tbps = 1,024 Gbps (or 10^12 bps)
Time = File Size (in bits) / Data Transfer Rate (in bps)