Python Array With Examples

Python Array With Examples

Array in Python

Array in Python

  • Array is the collection of items having same data type with contiguous memory location.
  • There is not a built-in array in Python, but we used other structure to store data like a list.
  • To use array in python, we have to install 3rd party library like numpy.


List used as Array

  • List is the mutable type of data type in Python that store different data type of data in a square bracket. e.g [“Rushikesh”, “Sagar”, “Sayali”, 12323, 902.323]
  • Different operations can be performed on list as pop, append, reverse, len, remove etc.


Array module Installation

NumPy installation

Just type in your terminal like cmd, PowerShell or gitbash to install numpy.

pip install numpy

pip is the package manager and package installer in python.


Use of NumPy

Import numpy

Creating array with numpy

Accessing array element using index number (Positive and Negative Index number)

Arithmetic operations on Array

Looping array elements

Reversing, Removing, Finding length of array elements etc


What is Array?

Array is the data structure which store the multiple items, that items is known as elements.

Items will have same data type

Items will have consecutive memory location

Items will have unique index number starting 0 to n-1 where n means total number of items

Array may be one dimensional or two dimensional (multidimensional)


Different Terminologies Of Array

Name of array: It show the array name, when we declare the array, then that variable is the name of array

Array Element: Items or data in the array is the array element

Index: Each array element have its own unique number, which help in accessing that element

Size: Total number of element is the size of array

Memory location: Each element are stored in the computer memory device which have their own specific and unique memory address or memory location


Array Programming Example:

Example: Write a Python Program To Create a Array Using Numpy To Get 5 Number From User, Store In a Array And Find Product Of All The Number As Stored In a Array.


from numpy import *

arr = array([])

for i in range(5):

??? v = input("Element:? ")

??? arr = append(arr, v)

print(arr)





要查看或添加评论,请登录

社区洞察

其他会员也浏览了