Python Basics Built-in Functions round(number, ndigits) : Rounds a number to a given precision. Example: round(5.678, 2) returns 5.68 abs(number) : Returns the absolute value of a number. Example: abs(-13.5) returns 13.5 pow(base, exp) : Returns base raised to the power of exp . Example: pow(3, 4) returns $3^4 = 81$ Data Types Integers ( int ): Whole numbers (e.g., 5 , -10 ) Floating-point numbers ( float ): Decimal numbers (e.g., 3.14 , -0.5 ) Strings ( str ): Sequences of characters (e.g., "hello" , 'Python' ) Booleans ( bool ): Truth values ( True , False ) Lists ( list ): Ordered, mutable collections (e.g., [1, 2, 3] ) Tuples ( tuple ): Ordered, immutable collections (e.g., (1, 2, 3) ) Dictionaries ( dict ): Unordered key-value pairs (e.g., {'a': 1, 'b': 2} ) Control Flow: Loops Feature For-Loop While-Loop Purpose Iterate over a sequence (e.g., list, range) or for a fixed number of times. Repeat a block of code as long as a condition is true. Iteration Count Typically known before the loop starts. Unknown; depends on when the condition becomes false. Structure for item in sequence: while condition: Common Use Processing elements of a collection, definite iteration. Repeating until a specific event occurs, indefinite iteration. Python Functions Largest of Three Numbers def max3(a, b, c): return max(a, b, c) # Alternative without max() def max3_manual(a, b, c): if a >= b and a >= c: return a elif b >= a and b >= c: return b else: return c Geometric Series Sum Function geoSum(r, N) to compute $1 + r + r^2 + \dots + r^N$. def geoSum(r, N): sum_val = 0 for i in range(N + 1): sum_val += r**i return sum_val Arithmetic Mean Function arithmeticMean(X) to compute the mean of a list $X = [x_1, x_2, \dots, x_n]$. def arithmeticMean(X): if not X: # Handle empty list case return 0 return sum(X) / len(X) Euclidean Distance Function distance(p, q) for points $p=(x_1, y_1)$ and $q=(x_2, y_2)$. $d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$ import math def distance(p, q): x1, y1 = p x2, y2 = q return math.sqrt((x2 - x1)**2 + (y2 - y1)**2) Fibonacci Sequence Function fib(n) to return a list of the first $n$ Fibonacci numbers. Sequence: $0, 1, 1, 2, 3, 5, \dots$ (where $F_0=0, F_1=1$) def fib(n): if n <= 0: return [] elif n == 1: return [0] else: list_fib = [0, 1] while len(list_fib) < n: next_fib = list_fib[-1] + list_fib[-2] list_fib.append(next_fib) return list_fib Numerical Methods Trapezoidal Rule for Integration Function trap(f, a, b, n) to approximate $\int_a^b f(x) dx$. Formula: $\int_a^b f(x) dx \approx \frac{h}{2} [f(a) + f(b) + 2 \sum_{i=1}^{n-1} f(a + ih)]$ where $h = \frac{b-a}{n}$ def trap(f, a, b, n): h = (b - a) / n integral = (f(a) + f(b)) / 2 for i in range(1, n): integral += f(a + i * h) return integral * h # Example usage: Integrate x^2 from 0 to 1 with 100 segments # def my_func(x): # return x**2 # print(trap(my_func, 0, 1, 100)) # Should be close to 1/3 Example Problem Solutions Computing $\sum_{k=1}^{100} \frac{1}{k^2}$ using a for-loop sum_val = 0 for k in range(1, 101): # k from 1 to 100 sum_val += 1 / (k**2) print(sum_val)