{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Fundamental Data Structure :\n", "\n", "The fundamental data structure in python includes \n", "\n", "- **Primitive type** ( ***Integer, Float, String***, and ***Boolean***) and \n", "- **Non-Primitive type** ( ***Array, List, Tuples, Dictionary, Set***, and ***File***) \n", "\n", "In this tutorial, we are going to discudd about List, Tuples, Set and Dictionary. \n", "\n", "\n", "\n", "### List\n", "\n", "List is built in data structure in python. It is \n", "- Mutable i.e., we can change or edite the size of the list by appending, inserting and deleting the elements.\n", "- List can hold heterogeneous objects (e.g., integer, string, boolean)\n", "\n", "Lets try to understand the List:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To initiate a blank List." ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "l = []" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To find the type of the object." ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(l)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To create a list from scratch." ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [], "source": [ "L = [1,2,3,4,5,6,342,34]" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 342, 34]" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Indexing of list." ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 2, 6)" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[0],L[1],L[5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Revers indexing is also possible." ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6, 5, 4)" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[-1],L[-2],L[-3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To find the length of list." ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(L)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To add the element from last." ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 12]" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L.append(12)\n", "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To find the sum of the elements (if they are of same types like int. double etc)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "33" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(L)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To find maximum and minimum of the list" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(12, 1)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "max(L), min(L)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To create a list of heterogeneous element types." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "L = [1,2.0,3,4,5,\"Apple\",True, False]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To find the type of elements of a list." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(float, str)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(L[1]),type(L[5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To create a list of list." ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [], "source": [ "L = [[1,2,3],[3,4,5],[5,7,9]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To find list inside a list." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[0]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[0][1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To add two list. It is not as ususal addition. The elements are accumulated." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([1, 2, 3, 2, 4, 6], {1, 2, 3, 4, 6})" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L1 = [1,2,3] ; L2 = [2,4,6]\n", "L1+L2, set(L1+L2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To add element from end of the list" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 2, 3, 5, 6, 7, 100]" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = [1,4,2,3,5,6,7]\n", "L.append(100)\n", "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To insert element (100) at specific index (1)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 100, 4, 2, 3, 5, 6, 7]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = [1,4,2,3,5,6,7]\n", "L.insert(1,100)\n", "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To remove specific element form list. It will remove the first occurance." ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 5, 6, 7, 4]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = [1,4,2,3,5,6,7,4]\n", "L.remove(4)\n", "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To remove the element from specific index" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 2, 3, 5, 6]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = [1,4,2,3,5,6,7]\n", "L.pop(-1)\n", "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To sort the list" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 5, 7, 10, 30, 60]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = [1,10,2,30,5,60,7]\n", "L.sort()\n", "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To reverse the list" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[7, 6, 5, 3, 2, 4, 1]" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = [1,4,2,3,5,6,7]\n", "L.reverse()\n", "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- List comprehension" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]\n" ] } ], "source": [ "L = [x for x in range(100)]\n", "print(L)" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]\n" ] } ], "source": [ "L = [x for x in range(100) if x%2==0]\n", "print(L)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "59" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import random as rn\n", "rn.randint(0,100)" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[89, 28, 17, 21, 30, 12, 2, 18, 44, 35, 39, 34, 36, 15, 62, 31, 29, 67, 89, 84, 52, 99, 12, 2, 65, 93, 37, 60, 65, 65, 67, 65, 83, 96, 6, 16, 96, 38, 43, 28, 38, 4, 60, 48, 5, 50, 82, 27, 75, 94, 3, 11, 55, 87, 56, 48, 71, 40, 7, 20, 45, 29, 100, 4, 23, 86, 0, 46, 69, 60, 85, 27, 45, 93, 6, 68, 13, 66, 89, 59, 46, 89, 41, 84, 69, 11, 22, 38, 24, 74, 82, 6, 12, 84, 14, 68, 16, 2, 91, 62, 57, 72, 98, 4, 98, 64, 72, 57, 73, 12, 78, 40, 0, 10, 36, 52, 71, 42, 27, 94, 32, 82, 22, 89, 37, 26, 55, 80, 49, 48, 69, 34, 48, 97, 39, 82, 78, 97, 11, 38, 77, 62, 55, 95, 52, 42, 61, 94, 100, 86, 28, 41, 75, 13, 40, 22, 7, 78, 2, 51, 84, 30, 45, 39, 54, 72, 88, 48, 10, 78, 9, 44, 18, 36, 23, 83, 1, 88, 37, 87, 85, 4, 65, 55, 11, 97, 90, 77, 1, 41, 75, 37, 43, 3, 45, 38, 4, 16, 87, 47]\n" ] } ], "source": [ "import random as rn\n", "R = [rn.randint(0,100) for k in range(200)]\n", "print(R)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Counter({37: 5, 17: 5, 67: 5, 15: 4, 99: 4, 88: 4, 61: 4, 54: 4, 96: 4, 80: 4, 51: 4, 83: 4, 14: 4, 10: 3, 78: 3, 27: 3, 49: 3, 62: 3, 38: 3, 48: 3, 94: 3, 66: 3, 81: 3, 72: 3, 95: 3, 1: 3, 21: 3, 100: 3, 40: 3, 28: 3, 43: 3, 29: 2, 16: 2, 7: 2, 74: 2, 60: 2, 64: 2, 85: 2, 50: 2, 39: 2, 44: 2, 8: 2, 90: 2, 58: 2, 0: 2, 79: 2, 24: 2, 98: 2, 93: 2, 22: 2, 68: 2, 42: 2, 86: 2, 30: 2, 41: 2, 77: 2, 59: 2, 34: 2, 4: 2, 31: 2, 57: 2, 6: 2, 73: 2, 45: 1, 69: 1, 91: 1, 71: 1, 75: 1, 87: 1, 70: 1, 47: 1, 82: 1, 18: 1, 9: 1, 26: 1, 55: 1, 56: 1, 23: 1, 52: 1, 33: 1, 13: 1, 2: 1, 46: 1, 36: 1, 53: 1, 92: 1, 84: 1, 12: 1, 20: 1, 5: 1})\n" ] } ], "source": [ "import collections\n", "#High Performance Counting\n", "C = collections.Counter(R)\n", "print(C)" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['A', 'T', 'C', 'T', 'T', 'C', 'A', 'C', 'G', 'G', 'A', 'T', 'G', 'G', 'C', 'T', 'G', 'C', 'A', 'T', 'A', 'T', 'T', 'G', 'G', 'G', 'A', 'C', 'G', 'G', 'C', 'C', 'A', 'T', 'A', 'A', 'G', 'G', 'G', 'A', 'A', 'G', 'C', 'C', 'A', 'A', 'C', 'G', 'C', 'G', 'T', 'A', 'A', 'A', 'G', 'G', 'T', 'T', 'A', 'G', 'T', 'T', 'G', 'A', 'C', 'G', 'C', 'C', 'G', 'T', 'C', 'T', 'A', 'C', 'G', 'G', 'G', 'T', 'G', 'A', 'A', 'G', 'C', 'T', 'T', 'C', 'C', 'A', 'A', 'G', 'A', 'C', 'G', 'C', 'T', 'T', 'T', 'T', 'A', 'A', 'T', 'C', 'C', 'T', 'G', 'A', 'C', 'G', 'C', 'A', 'G', 'A', 'C', 'C', 'C', 'A', 'G', 'T', 'A', 'G', 'C', 'A', 'G', 'C', 'G', 'G', 'G', 'T', 'T', 'T', 'A', 'A', 'C', 'T', 'G', 'A', 'T', 'A', 'G', 'G', 'G', 'C', 'G', 'T', 'C', 'C', 'T', 'G', 'G', 'C', 'G', 'A', 'G', 'C', 'C', 'A', 'T', 'A', 'T', 'T', 'C', 'A', 'C', 'T', 'G', 'T', 'G', 'T', 'T', 'T', 'C', 'T', 'G', 'C', 'A', 'G', 'G', 'C', 'T', 'T', 'C', 'G', 'C', 'T', 'C', 'G', 'A', 'G', 'T', 'G', 'A', 'C', 'C', 'T', 'A', 'G', 'C', 'T', 'T', 'T']\n" ] } ], "source": [ "R = [rn.choice(['A','T','G','C']) for i in range(200)]\n", "print(R)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ATCTTCACGGATGGCTGCATATTGGGACGGCCATAAGGGAAGCCAACGCGTAAAGGTTAGTTGACGCCGTCTACGGGTGAAGCTTCCAAGACGCTTTTAATCCTGACGCAGACCCAGTAGCAGCGGGTTTAACTGATAGGGCGTCCTGGCGAGCCATATTCACTGTGTTTCTGCAGGCTTCGCTCGAGTGACCTAGCTTT'" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DNA = ''.join(R)\n", "DNA" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(44, 9, 1)" ] }, "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DNA.count('A'), DNA.count('AT'), DNA.count('ATG')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Mini Assignment:\n", "Create a DNA string of 10,000 characters and count the following: A,T,G,C,all combination of two charaters, all combinations of three characters." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuples\n", "\n", "Tuples are non-mutable, which means we can ot add or remove elements once tuple is defind." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To define a tuples from scratch" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [], "source": [ "t = (2,3,4,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Find type" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Indexing" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t[1]" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "L = [(1,2),(2,3),(3,4)]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L[0][0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Create a list of tuples" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1, 2), ('a', 'b'), (True, False)]" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = [(1,2),(\"a\",\"b\"),(True, False)]\n", "L" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary\n", "\n", "Dictionary organizes the data with key-value pair. Dictionary can be nested with other data types." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To initiate a dictionary" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "D = dict()\n", "DD = {}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Create a dictionary from scratch" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "D = {\"fruit\":'apple',\n", " \"vegetable\" : 'carrot',\n", " \"rice\": 2.0,\n", " 'milk': 10,}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- What are keys?" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['fruit', 'vegetable', 'rice', 'milk'])" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D.keys()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- What are values?" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values(['apple', 'carrot', 2.0, 10])" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D.values()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Indexing" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('apple', 2.0)" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D['fruit'], D[\"rice\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Iteration over key and values" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "fruit apple\n", "vegetable carrot\n", "rice 2.0\n", "milk 10\n" ] } ], "source": [ "for key,value in D.items():\n", " print(key,value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To update a dictionary" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'fruit': 'apple', 'vegetable': 'carrot', 'rice': 2.0, 'milk': 10, 'salt': 2.0}" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "D.update({\"salt\": 2.0})\n", "D" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To create a list form a Dictionary. Only keys are collected." ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['fruit', 'vegetable', 'rice', 'milk']" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(D)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To create a list of keys only" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['fruit', 'vegetable', 'rice', 'milk']" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(D.keys())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To create a list of values" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['apple', 'carrot', 2.0, 10, 2.0]" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(D.values())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- To create Dictionary of with list, tuples and dictionary" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'names': ['John', 'Harry', 'Brat'],\n", " 'roll no': (1, 2, 3),\n", " 'plan': {'first': [12, 34, 56], 'second': [1, 3, 5]}}" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DD = {\"names\":(\"John\",\"Harry\", \"Brat\"),\\\n", " \"roll no\": [1,2,3],\\\n", " \"plan\":{\"first\":[12,34,56],\"second\":[1,3,5]}}\n", "\n", "DD" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7\n", " 1.8 1.9 2. 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3. 3.1]\n" ] } ], "source": [ "import numpy as np\n", "X = np.arange(0,np.pi,0.1)\n", "print(X)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "X = np.arange(0,np.pi,0.1)\n", "M = {\"sin\": [np.sin(x) for x in X],\\\n", " \"cos\": [np.cos(x) for x in X],\\\n", " \"plo\":[(x*x+x+1) for x in X],\\\n", " \"trig\": [np.cos(x) + np.sin(x) for x in X]}" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'sin': [0.0, 0.09983341664682815, 0.19866933079506122, 0.2955202066613396, 0.3894183423086505, 0.479425538604203, 0.5646424733950355, 0.6442176872376911, 0.7173560908995228, 0.7833269096274834, 0.8414709848078965, 0.8912073600614354, 0.9320390859672264, 0.963558185417193, 0.9854497299884603, 0.9974949866040544, 0.9995736030415051, 0.9916648104524686, 0.9738476308781951, 0.9463000876874145, 0.9092974268256817, 0.8632093666488737, 0.8084964038195901, 0.74570521217672, 0.6754631805511506, 0.5984721441039564, 0.5155013718214642, 0.4273798802338298, 0.33498815015590466, 0.23924932921398198, 0.1411200080598672, 0.04158066243329049], 'cos': [1.0, 0.9950041652780258, 0.9800665778412416, 0.955336489125606, 0.9210609940028851, 0.8775825618903728, 0.8253356149096782, 0.7648421872844884, 0.6967067093471654, 0.6216099682706644, 0.5403023058681398, 0.4535961214255773, 0.3623577544766734, 0.26749882862458735, 0.16996714290024081, 0.0707372016677029, -0.029199522301288815, -0.12884449429552486, -0.2272020946930871, -0.3232895668635036, -0.4161468365471424, -0.5048461045998576, -0.5885011172553458, -0.6662760212798244, -0.7373937155412458, -0.8011436155469337, -0.8568887533689473, -0.9040721420170612, -0.9422223406686583, -0.9709581651495907, -0.9899924966004454, -0.9991351502732795], 'plo': [1.0, 1.11, 1.24, 1.3900000000000001, 1.56, 1.75, 1.9600000000000002, 2.1900000000000004, 2.4400000000000004, 2.71, 3.0, 3.3100000000000005, 3.6400000000000006, 3.99, 4.36, 4.75, 5.16, 5.590000000000001, 6.04, 6.510000000000001, 7.0, 7.51, 8.040000000000001, 8.590000000000002, 9.160000000000002, 9.75, 10.360000000000001, 10.990000000000002, 11.640000000000002, 12.310000000000002, 13.0, 13.71], 'trig': [1.0, 1.094837581924854, 1.1787359086363027, 1.2508566957869456, 1.3104793363115357, 1.3570081004945758, 1.3899780883047137, 1.4090598745221796, 1.4140628002466882, 1.4049368778981477, 1.3817732906760363, 1.3448034814870127, 1.2943968404438997, 1.2310570140417803, 1.155416872888701, 1.0682321882717574, 0.9703740807402162, 0.8628203161569437, 0.7466455361851081, 0.623010520823911, 0.4931505902785393, 0.35836326204901614, 0.21999528656424427, 0.0794291908968956, -0.061930534990095154, -0.20267147144297726, -0.34138738154748316, -0.47669226178323143, -0.6072341905127536, -0.7317088359356086, -0.8488724885405782, -0.957554487839989]}\n" ] } ], "source": [ "print(M)" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | sin | \n", "cos | \n", "plo | \n", "trig | \n", "
|---|---|---|---|---|
| 0 | \n", "0.000000 | \n", "1.000000 | \n", "1.00 | \n", "1.000000 | \n", "
| 1 | \n", "0.099833 | \n", "0.995004 | \n", "1.11 | \n", "1.094838 | \n", "
| 2 | \n", "0.198669 | \n", "0.980067 | \n", "1.24 | \n", "1.178736 | \n", "
| 3 | \n", "0.295520 | \n", "0.955336 | \n", "1.39 | \n", "1.250857 | \n", "
| 4 | \n", "0.389418 | \n", "0.921061 | \n", "1.56 | \n", "1.310479 | \n", "
| 5 | \n", "0.479426 | \n", "0.877583 | \n", "1.75 | \n", "1.357008 | \n", "
| 6 | \n", "0.564642 | \n", "0.825336 | \n", "1.96 | \n", "1.389978 | \n", "
| 7 | \n", "0.644218 | \n", "0.764842 | \n", "2.19 | \n", "1.409060 | \n", "
| 8 | \n", "0.717356 | \n", "0.696707 | \n", "2.44 | \n", "1.414063 | \n", "
| 9 | \n", "0.783327 | \n", "0.621610 | \n", "2.71 | \n", "1.404937 | \n", "
| 10 | \n", "0.841471 | \n", "0.540302 | \n", "3.00 | \n", "1.381773 | \n", "
| 11 | \n", "0.891207 | \n", "0.453596 | \n", "3.31 | \n", "1.344803 | \n", "
| 12 | \n", "0.932039 | \n", "0.362358 | \n", "3.64 | \n", "1.294397 | \n", "
| 13 | \n", "0.963558 | \n", "0.267499 | \n", "3.99 | \n", "1.231057 | \n", "
| 14 | \n", "0.985450 | \n", "0.169967 | \n", "4.36 | \n", "1.155417 | \n", "
| 15 | \n", "0.997495 | \n", "0.070737 | \n", "4.75 | \n", "1.068232 | \n", "
| 16 | \n", "0.999574 | \n", "-0.029200 | \n", "5.16 | \n", "0.970374 | \n", "
| 17 | \n", "0.991665 | \n", "-0.128844 | \n", "5.59 | \n", "0.862820 | \n", "
| 18 | \n", "0.973848 | \n", "-0.227202 | \n", "6.04 | \n", "0.746646 | \n", "
| 19 | \n", "0.946300 | \n", "-0.323290 | \n", "6.51 | \n", "0.623011 | \n", "
| 20 | \n", "0.909297 | \n", "-0.416147 | \n", "7.00 | \n", "0.493151 | \n", "
| 21 | \n", "0.863209 | \n", "-0.504846 | \n", "7.51 | \n", "0.358363 | \n", "
| 22 | \n", "0.808496 | \n", "-0.588501 | \n", "8.04 | \n", "0.219995 | \n", "
| 23 | \n", "0.745705 | \n", "-0.666276 | \n", "8.59 | \n", "0.079429 | \n", "
| 24 | \n", "0.675463 | \n", "-0.737394 | \n", "9.16 | \n", "-0.061931 | \n", "
| 25 | \n", "0.598472 | \n", "-0.801144 | \n", "9.75 | \n", "-0.202671 | \n", "
| 26 | \n", "0.515501 | \n", "-0.856889 | \n", "10.36 | \n", "-0.341387 | \n", "
| 27 | \n", "0.427380 | \n", "-0.904072 | \n", "10.99 | \n", "-0.476692 | \n", "
| 28 | \n", "0.334988 | \n", "-0.942222 | \n", "11.64 | \n", "-0.607234 | \n", "
| 29 | \n", "0.239249 | \n", "-0.970958 | \n", "12.31 | \n", "-0.731709 | \n", "
| 30 | \n", "0.141120 | \n", "-0.989992 | \n", "13.00 | \n", "-0.848872 | \n", "
| 31 | \n", "0.041581 | \n", "-0.999135 | \n", "13.71 | \n", "-0.957554 | \n", "