Untitled document (3)
Untitled document (3)
Functions
NumPy Arrays
Built-in Functions
4o
You said:
in easy languge and understandble
ChatGPT said:
ChatGPT
Simple and Easy Answers for Oral Questions on NumPy, Functions, and
Lambda Functions
NumPy Arrays
1. NumPy is a Python library used for working with numbers and arrays.
2. We create a NumPy array using: np.array([1, 2, 3]).
3. NumPy arrays are faster and use less memory than Python lists.
4. np.zeros() makes a zero matrix, np.ones() makes a matrix of ones, np.eye()
creates an identity matrix.
5. We can add +, subtract -, multiply *, and divide / arrays directly.
6. To find sum, min, and max: array.sum(), array.min(), array.max().
7. Cumulative sum adds elements step by step: np.cumsum(array).
8. To sort an array: np.sort(array).
9. Join arrays using np.concatenate((arr1, arr2)).
10. Find unique values: np.unique(array).
11. Square numbers: np.square(array), Square root: np.sqrt(array).
12. Logarithm gives the natural log of each number: np.log(array).
13. Dot product (multiplication of two arrays): np.dot(arr1, arr2).
14. Mean (average): np.mean(array), Standard deviation: np.std(array).
Built-in Functions
15. Built-in functions are ready-to-use functions like max(), min(), len(), and
sorted().
16. Find the biggest and smallest number: max(list), min(list).
17. Sort a list in order: sorted(list).
18. Check if all values are true: all(list).
19. Convert text to uppercase: "hello".upper(), to lowercase:
"HELLO".lower().
20. Check if a string has only letters and numbers: "Hello123".isalnum().
21. Find position of a word in a string: "hello world".find("world").
User-Defined Functions
python
CopyEdit
def greet(name):
return "Hello, " + name
python
CopyEdit
def power(x, n):
return x ** n
python
CopyEdit
def values():
return 1, 2, 3
This is a simple and easy-to-understand version of the answers. Let me know if you need
any more explanations! 😊
4o
You said:
in detail ans but simple english language
ChatGPT said:
ChatGPT
NumPy Arrays
1. What is NumPy, and why is it used in Python?
NumPy (Numerical Python) is a library in Python that helps to work with numbers and arrays.
It makes calculations faster and more efficient compared to normal Python lists.
python
CopyEdit
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(arr1 + arr2) # [5 7 9]
print(arr1 - arr2) # [-3 -3 -3]
print(arr1 * arr2) # [4 10 18]
print(arr1 / 2) # [0.5 1.0 1.5]
6. How can you find the sum, minimum, and maximum values of a
NumPy array?
python
CopyEdit
arr = np.array([10, 20, 30, 40])
print(arr.sum()) # 100
print(arr.min()) # 10
print(arr.max()) # 40
7. What is a cumulative sum, and how is it computed in NumPy?
python
CopyEdit
arr = np.array([1, 2, 3, 4])
print(np.cumsum(arr)) # [1 3 6 10]
First element stays the same, then we add each next value.
python
CopyEdit
arr = np.array([5, 2, 8, 1])
print(np.sort(arr)) # [1 2 5 8]
python
CopyEdit
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(np.concatenate((arr1, arr2))) # [1 2 3 4 5 6]
We use np.unique():
python
CopyEdit
arr = np.array([1, 2, 3, 2, 1, 4])
print(np.unique(arr)) # [1 2 3 4]
11. How do you compute the square and square root of elements in an
array?
python
CopyEdit
arr = np.array([1, 4, 9])
print(np.square(arr)) # [1 16 81]
print(np.sqrt(arr)) # [1. 2. 3.]
python
CopyEdit
arr = np.array([1, 2, 3])
print(np.log(arr)) # [0. 0.6931 1.0986]
13. What is the dot product of two arrays, and how is it calculated?
python
CopyEdit
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(np.dot(arr1, arr2)) # (1*4 + 2*5 + 3*6) = 32
14. How do you compute the mean and standard deviation of a NumPy
array?
Built-in functions are functions that Python provides. Examples: max(), min(), sorted(),
len().
16. How do you find the maximum and minimum values in a list?
python
CopyEdit
numbers = [10, 20, 5, 40]
print(max(numbers)) # 40
print(min(numbers)) # 5
python
CopyEdit
numbers = [10, 3, 8]
print(sorted(numbers)) # [3, 8, 10]
python
CopyEdit
print(all([True, True, False])) # False
python
CopyEdit
numbers = [1, 2, 3]
print(list(map(lambda x: x * 2, numbers))) # [2, 4, 6]
python
CopyEdit
numbers = [1, 2, 3, 4]
print(list(filter(lambda x: x % 2 == 0, numbers))) # [2, 4]
This is detailed but simple so that you can understand easily. Let me know if you need
more help! 😊
4o