Write a Python program to demonstrate recursion. Explain the concept of iteration in Python with an example. How are custom exceptions created in Python? Write a Python program to... Write a Python program to demonstrate recursion. Explain the concept of iteration in Python with an example. How are custom exceptions created in Python? Write a Python program to demonstrate the concept of multiple inheritance. Write a Python code snippet to demonstrate searching and filtering in a NumPy array. What is the shape attribute in a NumPy array?
Understand the Problem
The question includes several parts that ask for Python programming demonstrations, explanations of concepts in Python, and specific coding tasks related to recursion, iteration, custom exceptions, multiple inheritance, searching and filtering in NumPy arrays, and the shape attribute of NumPy arrays.
Answer
NumPy shape gives array dimensions; recursion uses functions calling themselves. Create exceptions with `class MyError(Exception)`. Multiple inheritance lets a class inherit from multiple classes. Search and filter arrays with conditions.
The shape attribute in a NumPy array returns its dimensions. A simple recursive function can calculate factorial: def factorial(n): return 1 if n==0 else n*factorial(n-1)
. Iteration example: for i in range(5): print(i)
. Custom exceptions: class MyError(Exception): pass
. Multiple inheritance example: class A: pass; class B: pass; class C(A, B): pass
. NumPy filtering example: import numpy as np; arr = np.array([1, 2, 3]); print(arr[arr > 1])
.
Answer for screen readers
The shape attribute in a NumPy array returns its dimensions. A simple recursive function can calculate factorial: def factorial(n): return 1 if n==0 else n*factorial(n-1)
. Iteration example: for i in range(5): print(i)
. Custom exceptions: class MyError(Exception): pass
. Multiple inheritance example: class A: pass; class B: pass; class C(A, B): pass
. NumPy filtering example: import numpy as np; arr = np.array([1, 2, 3]); print(arr[arr > 1])
.
More Information
Recursion is useful for tasks like computing factorials or traversing trees. Iteration efficiently processes collections. Custom exceptions enhance error handling. Multiple inheritance can complicate design but allows more flexible class structures.
Tips
Common mistakes include endless recursion due to missing base case and misunderstandings in inheritance hierarchy leading to ambiguous method calls.
Sources
- Recursion in Python - GeeksforGeeks - geeksforgeeks.org
- How to Define Custom Exceptions in Python? (With Examples) - programiz.com
- Write the simplest program that will demonstrate iteration vs ... - Chegg - chegg.com
AI-generated content may contain errors. Please verify critical information