Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

16 ArraysinPython-JupyterNotebook

Uploaded by

Anjali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

16 ArraysinPython-JupyterNotebook

Uploaded by

Anjali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

13.06.2022 13:12 16.

Arrays in Python - Jupyter Notebook

Python Tutorial
Created by Mustafa Germec, PhD

16. Arrays in Python


Array is a container which can hold a fix number of items and these items should be of the same type.
Most of the data structures make use of arrays to implement their algorithms.
Lists can be used to form arrays.
Following are the important terms to understand the concept of Array.
Element: Each item stored in an array is called an element.
Index: Each location of an element in an array has a numerical index, which is used to identify the
element.

Array index begins with 0.


Each element in the array can be accessed with its index number.
The length of the array describes the capacity to store the elements.
Basic array operations are Traverse, Insertion, Deletion, Search, and Update.

Creating an array
You should import the module name 'array' as follows:

import array or from array import (*).


(*) means that it covers all features of the array.

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/16. Arrays in Py… 1/10


13.06.2022 13:12 16. Arrays in Python - Jupyter Notebook

In [4]:

1 # Import the module


2 import array as arr
3 from array import *

In [5]:

1 # To access more information regarding array, you can execute the following commands
2 help(arr)

Help on built-in module array:

NAME
array

DESCRIPTION
This module defines an object type which can efficiently represent
an array of basic values: characters, integers, floating point
numbers. Arrays are sequence types and behave very much like lists,
except that the type of objects stored in them is constrained.

CLASSES
builtins.object
array

ArrayType = class array(builtins.object)


| array(typecode [, initializer]) -> array
|
| Return a new array whose items are restricted by typecode, and
| initialized from the optional initializer value which must be a list

Type code
Arrays represent basic values and behave very much like lists, except the type of objects stored in them is
constrained.
The type is specified at object creation time by using a type code, which is a single character.
The following type codes are defined:

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/16. Arrays in Py… 2/10


13.06.2022 13:12 16. Arrays in Python - Jupyter Notebook

In [6]:

1 special_nums = arr.array('d', [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])


2 for i in special_nums:
3 print(i)

0.577
1.618
2.718
3.14
6.0
37.0
1729.0

Accessing

In [7]:

1 special_nums = arr.array('d', [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])


2 print(f'First element of numbers is {special_nums[0]}, called euler constant.')
3 print(f'Second element of numbers is {special_nums[1]}, called golden_ratio.')
4 print(f'Last element of numbers is {special_nums[-1]}, called Ramanujan-Hardy number.')

First element of numbers is 0.577, called euler constant.


Second element of numbers is 1.618, called golden_ratio.
Last element of numbers is 1729.0, called Ramanujan-Hardy number.

Changing or Updating

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/16. Arrays in Py… 3/10


13.06.2022 13:12 16. Arrays in Python - Jupyter Notebook

In [8]:

1 nums = arr.array('i', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34])


2
3 # Changing the first element of the array
4 nums[0] = 55
5 print(nums)
6
7 # Changing 2nd to 4th elements of the array
8 nums[1:4] =arr.array('i', [89, 144, 233, 377])
9 print(nums)

array('i', [55, 1, 1, 2, 3, 5, 8, 13, 21, 34])


array('i', [55, 89, 144, 233, 377, 3, 5, 8, 13, 21, 34])

Deleting

In [9]:

1 nums = arr.array('i', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34])


2
3 # Deleting the first element of the array
4 del nums[0]
5 print(nums)
6
7 # Deleting the 2nd to 4th elements of the array
8 del nums[1:4]
9 print(nums)

array('i', [1, 1, 2, 3, 5, 8, 13, 21, 34])


array('i', [1, 5, 8, 13, 21, 34])

Lenght of the array

In [10]:

1 special_nums = arr.array('d', [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])


2 print(f'The length of the array is {len(special_nums)}.')

The length of the array is 7.

Concatenation

In [11]:

1 special_nums = arr.array('d', [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])


2 fibonacci_nums = arr.array('d', [1, 1, 2, 3, 5, 8, 13, 21, 34])
3 special_fibonacci_nums = arr.array('d')
4 special_fibonacci_nums = special_nums + fibonacci_nums
5 print(f'The new array called special_fibonacci_nums is {special_fibonacci_nums}.')

The new array called special_fibonacci_nums is array('d', [0.577, 1.618, 2.718, 3.14, 6.0, 37.0, 1729.0, 1.
0, 1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 21.0, 34.0]).

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/16. Arrays in Py… 4/10


13.06.2022 13:12 16. Arrays in Python - Jupyter Notebook

Creating ID arrays

In [12]:

1 mult = 10
2 one_array = [1]*mult
3 print(one_array)

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

In [13]:

1 mult = 10
2 nums_array = [i for i in range(mult)]
3 print(nums_array)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Addition with the functions insert() and append()

In [14]:

1 # Using the function 'insert()'


