0% found this document useful (0 votes)
10 views3 pages

Unit 2 Introduction To Python (Introduction To NumPy)

NumPy is an open-source Python library designed for numerical computing, offering the ndarray object for efficient data handling and mathematical operations. It allows the creation of arrays from Python lists using the np.array() function, supporting various dimensions and homogeneous data types. Key advantages of NumPy arrays include faster performance, memory efficiency, and support for vectorized operations.

Uploaded by

hallboy2666
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)
10 views3 pages

Unit 2 Introduction To Python (Introduction To NumPy)

NumPy is an open-source Python library designed for numerical computing, offering the ndarray object for efficient data handling and mathematical operations. It allows the creation of arrays from Python lists using the np.array() function, supporting various dimensions and homogeneous data types. Key advantages of NumPy arrays include faster performance, memory efficiency, and support for vectorized operations.

Uploaded by

hallboy2666
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

Unit 2: Introduction to Python

Topic:- Introduction to NumPy: Introduction, Creation of


NumPy Arrays from List?

NumPy is a powerful Python library used for numerical computing. It provides


the ndarray object, which is faster and more memory-efficient than Python
lists. You can easily create arrays from lists using the [Link]() function.

1. Introduction to NumPy
 NumPy (Numerical Python) is an open-source library for scientific computing in
Python.
 It provides:
o ndarray (N-dimensional array): A fast, flexible container for large datasets.
o Mathematical functions for linear algebra, statistics, and more.
o Tools for handling multidimensional data.
 Why NumPy instead of lists?
o Lists are general-purpose but slow for numerical operations.
o NumPy arrays are contiguous in memory, making them faster.
o Supports vectorized operations (apply operations on entire arrays without
loops).

Example: Adding two lists vs. two NumPy arrays

import numpy as np

# Python list addition


list1 = [1, 2, 3]
list2 = [4, 5, 6]
# This will concatenate, not add
print(list1 + list2) # Output: [1, 2, 3, 4, 5, 6]

# NumPy array addition


arr1 = [Link]([1, 2, 3])
arr2 = [Link]([4, 5, 6])
print(arr1 + arr2) # Output: [5 7 9]

NumPy performs element-wise addition directly.


2. Creation of NumPy Arrays from List
The most common way to create arrays is by converting Python lists into NumPy arrays
using [Link]().

Example 1: 1-D Array from List


import numpy as np

list1 = [2, 4, 6, 8]
array1 = [Link](list1)
print(array1)

Output:

[2 4 6 8]

A one-dimensional array is created from a list.

Example 2: 2-D Array from Nested List


import numpy as np

list2 = [[1, 2], [3, 4]]


array2 = [Link](list2)
print(array2)

Output:

[[1 2]
[3 4]]

A two-dimensional array (matrix) is created from a nested list.

Example 3: Array with Mixed Data


import numpy as np

list3 = [1, 2.5, 3]


array3 = [Link](list3)
print(array3)
print([Link])

Output:

[1. 2.5 3. ]
float64
NumPy automatically converts all elements to a common data type (float64 here).

3. Key Points
 Use [Link](list) to convert lists into arrays.
 Arrays can be 1-D, 2-D, or higher dimensions.
 NumPy arrays are homogeneous (all elements have the same type).
 Arrays allow vectorized operations (fast mathematical calculations).

Questions with answers


 Q: Difference between Python list and NumPy array?
A: Lists can store mixed data types and are slower; NumPy arrays are homogeneous
and optimized for numerical operations.
 Q: How do you create a NumPy array from a list?
A: Use [Link](list_name).
 Q: What is the advantage of NumPy arrays?
A: Faster, memory-efficient, supports mathematical operations directly.

Practices
Array creation using built-in NumPy functions

1. [Link](),
2. [Link](),
3. [Link](),
4. [Link]())

You might also like