Live Jobs
NEWSoftware Engineer - Automation Tester@UplersNEWSenior Embedded Systems Software Engineer@Google India Pvt LtdNEWManager, Software Engineering - IOS - Player Team, Bangalore@Warner Bros. DiscoveryNEWSenior Software Engineer - Java@ClarivateNEWSoftware Engineer - Automation Tester@UplersSoftware Developer 2, Python Backend Developer@Kinaxis Inc.Software Developer Intern@Mexilet Technologies Private LimitedFull-Stack Software Engineer Intern - Remote@UplersSoftware Engineer, AIML - Fixed Term Contract (6 months)@NatWest GroupNEWSoftware Engineer - Automation Tester@UplersNEWSenior Embedded Systems Software Engineer@Google India Pvt LtdNEWManager, Software Engineering - IOS - Player Team, Bangalore@Warner Bros. DiscoveryNEWSenior Software Engineer - Java@ClarivateNEWSoftware Engineer - Automation Tester@UplersSoftware Developer 2, Python Backend Developer@Kinaxis Inc.Software Developer Intern@Mexilet Technologies Private LimitedFull-Stack Software Engineer Intern - Remote@UplersSoftware Engineer, AIML - Fixed Term Contract (6 months)@NatWest GroupNEWSoftware Engineer - Automation Tester@UplersNEWSenior Embedded Systems Software Engineer@Google India Pvt LtdNEWManager, Software Engineering - IOS - Player Team, Bangalore@Warner Bros. DiscoveryNEWSenior Software Engineer - Java@ClarivateNEWSoftware Engineer - Automation Tester@UplersSoftware Developer 2, Python Backend Developer@Kinaxis Inc.Software Developer Intern@Mexilet Technologies Private LimitedFull-Stack Software Engineer Intern - Remote@UplersSoftware Engineer, AIML - Fixed Term Contract (6 months)@NatWest Group

NumPy Scientific ComputingCheat Sheet

Master numerical computing with comprehensive NumPy arrays, operations, and performance tips

Array Operations
Math Functions
Performance Optimized

1
Array Creation & Properties

Create Arrays

Create NumPy arrays from lists or with specific values

np.array([1, 2, 3]) / np.zeros(shape) / np.ones(shape)
basiccreationbasics
Example:
arr = np.array([1, 2, 3, 4]) zeros = np.zeros((3, 4)) ones = np.ones((2, 3))
Quick Tip:
Use dtype parameter to specify data type: np.array([1,2,3], dtype=float)
✅ Best Practice:
DO: Specify dtype explicitly for memory efficiency, use np.empty() for uninitialized arrays when values will be overwritten, prefer np.zeros/ones over list comprehensions
❌ Common Mistake:
DON'T: Create arrays from nested Python lists without considering memory usage, forget to specify dtype leading to unexpected type conversions, use np.array() for large arrays when np.zeros/ones is more efficient

Range Arrays

Create arrays with evenly spaced values

np.arange(start, stop, step) / np.linspace(start, stop, num)
basiccreationsequences
Example:
np.arange(0, 10, 2) # [0, 2, 4, 6, 8] np.linspace(0, 1, 5) # [0, 0.25, 0.5, 0.75, 1]
Quick Tip:
arange excludes stop, linspace includes stop by default
✅ Best Practice:
DO: Use linspace for exact number of points, arange for specific step sizes, specify dtype to avoid floating point precision issues
❌ Common Mistake:
DON'T: Use arange with floating point steps (precision issues), forget that arange excludes endpoint while linspace includes it, use large step sizes without checking array size

Array Properties

Get information about array structure and properties

arr.shape / arr.dtype / arr.size / arr.ndim
basicinspectionproperties
Example:
arr = np.array([[1, 2], [3, 4]]) print(f"Shape: {arr.shape}") # (2, 2) print(f"Size: {arr.size}") # 4
Quick Tip:
Use arr.itemsize for bytes per element, arr.nbytes for total bytes
✅ Best Practice:
DO: Check array properties before operations, use .nbytes to monitor memory usage, understand the difference between size and shape
❌ Common Mistake:
DON'T: Confuse size (total elements) with shape (dimensions), ignore dtype when doing mathematical operations, assume all arrays have same memory layout

Reshape Arrays

Change array dimensions without changing data

