0% found this document useful (0 votes)
5 views

Untitled document (3)

The document provides a comprehensive overview of NumPy, built-in functions, lambda functions, and user-defined functions in Python. It covers key concepts, usage examples, and advantages of NumPy arrays, as well as how to create and utilize functions effectively. Additionally, it explains the differences between lambda and regular functions, along with practical applications of these concepts.

Uploaded by

sohamkadam5505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Untitled document (3)

The document provides a comprehensive overview of NumPy, built-in functions, lambda functions, and user-defined functions in Python. It covers key concepts, usage examples, and advantages of NumPy arrays, as well as how to create and utilize functions effectively. Additionally, it explains the differences between lambda and regular functions, along with practical applications of these concepts.

Uploaded by

sohamkadam5505
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Short Answers for Oral Questions on NumPy, Functions, and Lambda

Functions

NumPy Arrays

1. NumPy is a Python library for numerical computing, offering efficient array


operations.
2. np.array([1, 2, 3]) creates a NumPy array.
3. NumPy arrays are faster, memory-efficient, and support vectorized operations.
4. np.zeros((2,2)) → matrix of zeros, np.ones((3,3)) → matrix of ones,
np.eye(3) → identity matrix.
5. Use +, -, *, / for element-wise operations.
6. Use .sum(), .min(), .max() for sum, min, max values.
7. Cumulative sum: np.cumsum(arr), returns running sum of elements.
8. Sorting: np.sort(arr).
9. Concatenation: np.concatenate((arr1, arr2)).
10. Unique elements: np.unique(arr).
11. np.square(arr) for squares, np.sqrt(arr) for square roots.
12. np.log(arr), computes natural log (ln) of elements.
13. Dot product: np.dot(arr1, arr2).
14. Mean: np.mean(arr), Standard deviation: np.std(arr).

Built-in Functions

15. Functions like max(), min(), len(), sorted(), etc.


16. max(lst) and min(lst).
17. sorted(lst), returns a sorted list.
18. all() returns True if all elements in an iterable are True.
19. str.upper() → converts to uppercase, str.lower() → lowercase.
20. str.isalnum() checks if string contains only letters and numbers.
21. str.find('word') returns index of the first occurrence of 'word'.

Lambda (Anonymous) Functions

22. Lambda functions are small, single-expression functions without a name.


23. Lambda functions are one-liners; regular functions use def.
24. even_check = lambda x: x % 2 == 0
25. map(lambda x: x*2, [1,2,3]) doubles each element.
26. filter(lambda x: x>5, [3, 7, 2, 8]) filters elements greater than 5.
27. leap = lambda year: (year % 4 == 0 and year % 100 != 0) or
(year % 400 == 0)
28. [lambda x: x**2 for x in range(5)] → lambda in list comprehension.
29. Pros: Concise, useful for small tasks. Cons: Hard to read/debug.
User-Defined Functions

30. Functions defined by users using def.


31. def greet(name): return "Hello " + name
32. return sends back a value from a function.
33. Positional (fixed order), Default (pre-set values), Keyword (named arguments).
34. *args for multiple positional arguments, **kwargs for key-value pairs.
35. def power(x, n): return x ** n
36. If no return, function returns None.
37. Yes, return a, b returns multiple values as a tuple.

Let me know if you need more details! 😊

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").

Lambda (Anonymous) Functions

22. Lambda functions are short, one-line functions without a name.


23. Difference: Lambda functions are single-line, while normal functions use def.
24. Check if a number is even: lambda x: x % 2 == 0.
25. Use lambda with map(): map(lambda x: x * 2, [1, 2, 3]) → Doubles
each number.
26. Use lambda with filter(): filter(lambda x: x > 5, [2, 6, 8]) →
Keeps numbers > 5.
27. Leap year check: lambda year: (year % 4 == 0 and year % 100 != 0)
or (year % 400 == 0).
28. Lambda in list comprehension: [lambda x: x ** 2 for x in range(5)]
→ Squares numbers.
29. Good things: Short and quick. Bad things: Hard to understand if too complex.

