Skip to content

Instantly share code, notes, and snippets.

@arynas
Created March 18, 2021 12:20
Show Gist options
  • Save arynas/7b8272045d0ec05684d8fb0feb27c319 to your computer and use it in GitHub Desktop.
Save arynas/7b8272045d0ec05684d8fb0feb27c319 to your computer and use it in GitHub Desktop.
Array.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Array.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyM/6T6bNgZJF9xm5kGEOaEL",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/arynas/7b8272045d0ec05684d8fb0feb27c319/array.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "g84y9U0HtT_g"
},
"source": [
"# **One Dimensional Array**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "KcqDrO54tbUZ"
},
"source": [
"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. Following are the important terms to understand the concept of Array.\n",
"\n",
"* **Element** − Each item stored in an array is called an element.\n",
"* **Index** − Each location of an element in an array has a numerical index, which is used to identify the element."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dKDp7J08tt_u"
},
"source": [
"## Print an Array\n",
"The below code creates an array named array1"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "kzptSH_bo3WM",
"outputId": "70286672-d41a-4c2f-b093-aa0ba630aa5b"
},
"source": [
"from array import *\n",
"\n",
"array1 = array('i', [10,20,30,40,50]) # describe the use of this code!\n",
"\n",
"for x in array1: # describe the use of this code!\n",
" print(x)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"10\n",
"20\n",
"30\n",
"40\n",
"50\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jAJtMHRNt_oO"
},
"source": [
"## Accessing Array Element\n",
"We can access each element of an array using the index of the element. The below code shows how"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lfnS7jSLqNr3",
"outputId": "30ddf5de-66cf-403c-84b7-e56dbb22f786"
},
"source": [
"from array import *\n",
"\n",
"array1 = array('i', [10,20,30,40,50]) # describe the use of this code!\n",
"\n",
"print (array1[0]) # describe the use of this code!\n",
"\n",
"print (array1[2])\n",
"# try to print another index!"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"10\n",
"30\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_-AqkMJCuLER"
},
"source": [
"## Insertion Operation\n",
"Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array.\n",
"\n",
"Here, we add a data element at the middle of the array using the python in-built **insert()** method."
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "HLWZ67RGqQ4h",
"outputId": "d1ab3b80-2a88-43fe-b730-a621032c62db"
},
"source": [
"from array import *\n",
"\n",
"array1 = array('i', [10,20,30,40,50]) # describe the use of this code!\n",
"\n",
"array1.insert(1,60) # describe the use of this code!\n",
"\n",
"for x in array1:\n",
" print(x)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"10\n",
"60\n",
"20\n",
"30\n",
"40\n",
"50\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "p-LbwPfpuSVk"
},
"source": [
"## Deletion Operation\n",
"Deletion refers to removing an existing element from the array and re-organizing all elements of an array.\n",
"\n",
"Here, we remove a data element at the middle of the array using the python in-built **remove()** method."
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "XdRWadkgqWqe",
"outputId": "fb03901a-cb43-4069-c19a-adb8aab42e23"
},
"source": [
"from array import *\n",
"\n",
"array1 = array('i', [10,20,30,40,50]) # describe the use of this code!\n",
"\n",
"array1.remove(40) # describe the use of this code!\n",
"\n",
"for x in array1:\n",
" print(x)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"10\n",
"20\n",
"30\n",
"50\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_F9u1y1IuoML"
},
"source": [
"## Search Operation\n",
"You can perform a search for an array element based on its value or its index.\n",
"\n",
"Here, we search a data element using the python in-built **index()** method."
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "MveTdYd2qbea",
"outputId": "441f587a-329e-40ce-d638-ccc2d8769fff"
},
"source": [
"from array import *\n",
"\n",
"array1 = array('i', [10,20,30,40,50]) # describe the use of this code!\n",
"\n",
"print (array1.index(40)) # describe the use of this code!"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"3\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "d5fVPGrNuvOL"
},
"source": [
"## Update Operation\n",
"Update operation refers to updating an existing element from the array at a given index.\n",
"\n",
"Here, we simply reassign a new value to the desired index we want to update."
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "NRIpGLcTqd7V",
"outputId": "a0a84a71-ea2d-4765-9f44-89a824c2368c"
},
"source": [
"from array import *\n",
"\n",
"array1 = array('i', [10,20,30,40,50]) # describe the use of this code!\n",
"\n",
"array1[2] = 80 # describe the use of this code!\n",
"\n",
"for x in array1:\n",
" print(x)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"10\n",
"20\n",
"80\n",
"40\n",
"50\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "RzrOQKKHu8fR"
},
"source": [
"# **Two Dimensional Array**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ihktGx21vaog"
},
"source": [
"## Accessing Values in a Two Dimensional Array\n",
"The data elements in two dimesnional arrays can be accessed using two indices. One index referring to the main or parent array and another index referring to the position of the data element in the inner array. If we mention only one index then the entire inner array is printed for that index position. The example below illustrates how it works."
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OpMUn1ENqj7H",
"outputId": "cb222326-cafb-479b-e038-6923fbb5300e"
},
"source": [
"from array import *\n",
"\n",
"array2 = [[11, 12, 5, 2], [15, 6,10, 11], [10, 8, 12, 5], [12,15,8,6]]\n",
"\n",
"print(array2[0]) # describe the use of this code!\n",
"\n",
"print(array2[1][2]) # describe the use of this code!"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"[11, 12, 5, 2]\n",
"10\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "x1T_JIeIviJg"
},
"source": [
"To print out the entire two dimensional array we can use python for loop as shown below. We use end of line to print out the values in different rows."
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Jy2e8hnkqmOY",
"outputId": "d6148d59-2772-4358-ca03-fd494fb68927"
},
"source": [
"from array import *\n",
"\n",
"array2 = [[11, 12, 5, 2], [15, 6,10, 11], [10, 8, 12, 5], [12,15,8,6]]\n",
"\n",
"for r in array2: # describe the use of this code!\n",
" for c in r: # describe the use of this code!\n",
" print(c,end = \" \")\n",
" print()"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"11 12 5 2 \n",
"15 6 10 11 \n",
"10 8 12 5 \n",
"12 15 8 6 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9M_lRmpIvvKm"
},
"source": [
"## Inserting Values in Two Dimensional Array\n",
"We can insert new data elements at specific position by using the **insert() **method and specifying the index.\n",
"\n",
"In the below example a new data element is inserted at index position 2."
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "DYlxzUyBqtlI",
"outputId": "8fdbe8e9-ce84-41bc-e287-b06278984097"
},
"source": [
"from array import *\n",
"\n",
"array2 = [[11, 12, 5, 2], [15, 6,10, 11], [10, 8, 12, 5], [12,15,8,6]]\n",
"\n",
"array2.insert(2, [0,5,11,13,6]) # describe the use of this code!\n",
"\n",
"for r in array2:\n",
" for c in r:\n",
" print(c,end = \" \")\n",
" print()"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"11 12 5 2 \n",
"15 6 10 11 \n",
"0 5 11 13 6 \n",
"10 8 12 5 \n",
"12 15 8 6 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vMkmvH7rwA26"
},
"source": [
"## Updating Values in Two Dimensional Array\n",
"We can update the entire inner array or some specific data elements of the inner array by reassigning the values using the array index."
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "R0j2SL9wqzvm",
"outputId": "8a18c15b-5990-4fd7-d21f-44eaf227a242"
},
"source": [
"from array import *\n",
"\n",
"array2 = [[11, 12, 5, 2], [15, 6,10, 11], [10, 8, 12, 5], [12,15,8,6]]\n",
"\n",
"array2[2] = [11,9] # describe the use of this code!\n",
"array2[0][3] = 7 # describe the use of this code!\n",
"for r in array2:\n",
" for c in r:\n",
" print(c,end = \" \")\n",
" print()"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"11 12 5 7 \n",
"15 6 10 11 \n",
"11 9 \n",
"12 15 8 6 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9r5reC7LwN2Y"
},
"source": [
"## Deleting the Values in Two Dimensional Array"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "3FYwu_QZq-gJ",
"outputId": "b0c8a201-1857-405b-ee19-aafc48e337d1"
},
"source": [
"from array import *\n",
"\n",
"array2 = [[11, 12, 5, 2], [15, 6,10, 11], [10, 8, 12, 5], [12,15,8,6]]\n",
"del array2[2][3] # describe the use of this code!\n",
"\n",
"for r in array2:\n",
" for c in r:\n",
" print(c,end = \" \")\n",
" print()\n"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"11 12 5 2 \n",
"15 6 10 11 \n",
"10 8 12 \n",
"12 15 8 6 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ul7_Qx28wtfp"
},
"source": [
"# **Three Dimensional Array**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Dg0DvCDfwwpV"
},
"source": [
"## Print an Array"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "G1Cxo5-QsHAD",
"outputId": "a7e8c68e-3646-4600-9143-b2a6387c6f33"
},
"source": [
"from array import *\n",
"array3 = [[[11, 12, 5, 2], [15, 6,10,11], [2,5,9,11]], [[10, 8, 12, 5], [12,15,8,6],[8, 14, 10, 9]]]\n",
"\n",
"for x in array3: # describe the use of this code!\n",
" print(x)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"[[11, 12, 5, 2], [15, 6, 10, 11], [2, 5, 9, 11]]\n",
"[[10, 8, 12, 5], [12, 15, 8, 6], [8, 14, 10, 9]]\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SJ1WXv8Q0zQT"
},
"source": [
"## Accessing Values in a Three Dimensional Array"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "gJFYjzhn0ear",
"outputId": "7af95f00-8f74-44e0-fda7-ac7bafb8630f"
},
"source": [
"for r in array3: # describe the use of this code!\n",
" for c in r: # describe the use of this code!\n",
" for k in c:\n",
" print(k,end = \" \")\n",
" print()"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"11 12 5 2 15 6 10 11 2 5 9 11 \n",
"10 8 12 5 12 15 8 6 8 14 10 9 \n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "DH_2y_ER0LKh",
"outputId": "218b6be8-ecb8-4138-cb6e-b3f9bf382685"
},
"source": [
"print(array3[0]) # describe the use of this code!\n",
"\n",
"print(array3[1][2]) # describe the use of this code!\n",
"\n",
"print(array3[0][1][2]) # describe the use of this code!"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"[[11, 12, 5, 2], [15, 6, 10, 11], [2, 5, 9, 11]]\n",
"[8, 14, 10, 9]\n",
"10\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "oHQcQYVV2YtT"
},
"source": [
"# **Exercises**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fhM_DzQN4Bg5"
},
"source": [
"## #1\n",
"Write a Python program to create an array of 5 integers and display the array items. Access individual element through indexes. \n",
"\n",
"**Sample Output:**\n",
"\n",
"2\n",
"\n",
"3\n",
"\n",
"6\n",
"\n",
"7\n",
"\n",
"9\n",
"\n",
"Access first three items individually\n",
"\n",
"2\n",
"\n",
"3\n",
"\n",
"6"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2bjCGNOM4zPK"
},
"source": [
"## #2\n",
"Write a Python program to append a new item to the end of the array. \n",
"\n",
"**Sample Output:**\n",
"\n",
"Original array: array('i', [2, 3, 6, 7, 9])\n",
"\n",
"Append 11 at the end of the array:\n",
"\n",
"New array: array('i', [2, 3, 6, 7, 9, 11])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5XMa9J7A5WJ2"
},
"source": [
"## #3\n",
"Write a Python program to reverse the order of the items in the array. \n",
"\n",
"**Sample Output**\n",
"\n",
"Original array: array('i', [2, 3, 6, 3, 7, 1, 9, 3])\n",
"\n",
"Reverse the order of the items:\n",
"\n",
"array('i', [3, 9, 1, 7, 3, 6, 3, 2])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_OVe8pe06Asd"
},
"source": [
"## #4\n",
"\n",
"Write a Python program to insert a new item before the second element in an existing array.\n",
"\n",
"**Sample Output:**\n",
"\n",
"Original array: array('i', [2, 3, 6, 7, 9])\n",
"\n",
"Insert new value 4 before 3:\n",
"\n",
"New array: array('i', [2, 4, 3, 6, 7, 9])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6OqRlvJ36TpR"
},
"source": [
"## #5\n",
"Write a Python program to remove the first occurrence of a specified element from an array.\n",
"\n",
"**Sample Output:**\n",
"\n",
"Original array: array('i', [2, 3, 6, 3, 7, 1, 9, 3])\n",
"\n",
"Remove the first occurrence of 3 from the said array:\n",
"\n",
"New array: array('i', [2, 6, 3, 7, 1, 9, 3])"
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment