Python Programming Exercises Guide
Python Programming Exercises Guide
Python uses the modulus operator % to determine if a number is even or odd. If the remainder when the number is divided by 2 is zero, the number is even; otherwise, it is odd. This logic is implemented using an if-else statement .
A recursive function computes the factorial by multiplying the number by the factorial of the number minus one, until it reaches the base case of 0 or 1, where the factorial is defined as 1. This breakdown into smaller subproblems until reaching a base case exemplifies recursion, reducing complex problems into simpler solvable parts .
Python handles basic I/O operations using the input() function to take user input, which is then converted to an integer using int(). To add two integers, Python adds them using the + operator and prints the result using the print() function .
Python allows reversing a string using slicing with the syntax s[::-1]. This means the slice starts from the end (last character) to the beginning (first character), effectively reversing the order of characters in the string without using loops or additional data structures .
To compute the average of a list of numbers, Python sums up all elements using the sum() function and divides the result by the list's length using len(). This calculation is encapsulated in a function that returns the average. This approach ensures modularity and reusability .
Python implements a stack using a list to store elements. The push() method appends an item to the end of the list, while the pop() method removes and returns the last item, if the stack is not empty. The peek() method returns the last item without removing it, and is_empty() checks if the stack is empty. This LIFO structure is crucial for operations like undo mechanisms in software .
A loop can be used to iterate over a range of numbers, and within the loop, a conditional statement checks if the current number is divisible by the given integer using the modulus operator. If the result is zero, it indicates divisibility, and the number is printed. This approach efficiently finds all divisible numbers in the defined range .
A linear search algorithm iterates through a list from the first element to the last, comparing each element with the target. If the target is found, the algorithm returns its index. If the end of the list is reached without finding the target, it returns -1. This method is straightforward but inefficient for large lists due to its O(n) complexity .
Python's OOP features are used to define a class with a constructor (__init__) for initializing attributes such as name and grade. Methods like display() are used for specific actions, demonstrating encapsulation and the creation of objects (instances) of the class, allowing for modular and reusable code .
The bubble sort algorithm repeatedly steps through the list, comparing adjacent elements and swapping them if they are in the wrong order. This process of bubbling up the largest unsorted element continues until no more swaps are needed, indicating the list is sorted. The algorithm's complexity is O(n^2) due to its nested loops .