arraycreation.pptx
Array creation
1• To create a one-dimensional NumPy array, we can simply pass a Python list to the array() method
Example 3
( )
( )
• We can easily make out the difference between a list and an array
representation in Python. In list, elements are separated by a comma,
whereas an array is like a collection of elements without a comma
separator in between.
import numpy as np
lst = [1,2, 3,4]
a=np.array (lst)
print(lst)
print (a)
a
[1, 2, 3, 4]
[1 2 3 4]
array([1, 2, 3, 4])
2
fromstring() We can also create one-dimensional array from String.
[1 2 10 12]
Creation of 2D NumPy Array
• multiple lists can be used to create 2D arrays. To create a two-dimensional array, you can pass a list of lists to
the array method as shown below:
Dimension
NumPy Arrays provides the ndim attribute that returns an integer that
tells us how many dimensions the array have
import numpy as np
a = np.array(42)
b = np.array([1,2,3,4,5])
c = np.array([[1,2,3], [4,5,6]])
print("Dimension of array a is:", a.ndim)
print("Dimension of array b is:",b.ndim)
print("Dimension of array c is:",c.ndim)
Access Array Elements
• Array element can be accessed by referring to its index
number.
• The indexes in NumPy arrays start with 0, meaning that the
first element has index 0, and the second has index 1 etc.
Get third and fourth
elements from the array and
add them.
import numpy as np
arr = np.array([1,2,3,4])
print("The first element is :", arr[0])
print("The 2nd element is :",arr[1])
print("Sum of 3rd and 4th elements are :",arr[2] + arr[3])
• To access elements from 2-D arrays we can use comma
separated integers representing the dimension and the
index of the element.
import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('2nd element on 1st dim: ', arr[0, 1])#Access the 2nd element on 1st dim:
print('5th element on 2nd dim: ', arr[1, 4])#Access the 5th element on 2nd dim:
print('Last element from 2nd dim: ', arr[1, -1])#Use negative indexing to access an array from the end.
Ouput: 10
Example
arrayname [ rownum, columnnum ]
Output: NumPy arrays have an attribute called shape that returns a tuple with each
index having the number of corresponding elements.
import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.shape)
Output: (2, 4), which means that the array has 2 dimensions,
and each dimension has 4 elements.
Difference between NumPy Array and Python Lists
• NumPy is an alternative for lists in Python as it holds less
memory, has faster processing, and is more convenient to use.
•
• The difference between the elements is that the NumPy array
has to be homogenous. (All elements should be of the same
type)
• Arrays in NumPy are more compact when we compare it to
lists.
• NumPy arrays are stored at one continuous place in memory
• NumPy array supports vectorised operation, i.e., you need to pertorm any
function on every /element of a sequence one by one which is not supported
in Python lists.
Summary
arraycreation.pptx
arraycreation.pptx

More Related Content

PPTX
NUMPY-2.pptx
PPTX
object oriented programing in python and pip
PDF
Essential numpy before you start your Machine Learning journey in python.pdf
PPTX
NUMPY [Autosaved] .pptx
PPTX
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
PPTX
numpydocococ34554367827839271966666.pptx
PPTX
NumPy.pptx
PPTX
Numpy in python, Array operations using numpy and so on
NUMPY-2.pptx
object oriented programing in python and pip
Essential numpy before you start your Machine Learning journey in python.pdf
NUMPY [Autosaved] .pptx
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
numpydocococ34554367827839271966666.pptx
NumPy.pptx
Numpy in python, Array operations using numpy and so on

Similar to arraycreation.pptx (20)

PDF
Concept of Data science and Numpy concept
PPTX
ARRAY OPERATIONS.pptx
PPTX
1.NumPy is a Python library used for wor
PDF
Unit-5-Part1 Array in Python programming.pdf
PDF
Python programming : Arrays
PDF
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
PPTX
NumPy.pptx
PDF
Arrays in python
PPTX
arrays.pptx
PDF
CE344L-200365-Lab2.pdf
DOCX
Numpy in Python.docx
PPTX
Chapter 5-Numpy-Pandas.pptx python programming
PPTX
Data Preprocessing Introduction for Machine Learning
PPTX
NUMPY LIBRARY study materials PPT 2.pptx
PDF
PPTX
Numpy_Pandas_for beginners_________.pptx
PPTX
ACP-arrays.pptx
PPT
CAP776Numpy.ppt
PPT
CAP776Numpy (2).ppt
PDF
Numpy
Concept of Data science and Numpy concept
ARRAY OPERATIONS.pptx
1.NumPy is a Python library used for wor
Unit-5-Part1 Array in Python programming.pdf
Python programming : Arrays
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
NumPy.pptx
Arrays in python
arrays.pptx
CE344L-200365-Lab2.pdf
Numpy in Python.docx
Chapter 5-Numpy-Pandas.pptx python programming
Data Preprocessing Introduction for Machine Learning
NUMPY LIBRARY study materials PPT 2.pptx
Numpy_Pandas_for beginners_________.pptx
ACP-arrays.pptx
CAP776Numpy.ppt
CAP776Numpy (2).ppt
Numpy
Ad