User-Defined Functions

30. User-defined functions are functions that we create using def.


31. Example function:

python
CopyEdit
def greet(name):
return "Hello, " + name

32. return is used to give back a result from a function.


33. Function arguments:
● Positional: add(2,3).
● Default: def power(x, y=2).
● Keyword: add(x=2, y=3).
34. *args allows multiple values, **kwargs allows key-value pairs.
35. Power function:

python
CopyEdit
def power(x, n):
return x ** n

36. If no return, function gives None.


37. Yes, a function can return multiple values:

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

Detailed but Simple Answers for Oral Questions on NumPy, Functions,


and Lambda Functions

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.

2. How do you create a NumPy array?

We create a NumPy array using the np.array() function. Example:


python
CopyEdit
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)

3. What are the advantages of NumPy arrays over Python lists?

● Faster: NumPy arrays are optimized for performance.


● Less Memory: They use less memory than lists.
● Supports Math Operations: We can do direct addition, multiplication, etc.

4. Explain the difference between zeros(), ones(), and eye()


functions in NumPy.

● np.zeros((2,2)) → Creates a 2x2 matrix filled with zeros.


● np.ones((3,3)) → Creates a 3x3 matrix filled with ones.
● np.eye(3) → Creates a 3x3 identity matrix (diagonal ones, rest are zeros).

5. How do you perform element-wise addition, subtraction,


multiplication, and division on NumPy arrays?

NumPy allows simple math operations directly:

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?

Cumulative sum means adding numbers step by step. Example:

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.

8. How can you sort a NumPy array?

We use the np.sort() function. Example:

python
CopyEdit
arr = np.array([5, 2, 8, 1])
print(np.sort(arr)) # [1 2 5 8]

9. Explain how to concatenate two NumPy arrays.

Concatenation means joining arrays together:

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]

10. What function is used to find unique elements in a NumPy array?

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.]

12. Explain how the logarithm function works in NumPy.

It calculates the natural log (ln) of each number:

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?

Dot product is a special multiplication of two arrays:

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?

● Mean is the average: np.mean(arr).


● Standard deviation shows how much values vary: np.std(arr).

Built-in Functions in Python


15. What are built-in functions in Python? Give examples.

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

17. Explain how sorting works in Python.

Sorting arranges values in order:

python
CopyEdit
numbers = [10, 3, 8]
print(sorted(numbers)) # [3, 8, 10]

18. What does the all() function do in Python?

It checks if all values are True:

python
CopyEdit
print(all([True, True, False])) # False

19. How do you convert text to uppercase and lowercase in Python?


python
CopyEdit
text = "Hello"
print(text.upper()) # "HELLO"
print(text.lower()) # "hello"

Lambda (Anonymous) Functions


22. What is a lambda function in Python?

A lambda function is a small, one-line function without a name.

23. How is a lambda function different from a regular function?

● Lambda function: One-line, used for short tasks.


● Regular function: Uses def, can have multiple lines.
24. Write a lambda function to check if a number is even or odd.
python
CopyEdit
even = lambda x: x % 2 == 0
print(even(4)) # True

25. How can you use lambda functions with map()?

map() applies a function to each value in a list:

python
CopyEdit
numbers = [1, 2, 3]
print(list(map(lambda x: x * 2, numbers))) # [2, 4, 6]

26. How does filter() work with lambda functions?

It keeps values that match a condition:

python
CopyEdit
numbers = [1, 2, 3, 4]
print(list(filter(lambda x: x % 2 == 0, numbers))) # [2, 4]

27. Write a lambda function to check if a year is a leap year.


python
CopyEdit
leap = lambda year: (year % 4 == 0 and year % 100 != 0) or (year %
400 == 0)
print(leap(2024)) # True

This is detailed but simple so that you can understand easily. Let me know if you need
more help! 😊

4o

You might also like