2 fibonacci_nums = arr.array('i', [1, 1, 2, 3, 5, 8, 13, 21, 34])
3 print('Before any additon into fibonacci numbers')
4 for i in fibonacci_nums:
5 print(i, end = ' ')
6 print()
7 print('After an element additon into fibonacci numbers')
8 added_num = fibonacci_nums[-1] + fibonacci_nums[-2]
9 fibonacci_nums.insert(9, added_num)
10 for i in fibonacci_nums:
11 print(i, end = ' ')
12 print()
13 print('After an element additon into fibonacci numbers')
14 added_num = fibonacci_nums[-1] + fibonacci_nums[-2]
15 fibonacci_nums.insert(10, added_num)
16 for i in fibonacci_nums:
17 print(i, end = ' ')

Before any additon into fibonacci numbers


1 1 2 3 5 8 13 21 34
After an element additon into fibonacci numbers
1 1 2 3 5 8 13 21 34 55
After an element additon into fibonacci numbers
1 1 2 3 5 8 13 21 34 55 89

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/16. Arrays in Py… 5/10


13.06.2022 13:12 16. Arrays in Python - Jupyter Notebook

In [15]:

1 # Using the function 'append()'


2 fibonacci_nums = arr.array('i', [1, 1, 2, 3, 5, 8, 13, 21, 34])
3 print('Before any additon into fibonacci numbers')
4 for i in fibonacci_nums:
5 print(i, end = ' ')
6 print()
7 print('After an element additon into fibonacci numbers')
8 added_num = fibonacci_nums[-1] + fibonacci_nums[-2]
9 fibonacci_nums.append(added_num)
10 for i in fibonacci_nums:
11 print(i, end = ' ')
12 print()
13 print('After an element additon into fibonacci numbers')
14 added_num = fibonacci_nums[-1] + fibonacci_nums[-2]
15 fibonacci_nums.append(added_num)
16 for i in fibonacci_nums:
17 print(i, end = ' ')

Before any additon into fibonacci numbers


1 1 2 3 5 8 13 21 34
After an element additon into fibonacci numbers
1 1 2 3 5 8 13 21 34 55
After an element additon into fibonacci numbers
1 1 2 3 5 8 13 21 34 55 89

Removing with the function remove() and pop()

In [16]:

1 # Using the function 'remove()'


2 special_nums = arr.array('d', [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])
3 print('Before removing an element from the array')
4 for i in special_nums:
5 print(i, end = ' ')
6 print()
7 print('After removing an element from the array')
8 special_nums.remove(0.577)
9 for i in special_nums:
10 print(i, end=' ')
11 print()
12 print('After removing one more element from the array')
13 special_nums.remove(special_nums[0]) # We can make this using indexing
14 for i in special_nums:
15 print(i, end=' ')

Before removing an element from the array


0.577 1.618 2.718 3.14 6.0 37.0 1729.0
After removing an element from the array
1.618 2.718 3.14 6.0 37.0 1729.0
After removing one more element from the array
2.718 3.14 6.0 37.0 1729.0

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/16. Arrays in Py… 6/10


13.06.2022 13:12 16. Arrays in Python - Jupyter Notebook

In [17]:

1 # Using the function 'pop()'


2 # The function pop() removes the last element from the array
3 special_nums = arr.array('d', [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])
4 print('Before removing an element from the array')
5 for i in special_nums:
6 print(i, end = ' ')
7 print()
8 print('After removing last element from the array')
9 special_nums.pop()
10 for i in special_nums:
11 print(i, end=' ')
12 print()
13 print('After removing one more last element from the array')
14 special_nums.pop()
15 for i in special_nums:
16 print(i, end=' ')
17 print()
18 print('After removing one more element using index from the array')
19 special_nums.pop(3) # It deleted the pi number
20 for i in special_nums:
21 print(i, end=' ')
22 print()

Before removing an element from the array


0.577 1.618 2.718 3.14 6.0 37.0 1729.0
After removing last element from the array
0.577 1.618 2.718 3.14 6.0 37.0
After removing one more last element from the array
0.577 1.618 2.718 3.14 6.0
After removing one more element using index from the array
0.577 1.618 2.718 6.0

Slicing

In [18]:

1 special_nums = arr.array('d', [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])


2 sliced_special_nums = special_nums[1:5] # It returns between index 1 and index 4, not index 5.
3 print(sliced_special_nums)
4 # or using for loop
5 for i in sliced_special_nums:
6 print(i, end = " ")

array('d', [1.618, 2.718, 3.14, 6.0])


1.618 2.718 3.14 6.0

In [19]:

1 special_nums = arr.array('d', [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])


2 sliced_special_nums = special_nums[3:] # It returns index 3 and later.
3 print(sliced_special_nums)

array('d', [3.14, 6.0, 37.0, 1729.0])

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/16. Arrays in Py… 7/10


13.06.2022 13:12 16. Arrays in Python - Jupyter Notebook