Recently uploaded (20)

PDF
fundamentals-of-heat-and-mass-transfer-6th-edition_incropera.pdf
PDF
Compact First Student's Book Cambridge Official
PDF
Lecture on Viruses: Structure, Classification, Replication, Effects on Cells,...
PDF
Laparoscopic Colorectal Surgery at WLH Hospital
PDF
Health aspects of bilberry: A review on its general benefits
PDF
PUBH1000 - Module 6: Global Health Tute Slides
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PPTX
ACFE CERTIFICATION TRAINING ON LAW.pptx
PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PPTX
PLASMA AND ITS CONSTITUENTS 123.pptx
PPTX
Climate Change and Its Global Impact.pptx
PDF
Nurlina - Urban Planner Portfolio (english ver)
PDF
FYJC - Chemistry textbook - standard 11.
PDF
Everyday Spelling and Grammar by Kathi Wyldeck
PDF
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
PDF
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
PDF
Fun with Grammar (Communicative Activities for the Azar Grammar Series)
PPTX
Integrated Management of Neonatal and Childhood Illnesses (IMNCI) – Unit IV |...
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PPTX
2025 High Blood Pressure Guideline Slide Set.pptx
fundamentals-of-heat-and-mass-transfer-6th-edition_incropera.pdf
Compact First Student's Book Cambridge Official
Lecture on Viruses: Structure, Classification, Replication, Effects on Cells,...
Laparoscopic Colorectal Surgery at WLH Hospital
Health aspects of bilberry: A review on its general benefits
PUBH1000 - Module 6: Global Health Tute Slides
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
ACFE CERTIFICATION TRAINING ON LAW.pptx
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PLASMA AND ITS CONSTITUENTS 123.pptx
Climate Change and Its Global Impact.pptx
Nurlina - Urban Planner Portfolio (english ver)
FYJC - Chemistry textbook - standard 11.
Everyday Spelling and Grammar by Kathi Wyldeck
Myanmar Dental Journal, The Journal of the Myanmar Dental Association (2013).pdf
Horaris_Grups_25-26_Definitiu_15_07_25.pdf
Fun with Grammar (Communicative Activities for the Azar Grammar Series)
Integrated Management of Neonatal and Childhood Illnesses (IMNCI) – Unit IV |...
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
2025 High Blood Pressure Guideline Slide Set.pptx
Ad

arraycreation.pptx

  • 2. Array creation 1• To create a one-dimensional NumPy array, we can simply pass a Python list to the array() method Example 3 ( ) ( )
  • 3. • We can easily make out the difference between a list and an array representation in Python. In list, elements are separated by a comma, whereas an array is like a collection of elements without a comma separator in between. import numpy as np lst = [1,2, 3,4] a=np.array (lst) print(lst) print (a) a [1, 2, 3, 4] [1 2 3 4] array([1, 2, 3, 4])
  • 4. 2 fromstring() We can also create one-dimensional array from String. [1 2 10 12]
  • 5. Creation of 2D NumPy Array • multiple lists can be used to create 2D arrays. To create a two-dimensional array, you can pass a list of lists to the array method as shown below:
  • 6. Dimension NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have import numpy as np a = np.array(42) b = np.array([1,2,3,4,5]) c = np.array([[1,2,3], [4,5,6]]) print("Dimension of array a is:", a.ndim) print("Dimension of array b is:",b.ndim) print("Dimension of array c is:",c.ndim)
  • 7. Access Array Elements • Array element can be accessed by referring to its index number. • The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc. Get third and fourth elements from the array and add them. import numpy as np arr = np.array([1,2,3,4]) print("The first element is :", arr[0]) print("The 2nd element is :",arr[1]) print("Sum of 3rd and 4th elements are :",arr[2] + arr[3])
  • 8. • To access elements from 2-D arrays we can use comma separated integers representing the dimension and the index of the element. import numpy as np arr = np.array([[1,2,3,4,5], [6,7,8,9,10]]) print('2nd element on 1st dim: ', arr[0, 1])#Access the 2nd element on 1st dim: print('5th element on 2nd dim: ', arr[1, 4])#Access the 5th element on 2nd dim: print('Last element from 2nd dim: ', arr[1, -1])#Use negative indexing to access an array from the end. Ouput: 10 Example arrayname [ rownum, columnnum ]
  • 9. Output: NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elements. import numpy as np arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) print(arr.shape) Output: (2, 4), which means that the array has 2 dimensions, and each dimension has 4 elements.
  • 10. Difference between NumPy Array and Python Lists • NumPy is an alternative for lists in Python as it holds less memory, has faster processing, and is more convenient to use. • • The difference between the elements is that the NumPy array has to be homogenous. (All elements should be of the same type) • Arrays in NumPy are more compact when we compare it to lists. • NumPy arrays are stored at one continuous place in memory • NumPy array supports vectorised operation, i.e., you need to pertorm any function on every /element of a sequence one by one which is not supported in Python lists.