diff --git a/Python_Lists_&_Tuples.ipynb b/Python_Lists_&_Tuples.ipynb new file mode 100644 index 0000000..00949db --- /dev/null +++ b/Python_Lists_&_Tuples.ipynb @@ -0,0 +1,420 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Lists and Tuples in Python" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Lists and Tuples are the most versatile data-types used widely in Python environment. Lets dive into them one by one" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##Python Lists\n", + "A list can be defined as a collection of elements, similar to an array but not homogeneous." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['anish', 'debayan', 'richard', 'alexis']\n" + ] + } + ], + "source": [ + "a = ['anish', 'debayan', 'richard', 'alexis']\n", + "print(a) #Simple way to print a list" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b = ['debayan', 'alexis', 'richard', 'anish']\n", + "a==b #Lists are ordered so the lists are not the same in this case" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['hello', 123, 'Richard', True, 5.675]\n" + ] + } + ], + "source": [ + "a = ['hello', 123, 'Richard', True, 5.675] #List can contain non-homogenous objects\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[, , , ]\n" + ] + } + ], + "source": [ + "def foo():\n", + " pass\n", + "\n", + "import math\n", + "\n", + "a = [int, len, foo, math] #List can even hold complex objects\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "debayan\n", + "anish\n", + "richard\n", + "alexis\n", + "['debayan', 'alexis', 'richard', 'anish']\n", + "['richard', 'anish']\n", + "['debayan', 'alexis', 'richard']\n", + "['richard']\n" + ] + } + ], + "source": [ + "#Indexing of the List\n", + "a = ['debayan', 'alexis', 'richard', 'anish']\n", + "\n", + "#Printing single element\n", + "print(a[0])\n", + "print(a[3])\n", + "print(a[-2])\n", + "print(a[-3])\n", + "\n", + "#Printing multiple elements\n", + "print(a[:])\n", + "print(a[2:])\n", + "print(a[:3])\n", + "print(a[2:3])" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['debayan', 'alexis', 'richard', 'anish', 'harshit', 'umang']\n" + ] + } + ], + "source": [ + "#Appending to list\n", + "a=a+['harshit', 'umang']\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['foo', 'bar', 'baz', 'd', 'o', 'r', 'm']\n" + ] + } + ], + "source": [ + "#Warning\n", + "a = ['foo', 'bar', 'baz']\n", + "a += 'dorm'\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['debayan', 'alexis', 'richard', 'anish', 'harshit', 'umang', 'debayan', 'alexis', 'richard', 'anish', 'harshit', 'umang']\n" + ] + } + ], + "source": [ + "#Multiplying entries\n", + "print(a*2) #The list now contains the elements twice but in the original order" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "6\n", + "9\n", + "1\n" + ] + } + ], + "source": [ + "#Few basic properties\n", + "a = [1, 4, 6, 8, 2, 9]\n", + "print(len(a))\n", + "print(max(a))\n", + "print(min(a))" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "[4, 5]\n" + ] + } + ], + "source": [ + "#Lists can be nested\n", + "b = [1, 2, [4, 5], 6, [3], 9]\n", + "print(b[0])\n", + "print(b[2])" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 6, 2, 9]\n" + ] + } + ], + "source": [ + "#Deleting from a list\n", + "del a[3]\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 10, 11, 9]\n" + ] + } + ], + "source": [ + "#Lists are modifiable\n", + "a[2:4] = [10, 11]\n", + "print(a)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, [5, 6], 3]\n" + ] + } + ], + "source": [ + "a = [1, 2, 3]\n", + "a[1] = [5, 6]\n", + "print(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##Tuples\n", + "Python Tuples are similar to Lists in all respect, except in the following two conditions:\n", + "1. They are defined within closing parenthesis '('')'\n", + "2. They are immutable" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('anish', 'debayan', 'subha', 'harshit')\n", + "anish\n", + "('subha', 'harshit')\n" + ] + } + ], + "source": [ + "tup = ('anish', 'debayan', 'subha', 'harshit')\n", + "print(tup)\n", + "\n", + "print(tup[0])\n", + "print(tup[2: 4])" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('harshit', 'subha', 'debayan', 'anish')\n" + ] + } + ], + "source": [ + "#Printing in reverse\n", + "print(tup[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'tuple' object does not support item assignment", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m#Proving my point of being immutable\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mtup\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'umang'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtup\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", + "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" + ] + } + ], + "source": [ + "#Proving my point of being immutable\n", + "tup[2] = 'umang'\n", + "print(tup)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Reasons to use Tuples over List:\n", + "1. Program executaions are faster\n", + "2. Sometimes, the developer wants to safeguard the data and make it un-modifiable\n", + "3. Python dictionary data-type uses Tuples as its value in the key-value pairs" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#Another trick\n", + "print(t)\n", + "\n", + "(a, b, c, d) = t\n", + "print(a)\n", + "print()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}