Python Arrays, Recursion and Anonymous Functions

Summary

These are notes on arrays, recursion, and anonymous functions in Python. The notes cover creating and using arrays, including indexing and slicing. It details recursion with factorial and Fibonacci sequence examples, and also explains anonymous functions (lambda functions) with a code example.

Full Transcript

I. ARRAYS IN PYTHON Introduction to Arrays in Python Arrays are data structures that store multiple values of the same data type in a single variable. Arrays are useful when you need to store and manipulate a collection of data. Python doesn't have a built-in array data type like some other lan...

I. ARRAYS IN PYTHON Introduction to Arrays in Python Arrays are data structures that store multiple values of the same data type in a single variable. Arrays are useful when you need to store and manipulate a collection of data. Python doesn't have a built-in array data type like some other languages. However, you can the `array` module to create arrays. Python Array Module The array module allows creating arrays with constraints on data types. Supported Data Types - `i`: signed integer - `I`: unsigned integer - `f`: floating point - `u`: Unicode character (deprecated) Creating an Array `array.array(typecode, values_list)` Example import array int_array = array.array('i', [1, 2, 3, 4]) float_array = array.array('f', [1.1, 2.2, 3.3, 4.4]) Indexing and slicing in arrays in Python: Indexing allows you to access a single element in an array. Syntax `array_name[index]` Example import array my_array = array.array('i', [1, 2, 3, 4, 5]) print(my_array) # Output: 1 Slicing allows you to access a subset of elements in an array. Syntax `array_name[start:stop:step]` Example import array my_array = array.array('i', [1, 2, 3, 4, 5]) print(my_array[1:3]) # Output: array('i', [2, 3]) Explanation - `start`: The starting index of the slice. - `stop`: The ending index of the slice. - `step`: The increment between elements in the slice. II. RECURSION IN PYTHON Introduction to Recursion Recursion is a programming technique where a function calls itself repeatedly until it reaches a base case that stops the recursion. Recursion is useful for solving problems that have a recursive structure, such as tree or graph traversals, and dynamic programming. Recursive Functions It has two main components: 1. *Base case*: A trivial case that can be solved directly, stopping the recursion. 2. *Recursive case*: A case that breaks down the problem into smaller sub-problems, solved by calling the function itself. Examples of Recursive Algorithms Factorial The factorial of a number `n` (denoted as `n!`) is the product of all positive integers less than or equal to `n`. n! = n * (n-1) * (n-2) *... * 1 Here's a recursive implementation of the factorial function: def factorial(n): if n == 0: # base case return 1 else: return n * factorial(n-1) # recursive case Fibonacci Series The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1. def fibonacci(n): if n == 0 or n == 1: # base case return n else: return fibonacci(n-1) + fibonacci(n-2) # recursive case III. ANONYMOUS FUNCTIONS Anonymous functions, also known as lambda functions, are small, single-purpose functions that can be defined without a name. They are called "anonymous" because they are not declared with a name. Syntax of Anonymous Functions The syntax of an anonymous function in Python is: lambda arguments: expression Where: - `arguments` is a comma-separated list of variables that will be passed to the function. - `expression` is the code that will be executed when the function is called. Example of an Anonymous Function Here's an example of an anonymous function that takes a single argument and returns its square: square = lambda x: x ** 2 print(square(5)) # Output: 25