├── 9053.txt ├── Assignments ├── Assignment 1 │ ├── Assignment 1.pdf │ ├── Assignment1-Regex.7z │ ├── Assignment1Part2.zip │ ├── Readme.md │ └── python │ │ ├── RegexClass.py │ │ └── readme.md ├── Assignment Zero.docx ├── Assignment_2.zip ├── Bonus Assignment.docx └── Readme.md ├── Language_Model.ipynb ├── README.md ├── Tutorials ├── Intro_to_python.ipynb ├── Readme.me ├── Regex_Tutorial.ipynb └── reviews.csv └── docs ├── Readme.md ├── ZLogo.png ├── gitBash.jpg ├── gitPull.jpg ├── gitclone.jpg ├── nlp.png └── source ├── MainRepo.JPG └── _static └── images └── ZLogo.png /Assignments/Assignment 1/Assignment 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedbahaaeldin/Introduction-to-NLP-Class/fc7cf691b79575dbc9c8314dc1d322350d51bcd2/Assignments/Assignment 1/Assignment 1.pdf -------------------------------------------------------------------------------- /Assignments/Assignment 1/Assignment1-Regex.7z: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedbahaaeldin/Introduction-to-NLP-Class/fc7cf691b79575dbc9c8314dc1d322350d51bcd2/Assignments/Assignment 1/Assignment1-Regex.7z -------------------------------------------------------------------------------- /Assignments/Assignment 1/Assignment1Part2.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedbahaaeldin/Introduction-to-NLP-Class/fc7cf691b79575dbc9c8314dc1d322350d51bcd2/Assignments/Assignment 1/Assignment1Part2.zip -------------------------------------------------------------------------------- /Assignments/Assignment 1/Readme.md: -------------------------------------------------------------------------------- 1 | # Assignment 1: 2 | ## Part 1 : 3 | 4 | when you extract the zip you should have two folders : 5 | 6 | ``` 7 | Python 8 | 9 | Data 10 | ``` 11 | 12 | The code is inside the Python folder called 13 | ```regexclass.py``` 14 | 15 | 16 | ## As for Part2: 17 | 18 | Provide a pdf with answers to the questions,a txt file for the output and your code file 19 | -------------------------------------------------------------------------------- /Assignments/Assignment 1/python/RegexClass.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import os 3 | import re 4 | import pprint 5 | 6 | my_first_pat = '(\w)@(\w).edu' 7 | """ 8 | TODO 9 | """ 10 | def process_file(name, f): 11 | res = [] 12 | for line in f: 13 | email_matches = re.findall(my_first_pat,line) 14 | for m in email_matches: 15 | email = '%s@%s.edu' % m 16 | res.append((name,'e',email)) 17 | return res 18 | 19 | 20 | def process_dir(data_path): 21 | guess_list = [] 22 | for fname in os.listdir(data_path): 23 | if fname[0] == '.': 24 | continue 25 | path = os.path.join(data_path,fname) 26 | f = open(path,'r') 27 | f_guesses = process_file(fname, f) 28 | guess_list.extend(f_guesses) 29 | return guess_list 30 | 31 | def get_gold(gold_path): 32 | # get gold answers 33 | gold_list = [] 34 | f_gold = open(gold_path,'r') 35 | for line in f_gold: 36 | gold_list.append(tuple(line.strip().split('\t'))) 37 | return gold_list 38 | 39 | 40 | def score(guess_list, gold_list): 41 | guess_list = [(fname, _type, value.lower()) for (fname, _type, value) in guess_list] 42 | gold_list = [(fname, _type, value.lower()) for (fname, _type, value) in gold_list] 43 | guess_set = set(guess_list) 44 | gold_set = set(gold_list) 45 | 46 | tp = guess_set.intersection(gold_set) 47 | fp = guess_set - gold_set 48 | fn = gold_set - guess_set 49 | 50 | pp = pprint.PrettyPrinter() 51 | print('True Positives (%d): ' % len(tp)) 52 | pp.pprint(tp) 53 | print('False Positives (%d): ' % len(fp)) 54 | pp.pprint(fp) 55 | print('False Negatives (%d): ' % len(fn)) 56 | pp.pprint(fn) 57 | print('Summary: tp=%d, fp=%d, fn=%d' % (len(tp),len(fp),len(fn))) 58 | 59 | def main(data_path, gold_path): 60 | guess_list = process_dir(data_path) 61 | gold_list = get_gold(gold_path) 62 | score(guess_list, gold_list) 63 | 64 | 65 | if __name__ == '__main__': 66 | if (len(sys.argv) == 1): 67 | main('../data/dev', '../data/devGOLD') 68 | elif (len(sys.argv) == 3): 69 | main(sys.argv[1],sys.argv[2]) 70 | else: 71 | print('usage:\tSpamLord.py ') 72 | sys.exit(0) 73 | -------------------------------------------------------------------------------- /Assignments/Assignment 1/python/readme.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Assignments/Assignment Zero.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedbahaaeldin/Introduction-to-NLP-Class/fc7cf691b79575dbc9c8314dc1d322350d51bcd2/Assignments/Assignment Zero.docx -------------------------------------------------------------------------------- /Assignments/Assignment_2.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedbahaaeldin/Introduction-to-NLP-Class/fc7cf691b79575dbc9c8314dc1d322350d51bcd2/Assignments/Assignment_2.zip -------------------------------------------------------------------------------- /Assignments/Bonus Assignment.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedbahaaeldin/Introduction-to-NLP-Class/fc7cf691b79575dbc9c8314dc1d322350d51bcd2/Assignments/Bonus Assignment.docx -------------------------------------------------------------------------------- /Assignments/Readme.md: -------------------------------------------------------------------------------- 1 | ## Assignments 2 | 3 | -------------------------------------------------------------------------------- /Language_Model.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Language Model.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [], 9 | "authorship_tag": "ABX9TyNzH7bRE+QCWJWGpGHccpzk", 10 | "include_colab_link": true 11 | }, 12 | "kernelspec": { 13 | "name": "python3", 14 | "display_name": "Python 3" 15 | }, 16 | "language_info": { 17 | "name": "python" 18 | } 19 | }, 20 | "cells": [ 21 | { 22 | "cell_type": "markdown", 23 | "metadata": { 24 | "id": "view-in-github", 25 | "colab_type": "text" 26 | }, 27 | "source": [ 28 | "\"Open" 29 | ] 30 | }, 31 | { 32 | "cell_type": "code", 33 | "metadata": { 34 | "id": "zAsU-lGrD2yD" 35 | }, 36 | "source": [ 37 | "import re\n", 38 | "f = open(\"9053.txt\", \"r\")\n", 39 | "Dataset = []\n", 40 | "for line in f:\n", 41 | " line = re.sub(r'\\@\\@\\d{8} \\@\\d{7}/','',line)\n", 42 | " line = re.sub(r'|

