0% found this document useful (0 votes)
120 views13 pages

Python - Arrays

Arrays in Python are not the same as arrays in other languages like Java and C++. In Python, lists are more commonly used than arrays. Arrays must be imported from the array module, while lists are a built-in type. Arrays take up less memory than lists when holding elements of the same type, and are more efficient for math operations when used with NumPy. The array class and its methods like append(), insert(), and pop() allow adding, modifying, and removing elements from an array, making arrays mutable like lists.

Uploaded by

Quan Zhou
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
120 views13 pages

Python - Arrays

Arrays in Python are not the same as arrays in other languages like Java and C++. In Python, lists are more commonly used than arrays. Arrays must be imported from the array module, while lists are a built-in type. Arrays take up less memory than lists when holding elements of the same type, and are more efficient for math operations when used with NumPy. The array class and its methods like append(), insert(), and pop() allow adding, modifying, and removing elements from an array, making arrays mutable like lists.

Uploaded by

Quan Zhou
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 13

Arrays

Hint: When you’re stuck, always refer to the official Python documentation
Arrays in other languages != arrays in Python
In programming, an array is a collection of elements of the same type.
Arrays are popular in most programming languages like Java, C/C++, JavaScript
and so on.
However, in Python, they are not that common. When people talk about
Python arrays, more often than not, they are talking about Python lists.

It is important to know the difference between a list and an array so you know
the correct tools for your own software.
Why do we use array?
Reasons:

- In simple sequences that hold the same data type such as [1, 2, 3, 4, 5],
arrays will take up less memory space than lists. BECAUSE it is of the
same data type, sizeof(one object) * length bytes of memory.

- Intro to numpy.array #hopefully we can get to


- Numpy are efficient in doing maths.
Array is not a built in function
But what is?
Python has a total of 69 built in function (currently) that can be used as default,
these are: [diagram]. And array is not one of them. Which means that you are not
able to declare a variable to array like this array(1,2,3) without importing the
array module.

Python modules are .py files that consist of Python code. Any Python file can be
referenced as a module.

Some modules are available through the Python Standard Library and are therefore
installed with your Python installation. e.g. random, array
Others, you can install elsewhere and you can even create modules for yourself!
Built-in Functions

abs() delattr() hash() memoryview() set()

all() dict() help() min() setattr()

any() dir() hex() next() slice()

ascii() divmod() id() object() sorted()

bin() enumerate() input() oct() staticmethod()

bool() eval() int() open() str()

breakpoint() exec() isinstance() ord() sum()

bytearray() filter() issubclass() pow() super()

bytes() float() iter() print() tuple()

callable() format() len() property() type()

chr() frozenset() list() range() vars()

classmethod() getattr() locals() repr() zip()

compile() globals() map() reversed() __import__()

complex() hasattr() max() round()


How to import

You have now imported the class of array.


*In simple terms, class is what programmers use to keep related things together.
More details on class I added a link to the shared document if you want to learn
Syntax of array

x is the name of the array that you name


arr is the name given at import for the class of array
.array means that you are accessing the attribute <array> declared inside the class specified before the
dot which in this case, is the class <array>
Typecode

A typecode restricts the data type of this array.


Initializer
Initializer value is optional but it must be a list. It is where the values in your
arrays are declared initially.
Can someone interpret this code for us?

Test it out yourself at home.


Output:
Arrays are mutable (note: write example code output on board)

array.append(x)

Append a new item with value x to the end of the array.

array.count(x)

Return the number of occurrences of x in the array.

array.insert(i, x)

Insert a new item with value x in the array before position i. Negative values are treated as being relative
to the end of the array.
array.pop([i])

Removes the item with the index i from the array and returns it. The optional argument defaults to -1, so
that by default the last item is removed and returned.

array.remove(x)
Remove the first occurrence of x from the array.

array.reverse()
Reverse the order of the items in the array.

array.fromlist(list)
Append items from the list. This is equivalent to for x in list: a.append(x) except that if there is
a type error, the array is unchanged.

You might also like