0% found this document useful (0 votes)
8 views17 pages

Python Array Basics and Methods

Arrays in Python can be created using the array module. Arrays allow storing basic data types efficiently and have methods like append, insert, pop and reverse. Multidimensional arrays can be created using NumPy with attributes like shape, size and dtype.

Uploaded by

ppawah04
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)
8 views17 pages

Python Array Basics and Methods

Arrays in Python can be created using the array module. Arrays allow storing basic data types efficiently and have methods like append, insert, pop and reverse. Multidimensional arrays can be created using NumPy with attributes like shape, size and dtype.

Uploaded by

ppawah04
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

Array in Python

Array
syntax
Arrayname = array(typecode, [ n1,n2.,n3, elements]
Typecode
b= signed integer 1 byte
B= unsigned integer 1 byte
i= signed integer 2 byte
I= unsigned integer 2 byte
l= signed integer 4 byte
L= unsigned integer 4 byte
f= Floating point 4 byte
d= double precision floating point 8 byte
u= unicode integer 2 byte
Creating array (three ways)
Import array module
a= [Link](“i”, [1,2,3,4])
Or
Import array as arr
a = [Link](“i”, [1,2,3,4])
Or
From array import *
a = [Link](“i”, [1,2,3,4])
Array Methods

[Link]() Method
Like the sequence types, the array class also supports the reverse()
method which rearranges the elements in reverse order.
[Link]() Method

The count() method returns the number of times a given element occurs in the
array.
[Link]()
The index() method in array class finds the position of first occurrence of a given
element in the array.
[Link]()
The fromlist() method appends items from a Python list to the array object.
[Link]()
The tofile() method in array class writes all items in the array to the file object f.

After running the above code, a file named as "[Link]" will be created in the current
directory.
[Link]()
The fromfile() method reads a binary file and appends specified number of items
to the array object.

(rb) Read and


binary
Other methods
[Link](x) : add element x at the end of the array

[Link](x) : append x at the end of the array. X can be be another array or


iterable object

[Link](i,x) insert x at position i

[Link](x) :remove item ‘x’ from array and return it

[Link]() : remove lat element from array

[Link](x) : remove the first occurrence of x in the array


two-dimensional array
Use array method of numpy

print("\n")
arr2 = [Link]([[1,2,3,4], [5,6,7,8]])
print(arr2)
print(type(arr2))
Multidimensional array
Multidimensional array
Multidimensional array
array
Aliasing array : if “a” and “b” are two arrays then a = b means both pointing to
same array. When one changes other also

view() : if “a” and “b” are two array then b = [Link]() creates mirror images of
array called shallow copy. When one changes other also

copy() : if “a” and “b” are two array then b = [Link]() creates copy of array where
both array are independent. When one changes other do not. This is called deep
copy.
Attributes of array
ndim: represents number of dimensions or axes.

shape() : displays number of elements in one dimension array or display row and
column in two dimension array

Size : gives total number of elements in array

Dtype : gives datatype of elements in an array

Reshape () : used to change row to column and column to row

flatten() : collapse multidimensional array into one dimension


End

Common questions

Powered by AI

Type codes in Python arrays specify the type of elements the array will hold and determine the amount of memory used for storing these elements. For example, 'i' denotes a signed integer of 2 bytes, while 'f' indicates a floating point number of 4 bytes. This influences data storage by determining the size and layout of data in memory, impacting both storage efficiency and the range of values that can be stored .

The 'reverse' method allows the elements of an array to be rearranged in reverse order, which can be useful for reversing data sequences without manually swapping elements. The 'count' method returns the frequency of a specified element within the array, aiding in data analysis by allowing quick computation of how often a value appears .

Numpy provides robust capabilities for handling two-dimensional arrays in Python, allowing users to efficiently create, manipulate, and perform operations on matrices. For example, numpy can create 2D arrays using np.array, and provides various operations like reshaping and flattening. This enhances Python's array manipulation capabilities by supporting efficient computation and complex mathematical functions .

The 'append' method adds a single element to the end of an array, whereas 'extend' allows multiple elements from another iterable to be added. This difference means 'append' is suited for incremental data addition, while 'extend' is more efficient for bulk additions, affecting whether a strategy should involve piecemeal or batch processing .

The array module allows specification of data types using type codes, supporting elements like integers, floats, and doubles. This flexibility enables tailored memory usage and computational efficiency as different data types allocate memory space accordingly, optimizing performance for specific applications, such as high-precision computations needing 'double' type or compact storage with 'b' .

The 'reshape' attribute allows the transformation of an array's dimensions, enabling the conversion of rows to columns and vice versa. This is beneficial for adjusting data layouts to align with specific analytical requirements or algorithms, facilitating complex data manipulations without altering the underlying data .

The 'tofile' method writes all items of an array to a file object, making it ideal for saving array data to a file for persistent storage. Conversely, the 'fromfile' method reads a specified number of items from a binary file into the array, enabling data retrieval from a stored file. These methods complement each other for use cases involving data persistence and retrieval .

Array aliasing in Python occurs when two variables reference the same array object, meaning changes via one reference affect the other. This can lead to unintended side effects if both references are used simultaneously, as operations via one alias will directly affect the data viewable by the other alias .

The 'flatten' method reduces a multidimensional array into a one-dimensional array, making it useful for linear data processing operations such as concatenation or feeding datasets to machine learning algorithms that require flat input. This method simplifies the handling of complex datasets by standardizing their format .

A shallow copy, created with the 'view' method, generates a new array object that mirrors the original's data, meaning changes to the data in one reflect in the other. A deep copy, achieved with the 'copy' method, results in an independent copy of the array data, allowing changes to one without affecting the other. Shallow copies are suitable for read-only operations where memory conservation is important, while deep copies are preferable for independent data manipulations .

You might also like