Numpy is a powerful Python library for numerical computing that provides efficient array objects and methods for array manipulation and operations. Key methods include array creation functions like np.array() and np.zeros(), manipulation functions like np.reshape() and np.concatenate(), and operations like np.add() and np.dot(). Additionally, the document discusses how to create and manipulate dictionaries in Python, highlighting methods for creation, accessing values, and modifying key-value pairs.
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 ratings0% found this document useful (0 votes)
3 views
PS Assignment 3
Numpy is a powerful Python library for numerical computing that provides efficient array objects and methods for array manipulation and operations. Key methods include array creation functions like np.array() and np.zeros(), manipulation functions like np.reshape() and np.concatenate(), and operations like np.add() and np.dot(). Additionally, the document discusses how to create and manipulate dictionaries in Python, highlighting methods for creation, accessing values, and modifying key-value pairs.
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/ 2
Assignment 3
1. What is Numpy? Explain different methods of Numpy.
Numpy is a powerful Python library for numerical computing. It provides efficient array objects, functions, and tools for working with arrays, matrices, and other numerical data. Numpy is the foundation for many other scientific computing libraries in Python, such as SciPy and Pandas. Key Methods in Numpy: ● Array Creation: ○ np.array(): Creates an array from a list or tuple. ○ np.zeros(): Creates an array filled with zeros. ○ np.ones(): Creates an array filled with ones. ○ np.arange(): Creates an array with evenly spaced values within a given interval. ○ np.linspace(): Creates an array with evenly spaced values over a specified interval. ○ np.random.rand(): Creates an array of random values between 0 and 1. ○ np.random.randn(): Creates an array of random values from the standard normal distribution. ● Array Manipulation: ○ np.reshape(): Reshapes an array to a new shape. ○ np.transpose(): Transposes an array (switches rows and columns). ○ np.flatten(): Flattens a multi-dimensional array into a 1D array. ○ np.concatenate(): Joins multiple arrays along an existing axis. ○ np.vstack(): Stacks arrays vertically (row-wise). ○ np.hstack(): Stacks arrays horizontally (column-wise). ○ np.split(): Splits an array into multiple sub-arrays. ● Array Operations: ○ np.add(), np.subtract(), np.multiply(), np.divide(): Element-wise addition, subtraction, multiplication, and division. ○ np.dot(): Matrix multiplication. ○ np.mean(), np.median(), np.std(), np.var(): Calculates mean, median, standard deviation, and variance of an array. ○ np.min(), np.max(): Finds the minimum and maximum values in an array. ○ np.argmin(), np.argmax(): Returns the indices of the minimum and maximum values. ● Other Useful Methods: ○ np.linalg.inv(): Calculates the inverse of a matrix. ○ np.linalg.det(): Calculates the determinant of a matrix. ○ np.exp(), np.log(), np.sin(), np.cos(): Applies mathematical functions to an array. 2. How to create Dictionary in Python? Discuss various methods with example. A dictionary in Python is an unordered collection of key-value pairs. Each key is unique and associated with a corresponding value. Dictionaries are mutable, meaning you can add, modify, or remove key-value pairs after creation. Creating Dictionaries: ● Using curly braces {}: my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'} print(my_dict)
● Using the dict() constructor:
my_dict = dict(key1='value1', key2='value2', key3='value3') print(my_dict) Accessing Values: ● Use the key within square brackets []: value1 = my_dict['key1'] print(value1)
Adding or Modifying Values:
● Assign a new value to a key: my_dict['key4'] = 'value4' my_dict['key1'] = 'new_value1'
Removing Values: ● Use the del keyword: del my_dict['key2']
● Use the pop() method:
value3 = my_dict.pop('key3')
Other Useful Methods:
● keys(): Returns a view object containing all the keys in the dictionary. ● values(): Returns a view object containing all the values in the dictionary. ● items(): Returns a view object containing key-value pairs as tuples. ● get(): Returns the value for a key, or a default value if the key is not found. ● update(): Updates a dictionary with key-value pairs from another dictionary or an iterable.