arr.reshape(new_shape) / arr.flatten() / arr.ravel()
basicreshapingdimensions
Example:
arr = np.arange(12) reshaped = arr.reshape(3, 4) # 3x4 matrix flat = arr.flatten() # 1D array
Quick Tip:
Use -1 in reshape to auto-calculate dimension: arr.reshape(-1, 2)
✅ Best Practice:
DO: Use -1 for automatic dimension calculation, understand difference between flatten() (copy) and ravel() (view when possible), check total elements match
❌ Common Mistake:
DON'T: Reshape without checking if total elements match, confuse flatten() and ravel() behavior, use reshape on arrays without understanding memory layout implications

Random Arrays

Generate arrays with random values

np.random.random(size) / np.random.randint(low, high, size)
basicrandomsimulation
Example:
np.random.random((2, 3)) # Random floats [0, 1) np.random.randint(1, 10, (3, 3)) # Random integers
Quick Tip:
Use np.random.seed(42) for reproducible random numbers
✅ Best Practice:
DO: Set seed for reproducible results, use np.random.default_rng() for modern random generation, specify dtype when needed
❌ Common Mistake:
DON'T: Forget to set seed for reproducible research, use old np.random functions instead of Generator, ignore the random state in parallel processing

Special Matrices

Create identity matrices and diagonal matrices

np.eye(n) / np.identity(n) / np.diag([1,2,3])
basiclinear-algebramatrices
Example:
identity = np.eye(3) # 3x3 identity matrix diag_matrix = np.diag([1, 2, 3]) # Diagonal matrix
Quick Tip:
np.eye() can create rectangular matrices: np.eye(3, 4)
✅ Best Practice:
DO: Use np.eye() for rectangular identity matrices, np.identity() for square only, understand diagonal matrix properties
❌ Common Mistake:
DON'T: Confuse np.eye() and np.identity() capabilities, create large identity matrices unnecessarily, ignore sparse matrix alternatives for large matrices

2
Array Indexing & Slicing

Basic Indexing

Access array elements using indices and slices

arr[index] / arr[row, col] / arr[start:stop:step]
basicindexingselection
Example:
arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr[0, 1]) # 2 print(arr[:, 1]) # [2, 5]
Quick Tip:
Negative indices count from the end: arr[-1] is last element
✅ Best Practice:
DO: Use negative indices for end-relative access, understand that slicing creates views (not copies), use ellipsis (...) for multiple dimensions
❌ Common Mistake:
DON'T: Modify sliced arrays without understanding view vs copy behavior, use inefficient loops instead of vectorized indexing, forget that slicing is inclusive of start, exclusive of stop

Boolean Indexing

Filter arrays using boolean conditions

arr[arr > value] / arr[condition]
intermediatefilteringconditions
Example:
arr = np.array([1, 2, 3, 4, 5]) filtered = arr[arr > 3] # [4, 5] even = arr[arr % 2 == 0] # [2, 4]
Quick Tip:
Use & (and), | (or), ~ (not) for multiple conditions
✅ Best Practice:
DO: Use parentheses for complex conditions, understand that boolean indexing creates copies, combine conditions with & and |
❌ Common Mistake:
DON'T: Use "and"/"or" instead of "&"/"|" for array conditions, forget parentheses in complex conditions, ignore that boolean indexing flattens arrays

Fancy Indexing

Select multiple elements using arrays of indices

arr[[indices]] / arr[rows, cols]
intermediateselectionadvanced-indexing
Example:
arr = np.array([10, 20, 30, 40, 50]) selected = arr[[0, 2, 4]] # [10, 30, 50] rows = [0, 1]; cols = [1, 2]
Quick Tip:
Fancy indexing always returns a copy, not a view
✅ Best Practice:
DO: Understand that fancy indexing creates copies, use for non-contiguous selections, combine with boolean indexing when needed
❌ Common Mistake:
DON'T: Expect views from fancy indexing (always copies), use when simple slicing would work, ignore memory implications of copying

Conditional Selection

Select elements based on condition

np.where(condition, x, y)
intermediateconditionalselection
Example:
arr = np.array([1, 2, 3, 4, 5]) result = np.where(arr > 3, arr, 0) # [0, 0, 0, 4, 5]
Quick Tip:
np.where without x,y returns indices where condition is True
✅ Best Practice:
DO: Use np.where for conditional replacement, understand the three-argument form, use for vectorized if-else logic
❌ Common Mistake:
DON'T: Confuse np.where with boolean indexing, use when simple boolean indexing would suffice, ignore broadcasting rules for x and y