','',line)\n", 43 | " line = re.sub(r' ',' ',line)\n", 44 | " line = re.sub(r'\\@','',line)\n", 45 | " Dataset.append(line)" 46 | ], 47 | "execution_count": 53, 48 | "outputs": [] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "metadata": { 53 | "id": "Z8bWuno5Aj6p" 54 | }, 55 | "source": [ 56 | "def Bigram(string):\n", 57 | " string = \" \"+string+\" \"\n", 58 | " word_list = string.split(' ')\n", 59 | " bigrams = []\n", 60 | " for i in range(len(word_list)-1):\n", 61 | " # TODO\n", 62 | " return bigrams" 63 | ], 64 | "execution_count": 29, 65 | "outputs": [] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "metadata": { 70 | "id": "hBtTwz5pFsUb" 71 | }, 72 | "source": [ 73 | "string = \"Hello world\"\n", 74 | "assert len(Bigram(string)) == 3" 75 | ], 76 | "execution_count": 30, 77 | "outputs": [] 78 | }, 79 | { 80 | "cell_type": "code", 81 | "metadata": { 82 | "id": "93azsp17A8sQ" 83 | }, 84 | "source": [ 85 | "def Trigram(string):\n", 86 | " string = \" \"+ string +\" \"\n", 87 | " word_list = string.split(' ')\n", 88 | " Trigrams = []\n", 89 | " for i in range(len(word_list)-2):\n", 90 | " Trigrams.append([word_list[i],word_list[i+1],word_list[i+2]])\n", 91 | " return Trigrams" 92 | ], 93 | "execution_count": 40, 94 | "outputs": [] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "metadata": { 99 | "id": "ilx2mETkA_UO" 100 | }, 101 | "source": [ 102 | "string = \"Hello world today is good\"\n", 103 | "assert len(Trigram(string)) == 5" 104 | ], 105 | "execution_count": null, 106 | "outputs": [] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "metadata": { 111 | "id": "KlMStvXTHCI3" 112 | }, 113 | "source": [ 114 | "from collections import Counter, defaultdict\n", 115 | "Total_Count = 0\n", 116 | "Trigram_Dict = defaultdict(lambda: defaultdict(lambda: 0))\n", 117 | "for line in Dataset:\n", 118 | " list_of_grams = Trigram(line)\n", 119 | " Total_Count += len(list_of_grams)\n", 120 | " for w1,w2,w3 in list_of_grams:\n", 121 | " Trigram_Dict[(w1, w2)][w3] += 1" 122 | ], 123 | "execution_count": 76, 124 | "outputs": [] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "metadata": { 129 | "id": "MjVA6cZ_He0q" 130 | }, 131 | "source": [ 132 | "for w1_w2 in Trigram_Dict:\n", 133 | " for w3 in Trigram_Dict[w1_w2]:\n", 134 | " Trigram_Dict[w1_w2][w3] /= Total_Count" 135 | ], 136 | "execution_count": 81, 137 | "outputs": [] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "metadata": { 142 | "colab": { 143 | "base_uri": "https://localhost:8080/" 144 | }, 145 | "id": "fq_B6tgNOAAE", 146 | "outputId": "a4a7ee17-82cb-45ef-ab13-92aa83b979d6" 147 | }, 148 | "source": [ 149 | "Trigram_Dict['about','that']" 150 | ], 151 | "execution_count": 95, 152 | "outputs": [ 153 | { 154 | "output_type": "execute_result", 155 | "data": { 156 | "text/plain": [ 157 | "defaultdict(..>,\n", 158 | " {\"'s\": 4.739235419149828e-06,\n", 159 | " 'about': 0.0,\n", 160 | " 'idea': 2.369617709574914e-06,\n", 161 | " 'now': 4.739235419149828e-06,\n", 162 | " 'one': 4.739235419149828e-06,\n", 163 | " 'single': 2.369617709574914e-06,\n", 164 | " 'stuff': 2.369617709574914e-06,\n", 165 | " 'that': 0.0})" 166 | ] 167 | }, 168 | "metadata": { 169 | "tags": [] 170 | }, 171 | "execution_count": 95 172 | } 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "metadata": { 178 | "colab": { 179 | "base_uri": "https://localhost:8080/" 180 | }, 181 | "id": "NgE8IYJbJidp", 182 | "outputId": "9cabd90d-89d6-4c55-8f4d-c622c1ce79a2" 183 | }, 184 | "source": [ 185 | "Trigram_Dict['after','the']" 186 | ], 187 | "execution_count": 87, 188 | "outputs": [ 189 | { 190 | "output_type": "execute_result", 191 | "data": { 192 | "text/plain": [ 193 | "defaultdict(..>,\n", 194 | " {'after': 0.0,\n", 195 | " 'author': 2.369617709574914e-06,\n", 196 | " 'clause': 4.739235419149828e-06,\n", 197 | " 'end': 4.739235419149828e-06,\n", 198 | " 'fact': 4.739235419149828e-06,\n", 199 | " 'first': 2.369617709574914e-06,\n", 200 | " 'form': 7.108853128724743e-06,\n", 201 | " 'letter': 2.369617709574914e-06,\n", 202 | " 'noun': 7.108853128724743e-06,\n", 203 | " 's': 9.478470838299657e-06,\n", 204 | " 'strange': 2.369617709574914e-06,\n", 205 | " 'the': 0.0,\n", 206 | " 'thing': 7.108853128724743e-06,\n", 207 | " 'truth': 1.4217706257449485e-05,\n", 208 | " 'word': 4.739235419149828e-06})" 209 | ] 210 | }, 211 | "metadata": { 212 | "tags": [] 213 | }, 214 | "execution_count": 87 215 | } 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "metadata": { 221 | "id": "TVKnc-5QKs8i" 222 | }, 223 | "source": [ 224 | "" 225 | ], 226 | "execution_count": null, 227 | "outputs": [] 228 | } 229 | ] 230 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | **CIE 553:Introduction to NLP Class repository for Students Material.** 10 | 11 | 12 | ![Python 3.6](https://img.shields.io/badge/Python-3.6-brightgreen.svg) 13 | [![Eng.Ahmed Email](https://img.shields.io/badge/Eng.Ahmed-Email-blue)](mailto:ahmedbahaa944@gmail.com) 14 | [![Eng.Roba Email](https://img.shields.io/badge/Eng.Roba-Email-pink)](mailto:eng.robagamal@gmail.com) 15 | 16 |
17 | 18 | -------------------------------------------------------------------------------- 19 | 20 | ### You found a bug or have a fruitful suggesstion ? 21 | Please, [open an issue][issue]. 22 | 23 | If you solved it [open a pull request][pr]!. 24 | 25 | ## Contents: 26 | - [Tutorials](#tutorials) 27 | - [Installation](#installation) 28 | 29 | 30 | ## Tutorials 31 | ([↑up to contents](#contents)) 32 | 33 |
34 | 35 | Tutorial | Resource | Links 36 | ------------------------- | --------------- | ---------------- 37 | Tutorial 1 | [Introduction to python][tut1] | http://www.omahapython.org/IdiomaticPython.html 38 | Tutorial 2 |[Introduction to Regex][tut2] | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ahmedbahaaeldin/Introduction-to-NLP-Class/blob/main/Tutorials/Regex_Tutorial.ipynb) 39 | Tutorial 3 |[Language Modeling][tut3] | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ahmedbahaaeldin/Introduction-to-NLP-Class/blob/main/Language_Model.ipynb) 40 |
41 | 42 | -------------------------------------------------------------------------------- 43 | 44 | ## Installation 45 | ([↑up to contents](#contents)) 46 | 47 | First you need to download [Git][git]. Select your OS from macOS, Windows, Linux/Unix. 48 | 49 | After the completion of the installment, you should be able to clone any repository now. Git is mainly used as a version control tool as there is no perfect software and there is always bugs/issues. So there is an essential need to maintain a working version of the software while keeping track of progress. 50 | 51 | Cloning a repository makes contributing to open source projects easier as when you add new feature, you design it on your local machine then ask the owner to push your code. 52 | 53 | #### How to clone a repo ? 54 | - First you go to repo main page. Like our repo. 55 | 56 |
57 | 58 |
59 | 60 | -------------------------------------------------------------------------------- 61 | 62 | - Then you select the download button and click the copy button. 63 | 64 |
65 | 66 |
67 | 68 | -------------------------------------------------------------------------------- 69 | 70 | - Then you open the Git Bash. Using ``` cd: change directory ``` to allocate the repo in your work area. Then type ``` git clone + URL you copied ``` 71 | 72 |
73 | 74 |
75 | 76 | -------------------------------------------------------------------------------- 77 | 78 | - Now you successfully have the current version of the repo. If there is a release of a new assignment or any material. 79 | You can simply use ``` git pull ``` command where you literally pull the latest version of the code. You first go to the directory where you saved your repo and type the command. 80 | 81 |
82 | 83 |
84 | 85 | -------------------------------------------------------------------------------- 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | [issue]: https://github.com/ahmedbahaaeldin/Introduction-to-NLP-Class/issues/new 94 | [pr]: https://github.com/ahmedbahaaeldin/Introduction-to-NLP-Class/compare 95 | [git]: https://git-scm.com/download/ 96 | [tut1]: https://github.com/ahmedbahaaeldin/Introduction-to-NLP-Class/blob/main/Tutorials/Intro_to_python.ipynb 97 | [tut2]: https://github.com/ahmedbahaaeldin/Introduction-to-NLP-Class/blob/main/Tutorials/Regex_Tutorial.ipynb 98 | [tut3]: https://github.com/ahmedbahaaeldin/Introduction-to-NLP-Class/blob/main/Language_Model.ipynb 99 | -------------------------------------------------------------------------------- /Tutorials/Intro_to_python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "id": "Ja48055HN0B6" 7 | }, 8 | "source": [ 9 | "# Python Hello World" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": null, 15 | "metadata": { 16 | "id": "UGwEY1FDN0CB", 17 | "outputId": "fc649221-91f2-4492-a12d-ead8f143cb81" 18 | }, 19 | "outputs": [ 20 | { 21 | "name": "stdout", 22 | "output_type": "stream", 23 | "text": [ 24 | "Hello World\n", 25 | "Hello World\n", 26 | "Hello 1 True\n" 27 | ] 28 | } 29 | ], 30 | "source": [ 31 | "print(\"Hello World\")\n", 32 | "print(\"Hello\", \"World\")\n", 33 | "print(\"Hello\", 1, True)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "code", 38 | "execution_count": null, 39 | "metadata": { 40 | "id": "3YI9a0mqN0CC" 41 | }, 42 | "outputs": [], 43 | "source": [ 44 | "x=\"Good\"" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": null, 50 | "metadata": { 51 | "id": "0x0wyJpFN0CD", 52 | "outputId": "809e6474-67f2-4180-e0a7-bfbaa8435105" 53 | }, 54 | "outputs": [ 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "Good Bye\n" 60 | ] 61 | } 62 | ], 63 | "source": [ 64 | "print(x, \"Bye\")" 65 | ] 66 | }, 67 | { 68 | "cell_type": "markdown", 69 | "metadata": { 70 | "id": "sbo1nhXvN0CD" 71 | }, 72 | "source": [ 73 | "# This is Markdown\n", 74 | "It can be used for explanations" 75 | ] 76 | }, 77 | { 78 | "cell_type": "code", 79 | "execution_count": null, 80 | "metadata": { 81 | "id": "vLQt7SMSN0CE", 82 | "outputId": "01d4dc96-8df9-4018-89dc-b2c5dace2e7a" 83 | }, 84 | "outputs": [ 85 | { 86 | "name": "stdout", 87 | "output_type": "stream", 88 | "text": [ 89 | "x = 1\n" 90 | ] 91 | } 92 | ], 93 | "source": [ 94 | "#This is a comment\n", 95 | "#Below is how to define a variable\n", 96 | "x=1\n", 97 | "print('x =',x)" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": null, 103 | "metadata": { 104 | "id": "hw2genjaN0CE", 105 | "outputId": "3d9b40f4-e855-4001-9d5d-b2576cf30420" 106 | }, 107 | "outputs": [ 108 | { 109 | "data": { 110 | "text/plain": [ 111 | "'\\nThis is a multiline comment (and a string too :D)\\nUseful for long documentation\\n'" 112 | ] 113 | }, 114 | "execution_count": 3, 115 | "metadata": { 116 | "tags": [] 117 | }, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "\"\"\"\n", 123 | "This is a multiline comment (and a string too :D)\n", 124 | "Useful for long documentation\n", 125 | "\"\"\"" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": { 131 | "id": "EjAOOTZNN0CF" 132 | }, 133 | "source": [ 134 | "## Python is Dynamically typed and Strong Typed" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": null, 140 | "metadata": { 141 | "id": "RZy99OycN0CF", 142 | "outputId": "c4cd4645-8eb7-447c-a4de-17f080c7df06" 143 | }, 144 | "outputs": [ 145 | { 146 | "name": "stdout", 147 | "output_type": "stream", 148 | "text": [ 149 | "x = 1 \t\t\t=> with type \n", 150 | "x = 2.5 \t\t=> with type \n", 151 | "x = 1.1e+18 \t\t=> with type \n", 152 | "x = True \t\t=> with type \n", 153 | "x = Hello World \t=> with type \n", 154 | "x = \"Hello World\" \t=> with type \n", 155 | "x = (1+2j) \t\t=> with type \n", 156 | "\n", 157 | "x = [1, 2, 3] \t\t=> with type \n", 158 | "x = (1, 2, 3) \t\t=> with type \n", 159 | "x = (1,) \t\t=> with type \n", 160 | "x = {1, 2, 3} \t\t=> with type \n", 161 | "x = {'a': 1, 'b': 2, 'c': 3} \t\t=> with type \n", 162 | "x = None \t\t=> with type \n" 163 | ] 164 | } 165 | ], 166 | "source": [ 167 | "#Variables can change types dynamically\n", 168 | "x=1\n", 169 | "print('x =', x, '\\t\\t\\t=> with type', type(x))\n", 170 | "\n", 171 | "x=2.5\n", 172 | "print('x =', x, '\\t\\t=> with type', type(x))\n", 173 | "\n", 174 | "x=1.1e18\n", 175 | "print('x =', x, '\\t\\t=> with type', type(x))\n", 176 | "\n", 177 | "x=True #True with Uppercase T and False with Uppercase F\n", 178 | "print('x =', x, '\\t\\t=> with type', type(x))\n", 179 | "\n", 180 | "x=\"Hello World\"\n", 181 | "print('x =', x, '\\t=> with type', type(x))\n", 182 | "\n", 183 | "x='\"Hello World\"'\n", 184 | "print('x =', x, '\\t=> with type', type(x))\n", 185 | "\n", 186 | "x=1+2j\n", 187 | "print('x =', x, '\\t\\t=> with type', type(x))\n", 188 | "\n", 189 | "print()\n", 190 | "\n", 191 | "x=[1,2,3]\n", 192 | "print('x =', x, '\\t\\t=> with type', type(x))\n", 193 | "\n", 194 | "x=(1,2,3)\n", 195 | "print('x =', x, '\\t\\t=> with type', type(x))\n", 196 | "\n", 197 | "x=(1,)\n", 198 | "print('x =', x, '\\t\\t=> with type', type(x))\n", 199 | "\n", 200 | "x={1,2,3}\n", 201 | "print('x =', x, '\\t\\t=> with type', type(x))\n", 202 | "\n", 203 | "x={'a':1, 'b':2, 'c':3}\n", 204 | "print('x =', x, '\\t\\t=> with type', type(x))\n", 205 | "\n", 206 | "x=None\n", 207 | "print('x =', x, '\\t\\t=> with type', type(x))" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": null, 213 | "metadata": { 214 | "id": "_URY2FVWN0CG", 215 | "outputId": "e2fe3924-675a-4e8f-af47-20d3b9586686" 216 | }, 217 | "outputs": [ 218 | { 219 | "name": "stdout", 220 | "output_type": "stream", 221 | "text": [ 222 | "x = 1 \t\t=> with type \n" 223 | ] 224 | }, 225 | { 226 | "ename": "NameError", 227 | "evalue": "name 'x' is not defined", 228 | "output_type": "error", 229 | "traceback": [ 230 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 231 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 232 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'x ='\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'\\t\\t=> with type'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 233 | "\u001b[1;31mNameError\u001b[0m: name 'x' is not defined" 234 | ] 235 | } 236 | ], 237 | "source": [ 238 | "x = 1\n", 239 | "print('x =', x, '\\t\\t=> with type', type(x))\n", 240 | "\n", 241 | "del x\n", 242 | "print('x =', x, '\\t\\t=> with type', type(x))" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": null, 248 | "metadata": { 249 | "id": "cVW6bAjfN0CG", 250 | "outputId": "a80cf726-1dcc-4944-c918-ef71e8819366" 251 | }, 252 | "outputs": [ 253 | { 254 | "ename": "TypeError", 255 | "evalue": "must be str, not int", 256 | "output_type": "error", 257 | "traceback": [ 258 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 259 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 260 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m#python is also strongly typed so it will not implicitly change types\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[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m\"This number is \"\u001b[0m \u001b[1;33m+\u001b[0m \u001b[1;36m12\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 261 | "\u001b[1;31mTypeError\u001b[0m: must be str, not int" 262 | ] 263 | } 264 | ], 265 | "source": [ 266 | "#python is also strongly typed so it will not implicitly change types\n", 267 | "x = \"This number is \" + 12" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": null, 273 | "metadata": { 274 | "id": "oVQ2YejWN0CH", 275 | "outputId": "043cd276-a04b-4f53-afe5-238aafeff855" 276 | }, 277 | "outputs": [ 278 | { 279 | "name": "stdout", 280 | "output_type": "stream", 281 | "text": [ 282 | "This number is 12\n", 283 | "25\n" 284 | ] 285 | } 286 | ], 287 | "source": [ 288 | "#but you can cast variable if you wish\n", 289 | "x = \"This number is \" + str(12)\n", 290 | "print(x)\n", 291 | "x = int(\"13\") + 12\n", 292 | "print(x)" 293 | ] 294 | }, 295 | { 296 | "cell_type": "markdown", 297 | "metadata": { 298 | "id": "Lawj8avtN0CH" 299 | }, 300 | "source": [ 301 | "# Python Operators" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "metadata": { 308 | "id": "2yfTjoQmN0CI", 309 | "outputId": "304a9dba-d84c-4ff1-9b88-e9969f0b7d2c" 310 | }, 311 | "outputs": [ 312 | { 313 | "name": "stdout", 314 | "output_type": "stream", 315 | "text": [ 316 | "x+y = 9\n", 317 | "x-y = -1\n", 318 | "x*y = 20\n", 319 | "x/y = 0.8\n", 320 | "x//y = 0\n", 321 | "x%y = 4\n", 322 | "x**y = 1024\n" 323 | ] 324 | } 325 | ], 326 | "source": [ 327 | "x, y = 4, 5\n", 328 | "print('x+y = ', x+y)\n", 329 | "print('x-y = ', x-y)\n", 330 | "print('x*y = ', x*y)\n", 331 | "print('x/y = ', x/y)\n", 332 | "print('x//y = ', x//y)\n", 333 | "print('x%y = ', x%y)\n", 334 | "print('x**y = ', x**y)" 335 | ] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "execution_count": null, 340 | "metadata": { 341 | "id": "L6n1b2kXN0CI", 342 | "outputId": "5ff8a557-028c-4582-96a6-286378bc4d0d" 343 | }, 344 | "outputs": [ 345 | { 346 | "name": "stdout", 347 | "output_type": "stream", 348 | "text": [ 349 | "x==y = False\n", 350 | "x!=y = True\n", 351 | "xy = False\n", 353 | "x<=y = True\n", 354 | "x>=y = False\n" 355 | ] 356 | } 357 | ], 358 | "source": [ 359 | "x, y = 4, 5\n", 360 | "print('x==y = ', x==y)\n", 361 | "print('x!=y = ', x!=y)\n", 362 | "print('xy = ', x>y)\n", 364 | "print('x<=y = ', x<=y)\n", 365 | "print('x>=y = ', x>=y)" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": null, 371 | "metadata": { 372 | "id": "hfA8cdF9N0CI", 373 | "outputId": "36d2c623-aab5-4536-c67a-765cd97ce018" 374 | }, 375 | "outputs": [ 376 | { 377 | "name": "stdout", 378 | "output_type": "stream", 379 | "text": [ 380 | "x and y = False\n", 381 | "x or y = True\n", 382 | "not x = False\n" 383 | ] 384 | } 385 | ], 386 | "source": [ 387 | "x, y = True, False\n", 388 | "print('x and y = ', x and y)\n", 389 | "print('x or y = ', x or y)\n", 390 | "print('not x = ', not x)" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": null, 396 | "metadata": { 397 | "id": "m1OJ8Ts-N0CJ", 398 | "outputId": "4a25cc16-4623-44ed-9402-3a7e8052e494" 399 | }, 400 | "outputs": [ 401 | { 402 | "name": "stdout", 403 | "output_type": "stream", 404 | "text": [ 405 | "x & y = 0b1\n", 406 | "x | y = 0b111\n", 407 | "~ x = -0b110\n", 408 | "x ^ y = 0b110\n", 409 | "x << 1 0b1010\n", 410 | "x >> 1 0b10\n" 411 | ] 412 | } 413 | ], 414 | "source": [ 415 | "x, y = 0b0101, 0b0011\n", 416 | "print('x & y = ', bin(x & y))\n", 417 | "print('x | y = ', bin(x | y))\n", 418 | "print('~ x = ', bin(~ x))\n", 419 | "print('x ^ y = ', bin(x ^ y))\n", 420 | "print('x << 1', bin(x << 1))\n", 421 | "print('x >> 1', bin(x >> 1))" 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": null, 427 | "metadata": { 428 | "id": "G4Bsw2svN0CJ", 429 | "outputId": "b33b073b-d00f-4eac-dd55-b9545b5a7c4f" 430 | }, 431 | "outputs": [ 432 | { 433 | "name": "stdout", 434 | "output_type": "stream", 435 | "text": [ 436 | "x+y = HelloWorld\n", 437 | "x*3 = HelloHelloHello\n", 438 | "ascii of \"h\" = 104\n", 439 | "char for 104 = h\n" 440 | ] 441 | } 442 | ], 443 | "source": [ 444 | "x, y = 'Hello', \"World\"\n", 445 | "print('x+y =', x+y)\n", 446 | "print('x*3 =', x*3)\n", 447 | "print('ascii of \"h\" = ', ord('h'))\n", 448 | "print('char for 104 = ', chr(104))" 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": null, 454 | "metadata": { 455 | "id": "KfVNRBhEN0CK", 456 | "outputId": "3be3dd7a-0287-4137-f9e7-f30aec4d5004" 457 | }, 458 | "outputs": [ 459 | { 460 | "name": "stdout", 461 | "output_type": "stream", 462 | "text": [ 463 | "x+y = [1, 2, 3, 4, 5]\n", 464 | "x*3 = [1, 2, 3, 1, 2, 3, 1, 2, 3]\n", 465 | "\n", 466 | "x+y = (1, 2, 3, 4, 5)\n", 467 | "x*3 = (1, 2, 3, 1, 2, 3, 1, 2, 3)\n" 468 | ] 469 | } 470 | ], 471 | "source": [ 472 | "x, y = [1,2,3], [4,5]\n", 473 | "print('x+y =', x+y)\n", 474 | "print('x*3 =', x*3)\n", 475 | "\n", 476 | "print()\n", 477 | "\n", 478 | "x, y = (1,2,3), (4,5)\n", 479 | "print('x+y =', x+y)\n", 480 | "print('x*3 =', x*3)" 481 | ] 482 | }, 483 | { 484 | "cell_type": "code", 485 | "execution_count": null, 486 | "metadata": { 487 | "id": "sZ3LA-sBN0CK", 488 | "outputId": "eaa94faf-7f5c-4af5-ae75-97b640c9d78e" 489 | }, 490 | "outputs": [ 491 | { 492 | "name": "stdout", 493 | "output_type": "stream", 494 | "text": [ 495 | "1+(2*3) = 7\n" 496 | ] 497 | } 498 | ], 499 | "source": [ 500 | "#Python is an interpreted language so you can evaluate any string as python code\n", 501 | "x = \"1+(2*3)\"\n", 502 | "print(x,'=',eval(x))" 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": null, 508 | "metadata": { 509 | "id": "h3T7rmdGN0CK", 510 | "outputId": "a6988503-68c6-4ecd-981d-c175ceef9f0d" 511 | }, 512 | "outputs": [ 513 | { 514 | "name": "stdout", 515 | "output_type": "stream", 516 | "text": [ 517 | "x = 2\n", 518 | "x = 3\n", 519 | "x = 2\n", 520 | "x = 10\n", 521 | "x = 2.0\n", 522 | "x = 1024.0\n", 523 | "x = 102.0\n", 524 | "x = 2.0\n" 525 | ] 526 | } 527 | ], 528 | "source": [ 529 | "x = 2\n", 530 | "print('x =', x)\n", 531 | "x += 1\n", 532 | "print('x =', x)\n", 533 | "x -= 1\n", 534 | "print('x =', x)\n", 535 | "x *= 5\n", 536 | "print('x =', x)\n", 537 | "x /= 5\n", 538 | "print('x =', x)\n", 539 | "x **= 10\n", 540 | "print('x =', x)\n", 541 | "x //= 10\n", 542 | "print('x =', x)\n", 543 | "x %= 10\n", 544 | "print('x =', x)" 545 | ] 546 | }, 547 | { 548 | "cell_type": "code", 549 | "execution_count": null, 550 | "metadata": { 551 | "id": "6kjXeKadN0CL", 552 | "outputId": "31a62e8a-e8c7-40c6-cc62-fd0e50806519" 553 | }, 554 | "outputs": [ 555 | { 556 | "name": "stdout", 557 | "output_type": "stream", 558 | "text": [ 559 | "x = 6\n", 560 | "x = 2\n", 561 | "x = 5\n" 562 | ] 563 | } 564 | ], 565 | "source": [ 566 | "x = 4\n", 567 | "x |= 2\n", 568 | "print('x =', x)\n", 569 | "x &= 2\n", 570 | "print('x =', x)\n", 571 | "x ^= 7\n", 572 | "print('x =', x)" 573 | ] 574 | }, 575 | { 576 | "cell_type": "code", 577 | "execution_count": null, 578 | "metadata": { 579 | "id": "4k-hugdbN0CL", 580 | "outputId": "add7a437-482b-494e-fda6-b76f493fdf8e" 581 | }, 582 | "outputs": [ 583 | { 584 | "name": "stdout", 585 | "output_type": "stream", 586 | "text": [ 587 | "is 1 in list? = True\n", 588 | "is 4 in list? = False\n", 589 | "is 4 not in list? = True\n", 590 | "ll in hello True\n", 591 | "x in hello False\n", 592 | "is 1 in set? = True\n", 593 | "is 4 in set? = False\n", 594 | "is 1 in dict? = True\n", 595 | "is 4 in dict? = False\n" 596 | ] 597 | } 598 | ], 599 | "source": [ 600 | "x = [1,2,3]\n", 601 | "print('is 1 in list? =', 1 in x)\n", 602 | "print('is 4 in list? =', 4 in x)\n", 603 | "print('is 4 not in list? =', 4 not in x)\n", 604 | "\n", 605 | "x='hello'\n", 606 | "print('ll in hello', 'll' in x)\n", 607 | "print('x in hello', 'x' in x)\n", 608 | "\n", 609 | "x = {1,2,3}\n", 610 | "print('is 1 in set? =', 1 in x)\n", 611 | "print('is 4 in set? =', 4 in x)\n", 612 | "\n", 613 | "x = {1:'a',2:'b',3:'c'}\n", 614 | "print('is 1 in dict? =', 1 in x)\n", 615 | "print('is 4 in dict? =', 4 in x)" 616 | ] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "execution_count": null, 621 | "metadata": { 622 | "id": "RdhWZii1N0CL", 623 | "outputId": "ee768754-f028-4552-ba1f-31951263ed6b" 624 | }, 625 | "outputs": [ 626 | { 627 | "name": "stdout", 628 | "output_type": "stream", 629 | "text": [ 630 | "Same Reference\n", 631 | "x == y = True\n", 632 | "x is y = True\n", 633 | "x is not y = False\n", 634 | "Different Reference\n", 635 | "x == y = True\n", 636 | "x is y = False\n", 637 | "x is not y = True\n" 638 | ] 639 | } 640 | ], 641 | "source": [ 642 | "x = [1,2,3]\n", 643 | "y = x\n", 644 | "print(\"Same Reference\")\n", 645 | "print('x == y =', x==y)\n", 646 | "print('x is y =', x is y)\n", 647 | "print('x is not y =', x is not y)\n", 648 | "\n", 649 | "y = [1,2,3]\n", 650 | "print(\"Different Reference\")\n", 651 | "print('x == y =', x==y)\n", 652 | "print('x is y =', x is y)\n", 653 | "print('x is not y =', x is not y)" 654 | ] 655 | }, 656 | { 657 | "cell_type": "markdown", 658 | "metadata": { 659 | "id": "u37qlclLN0CM" 660 | }, 661 | "source": [ 662 | "# Strings" 663 | ] 664 | }, 665 | { 666 | "cell_type": "code", 667 | "execution_count": 1, 668 | "metadata": { 669 | "colab": { 670 | "base_uri": "https://localhost:8080/" 671 | }, 672 | "id": "kc85ha8ON0CM", 673 | "outputId": "9189aa6e-a626-4b25-f8b0-ffedaef62494" 674 | }, 675 | "outputs": [ 676 | { 677 | "name": "stdout", 678 | "output_type": "stream", 679 | "text": [ 680 | "x[0] = H\n", 681 | "x[-1] = d\n", 682 | "x[1:3] = el\n", 683 | "x[:3] = Hel\n", 684 | "x[1:] = ello World\n", 685 | "x[:-1] = Hello Worl\n", 686 | "x[1:5:2] = el\n", 687 | "x[::2] = HloWrd\n", 688 | "x[::-1] = dlroW olleH\n" 689 | ] 690 | } 691 | ], 692 | "source": [ 693 | "x = 'Hello World'\n", 694 | "\n", 695 | "print(\"x[0] =\",x[0])\n", 696 | "print(\"x[-1] =\", x[-1])\n", 697 | "print(\"x[1:3] = \", x[1:3]) #slice from 1 (inclusive) to 3 (exclusive)\n", 698 | "print(\"x[:3] = \", x[:3])\n", 699 | "print(\"x[1:] = \", x[1:])\n", 700 | "print(\"x[:-1] = \", x[:-1])\n", 701 | "print(\"x[1:5:2] = \", x[1:5:2])\n", 702 | "print(\"x[::2] = \", x[::2])\n", 703 | "print(\"x[::-1] = \", x[::-1])" 704 | ] 705 | }, 706 | { 707 | "cell_type": "code", 708 | "execution_count": 2, 709 | "metadata": { 710 | "colab": { 711 | "base_uri": "https://localhost:8080/" 712 | }, 713 | "id": "Nz-8rmclN0CM", 714 | "outputId": "09d3a99c-d26b-41a2-d2de-9cb32c019a1f" 715 | }, 716 | "outputs": [ 717 | { 718 | "name": "stdout", 719 | "output_type": "stream", 720 | "text": [ 721 | "length of x = 11\n", 722 | "x[-1] = x[len(x)-1] = d\n", 723 | "x[0] = x[-len(x)] = H\n" 724 | ] 725 | } 726 | ], 727 | "source": [ 728 | "x = 'Hello World'\n", 729 | "\n", 730 | "print('length of x =', len(x))\n", 731 | "print('x[-1] = x[len(x)-1] = ', x[len(x)-1])\n", 732 | "print('x[0] = x[-len(x)] =', x[-len(x)])" 733 | ] 734 | }, 735 | { 736 | "cell_type": "code", 737 | "execution_count": 3, 738 | "metadata": { 739 | "colab": { 740 | "base_uri": "https://localhost:8080/", 741 | "height": 189 742 | }, 743 | "id": "lavZ3XlpN0CN", 744 | "outputId": "c6092f70-2dbf-4e61-e8ec-935a8e9075bf" 745 | }, 746 | "outputs": [ 747 | { 748 | "ename": "IndexError", 749 | "evalue": "ignored", 750 | "output_type": "error", 751 | "traceback": [ 752 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 753 | "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", 754 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 755 | "\u001b[0;31mIndexError\u001b[0m: string index out of range" 756 | ] 757 | } 758 | ], 759 | "source": [ 760 | "print(x[len(x)]) #error\n", 761 | "print(x[-len(x)-1]) #error" 762 | ] 763 | }, 764 | { 765 | "cell_type": "code", 766 | "execution_count": 4, 767 | "metadata": { 768 | "colab": { 769 | "base_uri": "https://localhost:8080/", 770 | "height": 189 771 | }, 772 | "id": "xR5ymLa9N0CN", 773 | "outputId": "9e5959fe-eba4-4917-9865-f57852f6b4f5" 774 | }, 775 | "outputs": [ 776 | { 777 | "ename": "TypeError", 778 | "evalue": "ignored", 779 | "output_type": "error", 780 | "traceback": [ 781 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 782 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 783 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'Hello World'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'a'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 784 | "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" 785 | ] 786 | } 787 | ], 788 | "source": [ 789 | "x = 'Hello World'\n", 790 | "x[1] = 'a'" 791 | ] 792 | }, 793 | { 794 | "cell_type": "code", 795 | "execution_count": 5, 796 | "metadata": { 797 | "colab": { 798 | "base_uri": "https://localhost:8080/" 799 | }, 800 | "id": "BHHVe7pKN0CN", 801 | "outputId": "b6c32b6b-62c5-4404-d956-8eadde15bd09" 802 | }, 803 | "outputs": [ 804 | { 805 | "name": "stdout", 806 | "output_type": "stream", 807 | "text": [ 808 | "Hallo World\n" 809 | ] 810 | } 811 | ], 812 | "source": [ 813 | "x = x[0:1] + 'a' + x[2:]\n", 814 | "print(x)" 815 | ] 816 | }, 817 | { 818 | "cell_type": "code", 819 | "execution_count": 6, 820 | "metadata": { 821 | "colab": { 822 | "base_uri": "https://localhost:8080/" 823 | }, 824 | "id": "_UQ6w2bbN0CN", 825 | "outputId": "8d17c9d7-dcf8-43b7-96ac-60e8adedb1c2" 826 | }, 827 | "outputs": [ 828 | { 829 | "name": "stdout", 830 | "output_type": "stream", 831 | "text": [ 832 | "['1', '2', '3', '4']\n", 833 | "['1', '2', '3', '4']\n" 834 | ] 835 | } 836 | ], 837 | "source": [ 838 | "x = '1 2 3 4'\n", 839 | "print(x.split())\n", 840 | "\n", 841 | "x = '1/2/3/4'\n", 842 | "print(x.split('/'))" 843 | ] 844 | }, 845 | { 846 | "cell_type": "code", 847 | "execution_count": 7, 848 | "metadata": { 849 | "colab": { 850 | "base_uri": "https://localhost:8080/" 851 | }, 852 | "id": "NYSOPf1wN0CO", 853 | "outputId": "87df63cc-d0d2-45b0-eb24-ec89777c0a60" 854 | }, 855 | "outputs": [ 856 | { 857 | "name": "stdout", 858 | "output_type": "stream", 859 | "text": [ 860 | "1+2+3+4\n" 861 | ] 862 | } 863 | ], 864 | "source": [ 865 | "l = ['1','2','3','4']\n", 866 | "s = '+'.join(l)\n", 867 | "print(s)" 868 | ] 869 | }, 870 | { 871 | "cell_type": "code", 872 | "execution_count": 8, 873 | "metadata": { 874 | "colab": { 875 | "base_uri": "https://localhost:8080/" 876 | }, 877 | "id": "bvuFOhq3N0CO", 878 | "outputId": "c0128be3-ead1-4831-d37d-aed37e971b8d" 879 | }, 880 | "outputs": [ 881 | { 882 | "name": "stdout", 883 | "output_type": "stream", 884 | "text": [ 885 | "Hello Mazinger, as you know, if x=1, then 2*x+1=3\n" 886 | ] 887 | } 888 | ], 889 | "source": [ 890 | "x = 1\n", 891 | "y = \"Mazinger\"\n", 892 | "print(f'Hello {y}, as you know, if x={x}, then 2*x+1={2*x+1}')" 893 | ] 894 | }, 895 | { 896 | "cell_type": "markdown", 897 | "metadata": { 898 | "id": "G54HLxgGN0CO" 899 | }, 900 | "source": [ 901 | "# Lists" 902 | ] 903 | }, 904 | { 905 | "cell_type": "code", 906 | "execution_count": null, 907 | "metadata": { 908 | "id": "zP33iH7CN0CP", 909 | "outputId": "7815a4e3-74af-48e5-b7e6-c66ee854f9ab" 910 | }, 911 | "outputs": [ 912 | { 913 | "name": "stdout", 914 | "output_type": "stream", 915 | "text": [ 916 | "x[0] = 1\n", 917 | "x[-1] = (1+5j)\n", 918 | "x[1:3] = [2, 3]\n", 919 | "x[:3] = [1, 2, 3]\n", 920 | "x[1:] = [2, 3, 'hello', 1.2, (1+5j)]\n", 921 | "x[:-1] = [1, 2, 3, 'hello', 1.2]\n", 922 | "x[1:5:2] = [2, 'hello']\n", 923 | "x[::2] = [1, 3, 1.2]\n", 924 | "x[::-1] = [(1+5j), 1.2, 'hello', 3, 2, 1]\n" 925 | ] 926 | } 927 | ], 928 | "source": [ 929 | "x = [1,2,3,'hello',1.2,1+5j]\n", 930 | "\n", 931 | "print(\"x[0] =\",x[0])\n", 932 | "print(\"x[-1] =\", x[-1])\n", 933 | "print(\"x[1:3] = \", x[1:3]) #slice from 1 (inclusive) to 3 (exclusive)\n", 934 | "print(\"x[:3] = \", x[:3])\n", 935 | "print(\"x[1:] = \", x[1:])\n", 936 | "print(\"x[:-1] = \", x[:-1])\n", 937 | "print(\"x[1:5:2] = \", x[1:5:2])\n", 938 | "print(\"x[::2] = \", x[::2])\n", 939 | "print(\"x[::-1] = \", x[::-1])" 940 | ] 941 | }, 942 | { 943 | "cell_type": "code", 944 | "execution_count": null, 945 | "metadata": { 946 | "id": "zhHna9s5N0CP", 947 | "outputId": "1f36a61d-aa63-47e4-ea5f-325d39b6b272" 948 | }, 949 | "outputs": [ 950 | { 951 | "name": "stdout", 952 | "output_type": "stream", 953 | "text": [ 954 | "[1, 'a', 3, 4, 5]\n", 955 | "[1, 'add', 'this', 5]\n", 956 | "[1, 5]\n" 957 | ] 958 | } 959 | ], 960 | "source": [ 961 | "x = [1,2,3,4,5]\n", 962 | "\n", 963 | "x[1] = 'a'\n", 964 | "print(x)\n", 965 | "x[1:-1] = ['add', 'this']\n", 966 | "print(x)\n", 967 | "x[1:-1] = []\n", 968 | "print(x)" 969 | ] 970 | }, 971 | { 972 | "cell_type": "code", 973 | "execution_count": null, 974 | "metadata": { 975 | "id": "ePu4WAPcN0CP", 976 | "outputId": "4c49baee-2f24-4aa2-9633-3895c657f5f1" 977 | }, 978 | "outputs": [ 979 | { 980 | "name": "stdout", 981 | "output_type": "stream", 982 | "text": [ 983 | "x = [1, 'edit', 3, 4, 5]\n", 984 | "y = [1, 'edit', 3, 4, 5]\n" 985 | ] 986 | } 987 | ], 988 | "source": [ 989 | "x = [1,2,3,4,5]\n", 990 | "y = x\n", 991 | "y[1] = 'edit'\n", 992 | "print('x = ', x)\n", 993 | "print('y = ', y)" 994 | ] 995 | }, 996 | { 997 | "cell_type": "code", 998 | "execution_count": null, 999 | "metadata": { 1000 | "id": "xR3cbYmgN0CQ", 1001 | "outputId": "cacd2489-0557-41d7-bb09-0799f30b5789" 1002 | }, 1003 | "outputs": [ 1004 | { 1005 | "name": "stdout", 1006 | "output_type": "stream", 1007 | "text": [ 1008 | "x = [1, 2, 3, 4, 5]\n", 1009 | "y = [1, 'edit', 3, 4, 5]\n" 1010 | ] 1011 | } 1012 | ], 1013 | "source": [ 1014 | "x = [1,2,3,4,5]\n", 1015 | "y = x[:]\n", 1016 | "y[1] = 'edit'\n", 1017 | "print('x = ', x)\n", 1018 | "print('y = ', y)" 1019 | ] 1020 | }, 1021 | { 1022 | "cell_type": "code", 1023 | "execution_count": null, 1024 | "metadata": { 1025 | "id": "P1r3Dac4N0CQ", 1026 | "outputId": "94ca0267-a0d3-41fa-cabc-04b6d1ee8e59" 1027 | }, 1028 | "outputs": [ 1029 | { 1030 | "name": "stdout", 1031 | "output_type": "stream", 1032 | "text": [ 1033 | "[1, 2, 3, 4, 5, 'last']\n", 1034 | "['first', 1, 2, 3, 4, 5, 'last']\n", 1035 | "['first', 1, 2, 'middle', 3, 4, 5, 'last']\n" 1036 | ] 1037 | } 1038 | ], 1039 | "source": [ 1040 | "x = [1,2,3,4,5]\n", 1041 | "x.append('last')\n", 1042 | "print(x)\n", 1043 | "x.insert(0, 'first')\n", 1044 | "print(x)\n", 1045 | "x.insert(len(x)//2, 'middle')\n", 1046 | "print(x)" 1047 | ] 1048 | }, 1049 | { 1050 | "cell_type": "code", 1051 | "execution_count": null, 1052 | "metadata": { 1053 | "id": "CrW1ujyCN0CQ", 1054 | "outputId": "a0b60355-8495-49cc-a193-9fa67613a7ba" 1055 | }, 1056 | "outputs": [ 1057 | { 1058 | "name": "stdout", 1059 | "output_type": "stream", 1060 | "text": [ 1061 | "[1, 2, 3, 4]\n", 1062 | "[2, 3, 4]\n", 1063 | "[2, 4]\n" 1064 | ] 1065 | } 1066 | ], 1067 | "source": [ 1068 | "x = [1,2,3,4,5]\n", 1069 | "\n", 1070 | "x.pop()\n", 1071 | "print(x)\n", 1072 | "x.pop(0)\n", 1073 | "print(x)\n", 1074 | "x.pop(len(x)//2)\n", 1075 | "print(x)" 1076 | ] 1077 | }, 1078 | { 1079 | "cell_type": "code", 1080 | "execution_count": null, 1081 | "metadata": { 1082 | "id": "XIh447Q-N0CQ", 1083 | "outputId": "29ef1817-6f23-43d0-c7c9-52fddb51338d", 1084 | "scrolled": true 1085 | }, 1086 | "outputs": [ 1087 | { 1088 | "name": "stdout", 1089 | "output_type": "stream", 1090 | "text": [ 1091 | "count of 1 = 3\n", 1092 | "max = 3\n", 1093 | "min = 1\n", 1094 | "first index of 2 = 1\n", 1095 | "reversed = [1, 2, 1, 3, 2, 1]\n", 1096 | "x didn't change = [1, 2, 3, 1, 2, 1]\n", 1097 | "reversed inplace = [1, 2, 1, 3, 2, 1]\n", 1098 | "sorted = [1, 1, 1, 2, 2, 3]\n", 1099 | "x didn't change = [1, 2, 1, 3, 2, 1]\n", 1100 | "sorted inplace = [1, 1, 1, 2, 2, 3]\n", 1101 | "with [4,5] = [1, 1, 1, 2, 2, 3, 4, 5]\n", 1102 | "with 2 = [1, 1, 1, 2, 3, 4, 5]\n" 1103 | ] 1104 | } 1105 | ], 1106 | "source": [ 1107 | "x = [1,2,3,1,2,1]\n", 1108 | "\n", 1109 | "print('count of 1 =', x.count(1))\n", 1110 | "print('max =', max(x))\n", 1111 | "print('min =', min(x))\n", 1112 | "print('first index of 2 =', x.index(2))\n", 1113 | "\n", 1114 | "#reversing\n", 1115 | "print('reversed =', list(reversed(x)))\n", 1116 | "print('x didn\\'t change =', x)\n", 1117 | "x.reverse() #done inplace\n", 1118 | "print('reversed inplace =', x)\n", 1119 | "\n", 1120 | "#sorting\n", 1121 | "print('sorted =', list(sorted(x)))\n", 1122 | "print('x didn\\'t change =', x)\n", 1123 | "x.sort() #done inplace\n", 1124 | "print('sorted inplace =', x)\n", 1125 | "\n", 1126 | "x.extend([4, 5])\n", 1127 | "print('with [4,5] =', x)\n", 1128 | "x.remove(2)\n", 1129 | "print('with 2 =', x)" 1130 | ] 1131 | }, 1132 | { 1133 | "cell_type": "code", 1134 | "execution_count": null, 1135 | "metadata": { 1136 | "id": "dphWhNTbN0CR", 1137 | "outputId": "bb97bbc1-af2b-4021-8113-274c86ca37d1" 1138 | }, 1139 | "outputs": [ 1140 | { 1141 | "name": "stdout", 1142 | "output_type": "stream", 1143 | "text": [ 1144 | "[1, 2, 3, 1, 2, 1]\n", 1145 | "1 2 3 1 2 1\n" 1146 | ] 1147 | } 1148 | ], 1149 | "source": [ 1150 | "x = [1,2,3,1,2,1]\n", 1151 | "print(x)\n", 1152 | "print(*x)" 1153 | ] 1154 | }, 1155 | { 1156 | "cell_type": "code", 1157 | "execution_count": null, 1158 | "metadata": { 1159 | "id": "8AKEZoqaN0CR", 1160 | "outputId": "a6cfa44d-0f65-45d2-cc22-0587690d8a60" 1161 | }, 1162 | "outputs": [ 1163 | { 1164 | "name": "stdout", 1165 | "output_type": "stream", 1166 | "text": [ 1167 | "[1, 2, 3, 9, 8, 7]\n", 1168 | "[1, 2, 3, 5, 9, 8, 7]\n", 1169 | "[1, 9, 8, 7, 1, 2, 3]\n" 1170 | ] 1171 | } 1172 | ], 1173 | "source": [ 1174 | "x = [1,2,3]\n", 1175 | "y = [9,8,7]\n", 1176 | "print([*x, *y])\n", 1177 | "print([*x, 5, *y])\n", 1178 | "print([1, *y, *x])" 1179 | ] 1180 | }, 1181 | { 1182 | "cell_type": "markdown", 1183 | "metadata": { 1184 | "id": "wC4B6hGtN0CR" 1185 | }, 1186 | "source": [ 1187 | "# Tuples" 1188 | ] 1189 | }, 1190 | { 1191 | "cell_type": "markdown", 1192 | "metadata": { 1193 | "id": "b3eSd0jZN0CS" 1194 | }, 1195 | "source": [ 1196 | "### Tuple are similar to lists but can not be edited after creation similar to string" 1197 | ] 1198 | }, 1199 | { 1200 | "cell_type": "code", 1201 | "execution_count": null, 1202 | "metadata": { 1203 | "id": "jcA-wr0pN0CT", 1204 | "outputId": "f69962fc-fe86-4c3f-8e72-b42053d607b1" 1205 | }, 1206 | "outputs": [ 1207 | { 1208 | "name": "stdout", 1209 | "output_type": "stream", 1210 | "text": [ 1211 | "(1, 2)\n", 1212 | "(1, 2)\n" 1213 | ] 1214 | } 1215 | ], 1216 | "source": [ 1217 | "x = (1,2)\n", 1218 | "print(x)\n", 1219 | "x = 1,2\n", 1220 | "print(x)" 1221 | ] 1222 | }, 1223 | { 1224 | "cell_type": "code", 1225 | "execution_count": null, 1226 | "metadata": { 1227 | "id": "DZ7BU_-jN0CT", 1228 | "outputId": "4b3742ee-3416-44f1-d2a9-ed407edf200f" 1229 | }, 1230 | "outputs": [ 1231 | { 1232 | "name": "stdout", 1233 | "output_type": "stream", 1234 | "text": [ 1235 | "1 2\n" 1236 | ] 1237 | } 1238 | ], 1239 | "source": [ 1240 | "x = (1,2)\n", 1241 | "a, b = x\n", 1242 | "print(a, b)" 1243 | ] 1244 | }, 1245 | { 1246 | "cell_type": "code", 1247 | "execution_count": null, 1248 | "metadata": { 1249 | "id": "5dZu52ICN0CT", 1250 | "outputId": "45979c43-aa32-4b76-e5fd-4bf4a14e6ee6" 1251 | }, 1252 | "outputs": [ 1253 | { 1254 | "name": "stdout", 1255 | "output_type": "stream", 1256 | "text": [ 1257 | "1 2\n", 1258 | "2 1\n" 1259 | ] 1260 | } 1261 | ], 1262 | "source": [ 1263 | "x, y = 1, 2\n", 1264 | "print(x, y)\n", 1265 | "x, y = y, x #swapping\n", 1266 | "print(x, y)" 1267 | ] 1268 | }, 1269 | { 1270 | "cell_type": "code", 1271 | "execution_count": null, 1272 | "metadata": { 1273 | "id": "YuVEx2dnN0CT", 1274 | "outputId": "6caf5325-5fe7-42d5-d28d-347ced1f27ea" 1275 | }, 1276 | "outputs": [ 1277 | { 1278 | "name": "stdout", 1279 | "output_type": "stream", 1280 | "text": [ 1281 | "[1, 2, 3] (1, 2, 3)\n", 1282 | "(1, 2, 3) [1, 2, 3]\n" 1283 | ] 1284 | } 1285 | ], 1286 | "source": [ 1287 | "x = [1, 2, 3]\n", 1288 | "print(x, tuple(x))\n", 1289 | "x = (1,2,3)\n", 1290 | "print(x, list(x))" 1291 | ] 1292 | }, 1293 | { 1294 | "cell_type": "code", 1295 | "execution_count": null, 1296 | "metadata": { 1297 | "id": "CCTLjPyaN0CU", 1298 | "outputId": "25473e4c-42b3-4eab-c449-d7120f6df19f" 1299 | }, 1300 | "outputs": [ 1301 | { 1302 | "name": "stdout", 1303 | "output_type": "stream", 1304 | "text": [ 1305 | "x==y = True\n", 1306 | "x is y = True\n" 1307 | ] 1308 | } 1309 | ], 1310 | "source": [ 1311 | "x = (1,2,3)\n", 1312 | "y = tuple(x)\n", 1313 | "print('x==y =', x==y)\n", 1314 | "print('x is y =', x is y)" 1315 | ] 1316 | }, 1317 | { 1318 | "cell_type": "code", 1319 | "execution_count": null, 1320 | "metadata": { 1321 | "id": "R0sZDSUZN0CU", 1322 | "outputId": "e7258094-830d-4df6-89e3-c586fc54d873" 1323 | }, 1324 | "outputs": [ 1325 | { 1326 | "name": "stdout", 1327 | "output_type": "stream", 1328 | "text": [ 1329 | "x==y = True\n", 1330 | "x is y = False\n" 1331 | ] 1332 | } 1333 | ], 1334 | "source": [ 1335 | "x = [1,2,3]\n", 1336 | "y = list(x)\n", 1337 | "print('x==y =', x==y)\n", 1338 | "print('x is y =', x is y)" 1339 | ] 1340 | }, 1341 | { 1342 | "cell_type": "code", 1343 | "execution_count": null, 1344 | "metadata": { 1345 | "id": "i_Y7edNXN0CU", 1346 | "outputId": "535f8f55-43a2-4952-ec04-ddeaa96dec1c" 1347 | }, 1348 | "outputs": [ 1349 | { 1350 | "name": "stdout", 1351 | "output_type": "stream", 1352 | "text": [ 1353 | "(1, 2, 3, 4, 5, 6, 7)\n" 1354 | ] 1355 | } 1356 | ], 1357 | "source": [ 1358 | "x = (1,2,3)\n", 1359 | "y = (4,5,6)\n", 1360 | "print((*x, *y, 7))" 1361 | ] 1362 | }, 1363 | { 1364 | "cell_type": "markdown", 1365 | "metadata": { 1366 | "id": "vWZNFFdJN0CV" 1367 | }, 1368 | "source": [ 1369 | "# Sets" 1370 | ] 1371 | }, 1372 | { 1373 | "cell_type": "code", 1374 | "execution_count": null, 1375 | "metadata": { 1376 | "id": "nLB-IreDN0CV", 1377 | "outputId": "69b948c3-4d49-4c43-b8e0-278b9293bf2c" 1378 | }, 1379 | "outputs": [ 1380 | { 1381 | "name": "stdout", 1382 | "output_type": "stream", 1383 | "text": [ 1384 | "set()\n", 1385 | "{1, 2, 3}\n" 1386 | ] 1387 | } 1388 | ], 1389 | "source": [ 1390 | "x = set() #empty set\n", 1391 | "print(x)\n", 1392 | "x = {1,2,3}\n", 1393 | "print(x)" 1394 | ] 1395 | }, 1396 | { 1397 | "cell_type": "code", 1398 | "execution_count": null, 1399 | "metadata": { 1400 | "id": "oYknk3GYN0CV", 1401 | "outputId": "a42cfdcd-5536-489d-b398-7b82895d5ddc" 1402 | }, 1403 | "outputs": [ 1404 | { 1405 | "name": "stdout", 1406 | "output_type": "stream", 1407 | "text": [ 1408 | "{1, 2, 3}\n" 1409 | ] 1410 | } 1411 | ], 1412 | "source": [ 1413 | "x = [1,2,3,1,2,1]\n", 1414 | "print(set(x)) #sets are unique" 1415 | ] 1416 | }, 1417 | { 1418 | "cell_type": "code", 1419 | "execution_count": null, 1420 | "metadata": { 1421 | "id": "7eSBcCh3N0CW", 1422 | "outputId": "050e2690-265d-4ba7-9ac9-7a283d2aea08" 1423 | }, 1424 | "outputs": [ 1425 | { 1426 | "ename": "TypeError", 1427 | "evalue": "'set' object does not support indexing", 1428 | "output_type": "error", 1429 | "traceback": [ 1430 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1431 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 1432 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\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[0mx\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;31m#error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 1433 | "\u001b[1;31mTypeError\u001b[0m: 'set' object does not support indexing" 1434 | ] 1435 | } 1436 | ], 1437 | "source": [ 1438 | "x = {1,2,3}\n", 1439 | "x[0] #error" 1440 | ] 1441 | }, 1442 | { 1443 | "cell_type": "code", 1444 | "execution_count": null, 1445 | "metadata": { 1446 | "id": "MYowNv1cN0CW", 1447 | "outputId": "50ba2f38-21a6-4de3-94fb-511ed60ece4a" 1448 | }, 1449 | "outputs": [ 1450 | { 1451 | "name": "stdout", 1452 | "output_type": "stream", 1453 | "text": [ 1454 | "union = {1, 2, 3, 4}\n", 1455 | "intersection = {1}\n", 1456 | "difference = {2, 3}\n" 1457 | ] 1458 | } 1459 | ], 1460 | "source": [ 1461 | "x, y = {1,2,3}, {1,4}\n", 1462 | "print('union =', x | y)\n", 1463 | "print('intersection =', x & y)\n", 1464 | "print('difference =', x - y)" 1465 | ] 1466 | }, 1467 | { 1468 | "cell_type": "code", 1469 | "execution_count": null, 1470 | "metadata": { 1471 | "id": "mCQauEZEN0CW", 1472 | "outputId": "f6024413-ac48-4ff8-c2d6-b9cf53c39a5c" 1473 | }, 1474 | "outputs": [ 1475 | { 1476 | "name": "stdout", 1477 | "output_type": "stream", 1478 | "text": [ 1479 | "{1, 2, 3}\n", 1480 | "{1, 2, 3}\n", 1481 | "{1, 2, 3, 4}\n", 1482 | "{2, 3, 4}\n", 1483 | "length = 3\n" 1484 | ] 1485 | } 1486 | ], 1487 | "source": [ 1488 | "x = {1,2,3}\n", 1489 | "print(x)\n", 1490 | "x.add(1)\n", 1491 | "print(x)\n", 1492 | "x.add(4)\n", 1493 | "print(x)\n", 1494 | "x.remove(1)\n", 1495 | "print(x)\n", 1496 | "print('length =', len(x))" 1497 | ] 1498 | }, 1499 | { 1500 | "cell_type": "code", 1501 | "execution_count": null, 1502 | "metadata": { 1503 | "id": "2-Gbn0qBN0CX", 1504 | "outputId": "6357e837-107f-43ef-9af1-d55380c9f7d0" 1505 | }, 1506 | "outputs": [ 1507 | { 1508 | "name": "stdout", 1509 | "output_type": "stream", 1510 | "text": [ 1511 | "x==y = True\n", 1512 | "x is y = False\n" 1513 | ] 1514 | } 1515 | ], 1516 | "source": [ 1517 | "x = {1,2,3}\n", 1518 | "y = set(x)\n", 1519 | "print('x==y =', x==y)\n", 1520 | "print('x is y =', x is y)" 1521 | ] 1522 | }, 1523 | { 1524 | "cell_type": "code", 1525 | "execution_count": null, 1526 | "metadata": { 1527 | "id": "kRKhWmajN0CX", 1528 | "outputId": "71eb2da0-8e33-44b2-a9fa-984bfcffc3f5" 1529 | }, 1530 | "outputs": [ 1531 | { 1532 | "name": "stdout", 1533 | "output_type": "stream", 1534 | "text": [ 1535 | "{1, 2, 3, 4}\n" 1536 | ] 1537 | } 1538 | ], 1539 | "source": [ 1540 | "x = {1, 2, 3}\n", 1541 | "y = {2, 3, 4}\n", 1542 | "print({*x, *y})" 1543 | ] 1544 | }, 1545 | { 1546 | "cell_type": "markdown", 1547 | "metadata": { 1548 | "id": "b1nz2vMaN0CX" 1549 | }, 1550 | "source": [ 1551 | "# Dictionaries" 1552 | ] 1553 | }, 1554 | { 1555 | "cell_type": "code", 1556 | "execution_count": null, 1557 | "metadata": { 1558 | "id": "EFeJxI2PN0CX", 1559 | "outputId": "9cfd7ebe-6a8b-448c-c230-e45a57a0fec3" 1560 | }, 1561 | "outputs": [ 1562 | { 1563 | "name": "stdout", 1564 | "output_type": "stream", 1565 | "text": [ 1566 | "{}\n", 1567 | "{}\n", 1568 | "{'a': 1, 2: 'Hello', (1, 2): 'world'}\n" 1569 | ] 1570 | } 1571 | ], 1572 | "source": [ 1573 | "x = {} #empty dict\n", 1574 | "print(x)\n", 1575 | "x = dict() #empty dict\n", 1576 | "print(x)\n", 1577 | "x = {'a':1, 2:'Hello', (1,2):'world'}\n", 1578 | "print(x)" 1579 | ] 1580 | }, 1581 | { 1582 | "cell_type": "code", 1583 | "execution_count": null, 1584 | "metadata": { 1585 | "id": "nIP8ZZm2N0CY", 1586 | "outputId": "c09c56ad-a703-49e6-af48-9811043c9939" 1587 | }, 1588 | "outputs": [ 1589 | { 1590 | "name": "stdout", 1591 | "output_type": "stream", 1592 | "text": [ 1593 | "1\n", 1594 | "Hello\n", 1595 | "default\n" 1596 | ] 1597 | } 1598 | ], 1599 | "source": [ 1600 | "x = {'a':1, 2:'Hello', (1,2):'world'}\n", 1601 | "print(x['a'])\n", 1602 | "print(x[2])\n", 1603 | "print(x.get(1, 'default')) #use default if key doesn't exist" 1604 | ] 1605 | }, 1606 | { 1607 | "cell_type": "code", 1608 | "execution_count": null, 1609 | "metadata": { 1610 | "id": "Rpdsee1uN0CY", 1611 | "outputId": "bb2c8913-6e59-4033-d2df-b034ea148a29" 1612 | }, 1613 | "outputs": [ 1614 | { 1615 | "name": "stdout", 1616 | "output_type": "stream", 1617 | "text": [ 1618 | "{'a': 'String A', 1: 'Integer 1', (1, 2): 'It takes tuples too', None: 'And Nones', False: 'And Booleans'}\n" 1619 | ] 1620 | } 1621 | ], 1622 | "source": [ 1623 | "x = {}\n", 1624 | "x['a'] = 'String A'\n", 1625 | "x[1] = 'Integer 1'\n", 1626 | "x[(1,2)] = \"It takes tuples too\"\n", 1627 | "x[None] = 'And Nones'\n", 1628 | "x[False] = 'And Booleans'\n", 1629 | "print(x)" 1630 | ] 1631 | }, 1632 | { 1633 | "cell_type": "code", 1634 | "execution_count": 1, 1635 | "metadata": { 1636 | "id": "r_zm35FAN0CY", 1637 | "outputId": "4f521cdc-29b1-4736-a3d8-713591ffdb37" 1638 | }, 1639 | "outputs": [ 1640 | { 1641 | "ename": "TypeError", 1642 | "evalue": "unhashable type: 'set'", 1643 | "output_type": "error", 1644 | "traceback": [ 1645 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 1646 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 1647 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m#x[[1,2,3]] = 'No Lists :(' #error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"No Sets :(\"\u001b[0m \u001b[0;31m#error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;31m#x[x] = 'No Dicts :(' #error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 1648 | "\u001b[0;31mTypeError\u001b[0m: unhashable type: 'set'" 1649 | ] 1650 | } 1651 | ], 1652 | "source": [ 1653 | "x = {}\n", 1654 | "#x[[1,2,3]] = 'No Lists :(' #error\n", 1655 | "x[{1,2,3}] = \"No Sets :(\" #error\n", 1656 | "#x[x] = 'No Dicts :(' #error" 1657 | ] 1658 | }, 1659 | { 1660 | "cell_type": "code", 1661 | "execution_count": null, 1662 | "metadata": { 1663 | "id": "5hdrkPAtN0CZ", 1664 | "outputId": "740203e6-3b97-46d9-a868-610c9f0b4507" 1665 | }, 1666 | "outputs": [ 1667 | { 1668 | "name": "stdout", 1669 | "output_type": "stream", 1670 | "text": [ 1671 | "{'a': 1, 2: 'Hello', (1, 2): 'world'}\n", 1672 | "{2: 'Hello', (1, 2): 'world'}\n" 1673 | ] 1674 | } 1675 | ], 1676 | "source": [ 1677 | "x = {'a':1, 2:'Hello', (1,2):'world'}\n", 1678 | "print(x)\n", 1679 | "del x['a']\n", 1680 | "print(x)" 1681 | ] 1682 | }, 1683 | { 1684 | "cell_type": "code", 1685 | "execution_count": null, 1686 | "metadata": { 1687 | "id": "3ynU9EF0N0CZ", 1688 | "outputId": "eaa14d21-0f9e-45c5-a922-433baa6a3e0d" 1689 | }, 1690 | "outputs": [ 1691 | { 1692 | "name": "stdout", 1693 | "output_type": "stream", 1694 | "text": [ 1695 | "{'a': 'another', 2: 'Hello', (1, 2): 'world', 'b': 100}\n", 1696 | "{'a': 'another', 2: 'Hello', (1, 2): 'world', 'b': 100, 'bye': 10}\n" 1697 | ] 1698 | } 1699 | ], 1700 | "source": [ 1701 | "x = {'a':1, 2:'Hello', (1,2):'world'}\n", 1702 | "y = {'a':'another', 'b':100}\n", 1703 | "x.update(**y)\n", 1704 | "print(x)\n", 1705 | "x.update(bye=10)\n", 1706 | "print(x)" 1707 | ] 1708 | }, 1709 | { 1710 | "cell_type": "code", 1711 | "execution_count": null, 1712 | "metadata": { 1713 | "id": "oHKJKQMZN0CZ", 1714 | "outputId": "ade92465-0106-4354-e3cb-b6ee3d5bd2bf" 1715 | }, 1716 | "outputs": [ 1717 | { 1718 | "name": "stdout", 1719 | "output_type": "stream", 1720 | "text": [ 1721 | "{'a': 1, 2: 'Hello', (1, 2): 'world'}\n", 1722 | "['a', 2, (1, 2)]\n", 1723 | "dict_keys(['a', 2, (1, 2)])\n", 1724 | "dict_values([1, 'Hello', 'world'])\n", 1725 | "dict_items([('a', 1), (2, 'Hello'), ((1, 2), 'world')])\n" 1726 | ] 1727 | } 1728 | ], 1729 | "source": [ 1730 | "x = [('a',1), (2,'Hello'), ((1,2),'world')]\n", 1731 | "y = dict(x)\n", 1732 | "print(y)\n", 1733 | "print(list(y))\n", 1734 | "print(y.keys())\n", 1735 | "print(y.values())\n", 1736 | "print(y.items())" 1737 | ] 1738 | }, 1739 | { 1740 | "cell_type": "code", 1741 | "execution_count": null, 1742 | "metadata": { 1743 | "id": "O4BmYXA8N0CZ", 1744 | "outputId": "575d3e0f-e068-4606-abc3-e5bfd5096671" 1745 | }, 1746 | "outputs": [ 1747 | { 1748 | "name": "stdout", 1749 | "output_type": "stream", 1750 | "text": [ 1751 | "{'a': 1, 'b': 6, 'c': 7}\n", 1752 | "{'b': 2, 'c': 9, 'a': 1}\n" 1753 | ] 1754 | } 1755 | ], 1756 | "source": [ 1757 | "x = {'a':1, 'b':2}\n", 1758 | "y = {'b':6, 'c':7}\n", 1759 | "print({**x, **y})\n", 1760 | "print({**y, **x, 'c':9})" 1761 | ] 1762 | }, 1763 | { 1764 | "cell_type": "markdown", 1765 | "metadata": { 1766 | "id": "QxB4aaFmN0Ca" 1767 | }, 1768 | "source": [ 1769 | "# None" 1770 | ] 1771 | }, 1772 | { 1773 | "cell_type": "code", 1774 | "execution_count": null, 1775 | "metadata": { 1776 | "id": "ShkzjeuUN0Ca", 1777 | "outputId": "2a3512a1-b958-4e3e-b6cc-5f593909956f" 1778 | }, 1779 | "outputs": [ 1780 | { 1781 | "name": "stdout", 1782 | "output_type": "stream", 1783 | "text": [ 1784 | "None\n" 1785 | ] 1786 | } 1787 | ], 1788 | "source": [ 1789 | "x = None\n", 1790 | "print(x)" 1791 | ] 1792 | }, 1793 | { 1794 | "cell_type": "code", 1795 | "execution_count": null, 1796 | "metadata": { 1797 | "id": "wbBDU8B5N0Ca", 1798 | "outputId": "c1f65027-5cfe-4da4-b5a8-d1ad0eaafabf" 1799 | }, 1800 | "outputs": [ 1801 | { 1802 | "name": "stdout", 1803 | "output_type": "stream", 1804 | "text": [ 1805 | "True\n", 1806 | "False\n" 1807 | ] 1808 | } 1809 | ], 1810 | "source": [ 1811 | "x = None\n", 1812 | "print(x is None)\n", 1813 | "x = 1\n", 1814 | "print(x is None)" 1815 | ] 1816 | }, 1817 | { 1818 | "cell_type": "code", 1819 | "execution_count": null, 1820 | "metadata": { 1821 | "id": "PwB0yG0xN0Cb", 1822 | "outputId": "38044908-e9b9-4305-865b-a557ced7b27b" 1823 | }, 1824 | "outputs": [ 1825 | { 1826 | "name": "stdout", 1827 | "output_type": "stream", 1828 | "text": [ 1829 | "Hello\n", 1830 | "Something\n" 1831 | ] 1832 | } 1833 | ], 1834 | "source": [ 1835 | "x = None\n", 1836 | "print(x or \"Hello\")\n", 1837 | "x = 'Something'\n", 1838 | "print(x or \"Hello\")" 1839 | ] 1840 | }, 1841 | { 1842 | "cell_type": "markdown", 1843 | "metadata": { 1844 | "id": "Ln_gwh-WN0Cb" 1845 | }, 1846 | "source": [ 1847 | "# IF Conditions" 1848 | ] 1849 | }, 1850 | { 1851 | "cell_type": "code", 1852 | "execution_count": null, 1853 | "metadata": { 1854 | "id": "3TMJ1QV5N0Cb" 1855 | }, 1856 | "outputs": [], 1857 | "source": [ 1858 | "y = 5\n", 1859 | "if y > 10:\n", 1860 | " print('y is less than 10')\n", 1861 | " print('Hello')" 1862 | ] 1863 | }, 1864 | { 1865 | "cell_type": "code", 1866 | "execution_count": null, 1867 | "metadata": { 1868 | "id": "jjNe5HptN0Cb", 1869 | "outputId": "ccc76fdb-cd1b-4dc4-e618-ccdc6315ccbc" 1870 | }, 1871 | "outputs": [ 1872 | { 1873 | "name": "stdout", 1874 | "output_type": "stream", 1875 | "text": [ 1876 | "y is larger than 10\n" 1877 | ] 1878 | } 1879 | ], 1880 | "source": [ 1881 | "y = 15\n", 1882 | "if y < 10:\n", 1883 | " print('y is less than 10')\n", 1884 | "else:\n", 1885 | " print('y is larger than 10')" 1886 | ] 1887 | }, 1888 | { 1889 | "cell_type": "code", 1890 | "execution_count": null, 1891 | "metadata": { 1892 | "id": "n1yasPm_N0Cc", 1893 | "outputId": "ef044d80-bc07-49e6-b6e4-2b30a5425c0b" 1894 | }, 1895 | "outputs": [ 1896 | { 1897 | "name": "stdout", 1898 | "output_type": "stream", 1899 | "text": [ 1900 | "y is less than 20 and larger than 10\n" 1901 | ] 1902 | } 1903 | ], 1904 | "source": [ 1905 | "y = 15\n", 1906 | "if y < 10:\n", 1907 | " print('y is less than 10')\n", 1908 | "elif y < 20:\n", 1909 | " print('y is less than 20 and larger than 10')\n", 1910 | "else:\n", 1911 | " print('y is larger than 20')" 1912 | ] 1913 | }, 1914 | { 1915 | "cell_type": "code", 1916 | "execution_count": null, 1917 | "metadata": { 1918 | "id": "tTOUnC3RN0Cc" 1919 | }, 1920 | "outputs": [], 1921 | "source": [ 1922 | "y = 15\n", 1923 | "if y < 10:\n", 1924 | " print('y is less than 10')\n", 1925 | "elif y < 20:\n", 1926 | " pass\n", 1927 | "else:\n", 1928 | " print('y is larger than 20')" 1929 | ] 1930 | }, 1931 | { 1932 | "cell_type": "code", 1933 | "execution_count": null, 1934 | "metadata": { 1935 | "id": "4s0zL78zN0Cc", 1936 | "outputId": "9cc2a70f-8d87-4f30-b168-39b822cf4873" 1937 | }, 1938 | "outputs": [ 1939 | { 1940 | "name": "stdout", 1941 | "output_type": "stream", 1942 | "text": [ 1943 | "y is larger than 10\n" 1944 | ] 1945 | } 1946 | ], 1947 | "source": [ 1948 | "y = 15\n", 1949 | "print('y is less than 10' if y < 10 else 'y is larger than 10')" 1950 | ] 1951 | }, 1952 | { 1953 | "cell_type": "markdown", 1954 | "metadata": { 1955 | "id": "Aabi0zZwN0Cc" 1956 | }, 1957 | "source": [ 1958 | "# While Loops" 1959 | ] 1960 | }, 1961 | { 1962 | "cell_type": "code", 1963 | "execution_count": null, 1964 | "metadata": { 1965 | "id": "w87Un0xYN0Cd", 1966 | "outputId": "bea09999-70b2-4b78-e516-28979d02c833" 1967 | }, 1968 | "outputs": [ 1969 | { 1970 | "name": "stdout", 1971 | "output_type": "stream", 1972 | "text": [ 1973 | "1\n", 1974 | "2\n", 1975 | "4\n", 1976 | "8\n", 1977 | "16\n", 1978 | "32\n", 1979 | "64\n", 1980 | "128\n", 1981 | "256\n", 1982 | "512\n" 1983 | ] 1984 | } 1985 | ], 1986 | "source": [ 1987 | "i = 1\n", 1988 | "while i < 1000:\n", 1989 | " print(i)\n", 1990 | " i *= 2" 1991 | ] 1992 | }, 1993 | { 1994 | "cell_type": "code", 1995 | "execution_count": null, 1996 | "metadata": { 1997 | "id": "_csOk2E7N0Cd", 1998 | "outputId": "e1936842-75da-4058-8809-e178c468a309" 1999 | }, 2000 | "outputs": [ 2001 | { 2002 | "name": "stdout", 2003 | "output_type": "stream", 2004 | "text": [ 2005 | "1\n", 2006 | "2\n", 2007 | "4\n", 2008 | "8\n", 2009 | "16\n", 2010 | "32\n", 2011 | "64\n", 2012 | "128\n", 2013 | "256\n", 2014 | "512\n" 2015 | ] 2016 | } 2017 | ], 2018 | "source": [ 2019 | "i = 1\n", 2020 | "while True:\n", 2021 | " print(i)\n", 2022 | " i *= 2\n", 2023 | " if i >= 1000:\n", 2024 | " break" 2025 | ] 2026 | }, 2027 | { 2028 | "cell_type": "code", 2029 | "execution_count": null, 2030 | "metadata": { 2031 | "id": "3Hp9DqDgN0Cd", 2032 | "outputId": "91127e95-7758-4af9-848f-041468385d91" 2033 | }, 2034 | "outputs": [ 2035 | { 2036 | "name": "stdout", 2037 | "output_type": "stream", 2038 | "text": [ 2039 | "5\n", 2040 | "6\n", 2041 | "7\n", 2042 | "8\n", 2043 | "9\n", 2044 | "10\n" 2045 | ] 2046 | } 2047 | ], 2048 | "source": [ 2049 | "i = 0\n", 2050 | "while i < 10:\n", 2051 | " i+=1\n", 2052 | " if i < 5:\n", 2053 | " continue\n", 2054 | " print(i)" 2055 | ] 2056 | }, 2057 | { 2058 | "cell_type": "code", 2059 | "execution_count": null, 2060 | "metadata": { 2061 | "id": "S9Qkgs3AN0Cd", 2062 | "outputId": "965286f0-116a-4bb6-f33d-03062677d4d1" 2063 | }, 2064 | "outputs": [ 2065 | { 2066 | "name": "stdout", 2067 | "output_type": "stream", 2068 | "text": [ 2069 | "1\n", 2070 | "2\n", 2071 | "3\n", 2072 | "4\n", 2073 | "5\n", 2074 | "You can have an else too by the way\n" 2075 | ] 2076 | } 2077 | ], 2078 | "source": [ 2079 | "i = 0\n", 2080 | "while i < 5:\n", 2081 | " i+=1\n", 2082 | " print(i)\n", 2083 | "else:\n", 2084 | " print('You can have an else too by the way')" 2085 | ] 2086 | }, 2087 | { 2088 | "cell_type": "code", 2089 | "execution_count": null, 2090 | "metadata": { 2091 | "id": "GjaOMQiIN0Ce", 2092 | "outputId": "4288ae23-521a-4773-d40c-fff5e0903f22" 2093 | }, 2094 | "outputs": [ 2095 | { 2096 | "name": "stdout", 2097 | "output_type": "stream", 2098 | "text": [ 2099 | "1\n", 2100 | "2\n", 2101 | "3\n", 2102 | "4\n" 2103 | ] 2104 | } 2105 | ], 2106 | "source": [ 2107 | "i = 0\n", 2108 | "while i < 5:\n", 2109 | " i+=1\n", 2110 | " print(i)\n", 2111 | " if i == 4:\n", 2112 | " break\n", 2113 | "else:\n", 2114 | " print('You can have an else too by the way')" 2115 | ] 2116 | }, 2117 | { 2118 | "cell_type": "markdown", 2119 | "metadata": { 2120 | "id": "rn0OZI2DN0Ce" 2121 | }, 2122 | "source": [ 2123 | "# For loops" 2124 | ] 2125 | }, 2126 | { 2127 | "cell_type": "code", 2128 | "execution_count": null, 2129 | "metadata": { 2130 | "id": "A7jd7lciN0Ce", 2131 | "outputId": "d080ac00-1df8-42f8-c0d2-8ecbe412598c" 2132 | }, 2133 | "outputs": [ 2134 | { 2135 | "name": "stdout", 2136 | "output_type": "stream", 2137 | "text": [ 2138 | "1\n", 2139 | "2.5\n", 2140 | "Hello\n", 2141 | "True\n" 2142 | ] 2143 | } 2144 | ], 2145 | "source": [ 2146 | "items = [1, 2.5, \"Hello\", True]\n", 2147 | "for item in items:\n", 2148 | " print(item)\n", 2149 | "#you can use break and continue here too" 2150 | ] 2151 | }, 2152 | { 2153 | "cell_type": "code", 2154 | "execution_count": null, 2155 | "metadata": { 2156 | "id": "thz283DTN0Ce", 2157 | "outputId": "21c24657-e1bf-4220-8fb1-b2b85d0e0899" 2158 | }, 2159 | "outputs": [ 2160 | { 2161 | "name": "stdout", 2162 | "output_type": "stream", 2163 | "text": [ 2164 | "0\n", 2165 | "1\n", 2166 | "2\n", 2167 | "3\n", 2168 | "4\n", 2169 | "5\n", 2170 | "6\n", 2171 | "7\n", 2172 | "8\n", 2173 | "9\n" 2174 | ] 2175 | } 2176 | ], 2177 | "source": [ 2178 | "for i in range(10):\n", 2179 | " print(i)" 2180 | ] 2181 | }, 2182 | { 2183 | "cell_type": "code", 2184 | "execution_count": null, 2185 | "metadata": { 2186 | "id": "oPVqz7_SN0Cf", 2187 | "outputId": "0e257ad9-48df-4910-e4bf-7dc6387089e4" 2188 | }, 2189 | "outputs": [ 2190 | { 2191 | "name": "stdout", 2192 | "output_type": "stream", 2193 | "text": [ 2194 | "5\n", 2195 | "6\n", 2196 | "7\n", 2197 | "8\n", 2198 | "9\n" 2199 | ] 2200 | } 2201 | ], 2202 | "source": [ 2203 | "for i in range(5,10):\n", 2204 | " print(i)" 2205 | ] 2206 | }, 2207 | { 2208 | "cell_type": "code", 2209 | "execution_count": null, 2210 | "metadata": { 2211 | "id": "h_XkAo6kN0Cf", 2212 | "outputId": "aa53c3d0-d92b-433d-926b-6c5414f5b7b9" 2213 | }, 2214 | "outputs": [ 2215 | { 2216 | "name": "stdout", 2217 | "output_type": "stream", 2218 | "text": [ 2219 | "5\n", 2220 | "7\n", 2221 | "9\n" 2222 | ] 2223 | } 2224 | ], 2225 | "source": [ 2226 | "for i in range(5,10,2):\n", 2227 | " print(i)" 2228 | ] 2229 | }, 2230 | { 2231 | "cell_type": "code", 2232 | "execution_count": null, 2233 | "metadata": { 2234 | "id": "Z2dQ-0gMN0Cg", 2235 | "outputId": "2a9070f9-1ae2-4bcb-f3ca-57fb1479c2b0" 2236 | }, 2237 | "outputs": [ 2238 | { 2239 | "name": "stdout", 2240 | "output_type": "stream", 2241 | "text": [ 2242 | "10\n", 2243 | "9\n", 2244 | "8\n", 2245 | "7\n", 2246 | "6\n" 2247 | ] 2248 | } 2249 | ], 2250 | "source": [ 2251 | "for i in range(10,5,-1):\n", 2252 | " print(i)" 2253 | ] 2254 | }, 2255 | { 2256 | "cell_type": "code", 2257 | "execution_count": null, 2258 | "metadata": { 2259 | "id": "iWAtyazHN0Cg", 2260 | "outputId": "28e56393-9de8-4457-e4de-6c5bece92e05", 2261 | "scrolled": true 2262 | }, 2263 | "outputs": [ 2264 | { 2265 | "name": "stdout", 2266 | "output_type": "stream", 2267 | "text": [ 2268 | "1 a\n", 2269 | "2 b\n", 2270 | "3 c\n" 2271 | ] 2272 | } 2273 | ], 2274 | "source": [ 2275 | "for i, j in [(1,'a'), (2,'b'), (3, 'c')]:\n", 2276 | " print(i, j)" 2277 | ] 2278 | }, 2279 | { 2280 | "cell_type": "code", 2281 | "execution_count": null, 2282 | "metadata": { 2283 | "id": "ZbAdFzgsN0Cg", 2284 | "outputId": "a3877ad3-cf98-4d69-94dd-44517d3876b2" 2285 | }, 2286 | "outputs": [ 2287 | { 2288 | "name": "stdout", 2289 | "output_type": "stream", 2290 | "text": [ 2291 | "1 : a\n", 2292 | "2 : b\n", 2293 | "3 : c\n" 2294 | ] 2295 | } 2296 | ], 2297 | "source": [ 2298 | "d = {1:'a', 2:'b', 3:'c'}\n", 2299 | "for key, value in d.items():\n", 2300 | " print(key,':',value)" 2301 | ] 2302 | }, 2303 | { 2304 | "cell_type": "code", 2305 | "execution_count": null, 2306 | "metadata": { 2307 | "id": "GRDVYGRuN0Ch", 2308 | "outputId": "22969bfa-0e26-4095-9794-1fb08bdb70b3" 2309 | }, 2310 | "outputs": [ 2311 | { 2312 | "name": "stdout", 2313 | "output_type": "stream", 2314 | "text": [ 2315 | "0 : 1\n", 2316 | "1 : 2.5\n", 2317 | "2 : Hello\n", 2318 | "3 : True\n" 2319 | ] 2320 | } 2321 | ], 2322 | "source": [ 2323 | "items = [1, 2.5, \"Hello\", True]\n", 2324 | "for index, item in enumerate(items):\n", 2325 | " print(index,\":\",item)" 2326 | ] 2327 | }, 2328 | { 2329 | "cell_type": "markdown", 2330 | "metadata": { 2331 | "id": "Tpih1OC3N0Ch" 2332 | }, 2333 | "source": [ 2334 | "# List Comprehension" 2335 | ] 2336 | }, 2337 | { 2338 | "cell_type": "code", 2339 | "execution_count": null, 2340 | "metadata": { 2341 | "id": "mtiSmo_cN0Ch", 2342 | "outputId": "87520717-ecc8-4d22-b363-c73f973f523f" 2343 | }, 2344 | "outputs": [ 2345 | { 2346 | "name": "stdout", 2347 | "output_type": "stream", 2348 | "text": [ 2349 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n" 2350 | ] 2351 | } 2352 | ], 2353 | "source": [ 2354 | "x = [i for i in range(10)]\n", 2355 | "print(x)" 2356 | ] 2357 | }, 2358 | { 2359 | "cell_type": "code", 2360 | "execution_count": null, 2361 | "metadata": { 2362 | "id": "o-XX8dTSN0Ch", 2363 | "outputId": "a5feeb7b-d206-45c7-edad-49f713601ad2" 2364 | }, 2365 | "outputs": [ 2366 | { 2367 | "name": "stdout", 2368 | "output_type": "stream", 2369 | "text": [ 2370 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n" 2371 | ] 2372 | } 2373 | ], 2374 | "source": [ 2375 | "x = [i*i for i in range(10)]\n", 2376 | "print(x)" 2377 | ] 2378 | }, 2379 | { 2380 | "cell_type": "code", 2381 | "execution_count": null, 2382 | "metadata": { 2383 | "id": "Y0_EZ6xAN0Ci", 2384 | "outputId": "859f65a7-9916-46af-93ab-49e8385e292f" 2385 | }, 2386 | "outputs": [ 2387 | { 2388 | "name": "stdout", 2389 | "output_type": "stream", 2390 | "text": [ 2391 | "[0, 2, 4, 6, 8]\n" 2392 | ] 2393 | } 2394 | ], 2395 | "source": [ 2396 | "x = [i for i in range(10) if i%2 == 0]\n", 2397 | "print(x)" 2398 | ] 2399 | }, 2400 | { 2401 | "cell_type": "code", 2402 | "execution_count": null, 2403 | "metadata": { 2404 | "id": "9ihLF5LaN0Ci", 2405 | "outputId": "9a8ae2fe-434d-4197-8bca-dba27d0e1e6b" 2406 | }, 2407 | "outputs": [ 2408 | { 2409 | "name": "stdout", 2410 | "output_type": "stream", 2411 | "text": [ 2412 | "[2, 5.0, 'HelloHello', [3, 4, 3, 4]]\n" 2413 | ] 2414 | } 2415 | ], 2416 | "source": [ 2417 | "x = [1,2.5,\"Hello\",[3,4]]\n", 2418 | "y = [i*2 for i in x]\n", 2419 | "print(y)" 2420 | ] 2421 | }, 2422 | { 2423 | "cell_type": "code", 2424 | "execution_count": null, 2425 | "metadata": { 2426 | "id": "PHNrQ3QmN0Ci", 2427 | "outputId": "a8f50ca3-27a8-4213-8396-7ee0a06a637b" 2428 | }, 2429 | "outputs": [ 2430 | { 2431 | "name": "stdout", 2432 | "output_type": "stream", 2433 | "text": [ 2434 | "[[], [0], [0, 1], [0, 1, 2], [0, 1, 2, 3]]\n", 2435 | "[0, 0, 1, 0, 1, 2, 0, 1, 2, 3]\n" 2436 | ] 2437 | } 2438 | ], 2439 | "source": [ 2440 | "x = [[j for j in range(i)] for i in range(5)]\n", 2441 | "print(x)\n", 2442 | "x = [j for i in range(5) for j in range(i)]\n", 2443 | "print(x)" 2444 | ] 2445 | }, 2446 | { 2447 | "cell_type": "code", 2448 | "execution_count": null, 2449 | "metadata": { 2450 | "id": "sWOwftG9N0Cj", 2451 | "outputId": "21affb2d-e3a8-43b4-a33d-4f5a7e0f574c" 2452 | }, 2453 | "outputs": [ 2454 | { 2455 | "name": "stdout", 2456 | "output_type": "stream", 2457 | "text": [ 2458 | "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", 2459 | "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n" 2460 | ] 2461 | } 2462 | ], 2463 | "source": [ 2464 | "l = [[1,2,3],[4,5,6],[7,8,9]]\n", 2465 | "print(l)\n", 2466 | "print([j for i in l for j in i])" 2467 | ] 2468 | }, 2469 | { 2470 | "cell_type": "code", 2471 | "execution_count": null, 2472 | "metadata": { 2473 | "id": "MSGISLECN0Cj", 2474 | "outputId": "fee602e3-5ddd-465d-b4f2-aa0173db13a9" 2475 | }, 2476 | "outputs": [ 2477 | { 2478 | "name": "stdout", 2479 | "output_type": "stream", 2480 | "text": [ 2481 | "[5, 7, 9]\n" 2482 | ] 2483 | } 2484 | ], 2485 | "source": [ 2486 | "a = [1,2,3]\n", 2487 | "b = [4,5,6,7]\n", 2488 | "x = [i+j for i, j in zip(a,b)]\n", 2489 | "print(x)" 2490 | ] 2491 | }, 2492 | { 2493 | "cell_type": "code", 2494 | "execution_count": 11, 2495 | "metadata": { 2496 | "colab": { 2497 | "base_uri": "https://localhost:8080/" 2498 | }, 2499 | "id": "hjqFIrlVN0Cj", 2500 | "outputId": "64698368-b0d7-4b22-a2a3-3bd1ff9a32a3" 2501 | }, 2502 | "outputs": [ 2503 | { 2504 | "name": "stdout", 2505 | "output_type": "stream", 2506 | "text": [ 2507 | "[6, 15, 24]\n", 2508 | "[1, 2, 3] [4, 5, 6] [7, 8, 9]\n", 2509 | "[12, 15, 18]\n" 2510 | ] 2511 | } 2512 | ], 2513 | "source": [ 2514 | "l = [[1,2,3],[4,5,6],[7,8,9]]\n", 2515 | "x = [sum(i) for i in l]\n", 2516 | "print(x)\n", 2517 | "x = [sum(i) for i in zip(*l)]\n", 2518 | "print(x)" 2519 | ] 2520 | }, 2521 | { 2522 | "cell_type": "markdown", 2523 | "metadata": { 2524 | "id": "ccCrL9_yN0Cj" 2525 | }, 2526 | "source": [ 2527 | "# Dictionary Comprehension" 2528 | ] 2529 | }, 2530 | { 2531 | "cell_type": "code", 2532 | "execution_count": null, 2533 | "metadata": { 2534 | "id": "lPjgiTG4N0Ck", 2535 | "outputId": "852cb9d5-d2cd-4343-93a1-b055a992cc0d" 2536 | }, 2537 | "outputs": [ 2538 | { 2539 | "name": "stdout", 2540 | "output_type": "stream", 2541 | "text": [ 2542 | "{1: 4, 2: 5, 3: 6}\n" 2543 | ] 2544 | } 2545 | ], 2546 | "source": [ 2547 | "a = [1,2,3]\n", 2548 | "b = [4,5,6,7]\n", 2549 | "x = {i:j for i, j in zip(a,b)}\n", 2550 | "print(x)" 2551 | ] 2552 | }, 2553 | { 2554 | "cell_type": "code", 2555 | "execution_count": null, 2556 | "metadata": { 2557 | "id": "vkKR26XRN0Ck", 2558 | "outputId": "1570d9b0-7751-4abc-ba39-ba23a69eec7b" 2559 | }, 2560 | "outputs": [ 2561 | { 2562 | "name": "stdout", 2563 | "output_type": "stream", 2564 | "text": [ 2565 | "{1: 'a', 2: 'b', 3: 'c'}\n" 2566 | ] 2567 | } 2568 | ], 2569 | "source": [ 2570 | "d = {'a':1, 'b':2, 'c':3}\n", 2571 | "x = {j:i for i, j in d.items()}\n", 2572 | "print(x)" 2573 | ] 2574 | }, 2575 | { 2576 | "cell_type": "code", 2577 | "execution_count": null, 2578 | "metadata": { 2579 | "id": "dcjF_plSN0Ck", 2580 | "outputId": "88c849ee-5981-462f-a532-ce7fe1bdc031" 2581 | }, 2582 | "outputs": [ 2583 | { 2584 | "name": "stdout", 2585 | "output_type": "stream", 2586 | "text": [ 2587 | "{0: 0, 1: 1, 4: 2, 9: 3, 16: 4, 25: 5, 36: 6, 49: 7, 64: 8, 81: 9}\n" 2588 | ] 2589 | } 2590 | ], 2591 | "source": [ 2592 | "x = {i*i:i for i in range(10)}\n", 2593 | "print(x)" 2594 | ] 2595 | }, 2596 | { 2597 | "cell_type": "markdown", 2598 | "metadata": { 2599 | "id": "c3BT7KzMN0Ck" 2600 | }, 2601 | "source": [ 2602 | "# Set and Tuple Comprehension" 2603 | ] 2604 | }, 2605 | { 2606 | "cell_type": "code", 2607 | "execution_count": null, 2608 | "metadata": { 2609 | "id": "QdxBEbhgN0Cl", 2610 | "outputId": "033f018a-9416-4f4e-bf21-bc5538a92f4f" 2611 | }, 2612 | "outputs": [ 2613 | { 2614 | "name": "stdout", 2615 | "output_type": "stream", 2616 | "text": [ 2617 | "{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}\n" 2618 | ] 2619 | } 2620 | ], 2621 | "source": [ 2622 | "x = {i for i in range(10)}\n", 2623 | "print(x)" 2624 | ] 2625 | }, 2626 | { 2627 | "cell_type": "code", 2628 | "execution_count": null, 2629 | "metadata": { 2630 | "id": "djnZT4kEN0Cl", 2631 | "outputId": "305debd6-444c-4393-f574-7288f36dfc59" 2632 | }, 2633 | "outputs": [ 2634 | { 2635 | "name": "stdout", 2636 | "output_type": "stream", 2637 | "text": [ 2638 | "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n" 2639 | ] 2640 | } 2641 | ], 2642 | "source": [ 2643 | "x = tuple(i for i in range(10))\n", 2644 | "print(x)" 2645 | ] 2646 | }, 2647 | { 2648 | "cell_type": "markdown", 2649 | "metadata": { 2650 | "id": "iJ6VSBBgN0Cl" 2651 | }, 2652 | "source": [ 2653 | "# Functions" 2654 | ] 2655 | }, 2656 | { 2657 | "cell_type": "code", 2658 | "execution_count": null, 2659 | "metadata": { 2660 | "id": "a0V6r7Q3N0Cm", 2661 | "outputId": "c427a860-92be-421e-d316-e9b5d14c800f" 2662 | }, 2663 | "outputs": [ 2664 | { 2665 | "name": "stdout", 2666 | "output_type": "stream", 2667 | "text": [ 2668 | "This is Sparta\n" 2669 | ] 2670 | } 2671 | ], 2672 | "source": [ 2673 | "def print_this(x):\n", 2674 | " print(\"This is\", x)\n", 2675 | "\n", 2676 | "print_this(\"Sparta\")" 2677 | ] 2678 | }, 2679 | { 2680 | "cell_type": "code", 2681 | "execution_count": null, 2682 | "metadata": { 2683 | "id": "PeBXLiUTN0Cm", 2684 | "outputId": "2b54fee2-c71f-4ff9-b633-8a4bbca05c64" 2685 | }, 2686 | "outputs": [ 2687 | { 2688 | "name": "stdout", 2689 | "output_type": "stream", 2690 | "text": [ 2691 | "100\n" 2692 | ] 2693 | } 2694 | ], 2695 | "source": [ 2696 | "def sqr(x):\n", 2697 | " return x*x\n", 2698 | "\n", 2699 | "print(sqr(10))" 2700 | ] 2701 | }, 2702 | { 2703 | "cell_type": "code", 2704 | "execution_count": null, 2705 | "metadata": { 2706 | "id": "X6oc-xcON0Cm", 2707 | "outputId": "30657da9-7f5c-455d-bab3-ddeb3effcdd7" 2708 | }, 2709 | "outputs": [ 2710 | { 2711 | "name": "stdout", 2712 | "output_type": "stream", 2713 | "text": [ 2714 | "1000\n", 2715 | "1000\n", 2716 | "1000\n" 2717 | ] 2718 | } 2719 | ], 2720 | "source": [ 2721 | "def power(x, n):\n", 2722 | " return x**n\n", 2723 | "\n", 2724 | "print(power(10,3))\n", 2725 | "print(power(10,n=3))\n", 2726 | "print(power(n=3,x=10))" 2727 | ] 2728 | }, 2729 | { 2730 | "cell_type": "code", 2731 | "execution_count": null, 2732 | "metadata": { 2733 | "id": "7OzcrqbZN0Cm", 2734 | "outputId": "d32215c0-c249-4c1d-fd4d-13a3ce061061" 2735 | }, 2736 | "outputs": [ 2737 | { 2738 | "ename": "SyntaxError", 2739 | "evalue": "positional argument follows keyword argument (, line 1)", 2740 | "output_type": "error", 2741 | "traceback": [ 2742 | "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m print(power(x=10,3))\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n" 2743 | ] 2744 | } 2745 | ], 2746 | "source": [ 2747 | "print(power(x=10,3))" 2748 | ] 2749 | }, 2750 | { 2751 | "cell_type": "code", 2752 | "execution_count": null, 2753 | "metadata": { 2754 | "id": "aDsstpmeN0Cn", 2755 | "outputId": "d17b83f4-eeeb-484f-c380-561e2bba312d" 2756 | }, 2757 | "outputs": [ 2758 | { 2759 | "name": "stdout", 2760 | "output_type": "stream", 2761 | "text": [ 2762 | "(16, 2.0)\n" 2763 | ] 2764 | } 2765 | ], 2766 | "source": [ 2767 | "def sqr_and_root(x):\n", 2768 | " return x*x, x**0.5\n", 2769 | "\n", 2770 | "print(sqr_and_root(4))" 2771 | ] 2772 | }, 2773 | { 2774 | "cell_type": "code", 2775 | "execution_count": null, 2776 | "metadata": { 2777 | "id": "tIrDKIPTN0Cn", 2778 | "outputId": "da1701de-54f6-4327-c052-8244f152489b" 2779 | }, 2780 | "outputs": [ 2781 | { 2782 | "name": "stdout", 2783 | "output_type": "stream", 2784 | "text": [ 2785 | "square is 16 and root is 2.0\n", 2786 | "square is 256\n" 2787 | ] 2788 | } 2789 | ], 2790 | "source": [ 2791 | "s, r = sqr_and_root(4)\n", 2792 | "print(\"square is\", s, \"and root is\", r)\n", 2793 | "\n", 2794 | "s, _ = sqr_and_root(16) # _ is a valid variable name, usually used to mean don't care\n", 2795 | "print(\"square is\", s)" 2796 | ] 2797 | }, 2798 | { 2799 | "cell_type": "code", 2800 | "execution_count": null, 2801 | "metadata": { 2802 | "id": "T6JpLz1VN0Cn", 2803 | "outputId": "627da74b-afdb-4f24-a0a9-8c66bfaeaa45" 2804 | }, 2805 | "outputs": [ 2806 | { 2807 | "name": "stdout", 2808 | "output_type": "stream", 2809 | "text": [ 2810 | "(16, 2.0)\n", 2811 | "16\n" 2812 | ] 2813 | } 2814 | ], 2815 | "source": [ 2816 | "def sqr_and_root(x):\n", 2817 | " if x >= 0:\n", 2818 | " return x*x, x**0.5\n", 2819 | " else:\n", 2820 | " return x*x\n", 2821 | "\n", 2822 | "print(sqr_and_root(4))\n", 2823 | "print(sqr_and_root(-4))" 2824 | ] 2825 | }, 2826 | { 2827 | "cell_type": "code", 2828 | "execution_count": null, 2829 | "metadata": { 2830 | "id": "DYYiFGPON0Co", 2831 | "outputId": "f8263422-88e0-4b83-e0fb-cdac46f94cfc" 2832 | }, 2833 | "outputs": [ 2834 | { 2835 | "ename": "TypeError", 2836 | "evalue": "'int' object is not iterable", 2837 | "output_type": "error", 2838 | "traceback": [ 2839 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 2840 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 2841 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msqr_and_root\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m#error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 2842 | "\u001b[1;31mTypeError\u001b[0m: 'int' object is not iterable" 2843 | ] 2844 | } 2845 | ], 2846 | "source": [ 2847 | "s, r = sqr_and_root(-4) #error" 2848 | ] 2849 | }, 2850 | { 2851 | "cell_type": "code", 2852 | "execution_count": null, 2853 | "metadata": { 2854 | "id": "smhYu0hfN0Co" 2855 | }, 2856 | "outputs": [], 2857 | "source": [ 2858 | "def do_nothing():\n", 2859 | " pass\n", 2860 | "\n", 2861 | "print(do_nothing())" 2862 | ] 2863 | }, 2864 | { 2865 | "cell_type": "code", 2866 | "execution_count": null, 2867 | "metadata": { 2868 | "id": "qmQ3dALzN0Cp" 2869 | }, 2870 | "outputs": [], 2871 | "source": [ 2872 | "def prod(x,y):\n", 2873 | " return x*y\n", 2874 | "\n", 2875 | "print(prod(3,4))\n", 2876 | "\n", 2877 | "def prod(x,y,z):\n", 2878 | " return x*y*z\n", 2879 | "\n", 2880 | "print(prod(3,4,5))\n", 2881 | "print(prod(3,4))" 2882 | ] 2883 | }, 2884 | { 2885 | "cell_type": "code", 2886 | "execution_count": null, 2887 | "metadata": { 2888 | "id": "27pZ6qfvN0Cp", 2889 | "outputId": "89cad750-0a7b-4e96-b00f-a25d07fe666d" 2890 | }, 2891 | "outputs": [ 2892 | { 2893 | "name": "stdout", 2894 | "output_type": "stream", 2895 | "text": [ 2896 | "60\n", 2897 | "12\n" 2898 | ] 2899 | } 2900 | ], 2901 | "source": [ 2902 | "def prod(x,y,z=1):\n", 2903 | " return x*y*z\n", 2904 | "\n", 2905 | "print(prod(3,4,5))\n", 2906 | "print(prod(3,4))" 2907 | ] 2908 | }, 2909 | { 2910 | "cell_type": "code", 2911 | "execution_count": null, 2912 | "metadata": { 2913 | "id": "CcDiK3npN0Cp", 2914 | "outputId": "9cab3f1a-d43f-4b0f-e927-0abe4b551c6d" 2915 | }, 2916 | "outputs": [ 2917 | { 2918 | "name": "stdout", 2919 | "output_type": "stream", 2920 | "text": [ 2921 | "60\n", 2922 | "12\n", 2923 | "3\n", 2924 | "1\n" 2925 | ] 2926 | } 2927 | ], 2928 | "source": [ 2929 | "def prod(*l):\n", 2930 | " p = 1\n", 2931 | " for i in l:\n", 2932 | " p *= i\n", 2933 | " return p\n", 2934 | "\n", 2935 | "print(prod(3,4,5))\n", 2936 | "print(prod(3,4))\n", 2937 | "print(prod(3))\n", 2938 | "print(prod())" 2939 | ] 2940 | }, 2941 | { 2942 | "cell_type": "code", 2943 | "execution_count": null, 2944 | "metadata": { 2945 | "id": "jIK_e8VVN0Cp", 2946 | "outputId": "69b26770-35f7-4103-cbfe-a8c99b486fcc" 2947 | }, 2948 | "outputs": [ 2949 | { 2950 | "name": "stdout", 2951 | "output_type": "stream", 2952 | "text": [ 2953 | "6\n", 2954 | "6\n" 2955 | ] 2956 | } 2957 | ], 2958 | "source": [ 2959 | "def prod(x,y,z=1):\n", 2960 | " return x*y*z\n", 2961 | "\n", 2962 | "l = [1,2,3]\n", 2963 | "print(prod(*l))\n", 2964 | "\n", 2965 | "def prod(*l):\n", 2966 | " p = 1\n", 2967 | " for i in l:\n", 2968 | " p *= i\n", 2969 | " return p\n", 2970 | "\n", 2971 | "l = [1,2,3]\n", 2972 | "print(prod(*l))" 2973 | ] 2974 | }, 2975 | { 2976 | "cell_type": "code", 2977 | "execution_count": null, 2978 | "metadata": { 2979 | "id": "kI4w9L_-N0Cq", 2980 | "outputId": "aecf49fe-018d-47d0-e447-ac7f357b2e5d" 2981 | }, 2982 | "outputs": [ 2983 | { 2984 | "name": "stdout", 2985 | "output_type": "stream", 2986 | "text": [ 2987 | "1000\n" 2988 | ] 2989 | } 2990 | ], 2991 | "source": [ 2992 | "def power(x, n):\n", 2993 | " return x**n\n", 2994 | "\n", 2995 | "d = {'x':10, 'n': 3}\n", 2996 | "print(power(**d))" 2997 | ] 2998 | }, 2999 | { 3000 | "cell_type": "code", 3001 | "execution_count": null, 3002 | "metadata": { 3003 | "id": "XUTzBmOiN0Cq", 3004 | "outputId": "29b69535-f361-482e-e7ff-82d114a99080" 3005 | }, 3006 | "outputs": [ 3007 | { 3008 | "ename": "TypeError", 3009 | "evalue": "power() missing 1 required positional argument: 'n'", 3010 | "output_type": "error", 3011 | "traceback": [ 3012 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 3013 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 3014 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0md\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m'x'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m10\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[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpower\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 3015 | "\u001b[1;31mTypeError\u001b[0m: power() missing 1 required positional argument: 'n'" 3016 | ] 3017 | } 3018 | ], 3019 | "source": [ 3020 | "d = {'x':10}\n", 3021 | "print(power(**d))" 3022 | ] 3023 | }, 3024 | { 3025 | "cell_type": "code", 3026 | "execution_count": null, 3027 | "metadata": { 3028 | "id": "n6j1HUDyN0Cr", 3029 | "outputId": "ea756fa9-93f8-4348-a5ac-550f35910d44" 3030 | }, 3031 | "outputs": [ 3032 | { 3033 | "ename": "TypeError", 3034 | "evalue": "power() got an unexpected keyword argument 'a'", 3035 | "output_type": "error", 3036 | "traceback": [ 3037 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 3038 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 3039 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0md\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m'x'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'n'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'a'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m4\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[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpower\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m**\u001b[0m\u001b[0md\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 3040 | "\u001b[1;31mTypeError\u001b[0m: power() got an unexpected keyword argument 'a'" 3041 | ] 3042 | } 3043 | ], 3044 | "source": [ 3045 | "d = {'x':10, 'n':3, 'a':4}\n", 3046 | "print(power(**d))" 3047 | ] 3048 | }, 3049 | { 3050 | "cell_type": "code", 3051 | "execution_count": null, 3052 | "metadata": { 3053 | "id": "hFKm5PpJN0Cr", 3054 | "outputId": "db1ebab9-4798-4147-8bfb-e98b0bd741e2" 3055 | }, 3056 | "outputs": [ 3057 | { 3058 | "name": "stdout", 3059 | "output_type": "stream", 3060 | "text": [ 3061 | "60\n", 3062 | "12\n" 3063 | ] 3064 | } 3065 | ], 3066 | "source": [ 3067 | "def prod(x,y,z=1):\n", 3068 | " return x*y*z\n", 3069 | "\n", 3070 | "d = {'x':3, 'y':4, 'z':5}\n", 3071 | "print(prod(**d))\n", 3072 | "\n", 3073 | "d = {'x':3, 'y':4}\n", 3074 | "print(prod(**d))" 3075 | ] 3076 | }, 3077 | { 3078 | "cell_type": "code", 3079 | "execution_count": null, 3080 | "metadata": { 3081 | "id": "jIsTGFmlN0Cr", 3082 | "outputId": "5d5db225-cce2-477b-ef6d-e4d6bc2a61fd" 3083 | }, 3084 | "outputs": [ 3085 | { 3086 | "name": "stdout", 3087 | "output_type": "stream", 3088 | "text": [ 3089 | "I received 3 elements\n", 3090 | "They are {'hello': 1, 'bye': 'world', 'x': True}\n" 3091 | ] 3092 | } 3093 | ], 3094 | "source": [ 3095 | "def misc(**k):\n", 3096 | " print('I received %i elements' % len(k))\n", 3097 | " print('They are', k)\n", 3098 | "\n", 3099 | "misc(hello=1, bye='world', x=True)" 3100 | ] 3101 | }, 3102 | { 3103 | "cell_type": "code", 3104 | "execution_count": null, 3105 | "metadata": { 3106 | "id": "kwSHmIW5N0Cr", 3107 | "outputId": "b0e04c70-4fea-4dc5-abe4-7de72bd66288" 3108 | }, 3109 | "outputs": [ 3110 | { 3111 | "name": "stdout", 3112 | "output_type": "stream", 3113 | "text": [ 3114 | "0 ['hello']\n" 3115 | ] 3116 | } 3117 | ], 3118 | "source": [ 3119 | "def do_changes(x,l):\n", 3120 | " x += 1\n", 3121 | " l.append('hello')\n", 3122 | "\n", 3123 | "x, l = 0, []\n", 3124 | "do_changes(x,l)\n", 3125 | "print(x, l)" 3126 | ] 3127 | }, 3128 | { 3129 | "cell_type": "code", 3130 | "execution_count": null, 3131 | "metadata": { 3132 | "id": "FGe1r7-dN0Cs", 3133 | "outputId": "5937ac65-640b-44cb-d66d-e1568e777d51" 3134 | }, 3135 | "outputs": [ 3136 | { 3137 | "name": "stdout", 3138 | "output_type": "stream", 3139 | "text": [ 3140 | "0 []\n" 3141 | ] 3142 | } 3143 | ], 3144 | "source": [ 3145 | "def do_changes(x,l):\n", 3146 | " x += 1\n", 3147 | " l = l + ['hello']\n", 3148 | "\n", 3149 | "x, l = 0, []\n", 3150 | "do_changes(x,l)\n", 3151 | "print(x, l)" 3152 | ] 3153 | }, 3154 | { 3155 | "cell_type": "code", 3156 | "execution_count": null, 3157 | "metadata": { 3158 | "id": "-2IbHY2MN0Cs", 3159 | "outputId": "c28b4cc8-bfff-4437-97bf-e10c2d5f463e" 3160 | }, 3161 | "outputs": [ 3162 | { 3163 | "name": "stdout", 3164 | "output_type": "stream", 3165 | "text": [ 3166 | "12\n", 3167 | "14\n" 3168 | ] 3169 | } 3170 | ], 3171 | "source": [ 3172 | "def dbl(x):\n", 3173 | " return x*2\n", 3174 | "\n", 3175 | "def sqr(x):\n", 3176 | " return x*x\n", 3177 | "\n", 3178 | "def apply_and_sum(l, fn):\n", 3179 | " return sum(fn(i) for i in l)\n", 3180 | "\n", 3181 | "print(apply_and_sum([1,2,3], dbl))\n", 3182 | "print(apply_and_sum([1,2,3], sqr))" 3183 | ] 3184 | }, 3185 | { 3186 | "cell_type": "code", 3187 | "execution_count": null, 3188 | "metadata": { 3189 | "id": "sODjHx58N0Cs", 3190 | "outputId": "ec05f90b-e651-49ff-b72f-6a750f8a68a5" 3191 | }, 3192 | "outputs": [ 3193 | { 3194 | "name": "stdout", 3195 | "output_type": "stream", 3196 | "text": [ 3197 | "12\n", 3198 | "14\n", 3199 | "6\n" 3200 | ] 3201 | } 3202 | ], 3203 | "source": [ 3204 | "dbl = lambda x: x*2\n", 3205 | "sqr = lambda x: x*x\n", 3206 | "\n", 3207 | "def apply_and_sum(l, fn = lambda x: x):\n", 3208 | " return sum(fn(i) for i in l)\n", 3209 | "\n", 3210 | "print(apply_and_sum([1,2,3], dbl))\n", 3211 | "print(apply_and_sum([1,2,3], sqr))\n", 3212 | "print(apply_and_sum([1,2,3]))" 3213 | ] 3214 | }, 3215 | { 3216 | "cell_type": "code", 3217 | "execution_count": null, 3218 | "metadata": { 3219 | "id": "SXcXB28sN0Ct", 3220 | "outputId": "fbbb67e8-aec1-4422-dc87-996e45c0c5fb" 3221 | }, 3222 | "outputs": [ 3223 | { 3224 | "name": "stdout", 3225 | "output_type": "stream", 3226 | "text": [ 3227 | "5\n", 3228 | "11\n" 3229 | ] 3230 | } 3231 | ], 3232 | "source": [ 3233 | "no_param = lambda:5\n", 3234 | "print(no_param())\n", 3235 | "\n", 3236 | "two_param = lambda x,y:x+y\n", 3237 | "print(two_param(5, 6))" 3238 | ] 3239 | }, 3240 | { 3241 | "cell_type": "markdown", 3242 | "metadata": { 3243 | "id": "5SJwWuR4N0Ct" 3244 | }, 3245 | "source": [ 3246 | "# Variable Scope" 3247 | ] 3248 | }, 3249 | { 3250 | "cell_type": "code", 3251 | "execution_count": null, 3252 | "metadata": { 3253 | "id": "_XpT09rHN0Cu", 3254 | "outputId": "7f0465d4-07e9-4e0f-a478-f05431491921" 3255 | }, 3256 | "outputs": [ 3257 | { 3258 | "name": "stdout", 3259 | "output_type": "stream", 3260 | "text": [ 3261 | "1\n", 3262 | "hello 2\n", 3263 | "1\n" 3264 | ] 3265 | } 3266 | ], 3267 | "source": [ 3268 | "def hello():\n", 3269 | " x = 2\n", 3270 | " print(\"hello\", x)\n", 3271 | "\n", 3272 | "x=1\n", 3273 | "print(x)\n", 3274 | "hello()\n", 3275 | "print(x)" 3276 | ] 3277 | }, 3278 | { 3279 | "cell_type": "code", 3280 | "execution_count": null, 3281 | "metadata": { 3282 | "id": "2ZQVhYQNN0Cu", 3283 | "outputId": "3787fb5c-c7a6-47af-b49f-7a88077f882b" 3284 | }, 3285 | "outputs": [ 3286 | { 3287 | "name": "stdout", 3288 | "output_type": "stream", 3289 | "text": [ 3290 | "global 1\n", 3291 | "hello 2\n", 3292 | "global 2\n" 3293 | ] 3294 | } 3295 | ], 3296 | "source": [ 3297 | "def hello():\n", 3298 | " global x\n", 3299 | " x = 2\n", 3300 | " print(\"hello\", x)\n", 3301 | "\n", 3302 | "x=1\n", 3303 | "print(\"global\", x)\n", 3304 | "hello()\n", 3305 | "print(\"global\", x)" 3306 | ] 3307 | }, 3308 | { 3309 | "cell_type": "code", 3310 | "execution_count": 12, 3311 | "metadata": { 3312 | "colab": { 3313 | "base_uri": "https://localhost:8080/" 3314 | }, 3315 | "id": "LJ1_8Qv5N0Cu", 3316 | "outputId": "71939f25-8571-44c2-f4fd-6a9430a9cfe7" 3317 | }, 3318 | "outputs": [ 3319 | { 3320 | "name": "stdout", 3321 | "output_type": "stream", 3322 | "text": [ 3323 | "global 1\n", 3324 | "hello 2\n", 3325 | "bye 3\n", 3326 | "hello 2\n", 3327 | "global 1\n" 3328 | ] 3329 | } 3330 | ], 3331 | "source": [ 3332 | "def hello():\n", 3333 | " x = 2\n", 3334 | " \n", 3335 | " def bye():\n", 3336 | " x = 3\n", 3337 | " print(\"bye\", x)\n", 3338 | " \n", 3339 | " print(\"hello\", x)\n", 3340 | " bye()\n", 3341 | " print(\"hello\", x)\n", 3342 | " \n", 3343 | "\n", 3344 | "x=1\n", 3345 | "print(\"global\", x)\n", 3346 | "hello()\n", 3347 | "print(\"global\", x)" 3348 | ] 3349 | }, 3350 | { 3351 | "cell_type": "code", 3352 | "execution_count": null, 3353 | "metadata": { 3354 | "id": "rLLU7zemN0Cv", 3355 | "outputId": "a5ddbec8-87c0-4fbc-e561-c66967004a30" 3356 | }, 3357 | "outputs": [ 3358 | { 3359 | "name": "stdout", 3360 | "output_type": "stream", 3361 | "text": [ 3362 | "global 1\n", 3363 | "hello 2\n", 3364 | "bye 3\n", 3365 | "hello 2\n", 3366 | "global 3\n" 3367 | ] 3368 | } 3369 | ], 3370 | "source": [ 3371 | "def hello():\n", 3372 | " x = 2\n", 3373 | " \n", 3374 | " def bye():\n", 3375 | " global x\n", 3376 | " x = 3\n", 3377 | " print(\"bye\", x)\n", 3378 | " \n", 3379 | " print(\"hello\", x)\n", 3380 | " bye()\n", 3381 | " print(\"hello\", x)\n", 3382 | " \n", 3383 | "\n", 3384 | "x=1\n", 3385 | "print(\"global\", x)\n", 3386 | "hello()\n", 3387 | "print(\"global\", x)" 3388 | ] 3389 | }, 3390 | { 3391 | "cell_type": "code", 3392 | "execution_count": 3, 3393 | "metadata": { 3394 | "id": "uTY03H-HN0Cv", 3395 | "outputId": "00cf0984-d1e2-4649-b127-7b1f700b6a1e" 3396 | }, 3397 | "outputs": [ 3398 | { 3399 | "ename": "AttributeError", 3400 | "evalue": "'function' object has no attribute 'bye'", 3401 | "output_type": "error", 3402 | "traceback": [ 3403 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 3404 | "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", 3405 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"hello\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0mhello\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbye\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 14\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"global\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 3406 | "\u001b[0;31mAttributeError\u001b[0m: 'function' object has no attribute 'bye'" 3407 | ] 3408 | } 3409 | ], 3410 | "source": [ 3411 | "def hello():\n", 3412 | " x = 2\n", 3413 | " \n", 3414 | " def bye():\n", 3415 | " nonlocal x\n", 3416 | " x = 3\n", 3417 | " print(\"bye\", x)\n", 3418 | " \n", 3419 | " print(\"hello\", x)\n", 3420 | " bye()\n", 3421 | " print(\"hello\", x)\n", 3422 | " \n", 3423 | "\n", 3424 | "x=1\n", 3425 | "print(\"global\", x)\n", 3426 | "hello()\n", 3427 | "print(\"global\", x)" 3428 | ] 3429 | }, 3430 | { 3431 | "cell_type": "markdown", 3432 | "metadata": { 3433 | "id": "wiAnl7qiN0Cw" 3434 | }, 3435 | "source": [ 3436 | "### Note: you can read a variable from an outer scope with global or nonloacal. They are only neccessary for variable assignment." 3437 | ] 3438 | }, 3439 | { 3440 | "cell_type": "markdown", 3441 | "metadata": { 3442 | "id": "4reiU7hQN0Cw" 3443 | }, 3444 | "source": [ 3445 | "# Common Mistakes" 3446 | ] 3447 | }, 3448 | { 3449 | "cell_type": "code", 3450 | "execution_count": null, 3451 | "metadata": { 3452 | "id": "UaGx3Nk4N0Cw", 3453 | "outputId": "8b1fe0b1-d4f4-442c-f4ee-eaab568f1b75" 3454 | }, 3455 | "outputs": [ 3456 | { 3457 | "name": "stdout", 3458 | "output_type": "stream", 3459 | "text": [ 3460 | "['Hello', 8, 4, 2, 1]\n", 3461 | "[8, 4, 2, 1]\n", 3462 | "[8, 4, 2, 1, 8, 4, 2, 1]\n", 3463 | "[8, 4, 2, 1, 8, 4, 2, 1, 8, 4, 2, 1]\n", 3464 | "[8, 4, 2, 1]\n", 3465 | "[8, 4, 2, 1, 8, 4, 2, 1, 8, 4, 2, 1, 8, 4, 2, 1]\n" 3466 | ] 3467 | } 3468 | ], 3469 | "source": [ 3470 | "def addSeq(n, l=[]):\n", 3471 | " while n != 0:\n", 3472 | " l.append(n)\n", 3473 | " n //= 2\n", 3474 | " return l\n", 3475 | "\n", 3476 | "print(addSeq(8, ['Hello']))\n", 3477 | "print(addSeq(8))\n", 3478 | "print(addSeq(8))\n", 3479 | "print(addSeq(8))\n", 3480 | "print(addSeq(8,[]))\n", 3481 | "print(addSeq(8))" 3482 | ] 3483 | }, 3484 | { 3485 | "cell_type": "code", 3486 | "execution_count": null, 3487 | "metadata": { 3488 | "id": "-pbnxTlTN0Cw", 3489 | "outputId": "90f07900-763d-485c-86e9-7592de65342f" 3490 | }, 3491 | "outputs": [ 3492 | { 3493 | "name": "stdout", 3494 | "output_type": "stream", 3495 | "text": [ 3496 | "['Hello', 8, 4, 2, 1]\n", 3497 | "[8, 4, 2, 1]\n", 3498 | "[8, 4, 2, 1]\n", 3499 | "[8, 4, 2, 1]\n", 3500 | "[8, 4, 2, 1]\n", 3501 | "[8, 4, 2, 1]\n" 3502 | ] 3503 | } 3504 | ], 3505 | "source": [ 3506 | "def addSeq(n, l=None):\n", 3507 | " l = l or []\n", 3508 | " while n != 0:\n", 3509 | " l.append(n)\n", 3510 | " n //= 2\n", 3511 | " return l\n", 3512 | "\n", 3513 | "print(addSeq(8, ['Hello']))\n", 3514 | "print(addSeq(8))\n", 3515 | "print(addSeq(8))\n", 3516 | "print(addSeq(8))\n", 3517 | "print(addSeq(8,[]))\n", 3518 | "print(addSeq(8))" 3519 | ] 3520 | }, 3521 | { 3522 | "cell_type": "markdown", 3523 | "metadata": { 3524 | "id": "aQskQqPvN0Cx" 3525 | }, 3526 | "source": [ 3527 | "# Importing" 3528 | ] 3529 | }, 3530 | { 3531 | "cell_type": "code", 3532 | "execution_count": null, 3533 | "metadata": { 3534 | "id": "yC7fEaP2N0Cy" 3535 | }, 3536 | "outputs": [], 3537 | "source": [ 3538 | "%load_ext autoreload\n", 3539 | "%autoreload 2\n", 3540 | "#jupyter magic to reload package the have been changed" 3541 | ] 3542 | }, 3543 | { 3544 | "cell_type": "code", 3545 | "execution_count": 14, 3546 | "metadata": { 3547 | "id": "DqgTcmtLN0Cy" 3548 | }, 3549 | "outputs": [], 3550 | "source": [ 3551 | "import math\n", 3552 | "from math import sqrt\n", 3553 | "from math import*\n" 3554 | ] 3555 | }, 3556 | { 3557 | "cell_type": "markdown", 3558 | "metadata": { 3559 | "id": "pf4C5ZNiN0C0" 3560 | }, 3561 | "source": [ 3562 | "# Read and Write Files" 3563 | ] 3564 | }, 3565 | { 3566 | "cell_type": "code", 3567 | "execution_count": 15, 3568 | "metadata": { 3569 | "id": "O96XGIB1N0C0" 3570 | }, 3571 | "outputs": [], 3572 | "source": [ 3573 | "f = open('new.txt','w')\n", 3574 | "f.write('hello')\n", 3575 | "f.close() " 3576 | ] 3577 | }, 3578 | { 3579 | "cell_type": "code", 3580 | "execution_count": 16, 3581 | "metadata": { 3582 | "colab": { 3583 | "base_uri": "https://localhost:8080/" 3584 | }, 3585 | "id": "RX5qy80XN0C0", 3586 | "outputId": "107eb0be-4e7b-4738-fbc3-19ac1863f919" 3587 | }, 3588 | "outputs": [ 3589 | { 3590 | "name": "stdout", 3591 | "output_type": "stream", 3592 | "text": [ 3593 | "hello\n" 3594 | ] 3595 | } 3596 | ], 3597 | "source": [ 3598 | "f = open('new.txt','r')\n", 3599 | "s = f.read()\n", 3600 | "f.close()\n", 3601 | "print(s)" 3602 | ] 3603 | }, 3604 | { 3605 | "cell_type": "markdown", 3606 | "metadata": { 3607 | "id": "McYELn21N0C3" 3608 | }, 3609 | "source": [ 3610 | "# Classes" 3611 | ] 3612 | }, 3613 | { 3614 | "cell_type": "code", 3615 | "execution_count": null, 3616 | "metadata": { 3617 | "id": "eK4WjNRqN0C3", 3618 | "outputId": "e82eb1ef-a29d-4623-c733-a20b1cafe32b" 3619 | }, 3620 | "outputs": [ 3621 | { 3622 | "name": "stdout", 3623 | "output_type": "stream", 3624 | "text": [ 3625 | "My name is , I am 20 years old\n", 3626 | "My name is Mohamed, I am 50 years old\n" 3627 | ] 3628 | } 3629 | ], 3630 | "source": [ 3631 | "class Person:\n", 3632 | " def __init__(self, name=\"\", age=20):\n", 3633 | " self.name = name\n", 3634 | " self.age = age\n", 3635 | " \n", 3636 | " def introduce(self):\n", 3637 | " print(\"My name is %s, I am %i years old\" % (self.name, self.age))\n", 3638 | " \n", 3639 | "p = Person()\n", 3640 | "p.introduce()\n", 3641 | "p.name = \"Mohamed\"\n", 3642 | "p.age = 50\n", 3643 | "p.introduce()" 3644 | ] 3645 | }, 3646 | { 3647 | "cell_type": "code", 3648 | "execution_count": null, 3649 | "metadata": { 3650 | "id": "1Io5KDmAN0C3", 3651 | "outputId": "659ed2d4-34db-431f-8d13-93a0dfb96589" 3652 | }, 3653 | "outputs": [ 3654 | { 3655 | "name": "stdout", 3656 | "output_type": "stream", 3657 | "text": [ 3658 | "1 , 2\n", 3659 | "1 , 2\n" 3660 | ] 3661 | } 3662 | ], 3663 | "source": [ 3664 | "class EmptyObject:\n", 3665 | " def __init__(self):\n", 3666 | " pass\n", 3667 | "\n", 3668 | "o = EmptyObject()\n", 3669 | "o.x = 1\n", 3670 | "o.y = 2\n", 3671 | "print(o.x, ',', o.y)\n", 3672 | "\n", 3673 | "def display(self):\n", 3674 | " print(self.x, ',', self.y)\n", 3675 | "\n", 3676 | "EmptyObject.show = display\n", 3677 | "o.show()" 3678 | ] 3679 | }, 3680 | { 3681 | "cell_type": "code", 3682 | "execution_count": null, 3683 | "metadata": { 3684 | "id": "_yumeoNLN0C4", 3685 | "outputId": "6d608ac6-cdcb-4a7f-a935-abbf1d06e70a" 3686 | }, 3687 | "outputs": [ 3688 | { 3689 | "name": "stdout", 3690 | "output_type": "stream", 3691 | "text": [ 3692 | "My name is Mohamed, I was born in 1989\n" 3693 | ] 3694 | } 3695 | ], 3696 | "source": [ 3697 | "import datetime\n", 3698 | "\n", 3699 | "class Person:\n", 3700 | " def __init__(self, name=\"\", age=20):\n", 3701 | " self.name = name\n", 3702 | " self.age = age\n", 3703 | " \n", 3704 | " def getBirthYear(self):\n", 3705 | " return datetime.date.today().year - self.age\n", 3706 | " \n", 3707 | " def introduce(self):\n", 3708 | " print(\"My name is %s, I was born in %i\" % (self.name, self.getBirthYear()))\n", 3709 | "\n", 3710 | "p = Person(\"Mohamed\", 30)\n", 3711 | "p.introduce()" 3712 | ] 3713 | }, 3714 | { 3715 | "cell_type": "code", 3716 | "execution_count": 11, 3717 | "metadata": { 3718 | "id": "PHuM_Tk7N0C4", 3719 | "outputId": "a1c98ac9-569b-45e7-84e6-459d31bb7019" 3720 | }, 3721 | "outputs": [ 3722 | { 3723 | "name": "stdout", 3724 | "output_type": "stream", 3725 | "text": [ 3726 | "RIP Ahmed\n", 3727 | "My name is Mohamed, I was born in 1991\n", 3728 | "RIP Mohamed\n", 3729 | "My name is Ahmed, I was born in 2001\n" 3730 | ] 3731 | } 3732 | ], 3733 | "source": [ 3734 | "import datetime\n", 3735 | "\n", 3736 | "class Person:\n", 3737 | " def __init__(self, name=\"\", age=20):\n", 3738 | " self.name = name\n", 3739 | " self.age = age\n", 3740 | " \n", 3741 | " def getBirthYear(self):\n", 3742 | " return datetime.date.today().year - self.age\n", 3743 | " \n", 3744 | " def introduce(self):\n", 3745 | " print(\"My name is %s, I was born in %i\" % (self.name, self.getBirthYear()))\n", 3746 | " \n", 3747 | " def __del__(self):\n", 3748 | " print(\"RIP\", self.name)\n", 3749 | " def __add__(self,new_age):\n", 3750 | " self.age-=new_age\n", 3751 | "\n", 3752 | "p = Person(\"Mohamed\", 30)\n", 3753 | "p.introduce()\n", 3754 | "p = Person(\"Ahmed\", 20)\n", 3755 | "p.introduce()" 3756 | ] 3757 | }, 3758 | { 3759 | "cell_type": "code", 3760 | "execution_count": 12, 3761 | "metadata": { 3762 | "id": "-4exTeJcN0C4", 3763 | "outputId": "068c947c-8284-45cf-9630-3097aa33b118" 3764 | }, 3765 | "outputs": [ 3766 | { 3767 | "name": "stdout", 3768 | "output_type": "stream", 3769 | "text": [ 3770 | "My name is Ahmed, I was born in 2001\n", 3771 | "My name is Ahmed, I was born in 2006\n" 3772 | ] 3773 | } 3774 | ], 3775 | "source": [ 3776 | "#del p\n", 3777 | "p.introduce()\n", 3778 | "p+5\n", 3779 | "p.introduce()" 3780 | ] 3781 | }, 3782 | { 3783 | "cell_type": "code", 3784 | "execution_count": 17, 3785 | "metadata": { 3786 | "colab": { 3787 | "base_uri": "https://localhost:8080/" 3788 | }, 3789 | "id": "Fl53aBVfN0C4", 3790 | "outputId": "69913bf3-ba54-4070-8715-570e23fd4f8e" 3791 | }, 3792 | "outputs": [ 3793 | { 3794 | "name": "stdout", 3795 | "output_type": "stream", 3796 | "text": [ 3797 | "My name is Mohamed, I was born in 1991\n", 3798 | "Population = 1\n", 3799 | "RIP Mohamed\n", 3800 | "My name is Ahmed, I was born in 2001\n", 3801 | "Population = 1\n", 3802 | "My name is Samir, I was born in 1981\n", 3803 | "Population = 2\n", 3804 | "RIP Ahmed\n", 3805 | "RIP Samir\n" 3806 | ] 3807 | } 3808 | ], 3809 | "source": [ 3810 | "import datetime\n", 3811 | "\n", 3812 | "class Person:\n", 3813 | " \n", 3814 | " population = 0\n", 3815 | " \n", 3816 | " def __init__(self, name=\"\", age=20):\n", 3817 | " self.name = name\n", 3818 | " self.age = age\n", 3819 | " Person.population += 1\n", 3820 | " \n", 3821 | " def getBirthYear(self):\n", 3822 | " return datetime.date.today().year - self.age\n", 3823 | " \n", 3824 | " def introduce(self):\n", 3825 | " print(\"My name is %s, I was born in %i\" % (self.name, self.getBirthYear()))\n", 3826 | " \n", 3827 | " def __del__(self):\n", 3828 | " print(\"RIP\", self.name)\n", 3829 | " Person.population -= 1\n", 3830 | "\n", 3831 | "p = Person(\"Mohamed\", 30)\n", 3832 | "p.introduce()\n", 3833 | "print(\"Population =\", Person.population)\n", 3834 | "\n", 3835 | "p = Person(\"Ahmed\", 20)\n", 3836 | "p.introduce()\n", 3837 | "print(\"Population =\", Person.population)\n", 3838 | "\n", 3839 | "p2 = Person(\"Samir\", 40)\n", 3840 | "p2.introduce()\n", 3841 | "print(\"Population =\", Person.population)\n", 3842 | "\n", 3843 | "del p\n", 3844 | "del p2" 3845 | ] 3846 | }, 3847 | { 3848 | "cell_type": "code", 3849 | "execution_count": null, 3850 | "metadata": { 3851 | "id": "gRP0cMNTN0C5", 3852 | "outputId": "37f1d460-a691-497c-df24-2e1b5e6ae9e2" 3853 | }, 3854 | "outputs": [ 3855 | { 3856 | "name": "stdout", 3857 | "output_type": "stream", 3858 | "text": [ 3859 | "My name is Mohamed, I was born in 1989\n", 3860 | "The are 1 person(s)\n", 3861 | "RIP Mohamed\n", 3862 | "My name is Ahmed, I was born in 1999\n", 3863 | "The are 1 person(s)\n", 3864 | "My name is Samir, I was born in 1979\n", 3865 | "The are 2 person(s)\n", 3866 | "RIP Ahmed\n", 3867 | "RIP Samir\n" 3868 | ] 3869 | } 3870 | ], 3871 | "source": [ 3872 | "import datetime\n", 3873 | "\n", 3874 | "class Person:\n", 3875 | " \n", 3876 | " population = 0\n", 3877 | " \n", 3878 | " def __init__(self, name=\"\", age=20):\n", 3879 | " self.name = name\n", 3880 | " self.age = age\n", 3881 | " Person.population += 1\n", 3882 | " \n", 3883 | " def getBirthYear(self):\n", 3884 | " return datetime.date.today().year - self.age\n", 3885 | " \n", 3886 | " def introduce(self):\n", 3887 | " print(\"My name is %s, I was born in %i\" % (self.name, self.getBirthYear()))\n", 3888 | " \n", 3889 | " def __del__(self):\n", 3890 | " print(\"RIP\", self.name)\n", 3891 | " Person.population -= 1\n", 3892 | " \n", 3893 | " @staticmethod\n", 3894 | " def displayPopulation():\n", 3895 | " print(\"The are %i person(s)\" % Person.population)\n", 3896 | "\n", 3897 | "p = Person(\"Mohamed\", 30)\n", 3898 | "p.introduce()\n", 3899 | "Person.displayPopulation()\n", 3900 | "\n", 3901 | "p = Person(\"Ahmed\", 20)\n", 3902 | "p.introduce()\n", 3903 | "Person.displayPopulation()\n", 3904 | "\n", 3905 | "p2 = Person(\"Samir\", 40)\n", 3906 | "p2.introduce()\n", 3907 | "Person.displayPopulation()\n", 3908 | "\n", 3909 | "del p\n", 3910 | "del p2" 3911 | ] 3912 | }, 3913 | { 3914 | "cell_type": "markdown", 3915 | "metadata": { 3916 | "id": "ssQCSFMIN0C5" 3917 | }, 3918 | "source": [ 3919 | "# Inheritence" 3920 | ] 3921 | }, 3922 | { 3923 | "cell_type": "code", 3924 | "execution_count": null, 3925 | "metadata": { 3926 | "id": "Nn1seR-jN0C5", 3927 | "outputId": "f5a02b0b-f478-4fd2-bed2-9bfc338744c4" 3928 | }, 3929 | "outputs": [ 3930 | { 3931 | "name": "stdout", 3932 | "output_type": "stream", 3933 | "text": [ 3934 | "My name is Mohamed, I was born in 1989\n", 3935 | "My name is Ramy, I was born in 2009, I study at Future Schools\n", 3936 | "My name is Hoda, I was born in 1979, I work at Future Tech Co.\n", 3937 | "\n", 3938 | "is p1 a Person? True\n", 3939 | "is p1 a Student? False\n", 3940 | "is p1 a Employee? False\n", 3941 | "\n", 3942 | "is p2 a Person? True\n", 3943 | "is p2 a Student? True\n", 3944 | "is p2 a Employee? False\n", 3945 | "\n", 3946 | "is p3 a Person? True\n", 3947 | "is p3 a Student? False\n", 3948 | "is p3 a Employee? True\n" 3949 | ] 3950 | } 3951 | ], 3952 | "source": [ 3953 | "import datetime\n", 3954 | "\n", 3955 | "class Person:\n", 3956 | " def __init__(self, name=\"\", age=20):\n", 3957 | " self.name = name\n", 3958 | " self.age = age\n", 3959 | " \n", 3960 | " def getBirthYear(self):\n", 3961 | " return datetime.date.today().year - self.age\n", 3962 | " \n", 3963 | " def introduce(self):\n", 3964 | " print(\"My name is %s, I was born in %i\" % (self.name, self.getBirthYear()))\n", 3965 | "\n", 3966 | "class Student(Person):\n", 3967 | " def __init__(self, name=\"\", age=20, school=\"\"):\n", 3968 | " super().__init__(name, age)\n", 3969 | " self.school = school\n", 3970 | " \n", 3971 | " def introduce(self):\n", 3972 | " print(\"My name is %s, I was born in %i, I study at %s\" % (self.name, self.getBirthYear(), self.school))\n", 3973 | "\n", 3974 | "class Employee(Person):\n", 3975 | " def __init__(self, name=\"\", age=20, company=\"\"):\n", 3976 | " super().__init__(name, age)\n", 3977 | " self.company = company\n", 3978 | " \n", 3979 | " def introduce(self):\n", 3980 | " print(\"My name is %s, I was born in %i, I work at %s\" % (self.name, self.getBirthYear(), self.company))\n", 3981 | "\n", 3982 | "p1 = Person(\"Mohamed\", 30)\n", 3983 | "p1.introduce()\n", 3984 | "\n", 3985 | "p2 = Student(\"Ramy\", 10, \"Future Schools\")\n", 3986 | "p2.introduce()\n", 3987 | "\n", 3988 | "p3 = Employee(\"Hoda\", 40, \"Future Tech Co.\")\n", 3989 | "p3.introduce()\n", 3990 | "\n", 3991 | "print()\n", 3992 | "print(\"is p1 a Person?\", isinstance(p1, Person))\n", 3993 | "print(\"is p1 a Student?\", isinstance(p1, Student))\n", 3994 | "print(\"is p1 a Employee?\", isinstance(p1, Employee))\n", 3995 | "\n", 3996 | "print()\n", 3997 | "print(\"is p2 a Person?\", isinstance(p2, Person))\n", 3998 | "print(\"is p2 a Student?\", isinstance(p2, Student))\n", 3999 | "print(\"is p2 a Employee?\", isinstance(p2, Employee))\n", 4000 | "\n", 4001 | "\n", 4002 | "print()\n", 4003 | "print(\"is p3 a Person?\", isinstance(p3, Person))\n", 4004 | "print(\"is p3 a Student?\", isinstance(p3, Student))\n", 4005 | "print(\"is p3 a Employee?\", isinstance(p3, Employee))" 4006 | ] 4007 | }, 4008 | { 4009 | "cell_type": "markdown", 4010 | "metadata": { 4011 | "id": "XBM5p_fQN0C6" 4012 | }, 4013 | "source": [ 4014 | "# Operator Overloading" 4015 | ] 4016 | }, 4017 | { 4018 | "cell_type": "code", 4019 | "execution_count": null, 4020 | "metadata": { 4021 | "id": "TAhZE00WN0C6", 4022 | "outputId": "dc54326a-84d8-45d6-8c43-3a468eb744b6" 4023 | }, 4024 | "outputs": [ 4025 | { 4026 | "name": "stdout", 4027 | "output_type": "stream", 4028 | "text": [ 4029 | "(1.100000,2.200000) + (0.900000,0.800000) = (2.000000,3.000000)\n" 4030 | ] 4031 | } 4032 | ], 4033 | "source": [ 4034 | "class Vector2:\n", 4035 | " def __init__(self, x, y):\n", 4036 | " self.x = x\n", 4037 | " self.y = y\n", 4038 | " \n", 4039 | " def __str__(self):\n", 4040 | " return \"(%f,%f)\" % (self.x, self.y)\n", 4041 | " \n", 4042 | " def __add__(self, other):\n", 4043 | " return Vector2(self.x + other.x, self.y + other.y)\n", 4044 | " \n", 4045 | "v1 = Vector2(1.1, 2.2)\n", 4046 | "v2 = Vector2(0.9, 0.8)\n", 4047 | "print(v1, '+', v2, '=', v1+v2)" 4048 | ] 4049 | }, 4050 | { 4051 | "cell_type": "markdown", 4052 | "metadata": { 4053 | "id": "JwbqvjcKN0C6" 4054 | }, 4055 | "source": [ 4056 | "# Exceptions" 4057 | ] 4058 | }, 4059 | { 4060 | "cell_type": "code", 4061 | "execution_count": 18, 4062 | "metadata": { 4063 | "colab": { 4064 | "base_uri": "https://localhost:8080/" 4065 | }, 4066 | "id": "tQF8a7i7N0C6", 4067 | "outputId": "5e67ead0-7af6-4359-9846-47049649ce87" 4068 | }, 4069 | "outputs": [ 4070 | { 4071 | "name": "stdout", 4072 | "output_type": "stream", 4073 | "text": [ 4074 | "An exception was caught\n" 4075 | ] 4076 | } 4077 | ], 4078 | "source": [ 4079 | "try:\n", 4080 | " x = undefined_variable\n", 4081 | "except:\n", 4082 | " print(\"An exception was caught\")" 4083 | ] 4084 | }, 4085 | { 4086 | "cell_type": "code", 4087 | "execution_count": null, 4088 | "metadata": { 4089 | "id": "Wxhied79N0C7", 4090 | "outputId": "b525b22f-0aba-4ce1-9221-72a153e9c54f" 4091 | }, 4092 | "outputs": [ 4093 | { 4094 | "name": "stdout", 4095 | "output_type": "stream", 4096 | "text": [ 4097 | "5.0\n", 4098 | "No errors happened\n", 4099 | "This will run anyway\n", 4100 | "Can't Divide by Zero\n", 4101 | "This will run anyway\n", 4102 | "unsupported operand type(s) for /: 'str' and 'int'\n", 4103 | "This will run anyway\n" 4104 | ] 4105 | } 4106 | ], 4107 | "source": [ 4108 | "def div(x,y):\n", 4109 | " try:\n", 4110 | " print(x/y)\n", 4111 | " except ZeroDivisionError:\n", 4112 | " print(\"Can't Divide by Zero\")\n", 4113 | " except Exception as e:\n", 4114 | " print(e)\n", 4115 | " else:\n", 4116 | " print(\"No errors happened\")\n", 4117 | " finally:\n", 4118 | " print(\"This will run anyway\")\n", 4119 | "\n", 4120 | "div(10,2)\n", 4121 | "div(10,0)\n", 4122 | "div(\"Hello\",2)" 4123 | ] 4124 | }, 4125 | { 4126 | "cell_type": "code", 4127 | "execution_count": null, 4128 | "metadata": { 4129 | "id": "yQvq95T9N0C7", 4130 | "outputId": "01b93fd5-14a6-414f-f242-12b4dc240d64" 4131 | }, 4132 | "outputs": [ 4133 | { 4134 | "name": "stdout", 4135 | "output_type": "stream", 4136 | "text": [ 4137 | "Sorry, I do positive div only\n", 4138 | "This will run anyway\n" 4139 | ] 4140 | } 4141 | ], 4142 | "source": [ 4143 | "def div(x,y):\n", 4144 | " try:\n", 4145 | " assert x >= 0, \"Sorry, I do positive div only\"\n", 4146 | " print(x/y)\n", 4147 | " except ZeroDivisionError:\n", 4148 | " print(\"Can't Divide by Zero\")\n", 4149 | " except Exception as e:\n", 4150 | " print(e)\n", 4151 | " else:\n", 4152 | " print(\"No errors happened\")\n", 4153 | " finally:\n", 4154 | " print(\"This will run anyway\")\n", 4155 | "\n", 4156 | "div(-10,2)" 4157 | ] 4158 | }, 4159 | { 4160 | "cell_type": "code", 4161 | "execution_count": null, 4162 | "metadata": { 4163 | "id": "izHa4jiFN0C7", 4164 | "outputId": "6c583254-f001-44f3-959b-057b52ced7c7" 4165 | }, 4166 | "outputs": [ 4167 | { 4168 | "name": "stdout", 4169 | "output_type": "stream", 4170 | "text": [ 4171 | "Sorry, I do positive div only\n", 4172 | "This will run anyway\n" 4173 | ] 4174 | } 4175 | ], 4176 | "source": [ 4177 | "class CustomException(Exception):\n", 4178 | " pass\n", 4179 | "\n", 4180 | "def div(x,y):\n", 4181 | " try:\n", 4182 | " if x < 0:\n", 4183 | " raise CustomException()\n", 4184 | " print(x/y)\n", 4185 | " except ZeroDivisionError:\n", 4186 | " print(\"Can't Divide by Zero\")\n", 4187 | " except CustomException as e:\n", 4188 | " print(\"Sorry, I do positive div only\")\n", 4189 | " except Exception as e:\n", 4190 | " print(e)\n", 4191 | " else:\n", 4192 | " print(\"No errors happened\")\n", 4193 | " finally:\n", 4194 | " print(\"This will run anyway\")\n", 4195 | "\n", 4196 | "div(-10,2)" 4197 | ] 4198 | }, 4199 | { 4200 | "cell_type": "markdown", 4201 | "metadata": { 4202 | "id": "xZUUWBWNN0C8" 4203 | }, 4204 | "source": [ 4205 | "# Generators" 4206 | ] 4207 | }, 4208 | { 4209 | "cell_type": "code", 4210 | "execution_count": 19, 4211 | "metadata": { 4212 | "colab": { 4213 | "base_uri": "https://localhost:8080/" 4214 | }, 4215 | "id": "2e4yWLX5N0C8", 4216 | "outputId": "3ca17f2c-e778-4c64-e256-440dc8e14733" 4217 | }, 4218 | "outputs": [ 4219 | { 4220 | "name": "stdout", 4221 | "output_type": "stream", 4222 | "text": [ 4223 | "range(0, 10)\n", 4224 | "\n" 4225 | ] 4226 | } 4227 | ], 4228 | "source": [ 4229 | "x = range(10)\n", 4230 | "print(x)\n", 4231 | "it = iter(x)\n", 4232 | "print(it)" 4233 | ] 4234 | }, 4235 | { 4236 | "cell_type": "code", 4237 | "execution_count": 21, 4238 | "metadata": { 4239 | "colab": { 4240 | "base_uri": "https://localhost:8080/" 4241 | }, 4242 | "id": "iwIlnsdrN0C9", 4243 | "outputId": "64f0d23e-e4c2-461c-e124-6a041b16ab18", 4244 | "scrolled": true 4245 | }, 4246 | "outputs": [ 4247 | { 4248 | "name": "stdout", 4249 | "output_type": "stream", 4250 | "text": [ 4251 | "1\n" 4252 | ] 4253 | } 4254 | ], 4255 | "source": [ 4256 | "print(next(it))" 4257 | ] 4258 | }, 4259 | { 4260 | "cell_type": "code", 4261 | "execution_count": null, 4262 | "metadata": { 4263 | "id": "seIkR7EnN0C9", 4264 | "outputId": "13a1116e-e038-4983-ed3e-83b3887b9e39" 4265 | }, 4266 | "outputs": [ 4267 | { 4268 | "name": "stdout", 4269 | "output_type": "stream", 4270 | "text": [ 4271 | "[2, 3, 5, 7, 11, 13, 17, 19, 23]\n", 4272 | "\n" 4273 | ] 4274 | } 4275 | ], 4276 | "source": [ 4277 | "x = [2,3,5,7,11,13,17,19,23]\n", 4278 | "print(x)\n", 4279 | "it = iter(x)\n", 4280 | "print(it)" 4281 | ] 4282 | }, 4283 | { 4284 | "cell_type": "code", 4285 | "execution_count": 22, 4286 | "metadata": { 4287 | "colab": { 4288 | "base_uri": "https://localhost:8080/" 4289 | }, 4290 | "id": "LVaK2d-VN0C9", 4291 | "outputId": "38153df6-7969-49ba-a12d-a722ae593eae", 4292 | "scrolled": true 4293 | }, 4294 | "outputs": [ 4295 | { 4296 | "name": "stdout", 4297 | "output_type": "stream", 4298 | "text": [ 4299 | "2\n" 4300 | ] 4301 | } 4302 | ], 4303 | "source": [ 4304 | "print(next(it))" 4305 | ] 4306 | }, 4307 | { 4308 | "cell_type": "code", 4309 | "execution_count": 23, 4310 | "metadata": { 4311 | "colab": { 4312 | "base_uri": "https://localhost:8080/" 4313 | }, 4314 | "id": "DFCv8N7LN0C-", 4315 | "outputId": "5cea95af-c81d-4576-f6d0-372334a60d21" 4316 | }, 4317 | "outputs": [ 4318 | { 4319 | "name": "stdout", 4320 | "output_type": "stream", 4321 | "text": [ 4322 | " at 0x7fa1df318bd0>\n" 4323 | ] 4324 | } 4325 | ], 4326 | "source": [ 4327 | "it = (i*i for i in range(10))\n", 4328 | "print(it)" 4329 | ] 4330 | }, 4331 | { 4332 | "cell_type": "code", 4333 | "execution_count": 26, 4334 | "metadata": { 4335 | "colab": { 4336 | "base_uri": "https://localhost:8080/" 4337 | }, 4338 | "id": "UV5fd-cpN0C-", 4339 | "outputId": "72b9a2c3-3c77-4ef7-dc89-f55eeea59d72", 4340 | "scrolled": false 4341 | }, 4342 | "outputs": [ 4343 | { 4344 | "name": "stdout", 4345 | "output_type": "stream", 4346 | "text": [ 4347 | "4\n" 4348 | ] 4349 | } 4350 | ], 4351 | "source": [ 4352 | "print(next(it))" 4353 | ] 4354 | }, 4355 | { 4356 | "cell_type": "code", 4357 | "execution_count": null, 4358 | "metadata": { 4359 | "id": "GT3lDbn0N0DA", 4360 | "outputId": "da222cfc-ae00-44ec-9ed0-53a84b33bb68", 4361 | "scrolled": true 4362 | }, 4363 | "outputs": [ 4364 | { 4365 | "name": "stdout", 4366 | "output_type": "stream", 4367 | "text": [ 4368 | "[1, 3, 6, 10, 15, 21, 28, 36, 45]\n", 4369 | "[1, 3, 6, 10, 15, 21, 28, 36, 45]\n" 4370 | ] 4371 | } 4372 | ], 4373 | "source": [ 4374 | "def cumsum(it):\n", 4375 | " s = 0\n", 4376 | " for i in it:\n", 4377 | " s += i\n", 4378 | " yield s\n", 4379 | "\n", 4380 | "a = [1,2,3,4,5,6,7,8,9]\n", 4381 | "print([i for i in cumsum(a)])\n", 4382 | "print(list(cumsum(a)))" 4383 | ] 4384 | }, 4385 | { 4386 | "cell_type": "markdown", 4387 | "metadata": { 4388 | "id": "kVktL0YWN0DB" 4389 | }, 4390 | "source": [ 4391 | "# Context" 4392 | ] 4393 | }, 4394 | { 4395 | "cell_type": "code", 4396 | "execution_count": null, 4397 | "metadata": { 4398 | "id": "CpFSSMZRN0DB", 4399 | "outputId": "1c6cfc20-3dd4-4e3c-de15-5b6a67fdce10" 4400 | }, 4401 | "outputs": [ 4402 | { 4403 | "name": "stdout", 4404 | "output_type": "stream", 4405 | "text": [ 4406 | "Entering\n", 4407 | "Hello World\n", 4408 | "Exiting\n" 4409 | ] 4410 | } 4411 | ], 4412 | "source": [ 4413 | "class Context:\n", 4414 | " def __enter__(self):\n", 4415 | " print(\"Entering\")\n", 4416 | " \n", 4417 | " def __exit__(self, exc_type, exc_value, traceback):\n", 4418 | " print(\"Exiting\")\n", 4419 | "\n", 4420 | "with Context():\n", 4421 | " print(\"Hello World\")" 4422 | ] 4423 | }, 4424 | { 4425 | "cell_type": "code", 4426 | "execution_count": null, 4427 | "metadata": { 4428 | "id": "s1yRUG6_N0DC", 4429 | "outputId": "5fdc5ba6-51fd-429e-df1d-a64390af801c" 4430 | }, 4431 | "outputs": [ 4432 | { 4433 | "name": "stdout", 4434 | "output_type": "stream", 4435 | "text": [ 4436 | "Start Counting...\n", 4437 | "Hello World\n", 4438 | "Good Bye\n", 4439 | "Real Good Bye\n", 4440 | "Function called 3 time(s)\n" 4441 | ] 4442 | } 4443 | ], 4444 | "source": [ 4445 | "class CountedFunction:\n", 4446 | " def __init__(self, fn):\n", 4447 | " self.fn = fn\n", 4448 | " self.count = 0\n", 4449 | " \n", 4450 | " def __call__(self, *args, **kwargs):\n", 4451 | " self.count += 1\n", 4452 | " return self.fn(*args, **kwargs)\n", 4453 | " \n", 4454 | " def __enter__(self):\n", 4455 | " print(\"Start Counting...\")\n", 4456 | " self.count = 0\n", 4457 | " return self\n", 4458 | " \n", 4459 | " def __exit__(self, exc_type, exc_value, traceback):\n", 4460 | " print(f'Function called {self.count} time(s)')\n", 4461 | "\n", 4462 | "with CountedFunction(print) as p:\n", 4463 | " p(\"Hello World\")\n", 4464 | " p(\"Good Bye\")\n", 4465 | " p(\"Real Good Bye\")" 4466 | ] 4467 | }, 4468 | { 4469 | "cell_type": "code", 4470 | "execution_count": null, 4471 | "metadata": { 4472 | "id": "ijxx0ig3N0DD" 4473 | }, 4474 | "outputs": [], 4475 | "source": [] 4476 | } 4477 | ], 4478 | "metadata": { 4479 | "colab": { 4480 | "name": "Intro to python", 4481 | "provenance": [], 4482 | "toc_visible": true 4483 | }, 4484 | "kernelspec": { 4485 | "display_name": "Python 3", 4486 | "language": "python", 4487 | "name": "python3" 4488 | }, 4489 | "language_info": { 4490 | "codemirror_mode": { 4491 | "name": "ipython", 4492 | "version": 3 4493 | }, 4494 | "file_extension": ".py", 4495 | "mimetype": "text/x-python", 4496 | "name": "python", 4497 | "nbconvert_exporter": "python", 4498 | "pygments_lexer": "ipython3", 4499 | "version": "3.8.5" 4500 | } 4501 | }, 4502 | "nbformat": 4, 4503 | "nbformat_minor": 1 4504 | } 4505 | -------------------------------------------------------------------------------- /Tutorials/Readme.me: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Tutorials/Regex_Tutorial.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import re" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 109, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "['h']" 21 | ] 22 | }, 23 | "execution_count": 109, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "s = 'Ahmed'\n", 30 | "re.findall(r'h',s)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 110, 36 | "metadata": {}, 37 | "outputs": [ 38 | { 39 | "data": { 40 | "text/plain": [ 41 | "['hmed']" 42 | ] 43 | }, 44 | "execution_count": 110, 45 | "metadata": {}, 46 | "output_type": "execute_result" 47 | } 48 | ], 49 | "source": [ 50 | "s = 'Ahmed'\n", 51 | "re.findall(r'hmed',s)" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 18, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "data": { 61 | "text/plain": [ 62 | "['A', 'B']" 63 | ] 64 | }, 65 | "execution_count": 18, 66 | "metadata": {}, 67 | "output_type": "execute_result" 68 | } 69 | ], 70 | "source": [ 71 | "s = 'Ahmed Bahaa'\n", 72 | "re.findall(r'[A-Z]',s)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 143, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "data": { 82 | "text/plain": [ 83 | "['Ahmed', 'Bahaa']" 84 | ] 85 | }, 86 | "execution_count": 143, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "s = 'Ahmed Mohammed Bahaa'\n", 93 | "re.findall(r'Ahmed|Bahaa',s)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 172, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "text/plain": [ 104 | "['A', ' ', 'M', ' ', 'B']" 105 | ] 106 | }, 107 | "execution_count": 172, 108 | "metadata": {}, 109 | "output_type": "execute_result" 110 | } 111 | ], 112 | "source": [ 113 | "s = 'Ahmed Mohammed Bahaa'\n", 114 | "re.findall(r'[^ohamed]',s)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 19, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "data": { 124 | "text/plain": [ 125 | "['h', 'm', 'e', 'd', 'a', 'h', 'a', 'a']" 126 | ] 127 | }, 128 | "execution_count": 19, 129 | "metadata": {}, 130 | "output_type": "execute_result" 131 | } 132 | ], 133 | "source": [ 134 | "s = 'Ahmed Bahaa'\n", 135 | "re.findall(r'[a-z]',s)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 20, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "data": { 145 | "text/plain": [ 146 | "['AHMED']" 147 | ] 148 | }, 149 | "execution_count": 20, 150 | "metadata": {}, 151 | "output_type": "execute_result" 152 | } 153 | ], 154 | "source": [ 155 | "s = 'AHMED'\n", 156 | "re.findall(r'[A-Z]+',s)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 21, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "data": { 166 | "text/plain": [ 167 | "['ahmed']" 168 | ] 169 | }, 170 | "execution_count": 21, 171 | "metadata": {}, 172 | "output_type": "execute_result" 173 | } 174 | ], 175 | "source": [ 176 | "s = 'ahmed'\n", 177 | "re.findall(r'[a-z]+',s)" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 22, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "data": { 187 | "text/plain": [ 188 | "['Ahmed', 'Bahaa']" 189 | ] 190 | }, 191 | "execution_count": 22, 192 | "metadata": {}, 193 | "output_type": "execute_result" 194 | } 195 | ], 196 | "source": [ 197 | "s = 'Ahmed Bahaa'\n", 198 | "re.findall(r'[A-Za-z]+',s)" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": 23, 204 | "metadata": {}, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "['Ahmed', 'Bahaa']" 210 | ] 211 | }, 212 | "execution_count": 23, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "s = 'Ahmed Bahaa'\n", 219 | "re.findall(r'\\w+',s)" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 30, 225 | "metadata": {}, 226 | "outputs": [ 227 | { 228 | "data": { 229 | "text/plain": [ 230 | "['Ahmed123']" 231 | ] 232 | }, 233 | "execution_count": 30, 234 | "metadata": {}, 235 | "output_type": "execute_result" 236 | } 237 | ], 238 | "source": [ 239 | "s = 'Ahmed123'\n", 240 | "re.findall(r'[A-Za-z0-9]+',s)" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 35, 246 | "metadata": {}, 247 | "outputs": [ 248 | { 249 | "data": { 250 | "text/plain": [ 251 | "['ahmedbahaa_9']" 252 | ] 253 | }, 254 | "execution_count": 35, 255 | "metadata": {}, 256 | "output_type": "execute_result" 257 | } 258 | ], 259 | "source": [ 260 | "s = 'ahmedbahaa_9'\n", 261 | "re.findall(r'[A-Za-z0-9\\_]+',s)" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 36, 267 | "metadata": {}, 268 | "outputs": [ 269 | { 270 | "data": { 271 | "text/plain": [ 272 | "['****-*-*-*_*-*']" 273 | ] 274 | }, 275 | "execution_count": 36, 276 | "metadata": {}, 277 | "output_type": "execute_result" 278 | } 279 | ], 280 | "source": [ 281 | "s = '****-*-*-*_*-*'\n", 282 | "re.findall(r'[\\*\\-\\_]+',s)" 283 | ] 284 | }, 285 | { 286 | "cell_type": "code", 287 | "execution_count": 45, 288 | "metadata": {}, 289 | "outputs": [ 290 | { 291 | "data": { 292 | "text/plain": [ 293 | "['Ahmed']" 294 | ] 295 | }, 296 | "execution_count": 45, 297 | "metadata": {}, 298 | "output_type": "execute_result" 299 | } 300 | ], 301 | "source": [ 302 | "s = \"Ahmed is a cool guy\"\n", 303 | "re.findall(r'[\\w+]{5}',s)" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": 46, 309 | "metadata": {}, 310 | "outputs": [ 311 | { 312 | "data": { 313 | "text/plain": [ 314 | "['Ah', 'me', 'is', 'co', 'ol', 'gu']" 315 | ] 316 | }, 317 | "execution_count": 46, 318 | "metadata": {}, 319 | "output_type": "execute_result" 320 | } 321 | ], 322 | "source": [ 323 | "s = \"Ahmed is a cool guy\"\n", 324 | "re.findall(r'[\\w+]{2}',s)" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": 48, 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "data": { 334 | "text/plain": [ 335 | "['is']" 336 | ] 337 | }, 338 | "execution_count": 48, 339 | "metadata": {}, 340 | "output_type": "execute_result" 341 | } 342 | ], 343 | "source": [ 344 | "# We need word boundaries\n", 345 | "s = \"Ahmed is a cool guy\"\n", 346 | "re.findall(r'\\b[\\w+]{2}\\b',s)" 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": 127, 352 | "metadata": {}, 353 | "outputs": [ 354 | { 355 | "data": { 356 | "text/plain": [ 357 | "['9Ahmed', '9cool']" 358 | ] 359 | }, 360 | "execution_count": 127, 361 | "metadata": {}, 362 | "output_type": "execute_result" 363 | } 364 | ], 365 | "source": [ 366 | "s = \"9Ahmed is a 9cool guy\"\n", 367 | "re.findall(r'[0-9]+[A-Za-z]+',s)" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 128, 373 | "metadata": {}, 374 | "outputs": [ 375 | { 376 | "data": { 377 | "text/plain": [ 378 | "['9Ahmed', 'is', 'a', '9cool', 'guy']" 379 | ] 380 | }, 381 | "execution_count": 128, 382 | "metadata": {}, 383 | "output_type": "execute_result" 384 | } 385 | ], 386 | "source": [ 387 | "s = \"9Ahmed is a 9cool guy\"\n", 388 | "re.findall(r'[0-9]?[A-Za-z]+',s)" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": 130, 394 | "metadata": {}, 395 | "outputs": [ 396 | { 397 | "data": { 398 | "text/plain": [ 399 | "['9Ahmed', '9cool']" 400 | ] 401 | }, 402 | "execution_count": 130, 403 | "metadata": {}, 404 | "output_type": "execute_result" 405 | } 406 | ], 407 | "source": [ 408 | "s = \"9Ahmed is a 9cool guy\"\n", 409 | "re.findall(r'\\b[0-9]?[A-Za-z]{4,}\\b',s)" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 131, 415 | "metadata": {}, 416 | "outputs": [ 417 | { 418 | "data": { 419 | "text/plain": [ 420 | "['9Ahmed', '9cool', 'guys']" 421 | ] 422 | }, 423 | "execution_count": 131, 424 | "metadata": {}, 425 | "output_type": "execute_result" 426 | } 427 | ], 428 | "source": [ 429 | "s = \"9Ahmed is a 9cool guys\"\n", 430 | "re.findall(r'\\b[0-9]?[A-Za-z]{4,}\\b',s)" 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "execution_count": 50, 436 | "metadata": {}, 437 | "outputs": [ 438 | { 439 | "data": { 440 | "text/plain": [ 441 | "['999', '999']" 442 | ] 443 | }, 444 | "execution_count": 50, 445 | "metadata": {}, 446 | "output_type": "execute_result" 447 | } 448 | ], 449 | "source": [ 450 | "s = \"999-999\"\n", 451 | "re.findall(r'\\d{3}',s)" 452 | ] 453 | }, 454 | { 455 | "cell_type": "code", 456 | "execution_count": 51, 457 | "metadata": {}, 458 | "outputs": [ 459 | { 460 | "data": { 461 | "text/plain": [ 462 | "['999', '888']" 463 | ] 464 | }, 465 | "execution_count": 51, 466 | "metadata": {}, 467 | "output_type": "execute_result" 468 | } 469 | ], 470 | "source": [ 471 | "s = \"999-88888\"\n", 472 | "re.findall(r'\\d{3}',s)" 473 | ] 474 | }, 475 | { 476 | "cell_type": "code", 477 | "execution_count": 52, 478 | "metadata": {}, 479 | "outputs": [ 480 | { 481 | "data": { 482 | "text/plain": [ 483 | "['999']" 484 | ] 485 | }, 486 | "execution_count": 52, 487 | "metadata": {}, 488 | "output_type": "execute_result" 489 | } 490 | ], 491 | "source": [ 492 | "s = \"999-88888\"\n", 493 | "re.findall(r'\\b\\d{3}\\b',s)" 494 | ] 495 | }, 496 | { 497 | "cell_type": "code", 498 | "execution_count": 166, 499 | "metadata": {}, 500 | "outputs": [ 501 | { 502 | "data": { 503 | "text/plain": [ 504 | "['i will go away now']" 505 | ] 506 | }, 507 | "execution_count": 166, 508 | "metadata": {}, 509 | "output_type": "execute_result" 510 | } 511 | ], 512 | "source": [ 513 | "k = 'i will go away now'\n", 514 | "re.findall(r'^i[a-z\\s]+',k)" 515 | ] 516 | }, 517 | { 518 | "cell_type": "code", 519 | "execution_count": 164, 520 | "metadata": {}, 521 | "outputs": [ 522 | { 523 | "data": { 524 | "text/plain": [ 525 | "['i will go to the park']" 526 | ] 527 | }, 528 | "execution_count": 164, 529 | "metadata": {}, 530 | "output_type": "execute_result" 531 | } 532 | ], 533 | "source": [ 534 | "k = 'i will go to the park'\n", 535 | "re.findall(r'[a-z\\ ]+park$',k)" 536 | ] 537 | }, 538 | { 539 | "cell_type": "markdown", 540 | "metadata": {}, 541 | "source": [ 542 | "# Group the output of findall" 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "execution_count": 55, 548 | "metadata": {}, 549 | "outputs": [ 550 | { 551 | "data": { 552 | "text/plain": [ 553 | "['ahmed_9@hotmail.com']" 554 | ] 555 | }, 556 | "execution_count": 55, 557 | "metadata": {}, 558 | "output_type": "execute_result" 559 | } 560 | ], 561 | "source": [ 562 | "s = \"my email address is ahmed_9@hotmail.com\"\n", 563 | "re.findall(r'[\\w\\_]+\\@[\\w]+\\.[\\w]{3}',s)" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "execution_count": 135, 569 | "metadata": {}, 570 | "outputs": [ 571 | { 572 | "data": { 573 | "text/plain": [ 574 | "[('ahmed_9', 'hotmail')]" 575 | ] 576 | }, 577 | "execution_count": 135, 578 | "metadata": {}, 579 | "output_type": "execute_result" 580 | } 581 | ], 582 | "source": [ 583 | "s = \"my email address is ahmed_9@hotmail.com\"\n", 584 | "result = re.findall(r'([\\w\\_]+)\\@([\\w]+)\\.[\\w]{3}',s)\n", 585 | "result" 586 | ] 587 | }, 588 | { 589 | "cell_type": "code", 590 | "execution_count": 141, 591 | "metadata": {}, 592 | "outputs": [ 593 | { 594 | "name": "stdout", 595 | "output_type": "stream", 596 | "text": [ 597 | "The original groups we extracted are ('ahmed_9', 'hotmail')\n", 598 | "ahmed_9@hotmail.com\n" 599 | ] 600 | } 601 | ], 602 | "source": [ 603 | "for r in result:\n", 604 | " print('The original groups we extracted are',r)\n", 605 | " print('%s@%s.com' % r)" 606 | ] 607 | }, 608 | { 609 | "cell_type": "markdown", 610 | "metadata": {}, 611 | "source": [ 612 | "# Non Capturing Group" 613 | ] 614 | }, 615 | { 616 | "cell_type": "code", 617 | "execution_count": 142, 618 | "metadata": {}, 619 | "outputs": [ 620 | { 621 | "data": { 622 | "text/plain": [ 623 | "['ahmed_9']" 624 | ] 625 | }, 626 | "execution_count": 142, 627 | "metadata": {}, 628 | "output_type": "execute_result" 629 | } 630 | ], 631 | "source": [ 632 | "s = \"my email address is ahmed_9@hotmail.com\"\n", 633 | "result = re.findall(r'([\\w\\_]+)\\@(?:[\\w]+)\\.[\\w]{3}',s)\n", 634 | "result" 635 | ] 636 | }, 637 | { 638 | "cell_type": "markdown", 639 | "metadata": {}, 640 | "source": [ 641 | "# Regex sub" 642 | ] 643 | }, 644 | { 645 | "cell_type": "code", 646 | "execution_count": 111, 647 | "metadata": {}, 648 | "outputs": [ 649 | { 650 | "data": { 651 | "text/plain": [ 652 | "'my email address is ahmed_9@hotmail.gmail.com'" 653 | ] 654 | }, 655 | "execution_count": 111, 656 | "metadata": {}, 657 | "output_type": "execute_result" 658 | } 659 | ], 660 | "source": [ 661 | "s = \"my email address is ahmed_9@hotmail dot gmail dot com\"\n", 662 | "re.sub(' dot ','.',s)" 663 | ] 664 | }, 665 | { 666 | "cell_type": "code", 667 | "execution_count": 114, 668 | "metadata": {}, 669 | "outputs": [ 670 | { 671 | "data": { 672 | "text/plain": [ 673 | "'my mail address is '" 674 | ] 675 | }, 676 | "execution_count": 114, 677 | "metadata": {}, 678 | "output_type": "execute_result" 679 | } 680 | ], 681 | "source": [ 682 | "s = \"my email address is \"\n", 683 | "re.sub('email','mail',s)" 684 | ] 685 | }, 686 | { 687 | "cell_type": "code", 688 | "execution_count": 117, 689 | "metadata": {}, 690 | "outputs": [ 691 | { 692 | "data": { 693 | "text/plain": [ 694 | "['010 2569 8888']" 695 | ] 696 | }, 697 | "execution_count": 117, 698 | "metadata": {}, 699 | "output_type": "execute_result" 700 | } 701 | ], 702 | "source": [ 703 | "s = '010 2569 8888 is my new phone number but my old is not owrking 010 222 9999'\n", 704 | "re.findall(r'\\d{3}\\s*\\d{4}\\s*\\d{4}',s)" 705 | ] 706 | }, 707 | { 708 | "cell_type": "markdown", 709 | "metadata": {}, 710 | "source": [ 711 | "# More than one subdomain complex regex" 712 | ] 713 | }, 714 | { 715 | "cell_type": "code", 716 | "execution_count": 108, 717 | "metadata": {}, 718 | "outputs": [ 719 | { 720 | "data": { 721 | "text/plain": [ 722 | "['hotmail.gmail.s.']" 723 | ] 724 | }, 725 | "execution_count": 108, 726 | "metadata": {}, 727 | "output_type": "execute_result" 728 | } 729 | ], 730 | "source": [ 731 | "s = \"my email address is ahmed_9@hotmail.gmail.s.com\"\n", 732 | "re.findall(r'((?:[\\w]+\\.)*[\\w]+\\.)',s)" 733 | ] 734 | }, 735 | { 736 | "cell_type": "code", 737 | "execution_count": null, 738 | "metadata": {}, 739 | "outputs": [], 740 | "source": [] 741 | } 742 | ], 743 | "metadata": { 744 | "kernelspec": { 745 | "display_name": "Python 3", 746 | "language": "python", 747 | "name": "python3" 748 | }, 749 | "language_info": { 750 | "codemirror_mode": { 751 | "name": "ipython", 752 | "version": 3 753 | }, 754 | "file_extension": ".py", 755 | "mimetype": "text/x-python", 756 | "name": "python", 757 | "nbconvert_exporter": "python", 758 | "pygments_lexer": "ipython3", 759 | "version": "3.7.3" 760 | } 761 | }, 762 | "nbformat": 4, 763 | "nbformat_minor": 2 764 | } 765 | -------------------------------------------------------------------------------- /docs/Readme.md: -------------------------------------------------------------------------------- 1 | # Documents 2 | -------------------------------------------------------------------------------- /docs/ZLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedbahaaeldin/Introduction-to-NLP-Class/fc7cf691b79575dbc9c8314dc1d322350d51bcd2/docs/ZLogo.png -------------------------------------------------------------------------------- /docs/gitBash.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedbahaaeldin/Introduction-to-NLP-Class/fc7cf691b79575dbc9c8314dc1d322350d51bcd2/docs/gitBash.jpg -------------------------------------------------------------------------------- /docs/gitPull.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedbahaaeldin/Introduction-to-NLP-Class/fc7cf691b79575dbc9c8314dc1d322350d51bcd2/docs/gitPull.jpg -------------------------------------------------------------------------------- /docs/gitclone.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedbahaaeldin/Introduction-to-NLP-Class/fc7cf691b79575dbc9c8314dc1d322350d51bcd2/docs/gitclone.jpg -------------------------------------------------------------------------------- /docs/nlp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedbahaaeldin/Introduction-to-NLP-Class/fc7cf691b79575dbc9c8314dc1d322350d51bcd2/docs/nlp.png -------------------------------------------------------------------------------- /docs/source/MainRepo.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedbahaaeldin/Introduction-to-NLP-Class/fc7cf691b79575dbc9c8314dc1d322350d51bcd2/docs/source/MainRepo.JPG -------------------------------------------------------------------------------- /docs/source/_static/images/ZLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ahmedbahaaeldin/Introduction-to-NLP-Class/fc7cf691b79575dbc9c8314dc1d322350d51bcd2/docs/source/_static/images/ZLogo.png --------------------------------------------------------------------------------