In [20]:

1 special_nums = arr.array('d', [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])


2 sliced_special_nums = special_nums[:3] # It returns until index 2, not index 3.
3 print(sliced_special_nums)

array('d', [0.577, 1.618, 2.718])

In [21]:

1 special_nums = arr.array('d', [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])


2 sliced_special_nums = special_nums[:] # It returns all elements in the array
3 print(sliced_special_nums)

array('d', [0.577, 1.618, 2.718, 3.14, 6.0, 37.0, 1729.0])

In [22]:

1 special_nums = arr.array('d', [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])


2 sliced_special_nums = special_nums[::-1] # It reverses the array.
3 print(sliced_special_nums)

array('d', [1729.0, 37.0, 6.0, 3.14, 2.718, 1.618, 0.577])

Searching

In [23]:

1 # To make a search in an array, use the function index()


2 special_nums = arr.array('d', [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])
3 searched_item = special_nums.index(2.718)
4 print(f'The searched item euler number 2.718 is present at index {searched_item}.')
5 # printing with format
6 print('The searched item euler number {} is present at index {}.'.format(2.718, searched_item))

The searched item euler number 2.718 is present at index 2.


The searched item euler number 2.718 is present at index 2.

Copying

Copying using assignment

This process gives the same ID number.

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/16. Arrays in Py… 8/10


13.06.2022 13:12 16. Arrays in Python - Jupyter Notebook

In [24]:

1 special_nums = arr.array('d', [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])


2 copied_special_nums = special_nums
3 print(special_nums, 'with the ID number', id(special_nums))
4 print(copied_special_nums, 'with the ID number', id(copied_special_nums))
5
6 # Using for loop
7 for i in special_nums:
8 print(i, end= ' ')
9 print()
10 print(f'The ID number of the array special_nums is {id(special_nums)}.')
11 for i in copied_special_nums:
12 print(i, end= ' ')
13 print()
14 print(f'The ID number of the array copied_special_nums is {id(copied_special_nums)}.')

array('d', [0.577, 1.618, 2.718, 3.14, 6.0, 37.0, 1729.0]) with the ID number 2668250199472
array('d', [0.577, 1.618, 2.718, 3.14, 6.0, 37.0, 1729.0]) with the ID number 2668250199472
0.577 1.618 2.718 3.14 6.0 37.0 1729.0
The ID number of the array special_nums is 2668250199472.
0.577 1.618 2.718 3.14 6.0 37.0 1729.0
The ID number of the array copied_special_nums is 2668250199472.

Copying using view()

This process gives the different ID number.

In [25]:

1 # import numpy library


2 import numpy as np
3
4 # Copying the array
5 special_nums = np.array( [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])
6 copied_special_nums = special_nums.view()
7 print(special_nums, 'with the ID number', id(special_nums))
8 print(copied_special_nums, 'with the ID number', id(copied_special_nums))
9
10 #Using for loop
11 for i in special_nums:
12 print(i, end = ' ')
13 print()
14 for i in copied_special_nums:
15 print(i, end = ' ')
16

[5.770e-01 1.618e+00 2.718e+00 3.140e+00 6.000e+00 3.700e+01 1.729e+03] with the ID number 2668
248532144
[5.770e-01 1.618e+00 2.718e+00 3.140e+00 6.000e+00 3.700e+01 1.729e+03] with the ID number 2668
254732400
0.577 1.618 2.718 3.14 6.0 37.0 1729.0
0.577 1.618 2.718 3.14 6.0 37.0 1729.0

Copying using copy()

This process gives the different ID number.

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/16. Arrays in Py… 9/10


13.06.2022 13:12 16. Arrays in Python - Jupyter Notebook

In [26]:

1 # import numpy library


2 import numpy as np
3
4 # Copying the array
5 special_nums = np.array( [0.577, 1.618, 2.718, 3.14, 6, 37, 1729])
6 copied_special_nums = special_nums.copy()
7 print(special_nums, 'with the ID number', id(special_nums))
8 print(copied_special_nums, 'with the ID number', id(copied_special_nums))
9
10 #Using for loop
11 for i in special_nums:
12 print(i, end = ' ')
13 print()
14 for i in copied_special_nums:
15 print(i, end = ' ')
16

[5.770e-01 1.618e+00 2.718e+00 3.140e+00 6.000e+00 3.700e+01 1.729e+03] with the ID number 2668
254735376
[5.770e-01 1.618e+00 2.718e+00 3.140e+00 6.000e+00 3.700e+01 1.729e+03] with the ID number 2668
254736144
0.577 1.618 2.718 3.14 6.0 37.0 1729.0
0.577 1.618 2.718 3.14 6.0 37.0 1729.0

localhost:8888/notebooks/Desktop/PROGRAMMING/PYTHON_TUTORIAL/01. python_files_for_sharing/jupyter_notebook_files/16. Arrays in P… 10/10

You might also like