├── cap.py ├── test_cap.py ├── README.md ├── OOP Challenge.ipynb ├── Object Oriented Programming Homework.ipynb ├── Iterators and Generators Homework.ipynb ├── Errors and Exceptions Homework.ipynb ├── Advanced Python Objects Test.ipynb ├── 6. Errors and Exceptions Handling.ipynb ├── Functions and Methods Homework.ipynb ├── 2. Python Comparison Operators.ipynb ├── 7. Python Decorators & Generators.ipynb ├── 5. Object Oriented Programming.ipynb ├── 3. Python Statements.ipynb ├── 4. Methods and Functions.ipynb ├── Function Practice Exercises.ipynb └── 9. Advanced Python Objects and Data Structures.ipynb /cap.py: -------------------------------------------------------------------------------- 1 | def cap_text(text): 2 | ''' 3 | Input a string 4 | Output the capitalized string 5 | ''' 6 | return text.title() -------------------------------------------------------------------------------- /test_cap.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | import cap 3 | 4 | class TestCap(unittest.TestCase): 5 | 6 | def test_one_word(self): 7 | text = 'python' 8 | result = cap.cap_text(text) 9 | self.assertEqual(result, 'Python') 10 | 11 | def test_multiple_words(self): 12 | text = 'monty python' 13 | result = cap.cap_text(text) 14 | self.assertEqual(result, 'Monty Python') 15 | 16 | if __name__=='__main': 17 | unittest.main() 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-zero-to-hero 2 | [Complete Python Bootcamp: Go from zero to hero in Python 3](https://www.udemy.com/complete-python-bootcamp/) 3 | 4 | This course is taught by Jose Portilla at Udemy and it's a comprehensive yet straightforward course for Python programming language. The bootcamp covers a wide variety of topics that I'm learning, including: 5 | - Command Line Basics 6 | - Installing Python 7 | - Running Python Code 8 | - Strings 9 | - Lists 10 | - Dictionaries 11 | - Tuples 12 | - Sets 13 | - Number Data Types 14 | - Print Formatting 15 | - Functions 16 | - Scope 17 | - args/kwargs 18 | - Built-in Functions 19 | - Debugging and Error Handling 20 | - Modules 21 | - External Modules 22 | - Object Oriented Programming 23 | - Inheritance 24 | - File I/O 25 | - Advanced Methods 26 | - Unit Tests 27 | - and much more! 28 | 29 | This course is great so far and I'll update the repository as I go through the bootcamp. If you want to learn python, I highly recommend this course and you can click on the link above to get this bootcamp! 30 | -------------------------------------------------------------------------------- /OOP Challenge.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Object Oriented Programming Challenge\n", 8 | "\n", 9 | "For this challenge, create a bank account class that has two attributes:\n", 10 | "\n", 11 | "* owner\n", 12 | "* balance\n", 13 | "\n", 14 | "and two methods:\n", 15 | "\n", 16 | "* deposit\n", 17 | "* withdraw\n", 18 | "\n", 19 | "As an added requirement, withdrawals may not exceed the available balance.\n", 20 | "\n", 21 | "Instantiate your class, make several deposits and withdrawals, and test to make sure the account can't be overdrawn." 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "metadata": {}, 28 | "outputs": [], 29 | "source": [ 30 | "class Account:\n", 31 | " \n", 32 | " def __init__(self, owner, balance=0):\n", 33 | " \n", 34 | " self.owner = owner\n", 35 | " self.balance = balance\n", 36 | " \n", 37 | " def deposit(self, dep_amt):\n", 38 | " \n", 39 | " self.balance = self.balance + dep_amt\n", 40 | " print(f\"Added {dep_amt} to the balance\")\n", 41 | " \n", 42 | " def withdrawal(self, wd_amt):\n", 43 | " \n", 44 | " if self.balance >= wd_amt:\n", 45 | " self.balance = self.balance - wd_amt\n", 46 | " print(\"Withdrawal accepted\")\n", 47 | " else:\n", 48 | " print(\"Sorry not enough funds!\")\n", 49 | " \n", 50 | " def __str__(self):\n", 51 | " return f\"Owner: {self.owner} \\nBalance: {self.balance}\"" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 3, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "# 1. Instantiate the class\n", 61 | "acct1 = Account('Jose',100)" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 4, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "Owner: Jose \n", 74 | "Balance: 100\n" 75 | ] 76 | } 77 | ], 78 | "source": [ 79 | "# 2. Print the object\n", 80 | "print(acct1)" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 5, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "data": { 90 | "text/plain": [ 91 | "'Jose'" 92 | ] 93 | }, 94 | "execution_count": 5, 95 | "metadata": {}, 96 | "output_type": "execute_result" 97 | } 98 | ], 99 | "source": [ 100 | "# 3. Show the account owner attribute\n", 101 | "acct1.owner" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 6, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "100" 113 | ] 114 | }, 115 | "execution_count": 6, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "# 4. Show the account balance attribute\n", 122 | "acct1.balance" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 7, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "name": "stdout", 132 | "output_type": "stream", 133 | "text": [ 134 | "Added 50 to the balance\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "# 5. Make a series of deposits and withdrawals\n", 140 | "acct1.deposit(50)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 9, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "name": "stdout", 150 | "output_type": "stream", 151 | "text": [ 152 | "Withdrawal accepted\n" 153 | ] 154 | } 155 | ], 156 | "source": [ 157 | "acct1.withdrawal(75)" 158 | ] 159 | }, 160 | { 161 | "cell_type": "code", 162 | "execution_count": 10, 163 | "metadata": {}, 164 | "outputs": [ 165 | { 166 | "name": "stdout", 167 | "output_type": "stream", 168 | "text": [ 169 | "Sorry not enough funds!\n" 170 | ] 171 | } 172 | ], 173 | "source": [ 174 | "# 6. Make a withdrawal that exceeds the available balance\n", 175 | "acct1.withdrawal(500)" 176 | ] 177 | } 178 | ], 179 | "metadata": { 180 | "kernelspec": { 181 | "display_name": "Python 3", 182 | "language": "python", 183 | "name": "python3" 184 | }, 185 | "language_info": { 186 | "codemirror_mode": { 187 | "name": "ipython", 188 | "version": 3 189 | }, 190 | "file_extension": ".py", 191 | "mimetype": "text/x-python", 192 | "name": "python", 193 | "nbconvert_exporter": "python", 194 | "pygments_lexer": "ipython3", 195 | "version": "3.7.3" 196 | } 197 | }, 198 | "nbformat": 4, 199 | "nbformat_minor": 2 200 | } 201 | -------------------------------------------------------------------------------- /Object Oriented Programming Homework.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Object Oriented Programming\n", 8 | "## Homework Assignment\n", 9 | "\n", 10 | "#### Problem 1\n", 11 | "Fill in the Line class methods to accept coordinates as a pair of tuples and return the slope and distance of the line." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 2, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "class Line:\n", 21 | " \n", 22 | " def __init__(self,coor1,coor2):\n", 23 | " self.coor1 = coor1\n", 24 | " self.coor2 = coor2\n", 25 | " \n", 26 | " def distance(self):\n", 27 | " \n", 28 | " x1, y1 = self.coor1\n", 29 | " x2, y2 = self.coor2\n", 30 | " \n", 31 | " return((x2-x1)**2 + (y2-y1)**2)**0.5\n", 32 | " \n", 33 | " def slope(self):\n", 34 | " \n", 35 | " x1, y1 = self.coor1\n", 36 | " x2, y2 = self.coor2\n", 37 | " \n", 38 | " return (y2-y1) / (x2-x1)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 3, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "# EXAMPLE OUTPUT\n", 48 | "\n", 49 | "coordinate1 = (3,2)\n", 50 | "coordinate2 = (8,10)\n", 51 | "\n", 52 | "li = Line(coordinate1,coordinate2)" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 4, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "data": { 62 | "text/plain": [ 63 | "9.433981132056603" 64 | ] 65 | }, 66 | "execution_count": 4, 67 | "metadata": {}, 68 | "output_type": "execute_result" 69 | } 70 | ], 71 | "source": [ 72 | "li.distance()" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 5, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "data": { 82 | "text/plain": [ 83 | "1.6" 84 | ] 85 | }, 86 | "execution_count": 5, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "li.slope()" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "________\n", 100 | "#### Problem 2" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "Fill in the class " 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 7, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [ 116 | "class Cylinder:\n", 117 | " \n", 118 | " def __init__(self,height=1,radius=1):\n", 119 | " \n", 120 | " self.height = height\n", 121 | " self.radius = radius\n", 122 | " \n", 123 | " def volume(self):\n", 124 | " return self.height * 3.14 * (self.radius)**2\n", 125 | " \n", 126 | " def surface_area(self):\n", 127 | " \n", 128 | " top = 3.14 * (self.radius**2)\n", 129 | " \n", 130 | " return (2*top) + (2*3.14*self.radius*self.height)" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 8, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "# EXAMPLE OUTPUT\n", 140 | "c = Cylinder(2,3)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 9, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "56.52" 152 | ] 153 | }, 154 | "execution_count": 9, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "c.volume()" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 10, 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "data": { 170 | "text/plain": [ 171 | "94.2" 172 | ] 173 | }, 174 | "execution_count": 10, 175 | "metadata": {}, 176 | "output_type": "execute_result" 177 | } 178 | ], 179 | "source": [ 180 | "c.surface_area()" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": null, 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [] 189 | } 190 | ], 191 | "metadata": { 192 | "kernelspec": { 193 | "display_name": "Python 3", 194 | "language": "python", 195 | "name": "python3" 196 | }, 197 | "language_info": { 198 | "codemirror_mode": { 199 | "name": "ipython", 200 | "version": 3 201 | }, 202 | "file_extension": ".py", 203 | "mimetype": "text/x-python", 204 | "name": "python", 205 | "nbconvert_exporter": "python", 206 | "pygments_lexer": "ipython3", 207 | "version": "3.7.3" 208 | } 209 | }, 210 | "nbformat": 4, 211 | "nbformat_minor": 1 212 | } 213 | -------------------------------------------------------------------------------- /Iterators and Generators Homework.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Iterators and Generators Homework \n", 8 | "\n", 9 | "### Problem 1\n", 10 | "\n", 11 | "Create a generator that generates the squares of numbers up to some number N." 12 | ] 13 | }, 14 | { 15 | "cell_type": "code", 16 | "execution_count": 1, 17 | "metadata": {}, 18 | "outputs": [], 19 | "source": [ 20 | "def gensquares(N):\n", 21 | "\n", 22 | " for i in range(N):\n", 23 | " yield i**2" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "0\n", 36 | "1\n", 37 | "4\n", 38 | "9\n", 39 | "16\n", 40 | "25\n", 41 | "36\n", 42 | "49\n", 43 | "64\n", 44 | "81\n" 45 | ] 46 | } 47 | ], 48 | "source": [ 49 | "for x in gensquares(10):\n", 50 | " print(x)" 51 | ] 52 | }, 53 | { 54 | "cell_type": "markdown", 55 | "metadata": {}, 56 | "source": [ 57 | "### Problem 2\n", 58 | "\n", 59 | "Create a generator that yields \"n\" random numbers between a low and high number (that are inputs).
Note: Use the random library. For example:" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 4, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "data": { 69 | "text/plain": [ 70 | "1" 71 | ] 72 | }, 73 | "execution_count": 4, 74 | "metadata": {}, 75 | "output_type": "execute_result" 76 | } 77 | ], 78 | "source": [ 79 | "import random\n", 80 | "\n", 81 | "random.randint(1,10)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 5, 87 | "metadata": {}, 88 | "outputs": [], 89 | "source": [ 90 | "def rand_num(low,high,n):\n", 91 | "\n", 92 | " for i in range(n):\n", 93 | " yield random.randint(low, high)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 6, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "5\n", 106 | "8\n", 107 | "6\n", 108 | "2\n", 109 | "1\n", 110 | "8\n", 111 | "7\n", 112 | "1\n", 113 | "9\n", 114 | "7\n", 115 | "4\n", 116 | "7\n" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "for num in rand_num(1,10,12):\n", 122 | " print(num)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "### Problem 3\n", 130 | "\n", 131 | "Use the iter() function to convert the string below into an iterator:\n" 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 7, 137 | "metadata": {}, 138 | "outputs": [ 139 | { 140 | "name": "stdout", 141 | "output_type": "stream", 142 | "text": [ 143 | "h\n" 144 | ] 145 | } 146 | ], 147 | "source": [ 148 | "s = 'hello'\n", 149 | "\n", 150 | "s = iter(s)\n", 151 | "\n", 152 | "print(next(s))" 153 | ] 154 | }, 155 | { 156 | "cell_type": "markdown", 157 | "metadata": {}, 158 | "source": [ 159 | "### Problem 4\n", 160 | "Explain a use case for a generator using a yield statement where you would not want to use a normal function with a return statement.\n", 161 | "\n", 162 | "If the output has the potential of taking up a large amount of memory and you only intend to iterate through it, you would want to use a generator. \n", 163 | "\n" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "### Extra Credit!\n", 171 | "Can you explain what *gencomp* is in the code below? (Note: We never covered this in lecture! You will have to do some Googling/Stack Overflowing!)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 6, 177 | "metadata": {}, 178 | "outputs": [ 179 | { 180 | "name": "stdout", 181 | "output_type": "stream", 182 | "text": [ 183 | "4\n", 184 | "5\n" 185 | ] 186 | } 187 | ], 188 | "source": [ 189 | "my_list = [1,2,3,4,5]\n", 190 | "\n", 191 | "gencomp = (item for item in my_list if item > 3)\n", 192 | "\n", 193 | "for item in gencomp:\n", 194 | " print(item)" 195 | ] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": {}, 200 | "source": [ 201 | "Hint: Google *generator comprehension*!\n", 202 | "\n", 203 | "# Great Job!" 204 | ] 205 | } 206 | ], 207 | "metadata": { 208 | "kernelspec": { 209 | "display_name": "Python 3", 210 | "language": "python", 211 | "name": "python3" 212 | }, 213 | "language_info": { 214 | "codemirror_mode": { 215 | "name": "ipython", 216 | "version": 3 217 | }, 218 | "file_extension": ".py", 219 | "mimetype": "text/x-python", 220 | "name": "python", 221 | "nbconvert_exporter": "python", 222 | "pygments_lexer": "ipython3", 223 | "version": "3.7.3" 224 | } 225 | }, 226 | "nbformat": 4, 227 | "nbformat_minor": 1 228 | } 229 | -------------------------------------------------------------------------------- /Errors and Exceptions Homework.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Errors and Exceptions Homework" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### Problem 1\n", 15 | "Handle the exception thrown by the code below by using try and except blocks." 16 | ] 17 | }, 18 | { 19 | "cell_type": "code", 20 | "execution_count": 1, 21 | "metadata": {}, 22 | "outputs": [ 23 | { 24 | "ename": "TypeError", 25 | "evalue": "unsupported operand type(s) for ** or pow(): 'str' and 'int'", 26 | "output_type": "error", 27 | "traceback": [ 28 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 29 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 30 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;34m'a'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'b'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'c'\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[0mi\u001b[0m\u001b[1;33m**\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 31 | "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for ** or pow(): 'str' and 'int'" 32 | ] 33 | } 34 | ], 35 | "source": [ 36 | "for i in ['a','b','c']:\n", 37 | " print(i**2)" 38 | ] 39 | }, 40 | { 41 | "cell_type": "code", 42 | "execution_count": 2, 43 | "metadata": {}, 44 | "outputs": [ 45 | { 46 | "name": "stdout", 47 | "output_type": "stream", 48 | "text": [ 49 | "Type error! Watch out!\n" 50 | ] 51 | } 52 | ], 53 | "source": [ 54 | "try:\n", 55 | " for i in ['a', 'b', 'c']:\n", 56 | " print(i**2)\n", 57 | "except:\n", 58 | " print(\"Type error! Watch out!\")" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "### Problem 2\n", 66 | "Handle the exception thrown by the code below by using try and except blocks. Then use a finally block to print 'All Done.'" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 4, 72 | "metadata": { 73 | "scrolled": true 74 | }, 75 | "outputs": [ 76 | { 77 | "ename": "ZeroDivisionError", 78 | "evalue": "division by zero", 79 | "output_type": "error", 80 | "traceback": [ 81 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 82 | "\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 83 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[0mz\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 84 | "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero" 85 | ] 86 | } 87 | ], 88 | "source": [ 89 | "x = 5\n", 90 | "y = 0\n", 91 | "\n", 92 | "z = x/y" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 5, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "name": "stdout", 102 | "output_type": "stream", 103 | "text": [ 104 | "General Error! Watch out!\n", 105 | "All Done\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "try:\n", 111 | " x = 5\n", 112 | " y = 0\n", 113 | "\n", 114 | " z = x/y\n", 115 | "except:\n", 116 | " print(\"General Error! Watch out!\")\n", 117 | "finally:\n", 118 | " print(\"All Done\")" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "### Problem 3\n", 126 | "Write a function that asks for an integer and prints the square of it. Use a while loop with a try, except, else block to account for incorrect inputs." 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 9, 132 | "metadata": {}, 133 | "outputs": [], 134 | "source": [ 135 | "def ask():\n", 136 | " \n", 137 | " while True:\n", 138 | " try:\n", 139 | " n = int(input(\"Enter a number\"))\n", 140 | " except:\n", 141 | " print(\"Please try again! \\n\")\n", 142 | " continue\n", 143 | " else:\n", 144 | " break\n", 145 | " print(\"Your number squared is: \")\n", 146 | " print(n**2)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 11, 152 | "metadata": {}, 153 | "outputs": [ 154 | { 155 | "name": "stdout", 156 | "output_type": "stream", 157 | "text": [ 158 | "Enter a numberas\n", 159 | "Please try again! \n", 160 | "\n", 161 | "Enter a number3\n", 162 | "Your number squared is: \n", 163 | "9\n" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "ask()" 169 | ] 170 | }, 171 | { 172 | "cell_type": "markdown", 173 | "metadata": {}, 174 | "source": [ 175 | "# Great Job!" 176 | ] 177 | } 178 | ], 179 | "metadata": { 180 | "kernelspec": { 181 | "display_name": "Python 3", 182 | "language": "python", 183 | "name": "python3" 184 | }, 185 | "language_info": { 186 | "codemirror_mode": { 187 | "name": "ipython", 188 | "version": 3 189 | }, 190 | "file_extension": ".py", 191 | "mimetype": "text/x-python", 192 | "name": "python", 193 | "nbconvert_exporter": "python", 194 | "pygments_lexer": "ipython3", 195 | "version": "3.7.3" 196 | } 197 | }, 198 | "nbformat": 4, 199 | "nbformat_minor": 1 200 | } 201 | -------------------------------------------------------------------------------- /Advanced Python Objects Test.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Advanced Python Objects Test" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## Advanced Numbers\n", 15 | "\n", 16 | "**Problem 1: Convert 1024 to binary and hexadecimal representation**" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 5, 22 | "metadata": {}, 23 | "outputs": [ 24 | { 25 | "name": "stdout", 26 | "output_type": "stream", 27 | "text": [ 28 | "0b10000000000\n", 29 | "0x400\n" 30 | ] 31 | } 32 | ], 33 | "source": [ 34 | "print(bin(1024))\n", 35 | "print(hex(1024))" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "**Problem 2: Round 5.23222 to two decimal places**" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 6, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "5.23" 54 | ] 55 | }, 56 | "execution_count": 6, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "round(5.23222, 2)" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "## Advanced Strings\n", 70 | "**Problem 3: Check if every letter in the string s is lower case**" 71 | ] 72 | }, 73 | { 74 | "cell_type": "code", 75 | "execution_count": 7, 76 | "metadata": {}, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "False" 82 | ] 83 | }, 84 | "execution_count": 7, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "s = 'hello how are you Mary, are you feeling okay?'\n", 91 | "\n", 92 | "s.islower()" 93 | ] 94 | }, 95 | { 96 | "cell_type": "markdown", 97 | "metadata": {}, 98 | "source": [ 99 | "**Problem 4: How many times does the letter 'w' show up in the string below?**" 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": 8, 105 | "metadata": {}, 106 | "outputs": [ 107 | { 108 | "data": { 109 | "text/plain": [ 110 | "12" 111 | ] 112 | }, 113 | "execution_count": 8, 114 | "metadata": {}, 115 | "output_type": "execute_result" 116 | } 117 | ], 118 | "source": [ 119 | "s = 'twywywtwywbwhsjhwuwshshwuwwwjdjdid'\n", 120 | "s.count('w')" 121 | ] 122 | }, 123 | { 124 | "cell_type": "markdown", 125 | "metadata": {}, 126 | "source": [ 127 | "## Advanced Sets\n", 128 | "**Problem 5: Find the elements in set1 that are not in set2:**" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 9, 134 | "metadata": {}, 135 | "outputs": [ 136 | { 137 | "data": { 138 | "text/plain": [ 139 | "{2}" 140 | ] 141 | }, 142 | "execution_count": 9, 143 | "metadata": {}, 144 | "output_type": "execute_result" 145 | } 146 | ], 147 | "source": [ 148 | "set1 = {2,3,1,5,6,8}\n", 149 | "set2 = {3,1,7,5,6,8}\n", 150 | "\n", 151 | "set1.difference(set2)" 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "**Problem 6: Find all elements that are in either set:**" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 10, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "data": { 168 | "text/plain": [ 169 | "{1, 2, 3, 5, 6, 7, 8}" 170 | ] 171 | }, 172 | "execution_count": 10, 173 | "metadata": {}, 174 | "output_type": "execute_result" 175 | } 176 | ], 177 | "source": [ 178 | "set1.union(set2)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "## Advanced Dictionaries\n", 186 | "\n", 187 | "**Problem 7: Create this dictionary:\n", 188 | "{0: 0, 1: 1, 2: 8, 3: 27, 4: 64}\n", 189 | " using a dictionary comprehension.**" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 11, 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "data": { 199 | "text/plain": [ 200 | "{0: 0, 1: 1, 2: 8, 3: 27, 4: 64}" 201 | ] 202 | }, 203 | "execution_count": 11, 204 | "metadata": {}, 205 | "output_type": "execute_result" 206 | } 207 | ], 208 | "source": [ 209 | "{x:x**3 for x in range(5)}" 210 | ] 211 | }, 212 | { 213 | "cell_type": "markdown", 214 | "metadata": {}, 215 | "source": [ 216 | "## Advanced Lists\n", 217 | "\n", 218 | "**Problem 8: Reverse the list below:**" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 13, 224 | "metadata": {}, 225 | "outputs": [ 226 | { 227 | "data": { 228 | "text/plain": [ 229 | "[4, 3, 2, 1]" 230 | ] 231 | }, 232 | "execution_count": 13, 233 | "metadata": {}, 234 | "output_type": "execute_result" 235 | } 236 | ], 237 | "source": [ 238 | "list1 = [1,2,3,4]\n", 239 | "\n", 240 | "list1.reverse()\n", 241 | "\n", 242 | "list1" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "**Problem 9: Sort the list below:**" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 15, 255 | "metadata": {}, 256 | "outputs": [ 257 | { 258 | "data": { 259 | "text/plain": [ 260 | "[1, 2, 3, 4, 5]" 261 | ] 262 | }, 263 | "execution_count": 15, 264 | "metadata": {}, 265 | "output_type": "execute_result" 266 | } 267 | ], 268 | "source": [ 269 | "list2 = [3,4,2,5,1]\n", 270 | "\n", 271 | "list2.sort()\n", 272 | "\n", 273 | "list2" 274 | ] 275 | } 276 | ], 277 | "metadata": { 278 | "kernelspec": { 279 | "display_name": "Python 3", 280 | "language": "python", 281 | "name": "python3" 282 | }, 283 | "language_info": { 284 | "codemirror_mode": { 285 | "name": "ipython", 286 | "version": 3 287 | }, 288 | "file_extension": ".py", 289 | "mimetype": "text/x-python", 290 | "name": "python", 291 | "nbconvert_exporter": "python", 292 | "pygments_lexer": "ipython3", 293 | "version": "3.7.3" 294 | } 295 | }, 296 | "nbformat": 4, 297 | "nbformat_minor": 1 298 | } 299 | -------------------------------------------------------------------------------- /6. Errors and Exceptions Handling.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def add(n1, n2):\n", 10 | " print(n1+n2)" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 2, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "name": "stdout", 20 | "output_type": "stream", 21 | "text": [ 22 | "30\n" 23 | ] 24 | } 25 | ], 26 | "source": [ 27 | "add(10, 20)" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 3, 33 | "metadata": {}, 34 | "outputs": [], 35 | "source": [ 36 | "number1 = 10" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 5, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "Please provide a number: 20\n" 49 | ] 50 | } 51 | ], 52 | "source": [ 53 | "number2 = input(\"Please provide a number: \")" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 6, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "ename": "TypeError", 63 | "evalue": "unsupported operand type(s) for +: 'int' and 'str'", 64 | "output_type": "error", 65 | "traceback": [ 66 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 67 | "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", 68 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnumber1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnumber2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 69 | "\u001b[1;32m\u001b[0m in \u001b[0;36madd\u001b[1;34m(n1, n2)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0madd\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mn2\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[1;32m----> 2\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn1\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mn2\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 70 | "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" 71 | ] 72 | } 73 | ], 74 | "source": [ 75 | "add(number1, number2)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 12, 81 | "metadata": {}, 82 | "outputs": [ 83 | { 84 | "name": "stdout", 85 | "output_type": "stream", 86 | "text": [ 87 | "Add went well!\n", 88 | "20\n" 89 | ] 90 | } 91 | ], 92 | "source": [ 93 | "try:\n", 94 | " # Want to attempt this code\n", 95 | " # May have an error\n", 96 | " result = 10 + 10\n", 97 | "except:\n", 98 | " print(\"Hey it looks like you aren't adding correctly!\")\n", 99 | "else:\n", 100 | " print(\"Add went well!\")\n", 101 | " print(result)" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 9, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "20" 113 | ] 114 | }, 115 | "execution_count": 9, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "result" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 14, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "Hey you have an OS Error\n", 134 | "I always run\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "try:\n", 140 | " f = open('testfile', 'r')\n", 141 | " f.write('Write a test line')\n", 142 | "except TypeError:\n", 143 | " print(\"There was a type error!\")\n", 144 | "except OSError:\n", 145 | " print('Hey you have an OS Error')\n", 146 | "finally:\n", 147 | " print('I always run')" 148 | ] 149 | }, 150 | { 151 | "cell_type": "code", 152 | "execution_count": 25, 153 | "metadata": {}, 154 | "outputs": [], 155 | "source": [ 156 | "def ask_for_int():\n", 157 | " \n", 158 | " while True:\n", 159 | " \n", 160 | " try:\n", 161 | " result = int(input(\"Please provide number: \"))\n", 162 | " except:\n", 163 | " print(\"Whoops! That is not a number\")\n", 164 | " continue\n", 165 | " else:\n", 166 | " print(\"Yes thank you\")\n", 167 | " break" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 26, 173 | "metadata": {}, 174 | "outputs": [ 175 | { 176 | "name": "stdout", 177 | "output_type": "stream", 178 | "text": [ 179 | "Please provide number: 20\n", 180 | "Yes thank you\n" 181 | ] 182 | } 183 | ], 184 | "source": [ 185 | "ask_for_int()" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": null, 191 | "metadata": {}, 192 | "outputs": [], 193 | "source": [] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [] 201 | }, 202 | { 203 | "cell_type": "code", 204 | "execution_count": null, 205 | "metadata": {}, 206 | "outputs": [], 207 | "source": [] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": null, 212 | "metadata": {}, 213 | "outputs": [], 214 | "source": [] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": null, 219 | "metadata": {}, 220 | "outputs": [], 221 | "source": [] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": null, 226 | "metadata": {}, 227 | "outputs": [], 228 | "source": [] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": null, 233 | "metadata": {}, 234 | "outputs": [], 235 | "source": [] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": null, 240 | "metadata": {}, 241 | "outputs": [], 242 | "source": [] 243 | } 244 | ], 245 | "metadata": { 246 | "kernelspec": { 247 | "display_name": "Python 3", 248 | "language": "python", 249 | "name": "python3" 250 | }, 251 | "language_info": { 252 | "codemirror_mode": { 253 | "name": "ipython", 254 | "version": 3 255 | }, 256 | "file_extension": ".py", 257 | "mimetype": "text/x-python", 258 | "name": "python", 259 | "nbconvert_exporter": "python", 260 | "pygments_lexer": "ipython3", 261 | "version": "3.7.3" 262 | } 263 | }, 264 | "nbformat": 4, 265 | "nbformat_minor": 2 266 | } 267 | -------------------------------------------------------------------------------- /Functions and Methods Homework.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Functions and Methods Homework \n", 8 | "\n", 9 | "Complete the following questions:\n", 10 | "____\n", 11 | "**Write a function that computes the volume of a sphere given its radius.**\n", 12 | "

The volume of a sphere is given as $$\\frac{4}{3} πr^3$$

" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": 1, 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "def vol(rad):\n", 22 | " return (4.0/3)*(3.14)*(rad**3)" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 2, 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "data": { 32 | "text/plain": [ 33 | "33.49333333333333" 34 | ] 35 | }, 36 | "execution_count": 2, 37 | "metadata": {}, 38 | "output_type": "execute_result" 39 | } 40 | ], 41 | "source": [ 42 | "# Check\n", 43 | "vol(2)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "___\n", 51 | "**Write a function that checks whether a number is in a given range (inclusive of high and low)**" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 7, 57 | "metadata": {}, 58 | "outputs": [], 59 | "source": [ 60 | "def ran_check(num,low,high):\n", 61 | " \n", 62 | " # Check if num is between low and high (including low and high)\n", 63 | " if num in range(low,high+1):\n", 64 | " print('{} is in the range between {} and {}'.format(num,low,high))\n", 65 | " else:\n", 66 | " print('The number is outside the range.')" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 8, 72 | "metadata": {}, 73 | "outputs": [ 74 | { 75 | "name": "stdout", 76 | "output_type": "stream", 77 | "text": [ 78 | "5 is in the range between 2 and 7\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "# Check\n", 84 | "ran_check(5,2,7)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "If you only wanted to return a boolean:" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 9, 97 | "metadata": {}, 98 | "outputs": [], 99 | "source": [ 100 | "def ran_bool(num,low,high):\n", 101 | " return num in range(low, high)" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 10, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "True" 113 | ] 114 | }, 115 | "execution_count": 10, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "ran_bool(3,1,10)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "____\n", 129 | "**Write a Python function that accepts a string and calculates the number of upper case letters and lower case letters.**\n", 130 | "\n", 131 | " Sample String : 'Hello Mr. Rogers, how are you this fine Tuesday?'\n", 132 | " Expected Output : \n", 133 | " No. of Upper case characters : 4\n", 134 | " No. of Lower case Characters : 33\n", 135 | "\n", 136 | "HINT: Two string methods that might prove useful: **.isupper()** and **.islower()**\n", 137 | "\n", 138 | "If you feel ambitious, explore the Collections module to solve this problem!" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 13, 144 | "metadata": {}, 145 | "outputs": [], 146 | "source": [ 147 | "def up_low(s):\n", 148 | " d={'upper':0, 'lower':0}\n", 149 | " for c in s:\n", 150 | " if c.isupper():\n", 151 | " d['upper']+=1\n", 152 | " elif c.islower():\n", 153 | " d['lower']+=1\n", 154 | " else:\n", 155 | " pass\n", 156 | " print('Original String : ', s)\n", 157 | " print('No. of Upper case characters : ', d['upper'])\n", 158 | " print('No. of Lower case characters : ', d['lower'])" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 8, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "name": "stdout", 168 | "output_type": "stream", 169 | "text": [ 170 | "Original String : Hello Mr. Rogers, how are you this fine Tuesday?\n", 171 | "No. of Upper case characters : 4\n", 172 | "No. of Lower case Characters : 33\n" 173 | ] 174 | } 175 | ], 176 | "source": [ 177 | "s = 'Hello Mr. Rogers, how are you this fine Tuesday?'\n", 178 | "up_low(s)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "____\n", 186 | "**Write a Python function that takes a list and returns a new list with unique elements of the first list.**\n", 187 | "\n", 188 | " Sample List : [1,1,1,1,2,2,3,3,3,3,4,5]\n", 189 | " Unique List : [1, 2, 3, 4, 5]" 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 16, 195 | "metadata": {}, 196 | "outputs": [], 197 | "source": [ 198 | "def unique_list(l):\n", 199 | " x = []\n", 200 | " for a in l:\n", 201 | " if a not in x:\n", 202 | " x.append(a)\n", 203 | " return x" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 17, 209 | "metadata": {}, 210 | "outputs": [ 211 | { 212 | "data": { 213 | "text/plain": [ 214 | "[1, 2, 3, 4, 5]" 215 | ] 216 | }, 217 | "execution_count": 17, 218 | "metadata": {}, 219 | "output_type": "execute_result" 220 | } 221 | ], 222 | "source": [ 223 | "unique_list([1,1,1,1,2,2,3,3,3,3,4,5])" 224 | ] 225 | }, 226 | { 227 | "cell_type": "markdown", 228 | "metadata": {}, 229 | "source": [ 230 | "____\n", 231 | "**Write a Python function to multiply all the numbers in a list.**\n", 232 | "\n", 233 | " Sample List : [1, 2, 3, -4]\n", 234 | " Expected Output : -24" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 18, 240 | "metadata": {}, 241 | "outputs": [], 242 | "source": [ 243 | "def multiply(numbers): \n", 244 | " total = numbers[0]\n", 245 | " for x in numbers:\n", 246 | " total *= x\n", 247 | " return total" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 19, 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "data": { 257 | "text/plain": [ 258 | "-24" 259 | ] 260 | }, 261 | "execution_count": 19, 262 | "metadata": {}, 263 | "output_type": "execute_result" 264 | } 265 | ], 266 | "source": [ 267 | "multiply([1,2,3,-4])" 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "____\n", 275 | "**Write a Python function that checks whether a passed in string is palindrome or not.**\n", 276 | "\n", 277 | "Note: A palindrome is word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run." 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 20, 283 | "metadata": {}, 284 | "outputs": [], 285 | "source": [ 286 | "def palindrome(s):\n", 287 | " return s == s[::-1]" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 21, 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "data": { 297 | "text/plain": [ 298 | "True" 299 | ] 300 | }, 301 | "execution_count": 21, 302 | "metadata": {}, 303 | "output_type": "execute_result" 304 | } 305 | ], 306 | "source": [ 307 | "palindrome('helleh')" 308 | ] 309 | }, 310 | { 311 | "cell_type": "markdown", 312 | "metadata": {}, 313 | "source": [ 314 | "____\n", 315 | "#### Hard:\n", 316 | "\n", 317 | "**Write a Python function to check whether a string is pangram or not.**\n", 318 | "\n", 319 | " Note : Pangrams are words or sentences containing every letter of the alphabet at least once.\n", 320 | " For example : \"The quick brown fox jumps over the lazy dog\"\n", 321 | "\n", 322 | "Hint: Look at the string module" 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 22, 328 | "metadata": {}, 329 | "outputs": [], 330 | "source": [ 331 | "import string\n", 332 | "\n", 333 | "def ispangram(str1, alphabet=string.ascii_lowercase):\n", 334 | " alphaset = set(alphabet)\n", 335 | " return alphaset <= set(str1.lower())" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 23, 341 | "metadata": {}, 342 | "outputs": [ 343 | { 344 | "data": { 345 | "text/plain": [ 346 | "True" 347 | ] 348 | }, 349 | "execution_count": 23, 350 | "metadata": {}, 351 | "output_type": "execute_result" 352 | } 353 | ], 354 | "source": [ 355 | "ispangram(\"The quick brown fox jumps over the lazy dog\")" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": 24, 361 | "metadata": {}, 362 | "outputs": [ 363 | { 364 | "data": { 365 | "text/plain": [ 366 | "'abcdefghijklmnopqrstuvwxyz'" 367 | ] 368 | }, 369 | "execution_count": 24, 370 | "metadata": {}, 371 | "output_type": "execute_result" 372 | } 373 | ], 374 | "source": [ 375 | "string.ascii_lowercase" 376 | ] 377 | } 378 | ], 379 | "metadata": { 380 | "kernelspec": { 381 | "display_name": "Python 3", 382 | "language": "python", 383 | "name": "python3" 384 | }, 385 | "language_info": { 386 | "codemirror_mode": { 387 | "name": "ipython", 388 | "version": 3 389 | }, 390 | "file_extension": ".py", 391 | "mimetype": "text/x-python", 392 | "name": "python", 393 | "nbconvert_exporter": "python", 394 | "pygments_lexer": "ipython3", 395 | "version": "3.7.3" 396 | } 397 | }, 398 | "nbformat": 4, 399 | "nbformat_minor": 1 400 | } 401 | -------------------------------------------------------------------------------- /2. Python Comparison Operators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "True" 12 | ] 13 | }, 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "2 == 2" 21 | ] 22 | }, 23 | { 24 | "cell_type": "code", 25 | "execution_count": 2, 26 | "metadata": {}, 27 | "outputs": [ 28 | { 29 | "data": { 30 | "text/plain": [ 31 | "False" 32 | ] 33 | }, 34 | "execution_count": 2, 35 | "metadata": {}, 36 | "output_type": "execute_result" 37 | } 38 | ], 39 | "source": [ 40 | "2 == 1" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 3, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "False" 52 | ] 53 | }, 54 | "execution_count": 3, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [ 60 | "'hello' == 'bye'" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 4, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "data": { 70 | "text/plain": [ 71 | "False" 72 | ] 73 | }, 74 | "execution_count": 4, 75 | "metadata": {}, 76 | "output_type": "execute_result" 77 | } 78 | ], 79 | "source": [ 80 | "'Bye' == 'bye'" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 5, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "data": { 90 | "text/plain": [ 91 | "False" 92 | ] 93 | }, 94 | "execution_count": 5, 95 | "metadata": {}, 96 | "output_type": "execute_result" 97 | } 98 | ], 99 | "source": [ 100 | "'2' == 2" 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 6, 106 | "metadata": {}, 107 | "outputs": [ 108 | { 109 | "data": { 110 | "text/plain": [ 111 | "True" 112 | ] 113 | }, 114 | "execution_count": 6, 115 | "metadata": {}, 116 | "output_type": "execute_result" 117 | } 118 | ], 119 | "source": [ 120 | "2.0 == 2" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 7, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "data": { 130 | "text/plain": [ 131 | "False" 132 | ] 133 | }, 134 | "execution_count": 7, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "3 != 3" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 8, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "False" 152 | ] 153 | }, 154 | "execution_count": 8, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "1 > 2" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 9, 166 | "metadata": {}, 167 | "outputs": [ 168 | { 169 | "data": { 170 | "text/plain": [ 171 | "True" 172 | ] 173 | }, 174 | "execution_count": 9, 175 | "metadata": {}, 176 | "output_type": "execute_result" 177 | } 178 | ], 179 | "source": [ 180 | "2<= 2" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 10, 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "data": { 190 | "text/plain": [ 191 | "True" 192 | ] 193 | }, 194 | "execution_count": 10, 195 | "metadata": {}, 196 | "output_type": "execute_result" 197 | } 198 | ], 199 | "source": [ 200 | "2 <= 2" 201 | ] 202 | }, 203 | { 204 | "cell_type": "markdown", 205 | "metadata": {}, 206 | "source": [ 207 | "## Chaining Comparison Operators" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 11, 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "data": { 217 | "text/plain": [ 218 | "True" 219 | ] 220 | }, 221 | "execution_count": 11, 222 | "metadata": {}, 223 | "output_type": "execute_result" 224 | } 225 | ], 226 | "source": [ 227 | "1 < 2" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 12, 233 | "metadata": {}, 234 | "outputs": [ 235 | { 236 | "data": { 237 | "text/plain": [ 238 | "True" 239 | ] 240 | }, 241 | "execution_count": 12, 242 | "metadata": {}, 243 | "output_type": "execute_result" 244 | } 245 | ], 246 | "source": [ 247 | "2 < 3" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 13, 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "data": { 257 | "text/plain": [ 258 | "True" 259 | ] 260 | }, 261 | "execution_count": 13, 262 | "metadata": {}, 263 | "output_type": "execute_result" 264 | } 265 | ], 266 | "source": [ 267 | "1 < 2 < 3" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 14, 273 | "metadata": {}, 274 | "outputs": [ 275 | { 276 | "data": { 277 | "text/plain": [ 278 | "False" 279 | ] 280 | }, 281 | "execution_count": 14, 282 | "metadata": {}, 283 | "output_type": "execute_result" 284 | } 285 | ], 286 | "source": [ 287 | "1 < 2 and 2 > 3" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 15, 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "data": { 297 | "text/plain": [ 298 | "True" 299 | ] 300 | }, 301 | "execution_count": 15, 302 | "metadata": {}, 303 | "output_type": "execute_result" 304 | } 305 | ], 306 | "source": [ 307 | "'h' == 'h' and 2 == 2" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 17, 313 | "metadata": {}, 314 | "outputs": [ 315 | { 316 | "data": { 317 | "text/plain": [ 318 | "True" 319 | ] 320 | }, 321 | "execution_count": 17, 322 | "metadata": {}, 323 | "output_type": "execute_result" 324 | } 325 | ], 326 | "source": [ 327 | "100 == 1 or 2 == 2" 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "execution_count": 18, 333 | "metadata": {}, 334 | "outputs": [ 335 | { 336 | "data": { 337 | "text/plain": [ 338 | "True" 339 | ] 340 | }, 341 | "execution_count": 18, 342 | "metadata": {}, 343 | "output_type": "execute_result" 344 | } 345 | ], 346 | "source": [ 347 | "1 == 1" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 19, 353 | "metadata": {}, 354 | "outputs": [ 355 | { 356 | "data": { 357 | "text/plain": [ 358 | "False" 359 | ] 360 | }, 361 | "execution_count": 19, 362 | "metadata": {}, 363 | "output_type": "execute_result" 364 | } 365 | ], 366 | "source": [ 367 | "not(1==1)" 368 | ] 369 | }, 370 | { 371 | "cell_type": "markdown", 372 | "metadata": {}, 373 | "source": [ 374 | "## If, elif, else Statements" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": 25, 380 | "metadata": {}, 381 | "outputs": [ 382 | { 383 | "name": "stdout", 384 | "output_type": "stream", 385 | "text": [ 386 | "Feed me\n" 387 | ] 388 | } 389 | ], 390 | "source": [ 391 | "hungry = True\n", 392 | "\n", 393 | "if hungry:\n", 394 | " print('Feed me')\n", 395 | "else:\n", 396 | " print('im not hungry')" 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 27, 402 | "metadata": {}, 403 | "outputs": [ 404 | { 405 | "name": "stdout", 406 | "output_type": "stream", 407 | "text": [ 408 | "Cars are Cool!\n" 409 | ] 410 | } 411 | ], 412 | "source": [ 413 | "loc = 'Auto Shop'\n", 414 | "\n", 415 | "if loc == 'Auto Shop':\n", 416 | " print('Cars are Cool!') \n", 417 | "elif loc == 'Bank':\n", 418 | " print('Welcome to the store!')\n", 419 | "else:\n", 420 | " print('I do not know much')" 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": 30, 426 | "metadata": {}, 427 | "outputs": [ 428 | { 429 | "name": "stdout", 430 | "output_type": "stream", 431 | "text": [ 432 | "What is your name?\n" 433 | ] 434 | } 435 | ], 436 | "source": [ 437 | "name = 'Jose'\n", 438 | "\n", 439 | "if name == 'Frankie':\n", 440 | " print('Hello Frankie')\n", 441 | "elif name == 'Sammy':\n", 442 | " print('Hello Sammy')\n", 443 | "else:\n", 444 | " print('What is your name?')" 445 | ] 446 | }, 447 | { 448 | "cell_type": "markdown", 449 | "metadata": {}, 450 | "source": [ 451 | "## For Loops in Python" 452 | ] 453 | }, 454 | { 455 | "cell_type": "code", 456 | "execution_count": 31, 457 | "metadata": {}, 458 | "outputs": [ 459 | { 460 | "name": "stdout", 461 | "output_type": "stream", 462 | "text": [ 463 | "1\n", 464 | "2\n", 465 | "3\n", 466 | "4\n", 467 | "5\n", 468 | "6\n", 469 | "7\n", 470 | "8\n", 471 | "9\n", 472 | "10\n" 473 | ] 474 | } 475 | ], 476 | "source": [ 477 | "mylist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", 478 | "for num in mylist:\n", 479 | " print(num) # you can use any variable name, not just num" 480 | ] 481 | }, 482 | { 483 | "cell_type": "code", 484 | "execution_count": null, 485 | "metadata": {}, 486 | "outputs": [], 487 | "source": [] 488 | } 489 | ], 490 | "metadata": { 491 | "kernelspec": { 492 | "display_name": "Python 3", 493 | "language": "python", 494 | "name": "python3" 495 | }, 496 | "language_info": { 497 | "codemirror_mode": { 498 | "name": "ipython", 499 | "version": 3 500 | }, 501 | "file_extension": ".py", 502 | "mimetype": "text/x-python", 503 | "name": "python", 504 | "nbconvert_exporter": "python", 505 | "pygments_lexer": "ipython3", 506 | "version": "3.7.3" 507 | } 508 | }, 509 | "nbformat": 4, 510 | "nbformat_minor": 2 511 | } 512 | -------------------------------------------------------------------------------- /7. Python Decorators & Generators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 11, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "def func():\n", 10 | " return 1" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": 12, 16 | "metadata": {}, 17 | "outputs": [ 18 | { 19 | "data": { 20 | "text/plain": [ 21 | "1" 22 | ] 23 | }, 24 | "execution_count": 12, 25 | "metadata": {}, 26 | "output_type": "execute_result" 27 | } 28 | ], 29 | "source": [ 30 | "func()" 31 | ] 32 | }, 33 | { 34 | "cell_type": "code", 35 | "execution_count": 13, 36 | "metadata": {}, 37 | "outputs": [], 38 | "source": [ 39 | "def hello():\n", 40 | " return 'Hello!'" 41 | ] 42 | }, 43 | { 44 | "cell_type": "code", 45 | "execution_count": 14, 46 | "metadata": {}, 47 | "outputs": [ 48 | { 49 | "data": { 50 | "text/plain": [ 51 | "'Hello!'" 52 | ] 53 | }, 54 | "execution_count": 14, 55 | "metadata": {}, 56 | "output_type": "execute_result" 57 | } 58 | ], 59 | "source": [ 60 | "hello()" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 15, 66 | "metadata": {}, 67 | "outputs": [], 68 | "source": [ 69 | "greet = hello" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 16, 75 | "metadata": {}, 76 | "outputs": [ 77 | { 78 | "data": { 79 | "text/plain": [ 80 | "'Hello!'" 81 | ] 82 | }, 83 | "execution_count": 16, 84 | "metadata": {}, 85 | "output_type": "execute_result" 86 | } 87 | ], 88 | "source": [ 89 | "greet()" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 17, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "del hello" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 18, 104 | "metadata": {}, 105 | "outputs": [ 106 | { 107 | "ename": "NameError", 108 | "evalue": "name 'hello' is not defined", 109 | "output_type": "error", 110 | "traceback": [ 111 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 112 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 113 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhello\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", 114 | "\u001b[1;31mNameError\u001b[0m: name 'hello' is not defined" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "hello()" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": null, 125 | "metadata": {}, 126 | "outputs": [], 127 | "source": [ 128 | "greet()" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": null, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "def hello(name='Jose'):\n", 138 | " print('The hello() function has been executed!')\n", 139 | " \n", 140 | " def greet():\n", 141 | " return '\\t This is the greet() func inside hello!'\n", 142 | " \n", 143 | " def welcome():\n", 144 | " return '\\t this is welcome() func inside hello!'\n", 145 | " \n", 146 | " print(\"I am going to return a function! !\")\n", 147 | " \n", 148 | " if name == 'Jose':\n", 149 | " return greet\n", 150 | " else:\n", 151 | " return welcome" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [ 160 | "my_new_func = hello('Jose')" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": null, 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [ 169 | "print(my_new_func())" 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": null, 175 | "metadata": {}, 176 | "outputs": [], 177 | "source": [ 178 | "def cool():\n", 179 | " \n", 180 | " def super_cool():\n", 181 | " return 'I am very cool!'\n", 182 | " \n", 183 | " return super_cool" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": null, 189 | "metadata": {}, 190 | "outputs": [], 191 | "source": [ 192 | "some_func = cool()" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": null, 198 | "metadata": {}, 199 | "outputs": [], 200 | "source": [ 201 | "some_func" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": null, 207 | "metadata": {}, 208 | "outputs": [], 209 | "source": [ 210 | "def hello():\n", 211 | " return 'Hi Jose!'" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": null, 217 | "metadata": {}, 218 | "outputs": [], 219 | "source": [ 220 | "def other(some_def_func):\n", 221 | " print('Other code runs here!')\n", 222 | " print(some_def_func())" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": null, 228 | "metadata": {}, 229 | "outputs": [], 230 | "source": [ 231 | "hello" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": null, 237 | "metadata": {}, 238 | "outputs": [], 239 | "source": [ 240 | "hello()" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": null, 246 | "metadata": {}, 247 | "outputs": [], 248 | "source": [ 249 | "other(hello)" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": null, 255 | "metadata": {}, 256 | "outputs": [], 257 | "source": [ 258 | "def new_decorator(original_func):\n", 259 | " \n", 260 | " def wrap_func():\n", 261 | " \n", 262 | " print('Some extra code, before the original function')\n", 263 | " \n", 264 | " original_func()\n", 265 | " \n", 266 | " print('Some extra code, after the original function!')\n", 267 | " \n", 268 | " return wrap_func\n", 269 | " " 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": null, 275 | "metadata": {}, 276 | "outputs": [], 277 | "source": [ 278 | "def func_needs_decorator():\n", 279 | " print(\"I want to be decorated!\")" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": null, 285 | "metadata": {}, 286 | "outputs": [], 287 | "source": [ 288 | "func_needs_decorator()" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "execution_count": null, 294 | "metadata": {}, 295 | "outputs": [], 296 | "source": [ 297 | "decorated_func = new_decorator(func_needs_decorator)" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": null, 303 | "metadata": {}, 304 | "outputs": [], 305 | "source": [ 306 | "decorated_func()" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": null, 312 | "metadata": {}, 313 | "outputs": [], 314 | "source": [ 315 | "@new_decorator\n", 316 | "def func_needs_decorator():\n", 317 | " print(\"I want to be decorated!\")" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": null, 323 | "metadata": {}, 324 | "outputs": [], 325 | "source": [ 326 | "func_needs_decorator()" 327 | ] 328 | }, 329 | { 330 | "cell_type": "code", 331 | "execution_count": null, 332 | "metadata": {}, 333 | "outputs": [], 334 | "source": [ 335 | "def create_cubes(n):\n", 336 | " result = []\n", 337 | " for x in range(n):\n", 338 | " result.append(x**3)\n", 339 | " return result" 340 | ] 341 | }, 342 | { 343 | "cell_type": "code", 344 | "execution_count": 20, 345 | "metadata": {}, 346 | "outputs": [ 347 | { 348 | "data": { 349 | "text/plain": [ 350 | "[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]" 351 | ] 352 | }, 353 | "execution_count": 20, 354 | "metadata": {}, 355 | "output_type": "execute_result" 356 | } 357 | ], 358 | "source": [ 359 | "list(create_cubes(10))" 360 | ] 361 | }, 362 | { 363 | "cell_type": "code", 364 | "execution_count": 21, 365 | "metadata": {}, 366 | "outputs": [], 367 | "source": [ 368 | "def gen_fibon(n):\n", 369 | " a = 1\n", 370 | " b = 1\n", 371 | " for i in range(n):\n", 372 | " yield a\n", 373 | " a, b = b, a+b" 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "execution_count": 22, 379 | "metadata": {}, 380 | "outputs": [ 381 | { 382 | "name": "stdout", 383 | "output_type": "stream", 384 | "text": [ 385 | "1\n", 386 | "1\n", 387 | "2\n", 388 | "3\n", 389 | "5\n", 390 | "8\n", 391 | "13\n", 392 | "21\n", 393 | "34\n", 394 | "55\n" 395 | ] 396 | } 397 | ], 398 | "source": [ 399 | "for number in gen_fibon(10):\n", 400 | " print(number)" 401 | ] 402 | }, 403 | { 404 | "cell_type": "code", 405 | "execution_count": 23, 406 | "metadata": {}, 407 | "outputs": [], 408 | "source": [ 409 | "def simple_gen():\n", 410 | " for x in range(3):\n", 411 | " yield x" 412 | ] 413 | }, 414 | { 415 | "cell_type": "code", 416 | "execution_count": 24, 417 | "metadata": {}, 418 | "outputs": [ 419 | { 420 | "name": "stdout", 421 | "output_type": "stream", 422 | "text": [ 423 | "0\n", 424 | "1\n", 425 | "2\n" 426 | ] 427 | } 428 | ], 429 | "source": [ 430 | "for number in simple_gen():\n", 431 | " print(number)" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": 25, 437 | "metadata": {}, 438 | "outputs": [], 439 | "source": [ 440 | "g = simple_gen()" 441 | ] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": 26, 446 | "metadata": {}, 447 | "outputs": [ 448 | { 449 | "data": { 450 | "text/plain": [ 451 | "" 452 | ] 453 | }, 454 | "execution_count": 26, 455 | "metadata": {}, 456 | "output_type": "execute_result" 457 | } 458 | ], 459 | "source": [ 460 | "g" 461 | ] 462 | }, 463 | { 464 | "cell_type": "code", 465 | "execution_count": 27, 466 | "metadata": {}, 467 | "outputs": [ 468 | { 469 | "name": "stdout", 470 | "output_type": "stream", 471 | "text": [ 472 | "0\n" 473 | ] 474 | } 475 | ], 476 | "source": [ 477 | "print(next(g))" 478 | ] 479 | }, 480 | { 481 | "cell_type": "code", 482 | "execution_count": 28, 483 | "metadata": {}, 484 | "outputs": [ 485 | { 486 | "name": "stdout", 487 | "output_type": "stream", 488 | "text": [ 489 | "1\n" 490 | ] 491 | } 492 | ], 493 | "source": [ 494 | "print(next(g))" 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": 29, 500 | "metadata": {}, 501 | "outputs": [], 502 | "source": [ 503 | "s = 'hello'" 504 | ] 505 | }, 506 | { 507 | "cell_type": "code", 508 | "execution_count": 30, 509 | "metadata": {}, 510 | "outputs": [ 511 | { 512 | "name": "stdout", 513 | "output_type": "stream", 514 | "text": [ 515 | "h\n", 516 | "e\n", 517 | "l\n", 518 | "l\n", 519 | "o\n" 520 | ] 521 | } 522 | ], 523 | "source": [ 524 | "for letter in s:\n", 525 | " print(letter)" 526 | ] 527 | }, 528 | { 529 | "cell_type": "code", 530 | "execution_count": 31, 531 | "metadata": {}, 532 | "outputs": [], 533 | "source": [ 534 | "s_iter = iter(s)" 535 | ] 536 | }, 537 | { 538 | "cell_type": "code", 539 | "execution_count": 32, 540 | "metadata": {}, 541 | "outputs": [ 542 | { 543 | "data": { 544 | "text/plain": [ 545 | "'h'" 546 | ] 547 | }, 548 | "execution_count": 32, 549 | "metadata": {}, 550 | "output_type": "execute_result" 551 | } 552 | ], 553 | "source": [ 554 | "next(s_iter)" 555 | ] 556 | }, 557 | { 558 | "cell_type": "code", 559 | "execution_count": 33, 560 | "metadata": {}, 561 | "outputs": [ 562 | { 563 | "data": { 564 | "text/plain": [ 565 | "'e'" 566 | ] 567 | }, 568 | "execution_count": 33, 569 | "metadata": {}, 570 | "output_type": "execute_result" 571 | } 572 | ], 573 | "source": [ 574 | "next(s_iter)" 575 | ] 576 | }, 577 | { 578 | "cell_type": "code", 579 | "execution_count": null, 580 | "metadata": {}, 581 | "outputs": [], 582 | "source": [] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "execution_count": null, 587 | "metadata": {}, 588 | "outputs": [], 589 | "source": [] 590 | } 591 | ], 592 | "metadata": { 593 | "kernelspec": { 594 | "display_name": "Python 3", 595 | "language": "python", 596 | "name": "python3" 597 | }, 598 | "language_info": { 599 | "codemirror_mode": { 600 | "name": "ipython", 601 | "version": 3 602 | }, 603 | "file_extension": ".py", 604 | "mimetype": "text/x-python", 605 | "name": "python", 606 | "nbconvert_exporter": "python", 607 | "pygments_lexer": "ipython3", 608 | "version": "3.7.3" 609 | } 610 | }, 611 | "nbformat": 4, 612 | "nbformat_minor": 2 613 | } 614 | -------------------------------------------------------------------------------- /5. Object Oriented Programming.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "#### Attributes and Class Keyword" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [], 15 | "source": [ 16 | "mylist = [1,2,3]" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": {}, 23 | "outputs": [], 24 | "source": [ 25 | "myset = set()" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 3, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "data": { 35 | "text/plain": [ 36 | "set" 37 | ] 38 | }, 39 | "execution_count": 3, 40 | "metadata": {}, 41 | "output_type": "execute_result" 42 | } 43 | ], 44 | "source": [ 45 | "type(myset)" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 4, 51 | "metadata": {}, 52 | "outputs": [ 53 | { 54 | "data": { 55 | "text/plain": [ 56 | "list" 57 | ] 58 | }, 59 | "execution_count": 4, 60 | "metadata": {}, 61 | "output_type": "execute_result" 62 | } 63 | ], 64 | "source": [ 65 | "type(mylist)" 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 5, 71 | "metadata": {}, 72 | "outputs": [], 73 | "source": [ 74 | "class Sample():\n", 75 | " pass" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 6, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "my_sample = Sample()" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 7, 90 | "metadata": {}, 91 | "outputs": [ 92 | { 93 | "data": { 94 | "text/plain": [ 95 | "__main__.Sample" 96 | ] 97 | }, 98 | "execution_count": 7, 99 | "metadata": {}, 100 | "output_type": "execute_result" 101 | } 102 | ], 103 | "source": [ 104 | "type(my_sample)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 10, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [ 113 | "class Dog():\n", 114 | " \n", 115 | " def __init__(self, breed):\n", 116 | " \n", 117 | " self.breed = breed" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": 11, 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [ 126 | "my_dog = Dog(breed='Lab')" 127 | ] 128 | }, 129 | { 130 | "cell_type": "code", 131 | "execution_count": 12, 132 | "metadata": {}, 133 | "outputs": [ 134 | { 135 | "data": { 136 | "text/plain": [ 137 | "__main__.Dog" 138 | ] 139 | }, 140 | "execution_count": 12, 141 | "metadata": {}, 142 | "output_type": "execute_result" 143 | } 144 | ], 145 | "source": [ 146 | "type(my_dog)" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": null, 152 | "metadata": {}, 153 | "outputs": [], 154 | "source": [ 155 | "my_dog.breed" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 17, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "class Dog():\n", 165 | " \n", 166 | " def __init__(self, mybreed):\n", 167 | " \n", 168 | " # Attributes\n", 169 | " # We take in the argument\n", 170 | " # We assign it using self.attribute_name\n", 171 | " self.my_attribute = mybreed" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 18, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "my_dog = Dog(mybreed ='Huskie')" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 19, 186 | "metadata": {}, 187 | "outputs": [ 188 | { 189 | "data": { 190 | "text/plain": [ 191 | "__main__.Dog" 192 | ] 193 | }, 194 | "execution_count": 19, 195 | "metadata": {}, 196 | "output_type": "execute_result" 197 | } 198 | ], 199 | "source": [ 200 | "type(my_dog)" 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": 21, 206 | "metadata": {}, 207 | "outputs": [ 208 | { 209 | "data": { 210 | "text/plain": [ 211 | "'Huskie'" 212 | ] 213 | }, 214 | "execution_count": 21, 215 | "metadata": {}, 216 | "output_type": "execute_result" 217 | } 218 | ], 219 | "source": [ 220 | "my_dog.my_attribute" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 24, 226 | "metadata": {}, 227 | "outputs": [], 228 | "source": [ 229 | "class Dog():\n", 230 | " \n", 231 | " def __init__(self, breed, name, spots):\n", 232 | " \n", 233 | " # Attributes\n", 234 | " # We take in the argument\n", 235 | " # We assign it using self.attribute_name\n", 236 | " self.breed = breed\n", 237 | " self.name = name\n", 238 | " \n", 239 | " # Expects boolean True/False\n", 240 | " self.spots = spots" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 25, 246 | "metadata": {}, 247 | "outputs": [], 248 | "source": [ 249 | "my_dog = Dog(breed = 'Chihuahua', name = 'Bonnie', spots = True)" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 26, 255 | "metadata": {}, 256 | "outputs": [ 257 | { 258 | "data": { 259 | "text/plain": [ 260 | "__main__.Dog" 261 | ] 262 | }, 263 | "execution_count": 26, 264 | "metadata": {}, 265 | "output_type": "execute_result" 266 | } 267 | ], 268 | "source": [ 269 | "type(my_dog)" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 27, 275 | "metadata": {}, 276 | "outputs": [ 277 | { 278 | "data": { 279 | "text/plain": [ 280 | "'Chihuahua'" 281 | ] 282 | }, 283 | "execution_count": 27, 284 | "metadata": {}, 285 | "output_type": "execute_result" 286 | } 287 | ], 288 | "source": [ 289 | "my_dog.breed" 290 | ] 291 | }, 292 | { 293 | "cell_type": "code", 294 | "execution_count": 28, 295 | "metadata": {}, 296 | "outputs": [ 297 | { 298 | "data": { 299 | "text/plain": [ 300 | "'Bonnie'" 301 | ] 302 | }, 303 | "execution_count": 28, 304 | "metadata": {}, 305 | "output_type": "execute_result" 306 | } 307 | ], 308 | "source": [ 309 | "my_dog.name" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 29, 315 | "metadata": {}, 316 | "outputs": [ 317 | { 318 | "data": { 319 | "text/plain": [ 320 | "True" 321 | ] 322 | }, 323 | "execution_count": 29, 324 | "metadata": {}, 325 | "output_type": "execute_result" 326 | } 327 | ], 328 | "source": [ 329 | "my_dog.spots" 330 | ] 331 | }, 332 | { 333 | "cell_type": "markdown", 334 | "metadata": {}, 335 | "source": [ 336 | "#### Class Object Attributes and Methods " 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": 33, 342 | "metadata": {}, 343 | "outputs": [], 344 | "source": [ 345 | "class Dog():\n", 346 | " \n", 347 | " # Class object attribute\n", 348 | " # Same for any instance of a class\n", 349 | " species = 'Mammal'\n", 350 | " \n", 351 | " def __init__(self, breed, name, spots):\n", 352 | " \n", 353 | " # Attributes\n", 354 | " # We take in the argument\n", 355 | " # We assign it using self.attribute_name\n", 356 | " self.breed = breed\n", 357 | " self.name = name\n", 358 | " \n", 359 | " # Expects boolean True/False\n", 360 | " self.spots = spots" 361 | ] 362 | }, 363 | { 364 | "cell_type": "code", 365 | "execution_count": 36, 366 | "metadata": {}, 367 | "outputs": [], 368 | "source": [ 369 | "my_dog = Dog(breed = 'Lab', name = 'Haus', spots = False)" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": 35, 375 | "metadata": {}, 376 | "outputs": [ 377 | { 378 | "data": { 379 | "text/plain": [ 380 | "__main__.Dog" 381 | ] 382 | }, 383 | "execution_count": 35, 384 | "metadata": {}, 385 | "output_type": "execute_result" 386 | } 387 | ], 388 | "source": [ 389 | "type(my_dog)" 390 | ] 391 | }, 392 | { 393 | "cell_type": "code", 394 | "execution_count": 37, 395 | "metadata": {}, 396 | "outputs": [ 397 | { 398 | "data": { 399 | "text/plain": [ 400 | "'Mammal'" 401 | ] 402 | }, 403 | "execution_count": 37, 404 | "metadata": {}, 405 | "output_type": "execute_result" 406 | } 407 | ], 408 | "source": [ 409 | "my_dog.species" 410 | ] 411 | }, 412 | { 413 | "cell_type": "code", 414 | "execution_count": 38, 415 | "metadata": {}, 416 | "outputs": [ 417 | { 418 | "data": { 419 | "text/plain": [ 420 | "'Lab'" 421 | ] 422 | }, 423 | "execution_count": 38, 424 | "metadata": {}, 425 | "output_type": "execute_result" 426 | } 427 | ], 428 | "source": [ 429 | "my_dog.breed" 430 | ] 431 | }, 432 | { 433 | "cell_type": "code", 434 | "execution_count": 39, 435 | "metadata": {}, 436 | "outputs": [ 437 | { 438 | "data": { 439 | "text/plain": [ 440 | "'Haus'" 441 | ] 442 | }, 443 | "execution_count": 39, 444 | "metadata": {}, 445 | "output_type": "execute_result" 446 | } 447 | ], 448 | "source": [ 449 | "my_dog.name" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 55, 455 | "metadata": {}, 456 | "outputs": [], 457 | "source": [ 458 | "class Dog():\n", 459 | " \n", 460 | " # Class object attribute\n", 461 | " # Same for any instance of a class\n", 462 | " species = 'Mammal'\n", 463 | " \n", 464 | " def __init__(self, breed, name):\n", 465 | " \n", 466 | " # Attributes\n", 467 | " # We take in the argument\n", 468 | " # We assign it using self.attribute_name\n", 469 | " self.breed = breed\n", 470 | " self.name = name\n", 471 | " \n", 472 | " # Operations/Actions ----> Methods\n", 473 | " def bark(self, number):\n", 474 | " print('WOOF! My name is {} and the number is {}'.format(self.name, number))" 475 | ] 476 | }, 477 | { 478 | "cell_type": "code", 479 | "execution_count": 56, 480 | "metadata": {}, 481 | "outputs": [], 482 | "source": [ 483 | "my_dog = Dog('Lab', 'Frankie')" 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "execution_count": 57, 489 | "metadata": {}, 490 | "outputs": [ 491 | { 492 | "data": { 493 | "text/plain": [ 494 | "__main__.Dog" 495 | ] 496 | }, 497 | "execution_count": 57, 498 | "metadata": {}, 499 | "output_type": "execute_result" 500 | } 501 | ], 502 | "source": [ 503 | "type(my_dog)" 504 | ] 505 | }, 506 | { 507 | "cell_type": "code", 508 | "execution_count": 58, 509 | "metadata": {}, 510 | "outputs": [ 511 | { 512 | "data": { 513 | "text/plain": [ 514 | "'Mammal'" 515 | ] 516 | }, 517 | "execution_count": 58, 518 | "metadata": {}, 519 | "output_type": "execute_result" 520 | } 521 | ], 522 | "source": [ 523 | "my_dog.species" 524 | ] 525 | }, 526 | { 527 | "cell_type": "code", 528 | "execution_count": 59, 529 | "metadata": {}, 530 | "outputs": [ 531 | { 532 | "data": { 533 | "text/plain": [ 534 | "'Frankie'" 535 | ] 536 | }, 537 | "execution_count": 59, 538 | "metadata": {}, 539 | "output_type": "execute_result" 540 | } 541 | ], 542 | "source": [ 543 | "my_dog.name" 544 | ] 545 | }, 546 | { 547 | "cell_type": "code", 548 | "execution_count": 62, 549 | "metadata": {}, 550 | "outputs": [ 551 | { 552 | "name": "stdout", 553 | "output_type": "stream", 554 | "text": [ 555 | "WOOF! My name is Frankie and the number is 10\n" 556 | ] 557 | } 558 | ], 559 | "source": [ 560 | "my_dog.bark(10)" 561 | ] 562 | }, 563 | { 564 | "cell_type": "code", 565 | "execution_count": 72, 566 | "metadata": {}, 567 | "outputs": [], 568 | "source": [ 569 | "class Circle():\n", 570 | " \n", 571 | " # CLASS OBJECT ATTRIBUTE\n", 572 | " pi = 3.14\n", 573 | " \n", 574 | " def __init__(self, radius=1):\n", 575 | " \n", 576 | " self.radius = radius\n", 577 | " self.area = radius*radius*self.pi\n", 578 | " \n", 579 | " # METHOD\n", 580 | " def get_circumference(self):\n", 581 | " return self.radius * self.pi * 2" 582 | ] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "execution_count": 73, 587 | "metadata": {}, 588 | "outputs": [], 589 | "source": [ 590 | "my_circle = Circle(30)" 591 | ] 592 | }, 593 | { 594 | "cell_type": "code", 595 | "execution_count": 74, 596 | "metadata": {}, 597 | "outputs": [ 598 | { 599 | "data": { 600 | "text/plain": [ 601 | "3.14" 602 | ] 603 | }, 604 | "execution_count": 74, 605 | "metadata": {}, 606 | "output_type": "execute_result" 607 | } 608 | ], 609 | "source": [ 610 | "my_circle.pi" 611 | ] 612 | }, 613 | { 614 | "cell_type": "code", 615 | "execution_count": 75, 616 | "metadata": {}, 617 | "outputs": [ 618 | { 619 | "data": { 620 | "text/plain": [ 621 | "30" 622 | ] 623 | }, 624 | "execution_count": 75, 625 | "metadata": {}, 626 | "output_type": "execute_result" 627 | } 628 | ], 629 | "source": [ 630 | "my_circle.radius" 631 | ] 632 | }, 633 | { 634 | "cell_type": "code", 635 | "execution_count": 76, 636 | "metadata": {}, 637 | "outputs": [ 638 | { 639 | "data": { 640 | "text/plain": [ 641 | "2826.0" 642 | ] 643 | }, 644 | "execution_count": 76, 645 | "metadata": {}, 646 | "output_type": "execute_result" 647 | } 648 | ], 649 | "source": [ 650 | "my_circle.area" 651 | ] 652 | }, 653 | { 654 | "cell_type": "code", 655 | "execution_count": 71, 656 | "metadata": {}, 657 | "outputs": [ 658 | { 659 | "data": { 660 | "text/plain": [ 661 | "188.4" 662 | ] 663 | }, 664 | "execution_count": 71, 665 | "metadata": {}, 666 | "output_type": "execute_result" 667 | } 668 | ], 669 | "source": [ 670 | "my_circle.get_circumference()" 671 | ] 672 | }, 673 | { 674 | "cell_type": "markdown", 675 | "metadata": {}, 676 | "source": [ 677 | "#### Inheritance" 678 | ] 679 | }, 680 | { 681 | "cell_type": "code", 682 | "execution_count": 39, 683 | "metadata": {}, 684 | "outputs": [], 685 | "source": [ 686 | "class Animal():\n", 687 | " \n", 688 | " def __init__(self):\n", 689 | " print('ANIMAL CREATED')\n", 690 | " \n", 691 | " def who_am_i(self):\n", 692 | " print('I am an animal')\n", 693 | " def eat(self):\n", 694 | " print('I am eating')" 695 | ] 696 | }, 697 | { 698 | "cell_type": "code", 699 | "execution_count": 40, 700 | "metadata": {}, 701 | "outputs": [ 702 | { 703 | "name": "stdout", 704 | "output_type": "stream", 705 | "text": [ 706 | "ANIMAL CREATED\n" 707 | ] 708 | } 709 | ], 710 | "source": [ 711 | "myanimal = Animal()" 712 | ] 713 | }, 714 | { 715 | "cell_type": "code", 716 | "execution_count": 41, 717 | "metadata": {}, 718 | "outputs": [ 719 | { 720 | "name": "stdout", 721 | "output_type": "stream", 722 | "text": [ 723 | "I am eating\n" 724 | ] 725 | } 726 | ], 727 | "source": [ 728 | "myanimal.eat()" 729 | ] 730 | }, 731 | { 732 | "cell_type": "code", 733 | "execution_count": 42, 734 | "metadata": {}, 735 | "outputs": [ 736 | { 737 | "name": "stdout", 738 | "output_type": "stream", 739 | "text": [ 740 | "I am an animal\n" 741 | ] 742 | } 743 | ], 744 | "source": [ 745 | "myanimal.who_am_i()" 746 | ] 747 | }, 748 | { 749 | "cell_type": "code", 750 | "execution_count": 43, 751 | "metadata": {}, 752 | "outputs": [], 753 | "source": [ 754 | "class Dog(Animal):\n", 755 | " \n", 756 | " def __init__(self):\n", 757 | " Animal.__init__(self)\n", 758 | " print('Dog Created')\n", 759 | " \n", 760 | " def eat(self):\n", 761 | " print('I am a dog and eating')\n", 762 | " \n", 763 | " def bark(self):\n", 764 | " print('WOOF')" 765 | ] 766 | }, 767 | { 768 | "cell_type": "code", 769 | "execution_count": 44, 770 | "metadata": {}, 771 | "outputs": [ 772 | { 773 | "name": "stdout", 774 | "output_type": "stream", 775 | "text": [ 776 | "ANIMAL CREATED\n", 777 | "Dog Created\n" 778 | ] 779 | } 780 | ], 781 | "source": [ 782 | "mydog = Dog()" 783 | ] 784 | }, 785 | { 786 | "cell_type": "code", 787 | "execution_count": 46, 788 | "metadata": {}, 789 | "outputs": [ 790 | { 791 | "name": "stdout", 792 | "output_type": "stream", 793 | "text": [ 794 | "I am a dog and eating\n" 795 | ] 796 | } 797 | ], 798 | "source": [ 799 | "mydog.eat()" 800 | ] 801 | }, 802 | { 803 | "cell_type": "code", 804 | "execution_count": 47, 805 | "metadata": {}, 806 | "outputs": [ 807 | { 808 | "name": "stdout", 809 | "output_type": "stream", 810 | "text": [ 811 | "I am an animal\n" 812 | ] 813 | } 814 | ], 815 | "source": [ 816 | "mydog.who_am_i()" 817 | ] 818 | }, 819 | { 820 | "cell_type": "code", 821 | "execution_count": 48, 822 | "metadata": {}, 823 | "outputs": [ 824 | { 825 | "name": "stdout", 826 | "output_type": "stream", 827 | "text": [ 828 | "WOOF\n" 829 | ] 830 | } 831 | ], 832 | "source": [ 833 | "mydog.bark()" 834 | ] 835 | }, 836 | { 837 | "cell_type": "markdown", 838 | "metadata": {}, 839 | "source": [ 840 | "#### Special (Magic/Dunder) Methods" 841 | ] 842 | }, 843 | { 844 | "cell_type": "code", 845 | "execution_count": 57, 846 | "metadata": {}, 847 | "outputs": [], 848 | "source": [ 849 | "class Book():\n", 850 | " \n", 851 | " def __init__(self, title, author, pages):\n", 852 | " \n", 853 | " self.title = title\n", 854 | " self.author = author\n", 855 | " self.pages = pages\n", 856 | " \n", 857 | " def __str__(self):\n", 858 | " return f\"{self.title} by {self.author}\"\n", 859 | " \n", 860 | " def __len__(self):\n", 861 | " return self.pages\n", 862 | " \n", 863 | " def __del__(self):\n", 864 | " print('A book object has been deleted')" 865 | ] 866 | }, 867 | { 868 | "cell_type": "code", 869 | "execution_count": 58, 870 | "metadata": {}, 871 | "outputs": [], 872 | "source": [ 873 | "b = Book('Python rocks', 'Jose', 200)" 874 | ] 875 | }, 876 | { 877 | "cell_type": "code", 878 | "execution_count": 59, 879 | "metadata": {}, 880 | "outputs": [ 881 | { 882 | "name": "stdout", 883 | "output_type": "stream", 884 | "text": [ 885 | "Python rocks by Jose\n" 886 | ] 887 | } 888 | ], 889 | "source": [ 890 | "print(b)" 891 | ] 892 | }, 893 | { 894 | "cell_type": "code", 895 | "execution_count": 60, 896 | "metadata": {}, 897 | "outputs": [ 898 | { 899 | "data": { 900 | "text/plain": [ 901 | "200" 902 | ] 903 | }, 904 | "execution_count": 60, 905 | "metadata": {}, 906 | "output_type": "execute_result" 907 | } 908 | ], 909 | "source": [ 910 | "len(b)" 911 | ] 912 | }, 913 | { 914 | "cell_type": "code", 915 | "execution_count": 61, 916 | "metadata": {}, 917 | "outputs": [ 918 | { 919 | "name": "stdout", 920 | "output_type": "stream", 921 | "text": [ 922 | "A book object has been deleted\n" 923 | ] 924 | } 925 | ], 926 | "source": [ 927 | "del b" 928 | ] 929 | }, 930 | { 931 | "cell_type": "code", 932 | "execution_count": 62, 933 | "metadata": {}, 934 | "outputs": [ 935 | { 936 | "ename": "NameError", 937 | "evalue": "name 'b' is not defined", 938 | "output_type": "error", 939 | "traceback": [ 940 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 941 | "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", 942 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 943 | "\u001b[1;31mNameError\u001b[0m: name 'b' is not defined" 944 | ] 945 | } 946 | ], 947 | "source": [ 948 | "b" 949 | ] 950 | } 951 | ], 952 | "metadata": { 953 | "kernelspec": { 954 | "display_name": "Python 3", 955 | "language": "python", 956 | "name": "python3" 957 | }, 958 | "language_info": { 959 | "codemirror_mode": { 960 | "name": "ipython", 961 | "version": 3 962 | }, 963 | "file_extension": ".py", 964 | "mimetype": "text/x-python", 965 | "name": "python", 966 | "nbconvert_exporter": "python", 967 | "pygments_lexer": "ipython3", 968 | "version": "3.7.3" 969 | } 970 | }, 971 | "nbformat": 4, 972 | "nbformat_minor": 2 973 | } 974 | -------------------------------------------------------------------------------- /3. Python Statements.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "mylist = [1,2,3,4,5,6,7,8,9,10]" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "hello\n", 22 | "hello\n", 23 | "hello\n", 24 | "hello\n", 25 | "hello\n", 26 | "hello\n", 27 | "hello\n", 28 | "hello\n", 29 | "hello\n", 30 | "hello\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "for num in mylist:\n", 36 | " print('hello')" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "execution_count": 4, 42 | "metadata": {}, 43 | "outputs": [ 44 | { 45 | "name": "stdout", 46 | "output_type": "stream", 47 | "text": [ 48 | "Odd Number:1\n", 49 | "2\n", 50 | "Odd Number:3\n", 51 | "4\n", 52 | "Odd Number:5\n", 53 | "6\n", 54 | "Odd Number:7\n", 55 | "8\n", 56 | "Odd Number:9\n", 57 | "10\n" 58 | ] 59 | } 60 | ], 61 | "source": [ 62 | "for num in mylist:\n", 63 | " #check for even\n", 64 | " if num % 2 == 0:\n", 65 | " print(num)\n", 66 | " else:\n", 67 | " print(f'Odd Number:{num}')" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 5, 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "55\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "list_sum = 0\n", 85 | "\n", 86 | "for num in mylist:\n", 87 | " list_sum = list_sum + num\n", 88 | "\n", 89 | "print(list_sum)" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 6, 95 | "metadata": {}, 96 | "outputs": [ 97 | { 98 | "name": "stdout", 99 | "output_type": "stream", 100 | "text": [ 101 | "H\n", 102 | "e\n", 103 | "l\n", 104 | "l\n", 105 | "o\n", 106 | " \n", 107 | "W\n", 108 | "o\n", 109 | "r\n", 110 | "l\n", 111 | "d\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "mystring = 'Hello World'\n", 117 | "\n", 118 | "for letter in mystring:\n", 119 | " print(letter)" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 7, 125 | "metadata": {}, 126 | "outputs": [ 127 | { 128 | "name": "stdout", 129 | "output_type": "stream", 130 | "text": [ 131 | "1\n", 132 | "2\n", 133 | "3\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "tup = (1,2,3)\n", 139 | "\n", 140 | "for item in tup:\n", 141 | " print(item)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "code", 146 | "execution_count": 8, 147 | "metadata": {}, 148 | "outputs": [], 149 | "source": [ 150 | "mylist = [(1,2), (3,4), (5,6), (7,8)]" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 9, 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "data": { 160 | "text/plain": [ 161 | "4" 162 | ] 163 | }, 164 | "execution_count": 9, 165 | "metadata": {}, 166 | "output_type": "execute_result" 167 | } 168 | ], 169 | "source": [ 170 | "len(mylist)" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 10, 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "name": "stdout", 180 | "output_type": "stream", 181 | "text": [ 182 | "(1, 2)\n", 183 | "(3, 4)\n", 184 | "(5, 6)\n", 185 | "(7, 8)\n" 186 | ] 187 | } 188 | ], 189 | "source": [ 190 | "for item in mylist:\n", 191 | " print(item)" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 11, 197 | "metadata": {}, 198 | "outputs": [ 199 | { 200 | "name": "stdout", 201 | "output_type": "stream", 202 | "text": [ 203 | "1\n", 204 | "3\n", 205 | "5\n", 206 | "7\n" 207 | ] 208 | } 209 | ], 210 | "source": [ 211 | "for a, b in mylist:\n", 212 | " print(a)" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 14, 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "name": "stdout", 222 | "output_type": "stream", 223 | "text": [ 224 | "3\n", 225 | "6\n", 226 | "9\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "mylist = [(1,2,3), (4,5,6), (7,8,9)]\n", 232 | "\n", 233 | "for a, b, c in mylist:\n", 234 | " print(c)" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 15, 240 | "metadata": {}, 241 | "outputs": [ 242 | { 243 | "name": "stdout", 244 | "output_type": "stream", 245 | "text": [ 246 | "k1\n", 247 | "k2\n", 248 | "k3\n" 249 | ] 250 | } 251 | ], 252 | "source": [ 253 | "d = {'k1': 1, 'k2': 2, 'k3': 3}\n", 254 | "\n", 255 | "for item in d:\n", 256 | " print(item)" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": 16, 262 | "metadata": {}, 263 | "outputs": [ 264 | { 265 | "name": "stdout", 266 | "output_type": "stream", 267 | "text": [ 268 | "4\n", 269 | "5\n", 270 | "6\n" 271 | ] 272 | } 273 | ], 274 | "source": [ 275 | "d = {'k4': 4, 'k5' : 5, 'k6' : 6}\n", 276 | "\n", 277 | "for key, value in d.items():\n", 278 | " print(value)" 279 | ] 280 | }, 281 | { 282 | "cell_type": "markdown", 283 | "metadata": {}, 284 | "source": [ 285 | "## While Loops in Python" 286 | ] 287 | }, 288 | { 289 | "cell_type": "code", 290 | "execution_count": 18, 291 | "metadata": {}, 292 | "outputs": [ 293 | { 294 | "name": "stdout", 295 | "output_type": "stream", 296 | "text": [ 297 | "The current value of x is 0\n", 298 | "The current value of x is 2\n", 299 | "The current value of x is 4\n", 300 | "X is not less than 5\n" 301 | ] 302 | } 303 | ], 304 | "source": [ 305 | "x = 0\n", 306 | "\n", 307 | "while x < 5:\n", 308 | " print(f'The current value of x is {x}')\n", 309 | " x = x + 1\n", 310 | " x += 1\n", 311 | "else:\n", 312 | " print(\"X is not less than 5\")" 313 | ] 314 | }, 315 | { 316 | "cell_type": "code", 317 | "execution_count": 19, 318 | "metadata": {}, 319 | "outputs": [ 320 | { 321 | "name": "stdout", 322 | "output_type": "stream", 323 | "text": [ 324 | "End of my script\n" 325 | ] 326 | } 327 | ], 328 | "source": [ 329 | "x = [1,2,3]\n", 330 | "\n", 331 | "for item in x:\n", 332 | " pass\n", 333 | "print('End of my script')" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 20, 339 | "metadata": {}, 340 | "outputs": [], 341 | "source": [ 342 | "mystring = 'Sammy'" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 23, 348 | "metadata": {}, 349 | "outputs": [ 350 | { 351 | "name": "stdout", 352 | "output_type": "stream", 353 | "text": [ 354 | "S\n", 355 | "m\n", 356 | "m\n", 357 | "y\n" 358 | ] 359 | } 360 | ], 361 | "source": [ 362 | "for letter in mystring:\n", 363 | " if letter == 'a':\n", 364 | " continue\n", 365 | " \n", 366 | " print(letter)" 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 24, 372 | "metadata": {}, 373 | "outputs": [ 374 | { 375 | "name": "stdout", 376 | "output_type": "stream", 377 | "text": [ 378 | "S\n" 379 | ] 380 | } 381 | ], 382 | "source": [ 383 | "for letter in mystring:\n", 384 | " if letter == 'a':\n", 385 | " break\n", 386 | " print(letter)" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 26, 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "name": "stdout", 396 | "output_type": "stream", 397 | "text": [ 398 | "0\n", 399 | "1\n" 400 | ] 401 | } 402 | ], 403 | "source": [ 404 | "x = 0\n", 405 | "\n", 406 | "while x < 5:\n", 407 | " \n", 408 | " if x == 2:\n", 409 | " break\n", 410 | " print(x)\n", 411 | " x += 1" 412 | ] 413 | }, 414 | { 415 | "cell_type": "markdown", 416 | "metadata": {}, 417 | "source": [ 418 | "## Useful Operators in Python" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": 27, 424 | "metadata": {}, 425 | "outputs": [], 426 | "source": [ 427 | "mylist = [1,2,3]" 428 | ] 429 | }, 430 | { 431 | "cell_type": "code", 432 | "execution_count": 28, 433 | "metadata": {}, 434 | "outputs": [ 435 | { 436 | "name": "stdout", 437 | "output_type": "stream", 438 | "text": [ 439 | "0\n", 440 | "2\n", 441 | "4\n", 442 | "6\n", 443 | "8\n", 444 | "10\n" 445 | ] 446 | } 447 | ], 448 | "source": [ 449 | "for num in range (0,11,2):\n", 450 | " print(num)" 451 | ] 452 | }, 453 | { 454 | "cell_type": "code", 455 | "execution_count": 29, 456 | "metadata": {}, 457 | "outputs": [ 458 | { 459 | "name": "stdout", 460 | "output_type": "stream", 461 | "text": [ 462 | "a\n", 463 | "b\n", 464 | "c\n", 465 | "d\n", 466 | "e\n" 467 | ] 468 | } 469 | ], 470 | "source": [ 471 | "index_count = 0\n", 472 | "word = 'abcde'\n", 473 | "\n", 474 | "for letter in word:\n", 475 | " \n", 476 | " print(word[index_count])\n", 477 | " index_count += 1" 478 | ] 479 | }, 480 | { 481 | "cell_type": "code", 482 | "execution_count": 30, 483 | "metadata": {}, 484 | "outputs": [], 485 | "source": [ 486 | "# can be done this way too" 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "execution_count": 31, 492 | "metadata": {}, 493 | "outputs": [ 494 | { 495 | "name": "stdout", 496 | "output_type": "stream", 497 | "text": [ 498 | "(0, 'a')\n", 499 | "(1, 'b')\n", 500 | "(2, 'c')\n", 501 | "(3, 'd')\n", 502 | "(4, 'e')\n" 503 | ] 504 | } 505 | ], 506 | "source": [ 507 | "word = 'abcde'\n", 508 | "\n", 509 | "for item in enumerate(word):\n", 510 | " print(item)" 511 | ] 512 | }, 513 | { 514 | "cell_type": "code", 515 | "execution_count": 32, 516 | "metadata": {}, 517 | "outputs": [], 518 | "source": [ 519 | "mylist1 = [1,2,3]\n", 520 | "mylist2 = ['a','b','c']" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": 33, 526 | "metadata": {}, 527 | "outputs": [ 528 | { 529 | "name": "stdout", 530 | "output_type": "stream", 531 | "text": [ 532 | "(1, 'a')\n", 533 | "(2, 'b')\n", 534 | "(3, 'c')\n" 535 | ] 536 | } 537 | ], 538 | "source": [ 539 | "for item in zip(mylist1, mylist2):\n", 540 | " print(item)" 541 | ] 542 | }, 543 | { 544 | "cell_type": "code", 545 | "execution_count": 34, 546 | "metadata": {}, 547 | "outputs": [ 548 | { 549 | "data": { 550 | "text/plain": [ 551 | "[(1, 'a'), (2, 'b'), (3, 'c')]" 552 | ] 553 | }, 554 | "execution_count": 34, 555 | "metadata": {}, 556 | "output_type": "execute_result" 557 | } 558 | ], 559 | "source": [ 560 | "list(zip(mylist1, mylist2))" 561 | ] 562 | }, 563 | { 564 | "cell_type": "code", 565 | "execution_count": 35, 566 | "metadata": {}, 567 | "outputs": [ 568 | { 569 | "data": { 570 | "text/plain": [ 571 | "False" 572 | ] 573 | }, 574 | "execution_count": 35, 575 | "metadata": {}, 576 | "output_type": "execute_result" 577 | } 578 | ], 579 | "source": [ 580 | "d = {'mykey': 345}\n", 581 | "\n", 582 | "345 in d.keys()" 583 | ] 584 | }, 585 | { 586 | "cell_type": "code", 587 | "execution_count": 51, 588 | "metadata": {}, 589 | "outputs": [], 590 | "source": [ 591 | "from random import shuffle" 592 | ] 593 | }, 594 | { 595 | "cell_type": "code", 596 | "execution_count": 52, 597 | "metadata": {}, 598 | "outputs": [], 599 | "source": [ 600 | "mylist = [1,2,3,4,5,6,7,8,9,10]" 601 | ] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": 53, 606 | "metadata": {}, 607 | "outputs": [], 608 | "source": [ 609 | "shuffle(mylist)" 610 | ] 611 | }, 612 | { 613 | "cell_type": "code", 614 | "execution_count": 54, 615 | "metadata": {}, 616 | "outputs": [ 617 | { 618 | "data": { 619 | "text/plain": [ 620 | "[7, 2, 4, 1, 8, 9, 5, 3, 6, 10]" 621 | ] 622 | }, 623 | "execution_count": 54, 624 | "metadata": {}, 625 | "output_type": "execute_result" 626 | } 627 | ], 628 | "source": [ 629 | "mylist" 630 | ] 631 | }, 632 | { 633 | "cell_type": "code", 634 | "execution_count": 56, 635 | "metadata": {}, 636 | "outputs": [], 637 | "source": [ 638 | "from random import randint" 639 | ] 640 | }, 641 | { 642 | "cell_type": "code", 643 | "execution_count": 57, 644 | "metadata": {}, 645 | "outputs": [ 646 | { 647 | "data": { 648 | "text/plain": [ 649 | "58" 650 | ] 651 | }, 652 | "execution_count": 57, 653 | "metadata": {}, 654 | "output_type": "execute_result" 655 | } 656 | ], 657 | "source": [ 658 | "randint(0,100)" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 61, 664 | "metadata": {}, 665 | "outputs": [ 666 | { 667 | "name": "stdout", 668 | "output_type": "stream", 669 | "text": [ 670 | "What is your favorite number:27\n" 671 | ] 672 | } 673 | ], 674 | "source": [ 675 | "result = input('What is your favorite number:')" 676 | ] 677 | }, 678 | { 679 | "cell_type": "code", 680 | "execution_count": 62, 681 | "metadata": {}, 682 | "outputs": [ 683 | { 684 | "data": { 685 | "text/plain": [ 686 | "'27'" 687 | ] 688 | }, 689 | "execution_count": 62, 690 | "metadata": {}, 691 | "output_type": "execute_result" 692 | } 693 | ], 694 | "source": [ 695 | "result" 696 | ] 697 | }, 698 | { 699 | "cell_type": "markdown", 700 | "metadata": {}, 701 | "source": [ 702 | "## List Comprehensions in Python" 703 | ] 704 | }, 705 | { 706 | "cell_type": "code", 707 | "execution_count": 1, 708 | "metadata": {}, 709 | "outputs": [], 710 | "source": [ 711 | "mystring = 'hello'" 712 | ] 713 | }, 714 | { 715 | "cell_type": "code", 716 | "execution_count": 2, 717 | "metadata": {}, 718 | "outputs": [], 719 | "source": [ 720 | "mylist = []\n", 721 | "\n", 722 | "for letter in mystring:\n", 723 | " mylist.append(letter)" 724 | ] 725 | }, 726 | { 727 | "cell_type": "code", 728 | "execution_count": 3, 729 | "metadata": {}, 730 | "outputs": [ 731 | { 732 | "data": { 733 | "text/plain": [ 734 | "['h', 'e', 'l', 'l', 'o']" 735 | ] 736 | }, 737 | "execution_count": 3, 738 | "metadata": {}, 739 | "output_type": "execute_result" 740 | } 741 | ], 742 | "source": [ 743 | "mylist" 744 | ] 745 | }, 746 | { 747 | "cell_type": "code", 748 | "execution_count": 4, 749 | "metadata": {}, 750 | "outputs": [], 751 | "source": [ 752 | "mylist = []\n", 753 | "\n", 754 | "for letter in mystring:\n", 755 | " mylist.append(letter) # it is also the same if you do it this way, check below" 756 | ] 757 | }, 758 | { 759 | "cell_type": "code", 760 | "execution_count": 5, 761 | "metadata": {}, 762 | "outputs": [], 763 | "source": [ 764 | "mylist = [letter for letter in mystring]" 765 | ] 766 | }, 767 | { 768 | "cell_type": "code", 769 | "execution_count": 6, 770 | "metadata": {}, 771 | "outputs": [ 772 | { 773 | "data": { 774 | "text/plain": [ 775 | "['h', 'e', 'l', 'l', 'o']" 776 | ] 777 | }, 778 | "execution_count": 6, 779 | "metadata": {}, 780 | "output_type": "execute_result" 781 | } 782 | ], 783 | "source": [ 784 | "mylist" 785 | ] 786 | }, 787 | { 788 | "cell_type": "code", 789 | "execution_count": 7, 790 | "metadata": {}, 791 | "outputs": [], 792 | "source": [ 793 | "mylist = [x for x in 'word']" 794 | ] 795 | }, 796 | { 797 | "cell_type": "code", 798 | "execution_count": 8, 799 | "metadata": {}, 800 | "outputs": [ 801 | { 802 | "data": { 803 | "text/plain": [ 804 | "['w', 'o', 'r', 'd']" 805 | ] 806 | }, 807 | "execution_count": 8, 808 | "metadata": {}, 809 | "output_type": "execute_result" 810 | } 811 | ], 812 | "source": [ 813 | "mylist" 814 | ] 815 | }, 816 | { 817 | "cell_type": "code", 818 | "execution_count": 9, 819 | "metadata": {}, 820 | "outputs": [], 821 | "source": [ 822 | "mylist = [num for num in range(0,11)]" 823 | ] 824 | }, 825 | { 826 | "cell_type": "code", 827 | "execution_count": 10, 828 | "metadata": {}, 829 | "outputs": [ 830 | { 831 | "data": { 832 | "text/plain": [ 833 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" 834 | ] 835 | }, 836 | "execution_count": 10, 837 | "metadata": {}, 838 | "output_type": "execute_result" 839 | } 840 | ], 841 | "source": [ 842 | "mylist" 843 | ] 844 | }, 845 | { 846 | "cell_type": "code", 847 | "execution_count": 11, 848 | "metadata": {}, 849 | "outputs": [], 850 | "source": [ 851 | "mylist = [num**2 for num in range(0,11)]" 852 | ] 853 | }, 854 | { 855 | "cell_type": "code", 856 | "execution_count": 12, 857 | "metadata": {}, 858 | "outputs": [ 859 | { 860 | "data": { 861 | "text/plain": [ 862 | "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]" 863 | ] 864 | }, 865 | "execution_count": 12, 866 | "metadata": {}, 867 | "output_type": "execute_result" 868 | } 869 | ], 870 | "source": [ 871 | "mylist" 872 | ] 873 | }, 874 | { 875 | "cell_type": "code", 876 | "execution_count": 15, 877 | "metadata": {}, 878 | "outputs": [], 879 | "source": [ 880 | "mylist = [x**2 for x in range(0,11) if x%2==0]" 881 | ] 882 | }, 883 | { 884 | "cell_type": "code", 885 | "execution_count": 16, 886 | "metadata": {}, 887 | "outputs": [ 888 | { 889 | "data": { 890 | "text/plain": [ 891 | "[0, 4, 16, 36, 64, 100]" 892 | ] 893 | }, 894 | "execution_count": 16, 895 | "metadata": {}, 896 | "output_type": "execute_result" 897 | } 898 | ], 899 | "source": [ 900 | "mylist" 901 | ] 902 | }, 903 | { 904 | "cell_type": "code", 905 | "execution_count": 18, 906 | "metadata": {}, 907 | "outputs": [], 908 | "source": [ 909 | "celcius = [0,10,20,35.4]\n", 910 | "\n", 911 | "fahrenheit = [( (9/5)*temp + 32)for temp in celcius]" 912 | ] 913 | }, 914 | { 915 | "cell_type": "code", 916 | "execution_count": 19, 917 | "metadata": {}, 918 | "outputs": [ 919 | { 920 | "data": { 921 | "text/plain": [ 922 | "[32.0, 50.0, 68.0, 95.72]" 923 | ] 924 | }, 925 | "execution_count": 19, 926 | "metadata": {}, 927 | "output_type": "execute_result" 928 | } 929 | ], 930 | "source": [ 931 | "fahrenheit" 932 | ] 933 | }, 934 | { 935 | "cell_type": "code", 936 | "execution_count": 20, 937 | "metadata": {}, 938 | "outputs": [], 939 | "source": [ 940 | "# you can also do it this way" 941 | ] 942 | }, 943 | { 944 | "cell_type": "code", 945 | "execution_count": 21, 946 | "metadata": {}, 947 | "outputs": [], 948 | "source": [ 949 | "fahrenheit = []\n", 950 | "\n", 951 | "for temp in celcius:\n", 952 | " fahrenheit.append(((9/5)*temp + 32))" 953 | ] 954 | }, 955 | { 956 | "cell_type": "code", 957 | "execution_count": 22, 958 | "metadata": {}, 959 | "outputs": [ 960 | { 961 | "data": { 962 | "text/plain": [ 963 | "[32.0, 50.0, 68.0, 95.72]" 964 | ] 965 | }, 966 | "execution_count": 22, 967 | "metadata": {}, 968 | "output_type": "execute_result" 969 | } 970 | ], 971 | "source": [ 972 | "fahrenheit" 973 | ] 974 | }, 975 | { 976 | "cell_type": "code", 977 | "execution_count": null, 978 | "metadata": {}, 979 | "outputs": [], 980 | "source": [] 981 | } 982 | ], 983 | "metadata": { 984 | "kernelspec": { 985 | "display_name": "Python 3", 986 | "language": "python", 987 | "name": "python3" 988 | }, 989 | "language_info": { 990 | "codemirror_mode": { 991 | "name": "ipython", 992 | "version": 3 993 | }, 994 | "file_extension": ".py", 995 | "mimetype": "text/x-python", 996 | "name": "python", 997 | "nbconvert_exporter": "python", 998 | "pygments_lexer": "ipython3", 999 | "version": "3.7.3" 1000 | } 1001 | }, 1002 | "nbformat": 4, 1003 | "nbformat_minor": 2 1004 | } 1005 | -------------------------------------------------------------------------------- /4. Methods and Functions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "[1, 2, 3, 4]" 12 | ] 13 | }, 14 | "execution_count": 2, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "mylist = [1,2,3]\n", 21 | "mylist.append(4)\n", 22 | "mylist" 23 | ] 24 | }, 25 | { 26 | "cell_type": "code", 27 | "execution_count": 3, 28 | "metadata": {}, 29 | "outputs": [ 30 | { 31 | "data": { 32 | "text/plain": [ 33 | "4" 34 | ] 35 | }, 36 | "execution_count": 3, 37 | "metadata": {}, 38 | "output_type": "execute_result" 39 | } 40 | ], 41 | "source": [ 42 | "mylist.pop()" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 4, 48 | "metadata": {}, 49 | "outputs": [ 50 | { 51 | "data": { 52 | "text/plain": [ 53 | "[1, 2, 3]" 54 | ] 55 | }, 56 | "execution_count": 4, 57 | "metadata": {}, 58 | "output_type": "execute_result" 59 | } 60 | ], 61 | "source": [ 62 | "mylist" 63 | ] 64 | }, 65 | { 66 | "cell_type": "code", 67 | "execution_count": 5, 68 | "metadata": {}, 69 | "outputs": [ 70 | { 71 | "name": "stdout", 72 | "output_type": "stream", 73 | "text": [ 74 | "Help on built-in function insert:\n", 75 | "\n", 76 | "insert(index, object, /) method of builtins.list instance\n", 77 | " Insert object before index.\n", 78 | "\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "help(mylist.insert)" 84 | ] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": {}, 89 | "source": [ 90 | "### Functions in Python" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 10, 96 | "metadata": {}, 97 | "outputs": [], 98 | "source": [ 99 | "def name_function():\n", 100 | " '''\n", 101 | " DOCSTRING: Information about the function\n", 102 | " INPUT: no input...\n", 103 | " OUTPUT: no output...\n", 104 | " '''\n", 105 | " print('Hello')" 106 | ] 107 | }, 108 | { 109 | "cell_type": "code", 110 | "execution_count": 12, 111 | "metadata": {}, 112 | "outputs": [ 113 | { 114 | "name": "stdout", 115 | "output_type": "stream", 116 | "text": [ 117 | "Help on function name_function in module __main__:\n", 118 | "\n", 119 | "name_function()\n", 120 | " DOCSTRING: Information about the function\n", 121 | " INPUT: no input...\n", 122 | " OUTPUT: no output...\n", 123 | "\n" 124 | ] 125 | } 126 | ], 127 | "source": [ 128 | "help(name_function)" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 21, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "def say_hello(name='NAME'):\n", 138 | " print('hello ' + name)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 23, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "name": "stdout", 148 | "output_type": "stream", 149 | "text": [ 150 | "hello David\n" 151 | ] 152 | } 153 | ], 154 | "source": [ 155 | "say_hello('David')" 156 | ] 157 | }, 158 | { 159 | "cell_type": "code", 160 | "execution_count": 25, 161 | "metadata": {}, 162 | "outputs": [], 163 | "source": [ 164 | "def say_hello(name='NAME'):\n", 165 | " return 'hello '+name" 166 | ] 167 | }, 168 | { 169 | "cell_type": "code", 170 | "execution_count": 26, 171 | "metadata": {}, 172 | "outputs": [], 173 | "source": [ 174 | "result = say_hello('Jose')" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": 27, 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "data": { 184 | "text/plain": [ 185 | "'hello Jose'" 186 | ] 187 | }, 188 | "execution_count": 27, 189 | "metadata": {}, 190 | "output_type": "execute_result" 191 | } 192 | ], 193 | "source": [ 194 | "result" 195 | ] 196 | }, 197 | { 198 | "cell_type": "code", 199 | "execution_count": 28, 200 | "metadata": {}, 201 | "outputs": [], 202 | "source": [ 203 | "def add(n1, n2):\n", 204 | " return n1+n2" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 29, 210 | "metadata": {}, 211 | "outputs": [], 212 | "source": [ 213 | "result = add(20,30)" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": 30, 219 | "metadata": {}, 220 | "outputs": [ 221 | { 222 | "data": { 223 | "text/plain": [ 224 | "50" 225 | ] 226 | }, 227 | "execution_count": 30, 228 | "metadata": {}, 229 | "output_type": "execute_result" 230 | } 231 | ], 232 | "source": [ 233 | "result" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 1, 239 | "metadata": {}, 240 | "outputs": [], 241 | "source": [ 242 | "def dog_check(mystring):\n", 243 | " if 'dog' in mystring:\n", 244 | " return True\n", 245 | " else:\n", 246 | " return False" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 2, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "data": { 256 | "text/plain": [ 257 | "False" 258 | ] 259 | }, 260 | "execution_count": 2, 261 | "metadata": {}, 262 | "output_type": "execute_result" 263 | } 264 | ], 265 | "source": [ 266 | "dog_check('My cat ran away')" 267 | ] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": 3, 272 | "metadata": {}, 273 | "outputs": [ 274 | { 275 | "data": { 276 | "text/plain": [ 277 | "True" 278 | ] 279 | }, 280 | "execution_count": 3, 281 | "metadata": {}, 282 | "output_type": "execute_result" 283 | } 284 | ], 285 | "source": [ 286 | "dog_check('My dog ran away')" 287 | ] 288 | }, 289 | { 290 | "cell_type": "code", 291 | "execution_count": null, 292 | "metadata": {}, 293 | "outputs": [], 294 | "source": [ 295 | "# you actually don't need to specify the if statement because 'dog' in 'dog ran away' will return True statement" 296 | ] 297 | }, 298 | { 299 | "cell_type": "code", 300 | "execution_count": 4, 301 | "metadata": {}, 302 | "outputs": [], 303 | "source": [ 304 | "def dog_check(mystring):\n", 305 | " return 'dog' in mystring.lower()" 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 5, 311 | "metadata": {}, 312 | "outputs": [ 313 | { 314 | "data": { 315 | "text/plain": [ 316 | "True" 317 | ] 318 | }, 319 | "execution_count": 5, 320 | "metadata": {}, 321 | "output_type": "execute_result" 322 | } 323 | ], 324 | "source": [ 325 | "dog_check('Your dog ran away')" 326 | ] 327 | }, 328 | { 329 | "cell_type": "markdown", 330 | "metadata": {}, 331 | "source": [ 332 | "### Pig Latin" 333 | ] 334 | }, 335 | { 336 | "cell_type": "markdown", 337 | "metadata": {}, 338 | "source": [ 339 | "- If word starts with a vowel, add 'ay'to end\n", 340 | "- If word does not start with a vowel, put first letter at the end, then add 'ay'\n", 341 | "- word --> ordway\n", 342 | "- apple --> appleay" 343 | ] 344 | }, 345 | { 346 | "cell_type": "code", 347 | "execution_count": 7, 348 | "metadata": {}, 349 | "outputs": [], 350 | "source": [ 351 | "def pig_latin(word):\n", 352 | " \n", 353 | " first_letter = word[0]\n", 354 | " \n", 355 | " # Check if vowel\n", 356 | " if first_letter in 'aeiou':\n", 357 | " pig_word = word + 'ay'\n", 358 | " else:\n", 359 | " pig_word = word[1:] + first_letter + 'ay'\n", 360 | " \n", 361 | " return pig_word" 362 | ] 363 | }, 364 | { 365 | "cell_type": "code", 366 | "execution_count": 8, 367 | "metadata": {}, 368 | "outputs": [ 369 | { 370 | "data": { 371 | "text/plain": [ 372 | "'appleay'" 373 | ] 374 | }, 375 | "execution_count": 8, 376 | "metadata": {}, 377 | "output_type": "execute_result" 378 | } 379 | ], 380 | "source": [ 381 | "pig_latin('apple')" 382 | ] 383 | }, 384 | { 385 | "cell_type": "markdown", 386 | "metadata": {}, 387 | "source": [ 388 | "#### Lambda Expressions, Map, and Filter Functions" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": 1, 394 | "metadata": {}, 395 | "outputs": [], 396 | "source": [ 397 | "def square(num):\n", 398 | " return num**2" 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": 2, 404 | "metadata": {}, 405 | "outputs": [], 406 | "source": [ 407 | "my_nums = [1, 2, 3, 4, 5]" 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": 3, 413 | "metadata": {}, 414 | "outputs": [ 415 | { 416 | "data": { 417 | "text/plain": [ 418 | "" 419 | ] 420 | }, 421 | "execution_count": 3, 422 | "metadata": {}, 423 | "output_type": "execute_result" 424 | } 425 | ], 426 | "source": [ 427 | "map(square, my_nums)" 428 | ] 429 | }, 430 | { 431 | "cell_type": "code", 432 | "execution_count": 5, 433 | "metadata": {}, 434 | "outputs": [ 435 | { 436 | "name": "stdout", 437 | "output_type": "stream", 438 | "text": [ 439 | "1\n", 440 | "4\n", 441 | "9\n", 442 | "16\n", 443 | "25\n" 444 | ] 445 | } 446 | ], 447 | "source": [ 448 | "for item in map(square, my_nums):\n", 449 | " print(item)" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 4, 455 | "metadata": {}, 456 | "outputs": [ 457 | { 458 | "data": { 459 | "text/plain": [ 460 | "[1, 4, 9, 16, 25]" 461 | ] 462 | }, 463 | "execution_count": 4, 464 | "metadata": {}, 465 | "output_type": "execute_result" 466 | } 467 | ], 468 | "source": [ 469 | "list(map(square, my_nums))" 470 | ] 471 | }, 472 | { 473 | "cell_type": "code", 474 | "execution_count": 8, 475 | "metadata": {}, 476 | "outputs": [], 477 | "source": [ 478 | "def splicer(mystring):\n", 479 | " if len(mystring)%2 == 0:\n", 480 | " return 'EVEN'\n", 481 | " else:\n", 482 | " return mystring[0] " 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": 9, 488 | "metadata": {}, 489 | "outputs": [], 490 | "source": [ 491 | "names = ['Andy', 'Eve', 'Sally']" 492 | ] 493 | }, 494 | { 495 | "cell_type": "code", 496 | "execution_count": 10, 497 | "metadata": {}, 498 | "outputs": [ 499 | { 500 | "data": { 501 | "text/plain": [ 502 | "['EVEN', 'E', 'S']" 503 | ] 504 | }, 505 | "execution_count": 10, 506 | "metadata": {}, 507 | "output_type": "execute_result" 508 | } 509 | ], 510 | "source": [ 511 | "list(map(splicer, names))" 512 | ] 513 | }, 514 | { 515 | "cell_type": "code", 516 | "execution_count": 11, 517 | "metadata": {}, 518 | "outputs": [], 519 | "source": [ 520 | "def check_even(num):\n", 521 | " return num%2 == 0" 522 | ] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "execution_count": 12, 527 | "metadata": {}, 528 | "outputs": [], 529 | "source": [ 530 | "mynums = [1,2,3,4,5,6]" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": 14, 536 | "metadata": {}, 537 | "outputs": [ 538 | { 539 | "data": { 540 | "text/plain": [ 541 | "[2, 4, 6]" 542 | ] 543 | }, 544 | "execution_count": 14, 545 | "metadata": {}, 546 | "output_type": "execute_result" 547 | } 548 | ], 549 | "source": [ 550 | "list(filter(check_even, mynums))" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": 16, 556 | "metadata": {}, 557 | "outputs": [ 558 | { 559 | "name": "stdout", 560 | "output_type": "stream", 561 | "text": [ 562 | "2\n", 563 | "4\n", 564 | "6\n" 565 | ] 566 | } 567 | ], 568 | "source": [ 569 | "for n in filter(check_even, mynums):\n", 570 | " print(n)" 571 | ] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "execution_count": 17, 576 | "metadata": {}, 577 | "outputs": [], 578 | "source": [ 579 | "def square(num):\n", 580 | " result = num ** 2\n", 581 | " return result" 582 | ] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "execution_count": 18, 587 | "metadata": {}, 588 | "outputs": [ 589 | { 590 | "data": { 591 | "text/plain": [ 592 | "9" 593 | ] 594 | }, 595 | "execution_count": 18, 596 | "metadata": {}, 597 | "output_type": "execute_result" 598 | } 599 | ], 600 | "source": [ 601 | "square(3)" 602 | ] 603 | }, 604 | { 605 | "cell_type": "code", 606 | "execution_count": 20, 607 | "metadata": {}, 608 | "outputs": [], 609 | "source": [ 610 | "square = lambda num: num ** 2" 611 | ] 612 | }, 613 | { 614 | "cell_type": "code", 615 | "execution_count": 21, 616 | "metadata": {}, 617 | "outputs": [ 618 | { 619 | "data": { 620 | "text/plain": [ 621 | "25" 622 | ] 623 | }, 624 | "execution_count": 21, 625 | "metadata": {}, 626 | "output_type": "execute_result" 627 | } 628 | ], 629 | "source": [ 630 | "square(5)" 631 | ] 632 | }, 633 | { 634 | "cell_type": "code", 635 | "execution_count": 22, 636 | "metadata": {}, 637 | "outputs": [ 638 | { 639 | "data": { 640 | "text/plain": [ 641 | "[1, 4, 9, 16, 25, 36]" 642 | ] 643 | }, 644 | "execution_count": 22, 645 | "metadata": {}, 646 | "output_type": "execute_result" 647 | } 648 | ], 649 | "source": [ 650 | "list(map(lambda num: num ** 2, mynums))" 651 | ] 652 | }, 653 | { 654 | "cell_type": "code", 655 | "execution_count": 23, 656 | "metadata": {}, 657 | "outputs": [ 658 | { 659 | "data": { 660 | "text/plain": [ 661 | "[2, 4, 6]" 662 | ] 663 | }, 664 | "execution_count": 23, 665 | "metadata": {}, 666 | "output_type": "execute_result" 667 | } 668 | ], 669 | "source": [ 670 | "list(filter(lambda num:num%2 == 0, mynums))" 671 | ] 672 | }, 673 | { 674 | "cell_type": "code", 675 | "execution_count": 24, 676 | "metadata": {}, 677 | "outputs": [ 678 | { 679 | "data": { 680 | "text/plain": [ 681 | "['A', 'E', 'S']" 682 | ] 683 | }, 684 | "execution_count": 24, 685 | "metadata": {}, 686 | "output_type": "execute_result" 687 | } 688 | ], 689 | "source": [ 690 | "list(map(lambda x:x[0], names))" 691 | ] 692 | }, 693 | { 694 | "cell_type": "code", 695 | "execution_count": 25, 696 | "metadata": {}, 697 | "outputs": [ 698 | { 699 | "data": { 700 | "text/plain": [ 701 | "['ydnA', 'evE', 'yllaS']" 702 | ] 703 | }, 704 | "execution_count": 25, 705 | "metadata": {}, 706 | "output_type": "execute_result" 707 | } 708 | ], 709 | "source": [ 710 | "# if i want to reserve the name\n", 711 | "list(map(lambda x:x[::-1], names))" 712 | ] 713 | }, 714 | { 715 | "cell_type": "markdown", 716 | "metadata": {}, 717 | "source": [ 718 | "#### Nested Statements and Scope" 719 | ] 720 | }, 721 | { 722 | "cell_type": "code", 723 | "execution_count": 26, 724 | "metadata": {}, 725 | "outputs": [], 726 | "source": [ 727 | "x = 25\n", 728 | "\n", 729 | "def printer():\n", 730 | " x = 50\n", 731 | " return x" 732 | ] 733 | }, 734 | { 735 | "cell_type": "code", 736 | "execution_count": 27, 737 | "metadata": {}, 738 | "outputs": [ 739 | { 740 | "name": "stdout", 741 | "output_type": "stream", 742 | "text": [ 743 | "25\n" 744 | ] 745 | } 746 | ], 747 | "source": [ 748 | "print(x)" 749 | ] 750 | }, 751 | { 752 | "cell_type": "code", 753 | "execution_count": 28, 754 | "metadata": {}, 755 | "outputs": [ 756 | { 757 | "name": "stdout", 758 | "output_type": "stream", 759 | "text": [ 760 | "50\n" 761 | ] 762 | } 763 | ], 764 | "source": [ 765 | "print(printer())" 766 | ] 767 | }, 768 | { 769 | "cell_type": "markdown", 770 | "metadata": {}, 771 | "source": [ 772 | "LEGB Rule:\n", 773 | "- L: Local - Names assigned in any way within a function (def or lambda), and not declared global in that function.\n", 774 | "- E: Enclosing function locals - Names in the local scope of any and all enclosing functions (def or lambda), from inner to outer.\n", 775 | "- G: Global (module) - Names assigned at the top-level of a module file, or declared global in a def within the file. \n", 776 | "- B: Built-In (Python) - Names preassigned in the built-in names module: open, range, SyntaxError,..." 777 | ] 778 | }, 779 | { 780 | "cell_type": "code", 781 | "execution_count": 29, 782 | "metadata": {}, 783 | "outputs": [], 784 | "source": [ 785 | "# Lambda num:num **2" 786 | ] 787 | }, 788 | { 789 | "cell_type": "code", 790 | "execution_count": 31, 791 | "metadata": {}, 792 | "outputs": [ 793 | { 794 | "name": "stdout", 795 | "output_type": "stream", 796 | "text": [ 797 | "Hello I AM A LOCAL\n" 798 | ] 799 | } 800 | ], 801 | "source": [ 802 | "# GLOBAL\n", 803 | "name = 'THIS IS A GLOBAL STRING'\n", 804 | "\n", 805 | "def greet():\n", 806 | " \n", 807 | " # ENCLOSING\n", 808 | " name = 'Sammy'\n", 809 | " \n", 810 | " def hello():\n", 811 | " # LOCAL\n", 812 | " name = 'I AM A LOCAL'\n", 813 | " print('Hello '+name)\n", 814 | " \n", 815 | " hello()\n", 816 | "\n", 817 | "greet()" 818 | ] 819 | }, 820 | { 821 | "cell_type": "code", 822 | "execution_count": 32, 823 | "metadata": {}, 824 | "outputs": [ 825 | { 826 | "data": { 827 | "text/plain": [ 828 | "" 829 | ] 830 | }, 831 | "execution_count": 32, 832 | "metadata": {}, 833 | "output_type": "execute_result" 834 | } 835 | ], 836 | "source": [ 837 | "len" 838 | ] 839 | }, 840 | { 841 | "cell_type": "code", 842 | "execution_count": 37, 843 | "metadata": {}, 844 | "outputs": [], 845 | "source": [ 846 | "x = 50\n", 847 | "\n", 848 | "def func(x):\n", 849 | " print(f'X is {x}')\n", 850 | " \n", 851 | " # LOCAL REASSIGNMENT! \n", 852 | " x = 200\n", 853 | " \n", 854 | " print(f'I JUST LOCALLY CHANGED X TO {x}')" 855 | ] 856 | }, 857 | { 858 | "cell_type": "code", 859 | "execution_count": 38, 860 | "metadata": {}, 861 | "outputs": [ 862 | { 863 | "name": "stdout", 864 | "output_type": "stream", 865 | "text": [ 866 | "X is 50\n", 867 | "I JUST LOCALLY CHANGED X TO 200\n" 868 | ] 869 | } 870 | ], 871 | "source": [ 872 | "func(x)" 873 | ] 874 | }, 875 | { 876 | "cell_type": "code", 877 | "execution_count": 39, 878 | "metadata": {}, 879 | "outputs": [], 880 | "source": [ 881 | "x = 50\n", 882 | "\n", 883 | "def func():\n", 884 | " global x\n", 885 | " print(f'X is {x}')\n", 886 | " \n", 887 | " # LOCAL REASSIGNMENT ON A GLOBAL VARIABLE!\n", 888 | " x = 'NEW VALUE'\n", 889 | " print(f'I JUST LOCALLY CHANGED GLOBAL X TO {x}')" 890 | ] 891 | }, 892 | { 893 | "cell_type": "code", 894 | "execution_count": 40, 895 | "metadata": {}, 896 | "outputs": [ 897 | { 898 | "name": "stdout", 899 | "output_type": "stream", 900 | "text": [ 901 | "50\n" 902 | ] 903 | } 904 | ], 905 | "source": [ 906 | "print(x)" 907 | ] 908 | }, 909 | { 910 | "cell_type": "code", 911 | "execution_count": 41, 912 | "metadata": {}, 913 | "outputs": [ 914 | { 915 | "name": "stdout", 916 | "output_type": "stream", 917 | "text": [ 918 | "X is 50\n", 919 | "I JUST LOCALLY CHANGED GLOBAL X TO NEW VALUE\n" 920 | ] 921 | } 922 | ], 923 | "source": [ 924 | "func()" 925 | ] 926 | }, 927 | { 928 | "cell_type": "code", 929 | "execution_count": 42, 930 | "metadata": {}, 931 | "outputs": [ 932 | { 933 | "name": "stdout", 934 | "output_type": "stream", 935 | "text": [ 936 | "NEW VALUE\n" 937 | ] 938 | } 939 | ], 940 | "source": [ 941 | "print(x)" 942 | ] 943 | }, 944 | { 945 | "cell_type": "markdown", 946 | "metadata": {}, 947 | "source": [ 948 | "#### *args and **kwargs" 949 | ] 950 | }, 951 | { 952 | "cell_type": "code", 953 | "execution_count": 1, 954 | "metadata": {}, 955 | "outputs": [], 956 | "source": [ 957 | "# *args is argument and *kwargs is keyword argument" 958 | ] 959 | }, 960 | { 961 | "cell_type": "code", 962 | "execution_count": 2, 963 | "metadata": {}, 964 | "outputs": [], 965 | "source": [ 966 | "def myfunc(a,b):\n", 967 | " # Return 5% of the sum of a and b\n", 968 | " return sum ((a,b)) * 0.05" 969 | ] 970 | }, 971 | { 972 | "cell_type": "code", 973 | "execution_count": 3, 974 | "metadata": {}, 975 | "outputs": [ 976 | { 977 | "data": { 978 | "text/plain": [ 979 | "5.0" 980 | ] 981 | }, 982 | "execution_count": 3, 983 | "metadata": {}, 984 | "output_type": "execute_result" 985 | } 986 | ], 987 | "source": [ 988 | "myfunc(40,60)" 989 | ] 990 | }, 991 | { 992 | "cell_type": "code", 993 | "execution_count": 4, 994 | "metadata": {}, 995 | "outputs": [], 996 | "source": [ 997 | "def myfunc(*args):\n", 998 | " return sum(args) * 0.05" 999 | ] 1000 | }, 1001 | { 1002 | "cell_type": "code", 1003 | "execution_count": 6, 1004 | "metadata": {}, 1005 | "outputs": [ 1006 | { 1007 | "data": { 1008 | "text/plain": [ 1009 | "27.3" 1010 | ] 1011 | }, 1012 | "execution_count": 6, 1013 | "metadata": {}, 1014 | "output_type": "execute_result" 1015 | } 1016 | ], 1017 | "source": [ 1018 | "myfunc(40,60,100, 1, 345)" 1019 | ] 1020 | }, 1021 | { 1022 | "cell_type": "code", 1023 | "execution_count": 7, 1024 | "metadata": {}, 1025 | "outputs": [], 1026 | "source": [ 1027 | "# *args allows you to pass the parameter to the function as many as you want" 1028 | ] 1029 | }, 1030 | { 1031 | "cell_type": "code", 1032 | "execution_count": 10, 1033 | "metadata": {}, 1034 | "outputs": [], 1035 | "source": [ 1036 | "def myfunc(**kwargs):\n", 1037 | " print(kwargs)\n", 1038 | " if 'fruit' in kwargs:\n", 1039 | " print('My fruit choice is {}'.format(kwargs['fruit']))\n", 1040 | " else:\n", 1041 | " print('I did not find any fruit here')" 1042 | ] 1043 | }, 1044 | { 1045 | "cell_type": "code", 1046 | "execution_count": 11, 1047 | "metadata": {}, 1048 | "outputs": [ 1049 | { 1050 | "name": "stdout", 1051 | "output_type": "stream", 1052 | "text": [ 1053 | "{'fruit': 'apple', 'veggie': 'lettuce'}\n", 1054 | "My fruit choice is apple\n" 1055 | ] 1056 | } 1057 | ], 1058 | "source": [ 1059 | "myfunc(fruit='apple', veggie = 'lettuce')" 1060 | ] 1061 | }, 1062 | { 1063 | "cell_type": "code", 1064 | "execution_count": null, 1065 | "metadata": {}, 1066 | "outputs": [], 1067 | "source": [] 1068 | }, 1069 | { 1070 | "cell_type": "code", 1071 | "execution_count": null, 1072 | "metadata": {}, 1073 | "outputs": [], 1074 | "source": [] 1075 | }, 1076 | { 1077 | "cell_type": "code", 1078 | "execution_count": null, 1079 | "metadata": {}, 1080 | "outputs": [], 1081 | "source": [] 1082 | } 1083 | ], 1084 | "metadata": { 1085 | "kernelspec": { 1086 | "display_name": "Python 3", 1087 | "language": "python", 1088 | "name": "python3" 1089 | }, 1090 | "language_info": { 1091 | "codemirror_mode": { 1092 | "name": "ipython", 1093 | "version": 3 1094 | }, 1095 | "file_extension": ".py", 1096 | "mimetype": "text/x-python", 1097 | "name": "python", 1098 | "nbconvert_exporter": "python", 1099 | "pygments_lexer": "ipython3", 1100 | "version": "3.7.3" 1101 | } 1102 | }, 1103 | "nbformat": 4, 1104 | "nbformat_minor": 2 1105 | } 1106 | -------------------------------------------------------------------------------- /Function Practice Exercises.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Function Practice Exercises\n", 8 | "\n", 9 | "Problems are arranged in increasing difficulty:\n", 10 | "* Warmup - these can be solved using basic comparisons and methods\n", 11 | "* Level 1 - these may involve if/then conditional statements and simple methods\n", 12 | "* Level 2 - these may require iterating over sequences, usually with some kind of loop\n", 13 | "* Challenging - these will take some creativity to solve" 14 | ] 15 | }, 16 | { 17 | "cell_type": "markdown", 18 | "metadata": {}, 19 | "source": [ 20 | "## WARMUP SECTION:" 21 | ] 22 | }, 23 | { 24 | "cell_type": "markdown", 25 | "metadata": {}, 26 | "source": [ 27 | "#### LESSER OF TWO EVENS: Write a function that returns the lesser of two given numbers *if* both numbers are even, but returns the greater if one or both numbers are odd\n", 28 | " lesser_of_two_evens(2,4) --> 2\n", 29 | " lesser_of_two_evens(2,5) --> 5" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 1, 35 | "metadata": {}, 36 | "outputs": [], 37 | "source": [ 38 | "def lesser_of_two_evens(a,b):\n", 39 | " \n", 40 | " if a%2 == 0 and b%2 == 0:\n", 41 | " # Both numbers are even\n", 42 | " if a < b:\n", 43 | " result = a\n", 44 | " else:\n", 45 | " result = b\n", 46 | " \n", 47 | " else:\n", 48 | " # One or both numbers are odd\n", 49 | " if a > b:\n", 50 | " result = a\n", 51 | " else:\n", 52 | " result = b\n", 53 | " \n", 54 | " return result\n" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 11, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "# You can also do it another way\n", 64 | "\n", 65 | "def lesser_of_two_evens(a,b):\n", 66 | " \n", 67 | " if a%2 == 0 and b%2 == 0:\n", 68 | " # Both numbers are even\n", 69 | " return min(a,b)\n", 70 | " else:\n", 71 | " # one or both are odd\n", 72 | " return max(a,b)" 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": 12, 78 | "metadata": {}, 79 | "outputs": [ 80 | { 81 | "data": { 82 | "text/plain": [ 83 | "2" 84 | ] 85 | }, 86 | "execution_count": 12, 87 | "metadata": {}, 88 | "output_type": "execute_result" 89 | } 90 | ], 91 | "source": [ 92 | "# Check\n", 93 | "lesser_of_two_evens(2,4)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "code", 98 | "execution_count": 13, 99 | "metadata": {}, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "text/plain": [ 104 | "5" 105 | ] 106 | }, 107 | "execution_count": 13, 108 | "metadata": {}, 109 | "output_type": "execute_result" 110 | } 111 | ], 112 | "source": [ 113 | "# Check\n", 114 | "lesser_of_two_evens(2,5)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "markdown", 119 | "metadata": {}, 120 | "source": [ 121 | "#### ANIMAL CRACKERS: Write a function takes a two-word string and returns True if both words begin with same letter\n", 122 | " animal_crackers('Levelheaded Llama') --> True\n", 123 | " animal_crackers('Crazy Kangaroo') --> False" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 14, 129 | "metadata": {}, 130 | "outputs": [], 131 | "source": [ 132 | "def animal_crackers(text):\n", 133 | " wordlist = text.split()\n", 134 | " \n", 135 | " first = wordlist[0]\n", 136 | " second = wordlist[1]\n", 137 | " \n", 138 | " return first[0] == second[0]\n", 139 | " " 140 | ] 141 | }, 142 | { 143 | "cell_type": "code", 144 | "execution_count": 19, 145 | "metadata": {}, 146 | "outputs": [], 147 | "source": [ 148 | "# You can also do it this way\n", 149 | "def animal_crackers(text):\n", 150 | " wordlist = text.split()\n", 151 | " \n", 152 | " return wordlist[0][0] == wordlist[1][0]" 153 | ] 154 | }, 155 | { 156 | "cell_type": "code", 157 | "execution_count": 20, 158 | "metadata": {}, 159 | "outputs": [ 160 | { 161 | "data": { 162 | "text/plain": [ 163 | "True" 164 | ] 165 | }, 166 | "execution_count": 20, 167 | "metadata": {}, 168 | "output_type": "execute_result" 169 | } 170 | ], 171 | "source": [ 172 | "# Check\n", 173 | "animal_crackers('Levelheaded Llama')" 174 | ] 175 | }, 176 | { 177 | "cell_type": "code", 178 | "execution_count": 21, 179 | "metadata": {}, 180 | "outputs": [ 181 | { 182 | "data": { 183 | "text/plain": [ 184 | "False" 185 | ] 186 | }, 187 | "execution_count": 21, 188 | "metadata": {}, 189 | "output_type": "execute_result" 190 | } 191 | ], 192 | "source": [ 193 | "# Check\n", 194 | "animal_crackers('Crazy Kangaroo')" 195 | ] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": {}, 200 | "source": [ 201 | "#### MAKES TWENTY: Given two integers, return True if the sum of the integers is 20 *or* if one of the integers is 20. If not, return False\n", 202 | "\n", 203 | " makes_twenty(20,10) --> True\n", 204 | " makes_twenty(12,8) --> True\n", 205 | " makes_twenty(2,3) --> False" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": null, 211 | "metadata": {}, 212 | "outputs": [], 213 | "source": [ 214 | "def makes_twenty(n1,n2):\n", 215 | " if n1 + n2 == 20:\n", 216 | " return True\n", 217 | " elif n1 == 20:\n", 218 | " return True\n", 219 | " elif n2 == 20:\n", 220 | " return True\n", 221 | " else:\n", 222 | " return False" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 25, 228 | "metadata": {}, 229 | "outputs": [], 230 | "source": [ 231 | "# You can also do it this way\n", 232 | "def makes_twenty(n1, n2):\n", 233 | " return (n1+n2) == 20 or n1 == 20 or n2 == 20" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 26, 239 | "metadata": {}, 240 | "outputs": [ 241 | { 242 | "data": { 243 | "text/plain": [ 244 | "True" 245 | ] 246 | }, 247 | "execution_count": 26, 248 | "metadata": {}, 249 | "output_type": "execute_result" 250 | } 251 | ], 252 | "source": [ 253 | "# Check\n", 254 | "makes_twenty(20,10)" 255 | ] 256 | }, 257 | { 258 | "cell_type": "code", 259 | "execution_count": 27, 260 | "metadata": {}, 261 | "outputs": [ 262 | { 263 | "data": { 264 | "text/plain": [ 265 | "False" 266 | ] 267 | }, 268 | "execution_count": 27, 269 | "metadata": {}, 270 | "output_type": "execute_result" 271 | } 272 | ], 273 | "source": [ 274 | "# Check\n", 275 | "makes_twenty(2,3)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "# LEVEL 1 PROBLEMS" 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": {}, 288 | "source": [ 289 | "#### OLD MACDONALD: Write a function that capitalizes the first and fourth letters of a name\n", 290 | " \n", 291 | " old_macdonald('macdonald') --> MacDonald\n", 292 | " \n", 293 | "Note: `'macdonald'.capitalize()` returns `'Macdonald'`" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 28, 299 | "metadata": {}, 300 | "outputs": [], 301 | "source": [ 302 | "def old_macdonald(name):\n", 303 | " \n", 304 | " first_letter = name[0]\n", 305 | " inbetween = name[1:3]\n", 306 | " fourth_letter = name[3]\n", 307 | " rest = name[4:]\n", 308 | " \n", 309 | " return first_letter.upper() + inbetween + fourth_letter.upper() + rest" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 30, 315 | "metadata": {}, 316 | "outputs": [], 317 | "source": [ 318 | "# You can also do it this way\n", 319 | "def old_macdonald(name):\n", 320 | " \n", 321 | " first_half = name[:3]\n", 322 | " second_half = name[3:]\n", 323 | " \n", 324 | " return first_half.capitalize() + second_half.capitalize()" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": 31, 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "data": { 334 | "text/plain": [ 335 | "'MacDonald'" 336 | ] 337 | }, 338 | "execution_count": 31, 339 | "metadata": {}, 340 | "output_type": "execute_result" 341 | } 342 | ], 343 | "source": [ 344 | "# Check\n", 345 | "old_macdonald('macdonald')" 346 | ] 347 | }, 348 | { 349 | "cell_type": "markdown", 350 | "metadata": {}, 351 | "source": [ 352 | "#### MASTER YODA: Given a sentence, return a sentence with the words reversed\n", 353 | "\n", 354 | " master_yoda('I am home') --> 'home am I'\n", 355 | " master_yoda('We are ready') --> 'ready are We'\n", 356 | " \n", 357 | "Note: The .join() method may be useful here. The .join() method allows you to join together strings in a list with some connector string. For example, some uses of the .join() method:\n", 358 | "\n", 359 | " >>> \"--\".join(['a','b','c'])\n", 360 | " >>> 'a--b--c'\n", 361 | "\n", 362 | "This means if you had a list of words you wanted to turn back into a sentence, you could just join them with a single space string:\n", 363 | "\n", 364 | " >>> \" \".join(['Hello','world'])\n", 365 | " >>> \"Hello world\"" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": 35, 371 | "metadata": {}, 372 | "outputs": [], 373 | "source": [ 374 | "def master_yoda(text):\n", 375 | " wordlist = text.split()\n", 376 | " reverse_word_list = wordlist[::-1]\n", 377 | " return ' '.join(reverse_word_list)" 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 36, 383 | "metadata": {}, 384 | "outputs": [ 385 | { 386 | "data": { 387 | "text/plain": [ 388 | "'home am I'" 389 | ] 390 | }, 391 | "execution_count": 36, 392 | "metadata": {}, 393 | "output_type": "execute_result" 394 | } 395 | ], 396 | "source": [ 397 | "# Check\n", 398 | "master_yoda('I am home')" 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "execution_count": 37, 404 | "metadata": {}, 405 | "outputs": [ 406 | { 407 | "data": { 408 | "text/plain": [ 409 | "'ready are We'" 410 | ] 411 | }, 412 | "execution_count": 37, 413 | "metadata": {}, 414 | "output_type": "execute_result" 415 | } 416 | ], 417 | "source": [ 418 | "# Check\n", 419 | "master_yoda('We are ready')" 420 | ] 421 | }, 422 | { 423 | "cell_type": "markdown", 424 | "metadata": {}, 425 | "source": [ 426 | "#### ALMOST THERE: Given an integer n, return True if n is within 10 of either 100 or 200\n", 427 | "\n", 428 | " almost_there(90) --> True\n", 429 | " almost_there(104) --> True\n", 430 | " almost_there(150) --> False\n", 431 | " almost_there(209) --> True\n", 432 | " \n", 433 | "NOTE: `abs(num)` returns the absolute value of a number" 434 | ] 435 | }, 436 | { 437 | "cell_type": "code", 438 | "execution_count": 38, 439 | "metadata": {}, 440 | "outputs": [], 441 | "source": [ 442 | "def almost_there(n):\n", 443 | " \n", 444 | " return (abs(100-n) <= 10) or (abs(200-n) <= 10)" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": 39, 450 | "metadata": {}, 451 | "outputs": [ 452 | { 453 | "data": { 454 | "text/plain": [ 455 | "True" 456 | ] 457 | }, 458 | "execution_count": 39, 459 | "metadata": {}, 460 | "output_type": "execute_result" 461 | } 462 | ], 463 | "source": [ 464 | "# Check\n", 465 | "almost_there(104)" 466 | ] 467 | }, 468 | { 469 | "cell_type": "code", 470 | "execution_count": 40, 471 | "metadata": {}, 472 | "outputs": [ 473 | { 474 | "data": { 475 | "text/plain": [ 476 | "False" 477 | ] 478 | }, 479 | "execution_count": 40, 480 | "metadata": {}, 481 | "output_type": "execute_result" 482 | } 483 | ], 484 | "source": [ 485 | "# Check\n", 486 | "almost_there(150)" 487 | ] 488 | }, 489 | { 490 | "cell_type": "code", 491 | "execution_count": 41, 492 | "metadata": {}, 493 | "outputs": [ 494 | { 495 | "data": { 496 | "text/plain": [ 497 | "True" 498 | ] 499 | }, 500 | "execution_count": 41, 501 | "metadata": {}, 502 | "output_type": "execute_result" 503 | } 504 | ], 505 | "source": [ 506 | "# Check\n", 507 | "almost_there(209)" 508 | ] 509 | }, 510 | { 511 | "cell_type": "markdown", 512 | "metadata": {}, 513 | "source": [ 514 | "# LEVEL 2 PROBLEMS" 515 | ] 516 | }, 517 | { 518 | "cell_type": "markdown", 519 | "metadata": {}, 520 | "source": [ 521 | "#### FIND 33: \n", 522 | "\n", 523 | "Given a list of ints, return True if the array contains a 3 next to a 3 somewhere.\n", 524 | "\n", 525 | " has_33([1, 3, 3]) → True\n", 526 | " has_33([1, 3, 1, 3]) → False\n", 527 | " has_33([3, 1, 3]) → False" 528 | ] 529 | }, 530 | { 531 | "cell_type": "code", 532 | "execution_count": 1, 533 | "metadata": {}, 534 | "outputs": [], 535 | "source": [ 536 | "def has_33(nums):\n", 537 | " \n", 538 | " for i in range(0, len(nums)-1):\n", 539 | " if nums[i] == 3 and nums[i+1] == 3:\n", 540 | " return True\n", 541 | " return False" 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "execution_count": 2, 547 | "metadata": {}, 548 | "outputs": [ 549 | { 550 | "data": { 551 | "text/plain": [ 552 | "False" 553 | ] 554 | }, 555 | "execution_count": 2, 556 | "metadata": {}, 557 | "output_type": "execute_result" 558 | } 559 | ], 560 | "source": [ 561 | "# Check\n", 562 | "has_33([1, 3, 3])" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": 3, 568 | "metadata": {}, 569 | "outputs": [ 570 | { 571 | "data": { 572 | "text/plain": [ 573 | "False" 574 | ] 575 | }, 576 | "execution_count": 3, 577 | "metadata": {}, 578 | "output_type": "execute_result" 579 | } 580 | ], 581 | "source": [ 582 | "# Check\n", 583 | "has_33([1, 3, 1, 3])" 584 | ] 585 | }, 586 | { 587 | "cell_type": "code", 588 | "execution_count": 4, 589 | "metadata": {}, 590 | "outputs": [ 591 | { 592 | "data": { 593 | "text/plain": [ 594 | "False" 595 | ] 596 | }, 597 | "execution_count": 4, 598 | "metadata": {}, 599 | "output_type": "execute_result" 600 | } 601 | ], 602 | "source": [ 603 | "# Check\n", 604 | "has_33([3, 1, 3])" 605 | ] 606 | }, 607 | { 608 | "cell_type": "markdown", 609 | "metadata": {}, 610 | "source": [ 611 | "#### PAPER DOLL: Given a string, return a string where for every character in the original there are three characters\n", 612 | " paper_doll('Hello') --> 'HHHeeellllllooo'\n", 613 | " paper_doll('Mississippi') --> 'MMMiiissssssiiippppppiii'" 614 | ] 615 | }, 616 | { 617 | "cell_type": "code", 618 | "execution_count": 5, 619 | "metadata": {}, 620 | "outputs": [], 621 | "source": [ 622 | "def paper_doll(text):\n", 623 | " result = ''\n", 624 | " \n", 625 | " for char in text:\n", 626 | " result += char*3\n", 627 | " return result" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": 6, 633 | "metadata": {}, 634 | "outputs": [ 635 | { 636 | "data": { 637 | "text/plain": [ 638 | "'HHHeeellllllooo'" 639 | ] 640 | }, 641 | "execution_count": 6, 642 | "metadata": {}, 643 | "output_type": "execute_result" 644 | } 645 | ], 646 | "source": [ 647 | "# Check\n", 648 | "paper_doll('Hello')" 649 | ] 650 | }, 651 | { 652 | "cell_type": "code", 653 | "execution_count": 7, 654 | "metadata": {}, 655 | "outputs": [ 656 | { 657 | "data": { 658 | "text/plain": [ 659 | "'MMMiiissssssiiissssssiiippppppiii'" 660 | ] 661 | }, 662 | "execution_count": 7, 663 | "metadata": {}, 664 | "output_type": "execute_result" 665 | } 666 | ], 667 | "source": [ 668 | "# Check\n", 669 | "paper_doll('Mississippi')" 670 | ] 671 | }, 672 | { 673 | "cell_type": "markdown", 674 | "metadata": {}, 675 | "source": [ 676 | "#### BLACKJACK: Given three integers between 1 and 11, if their sum is less than or equal to 21, return their sum. If their sum exceeds 21 *and* there's an eleven, reduce the total sum by 10. Finally, if the sum (even after adjustment) exceeds 21, return 'BUST'\n", 677 | " blackjack(5,6,7) --> 18\n", 678 | " blackjack(9,9,9) --> 'BUST'\n", 679 | " blackjack(9,9,11) --> 19" 680 | ] 681 | }, 682 | { 683 | "cell_type": "code", 684 | "execution_count": 18, 685 | "metadata": {}, 686 | "outputs": [], 687 | "source": [ 688 | "def blackjack(a,b,c):\n", 689 | " \n", 690 | " if sum ([a,b,c]) <= 21:\n", 691 | " return sum([a,b,c])\n", 692 | " elif 11 in [a,b,c] and sum([a,b,c])-10 <= 21:\n", 693 | " return sum([a,b,c])-10\n", 694 | " else:\n", 695 | " return 'BUST'" 696 | ] 697 | }, 698 | { 699 | "cell_type": "code", 700 | "execution_count": 19, 701 | "metadata": {}, 702 | "outputs": [ 703 | { 704 | "data": { 705 | "text/plain": [ 706 | "18" 707 | ] 708 | }, 709 | "execution_count": 19, 710 | "metadata": {}, 711 | "output_type": "execute_result" 712 | } 713 | ], 714 | "source": [ 715 | "# Check\n", 716 | "blackjack(5,6,7)" 717 | ] 718 | }, 719 | { 720 | "cell_type": "code", 721 | "execution_count": 20, 722 | "metadata": {}, 723 | "outputs": [ 724 | { 725 | "data": { 726 | "text/plain": [ 727 | "'BUST'" 728 | ] 729 | }, 730 | "execution_count": 20, 731 | "metadata": {}, 732 | "output_type": "execute_result" 733 | } 734 | ], 735 | "source": [ 736 | "# Check\n", 737 | "blackjack(9,9,9)" 738 | ] 739 | }, 740 | { 741 | "cell_type": "code", 742 | "execution_count": 21, 743 | "metadata": {}, 744 | "outputs": [ 745 | { 746 | "data": { 747 | "text/plain": [ 748 | "19" 749 | ] 750 | }, 751 | "execution_count": 21, 752 | "metadata": {}, 753 | "output_type": "execute_result" 754 | } 755 | ], 756 | "source": [ 757 | "# Check\n", 758 | "blackjack(9,9,11)" 759 | ] 760 | }, 761 | { 762 | "cell_type": "markdown", 763 | "metadata": {}, 764 | "source": [ 765 | "#### SUMMER OF '69: Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 9 (every 6 will be followed by at least one 9). Return 0 for no numbers.\n", 766 | " \n", 767 | " summer_69([1, 3, 5]) --> 9\n", 768 | " summer_69([4, 5, 6, 7, 8, 9]) --> 9\n", 769 | " summer_69([2, 1, 6, 9, 11]) --> 14" 770 | ] 771 | }, 772 | { 773 | "cell_type": "code", 774 | "execution_count": 24, 775 | "metadata": {}, 776 | "outputs": [], 777 | "source": [ 778 | "def summer_69(arr):\n", 779 | " \n", 780 | " total = 0\n", 781 | " add = True\n", 782 | " \n", 783 | " for num in arr:\n", 784 | " while add:\n", 785 | " if num != 6:\n", 786 | " total += num\n", 787 | " break\n", 788 | " else:\n", 789 | " add = False\n", 790 | " while not add:\n", 791 | " if num !=9:\n", 792 | " break\n", 793 | " else:\n", 794 | " add = True\n", 795 | " break\n", 796 | " return total" 797 | ] 798 | }, 799 | { 800 | "cell_type": "code", 801 | "execution_count": 25, 802 | "metadata": {}, 803 | "outputs": [ 804 | { 805 | "data": { 806 | "text/plain": [ 807 | "1" 808 | ] 809 | }, 810 | "execution_count": 25, 811 | "metadata": {}, 812 | "output_type": "execute_result" 813 | } 814 | ], 815 | "source": [ 816 | "# Check\n", 817 | "summer_69([1, 3, 5])" 818 | ] 819 | }, 820 | { 821 | "cell_type": "code", 822 | "execution_count": 26, 823 | "metadata": {}, 824 | "outputs": [ 825 | { 826 | "data": { 827 | "text/plain": [ 828 | "4" 829 | ] 830 | }, 831 | "execution_count": 26, 832 | "metadata": {}, 833 | "output_type": "execute_result" 834 | } 835 | ], 836 | "source": [ 837 | "# Check\n", 838 | "summer_69([4, 5, 6, 7, 8, 9])" 839 | ] 840 | }, 841 | { 842 | "cell_type": "code", 843 | "execution_count": 27, 844 | "metadata": {}, 845 | "outputs": [ 846 | { 847 | "data": { 848 | "text/plain": [ 849 | "2" 850 | ] 851 | }, 852 | "execution_count": 27, 853 | "metadata": {}, 854 | "output_type": "execute_result" 855 | } 856 | ], 857 | "source": [ 858 | "# Check\n", 859 | "summer_69([2, 1, 6, 9, 11])" 860 | ] 861 | }, 862 | { 863 | "cell_type": "markdown", 864 | "metadata": {}, 865 | "source": [ 866 | "# CHALLENGING PROBLEMS" 867 | ] 868 | }, 869 | { 870 | "cell_type": "markdown", 871 | "metadata": {}, 872 | "source": [ 873 | "#### SPY GAME: Write a function that takes in a list of integers and returns True if it contains 007 in order\n", 874 | "\n", 875 | " spy_game([1,2,4,0,0,7,5]) --> True\n", 876 | " spy_game([1,0,2,4,0,5,7]) --> True\n", 877 | " spy_game([1,7,2,0,4,5,0]) --> False\n" 878 | ] 879 | }, 880 | { 881 | "cell_type": "code", 882 | "execution_count": 28, 883 | "metadata": {}, 884 | "outputs": [], 885 | "source": [ 886 | "def spy_game(nums):\n", 887 | " code = [0,0,7,'x']\n", 888 | " #[0,0,'x']\n", 889 | " #[7,'x']\n", 890 | " #['x'] length=1\n", 891 | " for num in nums:\n", 892 | " if num == code[0]:\n", 893 | " code.pop(0)\n", 894 | " \n", 895 | " return len(code) == 1\n" 896 | ] 897 | }, 898 | { 899 | "cell_type": "code", 900 | "execution_count": 29, 901 | "metadata": {}, 902 | "outputs": [ 903 | { 904 | "data": { 905 | "text/plain": [ 906 | "True" 907 | ] 908 | }, 909 | "execution_count": 29, 910 | "metadata": {}, 911 | "output_type": "execute_result" 912 | } 913 | ], 914 | "source": [ 915 | "# Check\n", 916 | "spy_game([1,2,4,0,0,7,5])" 917 | ] 918 | }, 919 | { 920 | "cell_type": "code", 921 | "execution_count": 30, 922 | "metadata": {}, 923 | "outputs": [ 924 | { 925 | "data": { 926 | "text/plain": [ 927 | "True" 928 | ] 929 | }, 930 | "execution_count": 30, 931 | "metadata": {}, 932 | "output_type": "execute_result" 933 | } 934 | ], 935 | "source": [ 936 | "# Check\n", 937 | "spy_game([1,0,2,4,0,5,7])" 938 | ] 939 | }, 940 | { 941 | "cell_type": "code", 942 | "execution_count": 31, 943 | "metadata": {}, 944 | "outputs": [ 945 | { 946 | "data": { 947 | "text/plain": [ 948 | "False" 949 | ] 950 | }, 951 | "execution_count": 31, 952 | "metadata": {}, 953 | "output_type": "execute_result" 954 | } 955 | ], 956 | "source": [ 957 | "# Check\n", 958 | "spy_game([1,7,2,0,4,5,0])" 959 | ] 960 | }, 961 | { 962 | "cell_type": "markdown", 963 | "metadata": {}, 964 | "source": [ 965 | "#### COUNT PRIMES: Write a function that returns the *number* of prime numbers that exist up to and including a given number\n", 966 | " count_primes(100) --> 25\n", 967 | "\n", 968 | "By convention, 0 and 1 are not prime." 969 | ] 970 | }, 971 | { 972 | "cell_type": "code", 973 | "execution_count": 32, 974 | "metadata": {}, 975 | "outputs": [], 976 | "source": [ 977 | "def count_primes(num):\n", 978 | " # Check for 0 or 1 input\n", 979 | " if num < 2:\n", 980 | " return 0\n", 981 | " # 2 or greater\n", 982 | " \n", 983 | " # Store our prime numbers\n", 984 | " primes = [2]\n", 985 | " # Counter going up to the input num\n", 986 | " x = 3\n", 987 | " \n", 988 | " # x is going through every number up to input num\n", 989 | " while x <= num:\n", 990 | " # Check if x is prime\n", 991 | " for y in primes:\n", 992 | " if x%y == 0:\n", 993 | " x+=2\n", 994 | " break\n", 995 | " else:\n", 996 | " primes.append(x)\n", 997 | " x+=2\n", 998 | " print(primes)\n", 999 | " return len(primes)\n", 1000 | " " 1001 | ] 1002 | }, 1003 | { 1004 | "cell_type": "code", 1005 | "execution_count": 33, 1006 | "metadata": {}, 1007 | "outputs": [ 1008 | { 1009 | "name": "stdout", 1010 | "output_type": "stream", 1011 | "text": [ 1012 | "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]\n" 1013 | ] 1014 | }, 1015 | { 1016 | "data": { 1017 | "text/plain": [ 1018 | "25" 1019 | ] 1020 | }, 1021 | "execution_count": 33, 1022 | "metadata": {}, 1023 | "output_type": "execute_result" 1024 | } 1025 | ], 1026 | "source": [ 1027 | "# Check\n", 1028 | "count_primes(100)" 1029 | ] 1030 | }, 1031 | { 1032 | "cell_type": "markdown", 1033 | "metadata": {}, 1034 | "source": [ 1035 | "### Just for fun:\n", 1036 | "#### PRINT BIG: Write a function that takes in a single letter, and returns a 5x5 representation of that letter\n", 1037 | " print_big('a')\n", 1038 | " \n", 1039 | " out: * \n", 1040 | " * *\n", 1041 | " *****\n", 1042 | " * *\n", 1043 | " * *\n", 1044 | "HINT: Consider making a dictionary of possible patterns, and mapping the alphabet to specific 5-line combinations of patterns.
For purposes of this exercise, it's ok if your dictionary stops at \"E\"." 1045 | ] 1046 | }, 1047 | { 1048 | "cell_type": "code", 1049 | "execution_count": 56, 1050 | "metadata": {}, 1051 | "outputs": [], 1052 | "source": [ 1053 | "def print_big(letter):\n", 1054 | " patterns = {1:' * ',2:' * * ',3:'* *',4:'*****',5:'**** ',6:' * ',7:' * ',8:'* * ',9:'* '}\n", 1055 | " alphabet = {'A':[1,2,4,3,3],'B':[5,3,5,3,5],'C':[4,9,9,9,4],'D':[5,3,3,3,5],'E':[4,9,4,9,4]}\n", 1056 | " for pattern in alphabet[letter.upper()]:\n", 1057 | " print(patterns[pattern])" 1058 | ] 1059 | }, 1060 | { 1061 | "cell_type": "code", 1062 | "execution_count": 57, 1063 | "metadata": {}, 1064 | "outputs": [ 1065 | { 1066 | "name": "stdout", 1067 | "output_type": "stream", 1068 | "text": [ 1069 | " * \n", 1070 | " * * \n", 1071 | "*****\n", 1072 | "* *\n", 1073 | "* *\n" 1074 | ] 1075 | } 1076 | ], 1077 | "source": [ 1078 | "print_big('a')" 1079 | ] 1080 | }, 1081 | { 1082 | "cell_type": "markdown", 1083 | "metadata": {}, 1084 | "source": [ 1085 | "## Great Job!" 1086 | ] 1087 | } 1088 | ], 1089 | "metadata": { 1090 | "kernelspec": { 1091 | "display_name": "Python 3", 1092 | "language": "python", 1093 | "name": "python3" 1094 | }, 1095 | "language_info": { 1096 | "codemirror_mode": { 1097 | "name": "ipython", 1098 | "version": 3 1099 | }, 1100 | "file_extension": ".py", 1101 | "mimetype": "text/x-python", 1102 | "name": "python", 1103 | "nbconvert_exporter": "python", 1104 | "pygments_lexer": "ipython3", 1105 | "version": "3.7.3" 1106 | } 1107 | }, 1108 | "nbformat": 4, 1109 | "nbformat_minor": 2 1110 | } 1111 | -------------------------------------------------------------------------------- /9. Advanced Python Objects and Data Structures.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### Advanced Numbers" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": {}, 14 | "outputs": [ 15 | { 16 | "data": { 17 | "text/plain": [ 18 | "'0xc'" 19 | ] 20 | }, 21 | "execution_count": 1, 22 | "metadata": {}, 23 | "output_type": "execute_result" 24 | } 25 | ], 26 | "source": [ 27 | "hex(12)" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "'0x4b0'" 39 | ] 40 | }, 41 | "execution_count": 2, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "hex(1200)" 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 3, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "data": { 57 | "text/plain": [ 58 | "'0b11000000111001'" 59 | ] 60 | }, 61 | "execution_count": 3, 62 | "metadata": {}, 63 | "output_type": "execute_result" 64 | } 65 | ], 66 | "source": [ 67 | "bin(12345)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 4, 73 | "metadata": {}, 74 | "outputs": [ 75 | { 76 | "data": { 77 | "text/plain": [ 78 | "'0b1000000000'" 79 | ] 80 | }, 81 | "execution_count": 4, 82 | "metadata": {}, 83 | "output_type": "execute_result" 84 | } 85 | ], 86 | "source": [ 87 | "bin(512)" 88 | ] 89 | }, 90 | { 91 | "cell_type": "code", 92 | "execution_count": 5, 93 | "metadata": {}, 94 | "outputs": [ 95 | { 96 | "data": { 97 | "text/plain": [ 98 | "16" 99 | ] 100 | }, 101 | "execution_count": 5, 102 | "metadata": {}, 103 | "output_type": "execute_result" 104 | } 105 | ], 106 | "source": [ 107 | "2**4" 108 | ] 109 | }, 110 | { 111 | "cell_type": "code", 112 | "execution_count": 7, 113 | "metadata": {}, 114 | "outputs": [], 115 | "source": [ 116 | "#The function pow() takes two arguments, equivalent to x^y" 117 | ] 118 | }, 119 | { 120 | "cell_type": "code", 121 | "execution_count": 6, 122 | "metadata": {}, 123 | "outputs": [ 124 | { 125 | "data": { 126 | "text/plain": [ 127 | "16" 128 | ] 129 | }, 130 | "execution_count": 6, 131 | "metadata": {}, 132 | "output_type": "execute_result" 133 | } 134 | ], 135 | "source": [ 136 | "pow(2,4)" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 11, 142 | "metadata": {}, 143 | "outputs": [ 144 | { 145 | "data": { 146 | "text/plain": [ 147 | "1" 148 | ] 149 | }, 150 | "execution_count": 11, 151 | "metadata": {}, 152 | "output_type": "execute_result" 153 | } 154 | ], 155 | "source": [ 156 | "pow(2,4,3)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 12, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "data": { 166 | "text/plain": [ 167 | "3" 168 | ] 169 | }, 170 | "execution_count": 12, 171 | "metadata": {}, 172 | "output_type": "execute_result" 173 | } 174 | ], 175 | "source": [ 176 | "# absolute number\n", 177 | "abs(-3)" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 13, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "data": { 187 | "text/plain": [ 188 | "2" 189 | ] 190 | }, 191 | "execution_count": 13, 192 | "metadata": {}, 193 | "output_type": "execute_result" 194 | } 195 | ], 196 | "source": [ 197 | "abs(2)" 198 | ] 199 | }, 200 | { 201 | "cell_type": "code", 202 | "execution_count": 15, 203 | "metadata": {}, 204 | "outputs": [ 205 | { 206 | "data": { 207 | "text/plain": [ 208 | "4" 209 | ] 210 | }, 211 | "execution_count": 15, 212 | "metadata": {}, 213 | "output_type": "execute_result" 214 | } 215 | ], 216 | "source": [ 217 | "round(3.9)" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 16, 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "data": { 227 | "text/plain": [ 228 | "3.123" 229 | ] 230 | }, 231 | "execution_count": 16, 232 | "metadata": {}, 233 | "output_type": "execute_result" 234 | } 235 | ], 236 | "source": [ 237 | "round(3.1232323, 3)" 238 | ] 239 | }, 240 | { 241 | "cell_type": "markdown", 242 | "metadata": {}, 243 | "source": [ 244 | "### Advanced Strings" 245 | ] 246 | }, 247 | { 248 | "cell_type": "code", 249 | "execution_count": 17, 250 | "metadata": {}, 251 | "outputs": [], 252 | "source": [ 253 | "s = 'hello world'" 254 | ] 255 | }, 256 | { 257 | "cell_type": "code", 258 | "execution_count": 18, 259 | "metadata": {}, 260 | "outputs": [ 261 | { 262 | "data": { 263 | "text/plain": [ 264 | "'Hello world'" 265 | ] 266 | }, 267 | "execution_count": 18, 268 | "metadata": {}, 269 | "output_type": "execute_result" 270 | } 271 | ], 272 | "source": [ 273 | "s.capitalize()" 274 | ] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": 19, 279 | "metadata": {}, 280 | "outputs": [ 281 | { 282 | "data": { 283 | "text/plain": [ 284 | "'HELLO WORLD'" 285 | ] 286 | }, 287 | "execution_count": 19, 288 | "metadata": {}, 289 | "output_type": "execute_result" 290 | } 291 | ], 292 | "source": [ 293 | "s.upper()" 294 | ] 295 | }, 296 | { 297 | "cell_type": "code", 298 | "execution_count": 20, 299 | "metadata": {}, 300 | "outputs": [ 301 | { 302 | "data": { 303 | "text/plain": [ 304 | "'hello world'" 305 | ] 306 | }, 307 | "execution_count": 20, 308 | "metadata": {}, 309 | "output_type": "execute_result" 310 | } 311 | ], 312 | "source": [ 313 | "s.lower()" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 21, 319 | "metadata": {}, 320 | "outputs": [ 321 | { 322 | "data": { 323 | "text/plain": [ 324 | "2" 325 | ] 326 | }, 327 | "execution_count": 21, 328 | "metadata": {}, 329 | "output_type": "execute_result" 330 | } 331 | ], 332 | "source": [ 333 | "s.count('o')" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 22, 339 | "metadata": {}, 340 | "outputs": [ 341 | { 342 | "data": { 343 | "text/plain": [ 344 | "4" 345 | ] 346 | }, 347 | "execution_count": 22, 348 | "metadata": {}, 349 | "output_type": "execute_result" 350 | } 351 | ], 352 | "source": [ 353 | "s.find('o')" 354 | ] 355 | }, 356 | { 357 | "cell_type": "code", 358 | "execution_count": 23, 359 | "metadata": {}, 360 | "outputs": [ 361 | { 362 | "data": { 363 | "text/plain": [ 364 | "'hello world'" 365 | ] 366 | }, 367 | "execution_count": 23, 368 | "metadata": {}, 369 | "output_type": "execute_result" 370 | } 371 | ], 372 | "source": [ 373 | "s" 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "execution_count": 25, 379 | "metadata": {}, 380 | "outputs": [ 381 | { 382 | "data": { 383 | "text/plain": [ 384 | "'zzzzhello worldzzzzz'" 385 | ] 386 | }, 387 | "execution_count": 25, 388 | "metadata": {}, 389 | "output_type": "execute_result" 390 | } 391 | ], 392 | "source": [ 393 | "s.center(20, 'z')" 394 | ] 395 | }, 396 | { 397 | "cell_type": "code", 398 | "execution_count": 26, 399 | "metadata": {}, 400 | "outputs": [ 401 | { 402 | "data": { 403 | "text/plain": [ 404 | "'hello hi'" 405 | ] 406 | }, 407 | "execution_count": 26, 408 | "metadata": {}, 409 | "output_type": "execute_result" 410 | } 411 | ], 412 | "source": [ 413 | "'hello\\thi'.expandtabs()" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": 27, 419 | "metadata": {}, 420 | "outputs": [], 421 | "source": [ 422 | "s = 'hello'" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": 28, 428 | "metadata": {}, 429 | "outputs": [ 430 | { 431 | "data": { 432 | "text/plain": [ 433 | "True" 434 | ] 435 | }, 436 | "execution_count": 28, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "s.isalnum()" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 29, 448 | "metadata": {}, 449 | "outputs": [ 450 | { 451 | "data": { 452 | "text/plain": [ 453 | "True" 454 | ] 455 | }, 456 | "execution_count": 29, 457 | "metadata": {}, 458 | "output_type": "execute_result" 459 | } 460 | ], 461 | "source": [ 462 | "s.isalpha()" 463 | ] 464 | }, 465 | { 466 | "cell_type": "code", 467 | "execution_count": 30, 468 | "metadata": {}, 469 | "outputs": [ 470 | { 471 | "data": { 472 | "text/plain": [ 473 | "False" 474 | ] 475 | }, 476 | "execution_count": 30, 477 | "metadata": {}, 478 | "output_type": "execute_result" 479 | } 480 | ], 481 | "source": [ 482 | "s.isspace()" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": 31, 488 | "metadata": {}, 489 | "outputs": [ 490 | { 491 | "data": { 492 | "text/plain": [ 493 | "False" 494 | ] 495 | }, 496 | "execution_count": 31, 497 | "metadata": {}, 498 | "output_type": "execute_result" 499 | } 500 | ], 501 | "source": [ 502 | "s.istitle()" 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": 32, 508 | "metadata": {}, 509 | "outputs": [ 510 | { 511 | "data": { 512 | "text/plain": [ 513 | "False" 514 | ] 515 | }, 516 | "execution_count": 32, 517 | "metadata": {}, 518 | "output_type": "execute_result" 519 | } 520 | ], 521 | "source": [ 522 | "s.isupper()" 523 | ] 524 | }, 525 | { 526 | "cell_type": "code", 527 | "execution_count": 33, 528 | "metadata": {}, 529 | "outputs": [ 530 | { 531 | "data": { 532 | "text/plain": [ 533 | "True" 534 | ] 535 | }, 536 | "execution_count": 33, 537 | "metadata": {}, 538 | "output_type": "execute_result" 539 | } 540 | ], 541 | "source": [ 542 | "'HELLO'.isupper()" 543 | ] 544 | }, 545 | { 546 | "cell_type": "code", 547 | "execution_count": 34, 548 | "metadata": {}, 549 | "outputs": [ 550 | { 551 | "data": { 552 | "text/plain": [ 553 | "True" 554 | ] 555 | }, 556 | "execution_count": 34, 557 | "metadata": {}, 558 | "output_type": "execute_result" 559 | } 560 | ], 561 | "source": [ 562 | "s.endswith('o')" 563 | ] 564 | }, 565 | { 566 | "cell_type": "code", 567 | "execution_count": 35, 568 | "metadata": {}, 569 | "outputs": [ 570 | { 571 | "data": { 572 | "text/plain": [ 573 | "True" 574 | ] 575 | }, 576 | "execution_count": 35, 577 | "metadata": {}, 578 | "output_type": "execute_result" 579 | } 580 | ], 581 | "source": [ 582 | "# it's basically the same as \n", 583 | "s[-1] == 'o'" 584 | ] 585 | }, 586 | { 587 | "cell_type": "code", 588 | "execution_count": 36, 589 | "metadata": {}, 590 | "outputs": [ 591 | { 592 | "data": { 593 | "text/plain": [ 594 | "['h', 'llo']" 595 | ] 596 | }, 597 | "execution_count": 36, 598 | "metadata": {}, 599 | "output_type": "execute_result" 600 | } 601 | ], 602 | "source": [ 603 | "s.split('e')" 604 | ] 605 | }, 606 | { 607 | "cell_type": "markdown", 608 | "metadata": {}, 609 | "source": [ 610 | "### Advanced Sets" 611 | ] 612 | }, 613 | { 614 | "cell_type": "code", 615 | "execution_count": 37, 616 | "metadata": {}, 617 | "outputs": [], 618 | "source": [ 619 | "s = set()" 620 | ] 621 | }, 622 | { 623 | "cell_type": "code", 624 | "execution_count": 38, 625 | "metadata": {}, 626 | "outputs": [], 627 | "source": [ 628 | "s.add(1)" 629 | ] 630 | }, 631 | { 632 | "cell_type": "code", 633 | "execution_count": 39, 634 | "metadata": {}, 635 | "outputs": [], 636 | "source": [ 637 | "s.add(2)" 638 | ] 639 | }, 640 | { 641 | "cell_type": "code", 642 | "execution_count": 40, 643 | "metadata": {}, 644 | "outputs": [ 645 | { 646 | "data": { 647 | "text/plain": [ 648 | "{1, 2}" 649 | ] 650 | }, 651 | "execution_count": 40, 652 | "metadata": {}, 653 | "output_type": "execute_result" 654 | } 655 | ], 656 | "source": [ 657 | "s" 658 | ] 659 | }, 660 | { 661 | "cell_type": "code", 662 | "execution_count": 41, 663 | "metadata": {}, 664 | "outputs": [], 665 | "source": [ 666 | "s.clear()" 667 | ] 668 | }, 669 | { 670 | "cell_type": "code", 671 | "execution_count": 42, 672 | "metadata": {}, 673 | "outputs": [ 674 | { 675 | "data": { 676 | "text/plain": [ 677 | "set()" 678 | ] 679 | }, 680 | "execution_count": 42, 681 | "metadata": {}, 682 | "output_type": "execute_result" 683 | } 684 | ], 685 | "source": [ 686 | "s" 687 | ] 688 | }, 689 | { 690 | "cell_type": "code", 691 | "execution_count": 43, 692 | "metadata": {}, 693 | "outputs": [], 694 | "source": [ 695 | "s = {1,2,3}" 696 | ] 697 | }, 698 | { 699 | "cell_type": "code", 700 | "execution_count": 44, 701 | "metadata": {}, 702 | "outputs": [], 703 | "source": [ 704 | "sc = s.copy()" 705 | ] 706 | }, 707 | { 708 | "cell_type": "code", 709 | "execution_count": 45, 710 | "metadata": {}, 711 | "outputs": [ 712 | { 713 | "data": { 714 | "text/plain": [ 715 | "{1, 2, 3}" 716 | ] 717 | }, 718 | "execution_count": 45, 719 | "metadata": {}, 720 | "output_type": "execute_result" 721 | } 722 | ], 723 | "source": [ 724 | "sc" 725 | ] 726 | }, 727 | { 728 | "cell_type": "code", 729 | "execution_count": 46, 730 | "metadata": {}, 731 | "outputs": [], 732 | "source": [ 733 | "s.add(4)" 734 | ] 735 | }, 736 | { 737 | "cell_type": "code", 738 | "execution_count": 47, 739 | "metadata": {}, 740 | "outputs": [ 741 | { 742 | "data": { 743 | "text/plain": [ 744 | "{1, 2, 3, 4}" 745 | ] 746 | }, 747 | "execution_count": 47, 748 | "metadata": {}, 749 | "output_type": "execute_result" 750 | } 751 | ], 752 | "source": [ 753 | "s" 754 | ] 755 | }, 756 | { 757 | "cell_type": "code", 758 | "execution_count": 48, 759 | "metadata": {}, 760 | "outputs": [ 761 | { 762 | "data": { 763 | "text/plain": [ 764 | "{4}" 765 | ] 766 | }, 767 | "execution_count": 48, 768 | "metadata": {}, 769 | "output_type": "execute_result" 770 | } 771 | ], 772 | "source": [ 773 | "s.difference(sc)" 774 | ] 775 | }, 776 | { 777 | "cell_type": "code", 778 | "execution_count": 49, 779 | "metadata": {}, 780 | "outputs": [], 781 | "source": [ 782 | "s1 = {1,2,3}" 783 | ] 784 | }, 785 | { 786 | "cell_type": "code", 787 | "execution_count": 50, 788 | "metadata": {}, 789 | "outputs": [], 790 | "source": [ 791 | "s2 = {1,4,5}" 792 | ] 793 | }, 794 | { 795 | "cell_type": "code", 796 | "execution_count": 51, 797 | "metadata": {}, 798 | "outputs": [], 799 | "source": [ 800 | "s1.difference_update(s2)" 801 | ] 802 | }, 803 | { 804 | "cell_type": "code", 805 | "execution_count": 52, 806 | "metadata": {}, 807 | "outputs": [ 808 | { 809 | "data": { 810 | "text/plain": [ 811 | "{2, 3}" 812 | ] 813 | }, 814 | "execution_count": 52, 815 | "metadata": {}, 816 | "output_type": "execute_result" 817 | } 818 | ], 819 | "source": [ 820 | "s1" 821 | ] 822 | }, 823 | { 824 | "cell_type": "code", 825 | "execution_count": 53, 826 | "metadata": {}, 827 | "outputs": [ 828 | { 829 | "data": { 830 | "text/plain": [ 831 | "{1, 2, 3, 4}" 832 | ] 833 | }, 834 | "execution_count": 53, 835 | "metadata": {}, 836 | "output_type": "execute_result" 837 | } 838 | ], 839 | "source": [ 840 | "s" 841 | ] 842 | }, 843 | { 844 | "cell_type": "code", 845 | "execution_count": 54, 846 | "metadata": {}, 847 | "outputs": [], 848 | "source": [ 849 | "s.discard(2)" 850 | ] 851 | }, 852 | { 853 | "cell_type": "code", 854 | "execution_count": 55, 855 | "metadata": {}, 856 | "outputs": [ 857 | { 858 | "data": { 859 | "text/plain": [ 860 | "{1, 3, 4}" 861 | ] 862 | }, 863 | "execution_count": 55, 864 | "metadata": {}, 865 | "output_type": "execute_result" 866 | } 867 | ], 868 | "source": [ 869 | "s" 870 | ] 871 | }, 872 | { 873 | "cell_type": "code", 874 | "execution_count": 56, 875 | "metadata": {}, 876 | "outputs": [], 877 | "source": [ 878 | "s1 = {1,2,3}" 879 | ] 880 | }, 881 | { 882 | "cell_type": "code", 883 | "execution_count": 57, 884 | "metadata": {}, 885 | "outputs": [], 886 | "source": [ 887 | "s2 = {1,2,4}" 888 | ] 889 | }, 890 | { 891 | "cell_type": "code", 892 | "execution_count": 58, 893 | "metadata": {}, 894 | "outputs": [ 895 | { 896 | "data": { 897 | "text/plain": [ 898 | "{1, 2}" 899 | ] 900 | }, 901 | "execution_count": 58, 902 | "metadata": {}, 903 | "output_type": "execute_result" 904 | } 905 | ], 906 | "source": [ 907 | "s1.intersection(s2)" 908 | ] 909 | }, 910 | { 911 | "cell_type": "code", 912 | "execution_count": 59, 913 | "metadata": {}, 914 | "outputs": [], 915 | "source": [ 916 | "s1.intersection_update(s2)" 917 | ] 918 | }, 919 | { 920 | "cell_type": "code", 921 | "execution_count": 60, 922 | "metadata": {}, 923 | "outputs": [ 924 | { 925 | "data": { 926 | "text/plain": [ 927 | "{1, 2, 4}" 928 | ] 929 | }, 930 | "execution_count": 60, 931 | "metadata": {}, 932 | "output_type": "execute_result" 933 | } 934 | ], 935 | "source": [ 936 | "s2" 937 | ] 938 | }, 939 | { 940 | "cell_type": "code", 941 | "execution_count": 61, 942 | "metadata": {}, 943 | "outputs": [ 944 | { 945 | "data": { 946 | "text/plain": [ 947 | "{1, 2}" 948 | ] 949 | }, 950 | "execution_count": 61, 951 | "metadata": {}, 952 | "output_type": "execute_result" 953 | } 954 | ], 955 | "source": [ 956 | "s1" 957 | ] 958 | }, 959 | { 960 | "cell_type": "code", 961 | "execution_count": 62, 962 | "metadata": {}, 963 | "outputs": [], 964 | "source": [ 965 | "s1 = {1,2}\n", 966 | "s2 = {1,2,4}\n", 967 | "s3 = {5}" 968 | ] 969 | }, 970 | { 971 | "cell_type": "code", 972 | "execution_count": 63, 973 | "metadata": {}, 974 | "outputs": [ 975 | { 976 | "data": { 977 | "text/plain": [ 978 | "False" 979 | ] 980 | }, 981 | "execution_count": 63, 982 | "metadata": {}, 983 | "output_type": "execute_result" 984 | } 985 | ], 986 | "source": [ 987 | "s1.isdisjoint(s2)" 988 | ] 989 | }, 990 | { 991 | "cell_type": "code", 992 | "execution_count": 64, 993 | "metadata": {}, 994 | "outputs": [ 995 | { 996 | "data": { 997 | "text/plain": [ 998 | "True" 999 | ] 1000 | }, 1001 | "execution_count": 64, 1002 | "metadata": {}, 1003 | "output_type": "execute_result" 1004 | } 1005 | ], 1006 | "source": [ 1007 | "s1.isdisjoint(s3)" 1008 | ] 1009 | }, 1010 | { 1011 | "cell_type": "code", 1012 | "execution_count": 65, 1013 | "metadata": {}, 1014 | "outputs": [ 1015 | { 1016 | "data": { 1017 | "text/plain": [ 1018 | "True" 1019 | ] 1020 | }, 1021 | "execution_count": 65, 1022 | "metadata": {}, 1023 | "output_type": "execute_result" 1024 | } 1025 | ], 1026 | "source": [ 1027 | "s1.issubset(s2)" 1028 | ] 1029 | }, 1030 | { 1031 | "cell_type": "code", 1032 | "execution_count": 66, 1033 | "metadata": {}, 1034 | "outputs": [ 1035 | { 1036 | "data": { 1037 | "text/plain": [ 1038 | "True" 1039 | ] 1040 | }, 1041 | "execution_count": 66, 1042 | "metadata": {}, 1043 | "output_type": "execute_result" 1044 | } 1045 | ], 1046 | "source": [ 1047 | "s2.issuperset(s1)" 1048 | ] 1049 | }, 1050 | { 1051 | "cell_type": "code", 1052 | "execution_count": 67, 1053 | "metadata": {}, 1054 | "outputs": [ 1055 | { 1056 | "data": { 1057 | "text/plain": [ 1058 | "{4}" 1059 | ] 1060 | }, 1061 | "execution_count": 67, 1062 | "metadata": {}, 1063 | "output_type": "execute_result" 1064 | } 1065 | ], 1066 | "source": [ 1067 | "s1.symmetric_difference(s2)" 1068 | ] 1069 | }, 1070 | { 1071 | "cell_type": "code", 1072 | "execution_count": 68, 1073 | "metadata": {}, 1074 | "outputs": [ 1075 | { 1076 | "data": { 1077 | "text/plain": [ 1078 | "{1, 2, 4}" 1079 | ] 1080 | }, 1081 | "execution_count": 68, 1082 | "metadata": {}, 1083 | "output_type": "execute_result" 1084 | } 1085 | ], 1086 | "source": [ 1087 | "s1.union(s2)" 1088 | ] 1089 | }, 1090 | { 1091 | "cell_type": "code", 1092 | "execution_count": 69, 1093 | "metadata": {}, 1094 | "outputs": [], 1095 | "source": [ 1096 | "s1.update(s2)" 1097 | ] 1098 | }, 1099 | { 1100 | "cell_type": "code", 1101 | "execution_count": 70, 1102 | "metadata": {}, 1103 | "outputs": [ 1104 | { 1105 | "data": { 1106 | "text/plain": [ 1107 | "{1, 2, 4}" 1108 | ] 1109 | }, 1110 | "execution_count": 70, 1111 | "metadata": {}, 1112 | "output_type": "execute_result" 1113 | } 1114 | ], 1115 | "source": [ 1116 | "s1" 1117 | ] 1118 | }, 1119 | { 1120 | "cell_type": "markdown", 1121 | "metadata": {}, 1122 | "source": [ 1123 | "### Advanced Dictionaries" 1124 | ] 1125 | }, 1126 | { 1127 | "cell_type": "code", 1128 | "execution_count": 71, 1129 | "metadata": {}, 1130 | "outputs": [], 1131 | "source": [ 1132 | "d = {'k1':1, 'k2':2}" 1133 | ] 1134 | }, 1135 | { 1136 | "cell_type": "code", 1137 | "execution_count": 72, 1138 | "metadata": {}, 1139 | "outputs": [ 1140 | { 1141 | "data": { 1142 | "text/plain": [ 1143 | "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}" 1144 | ] 1145 | }, 1146 | "execution_count": 72, 1147 | "metadata": {}, 1148 | "output_type": "execute_result" 1149 | } 1150 | ], 1151 | "source": [ 1152 | "{x:x**2 for x in range(10)}" 1153 | ] 1154 | }, 1155 | { 1156 | "cell_type": "code", 1157 | "execution_count": 76, 1158 | "metadata": {}, 1159 | "outputs": [ 1160 | { 1161 | "name": "stdout", 1162 | "output_type": "stream", 1163 | "text": [ 1164 | "k1\n", 1165 | "k2\n" 1166 | ] 1167 | } 1168 | ], 1169 | "source": [ 1170 | "for k in d.keys():\n", 1171 | " print (k)" 1172 | ] 1173 | }, 1174 | { 1175 | "cell_type": "markdown", 1176 | "metadata": {}, 1177 | "source": [ 1178 | "### Advanced Lists" 1179 | ] 1180 | }, 1181 | { 1182 | "cell_type": "code", 1183 | "execution_count": 78, 1184 | "metadata": {}, 1185 | "outputs": [], 1186 | "source": [ 1187 | "l = [1,2,3]" 1188 | ] 1189 | }, 1190 | { 1191 | "cell_type": "code", 1192 | "execution_count": 79, 1193 | "metadata": {}, 1194 | "outputs": [], 1195 | "source": [ 1196 | "l.append(4)" 1197 | ] 1198 | }, 1199 | { 1200 | "cell_type": "code", 1201 | "execution_count": 80, 1202 | "metadata": {}, 1203 | "outputs": [ 1204 | { 1205 | "data": { 1206 | "text/plain": [ 1207 | "[1, 2, 3, 4]" 1208 | ] 1209 | }, 1210 | "execution_count": 80, 1211 | "metadata": {}, 1212 | "output_type": "execute_result" 1213 | } 1214 | ], 1215 | "source": [ 1216 | "l" 1217 | ] 1218 | }, 1219 | { 1220 | "cell_type": "code", 1221 | "execution_count": 81, 1222 | "metadata": {}, 1223 | "outputs": [ 1224 | { 1225 | "data": { 1226 | "text/plain": [ 1227 | "0" 1228 | ] 1229 | }, 1230 | "execution_count": 81, 1231 | "metadata": {}, 1232 | "output_type": "execute_result" 1233 | } 1234 | ], 1235 | "source": [ 1236 | "l.count(10)" 1237 | ] 1238 | }, 1239 | { 1240 | "cell_type": "code", 1241 | "execution_count": 82, 1242 | "metadata": {}, 1243 | "outputs": [ 1244 | { 1245 | "data": { 1246 | "text/plain": [ 1247 | "1" 1248 | ] 1249 | }, 1250 | "execution_count": 82, 1251 | "metadata": {}, 1252 | "output_type": "execute_result" 1253 | } 1254 | ], 1255 | "source": [ 1256 | "l.count(1)" 1257 | ] 1258 | }, 1259 | { 1260 | "cell_type": "code", 1261 | "execution_count": 85, 1262 | "metadata": {}, 1263 | "outputs": [ 1264 | { 1265 | "name": "stdout", 1266 | "output_type": "stream", 1267 | "text": [ 1268 | "[1, 2, 3, [4, 5]]\n" 1269 | ] 1270 | } 1271 | ], 1272 | "source": [ 1273 | "x = [1,2,3]\n", 1274 | "x.append([4,5])\n", 1275 | "print (x)" 1276 | ] 1277 | }, 1278 | { 1279 | "cell_type": "code", 1280 | "execution_count": 86, 1281 | "metadata": {}, 1282 | "outputs": [ 1283 | { 1284 | "name": "stdout", 1285 | "output_type": "stream", 1286 | "text": [ 1287 | "[1, 2, 3, 4, 5]\n" 1288 | ] 1289 | } 1290 | ], 1291 | "source": [ 1292 | "x = [1,2,3]\n", 1293 | "x.extend([4,5])\n", 1294 | "print(x)" 1295 | ] 1296 | }, 1297 | { 1298 | "cell_type": "code", 1299 | "execution_count": 87, 1300 | "metadata": {}, 1301 | "outputs": [ 1302 | { 1303 | "data": { 1304 | "text/plain": [ 1305 | "[1, 2, 3, 4]" 1306 | ] 1307 | }, 1308 | "execution_count": 87, 1309 | "metadata": {}, 1310 | "output_type": "execute_result" 1311 | } 1312 | ], 1313 | "source": [ 1314 | "l" 1315 | ] 1316 | }, 1317 | { 1318 | "cell_type": "code", 1319 | "execution_count": 88, 1320 | "metadata": {}, 1321 | "outputs": [ 1322 | { 1323 | "data": { 1324 | "text/plain": [ 1325 | "1" 1326 | ] 1327 | }, 1328 | "execution_count": 88, 1329 | "metadata": {}, 1330 | "output_type": "execute_result" 1331 | } 1332 | ], 1333 | "source": [ 1334 | "l.index(2)" 1335 | ] 1336 | }, 1337 | { 1338 | "cell_type": "code", 1339 | "execution_count": 89, 1340 | "metadata": {}, 1341 | "outputs": [ 1342 | { 1343 | "ename": "ValueError", 1344 | "evalue": "20 is not in list", 1345 | "output_type": "error", 1346 | "traceback": [ 1347 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", 1348 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", 1349 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ml\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m20\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 1350 | "\u001b[1;31mValueError\u001b[0m: 20 is not in list" 1351 | ] 1352 | } 1353 | ], 1354 | "source": [ 1355 | "l.index(20)" 1356 | ] 1357 | }, 1358 | { 1359 | "cell_type": "code", 1360 | "execution_count": 90, 1361 | "metadata": {}, 1362 | "outputs": [], 1363 | "source": [ 1364 | "l.insert(2, 'inserted')" 1365 | ] 1366 | }, 1367 | { 1368 | "cell_type": "code", 1369 | "execution_count": 91, 1370 | "metadata": {}, 1371 | "outputs": [ 1372 | { 1373 | "data": { 1374 | "text/plain": [ 1375 | "[1, 2, 'inserted', 3, 4]" 1376 | ] 1377 | }, 1378 | "execution_count": 91, 1379 | "metadata": {}, 1380 | "output_type": "execute_result" 1381 | } 1382 | ], 1383 | "source": [ 1384 | "l" 1385 | ] 1386 | }, 1387 | { 1388 | "cell_type": "code", 1389 | "execution_count": 92, 1390 | "metadata": {}, 1391 | "outputs": [], 1392 | "source": [ 1393 | "ele = l.pop()" 1394 | ] 1395 | }, 1396 | { 1397 | "cell_type": "code", 1398 | "execution_count": 93, 1399 | "metadata": {}, 1400 | "outputs": [ 1401 | { 1402 | "data": { 1403 | "text/plain": [ 1404 | "4" 1405 | ] 1406 | }, 1407 | "execution_count": 93, 1408 | "metadata": {}, 1409 | "output_type": "execute_result" 1410 | } 1411 | ], 1412 | "source": [ 1413 | "ele" 1414 | ] 1415 | }, 1416 | { 1417 | "cell_type": "code", 1418 | "execution_count": 94, 1419 | "metadata": {}, 1420 | "outputs": [ 1421 | { 1422 | "data": { 1423 | "text/plain": [ 1424 | "[1, 2, 'inserted', 3]" 1425 | ] 1426 | }, 1427 | "execution_count": 94, 1428 | "metadata": {}, 1429 | "output_type": "execute_result" 1430 | } 1431 | ], 1432 | "source": [ 1433 | "l" 1434 | ] 1435 | }, 1436 | { 1437 | "cell_type": "code", 1438 | "execution_count": 95, 1439 | "metadata": {}, 1440 | "outputs": [], 1441 | "source": [ 1442 | "l.remove('inserted')" 1443 | ] 1444 | }, 1445 | { 1446 | "cell_type": "code", 1447 | "execution_count": 96, 1448 | "metadata": {}, 1449 | "outputs": [ 1450 | { 1451 | "data": { 1452 | "text/plain": [ 1453 | "[1, 2, 3]" 1454 | ] 1455 | }, 1456 | "execution_count": 96, 1457 | "metadata": {}, 1458 | "output_type": "execute_result" 1459 | } 1460 | ], 1461 | "source": [ 1462 | "l" 1463 | ] 1464 | }, 1465 | { 1466 | "cell_type": "code", 1467 | "execution_count": 97, 1468 | "metadata": {}, 1469 | "outputs": [], 1470 | "source": [ 1471 | "l = [1,2,3,4,3]" 1472 | ] 1473 | }, 1474 | { 1475 | "cell_type": "code", 1476 | "execution_count": 98, 1477 | "metadata": {}, 1478 | "outputs": [], 1479 | "source": [ 1480 | "l.remove(3)" 1481 | ] 1482 | }, 1483 | { 1484 | "cell_type": "code", 1485 | "execution_count": 99, 1486 | "metadata": {}, 1487 | "outputs": [ 1488 | { 1489 | "data": { 1490 | "text/plain": [ 1491 | "[1, 2, 4, 3]" 1492 | ] 1493 | }, 1494 | "execution_count": 99, 1495 | "metadata": {}, 1496 | "output_type": "execute_result" 1497 | } 1498 | ], 1499 | "source": [ 1500 | "l" 1501 | ] 1502 | }, 1503 | { 1504 | "cell_type": "code", 1505 | "execution_count": 100, 1506 | "metadata": {}, 1507 | "outputs": [], 1508 | "source": [ 1509 | "l.reverse()" 1510 | ] 1511 | }, 1512 | { 1513 | "cell_type": "code", 1514 | "execution_count": 101, 1515 | "metadata": {}, 1516 | "outputs": [ 1517 | { 1518 | "data": { 1519 | "text/plain": [ 1520 | "[3, 4, 2, 1]" 1521 | ] 1522 | }, 1523 | "execution_count": 101, 1524 | "metadata": {}, 1525 | "output_type": "execute_result" 1526 | } 1527 | ], 1528 | "source": [ 1529 | "l" 1530 | ] 1531 | }, 1532 | { 1533 | "cell_type": "code", 1534 | "execution_count": 102, 1535 | "metadata": {}, 1536 | "outputs": [], 1537 | "source": [ 1538 | "l.sort()" 1539 | ] 1540 | }, 1541 | { 1542 | "cell_type": "code", 1543 | "execution_count": 103, 1544 | "metadata": {}, 1545 | "outputs": [ 1546 | { 1547 | "data": { 1548 | "text/plain": [ 1549 | "[1, 2, 3, 4]" 1550 | ] 1551 | }, 1552 | "execution_count": 103, 1553 | "metadata": {}, 1554 | "output_type": "execute_result" 1555 | } 1556 | ], 1557 | "source": [ 1558 | "l" 1559 | ] 1560 | }, 1561 | { 1562 | "cell_type": "code", 1563 | "execution_count": null, 1564 | "metadata": {}, 1565 | "outputs": [], 1566 | "source": [] 1567 | } 1568 | ], 1569 | "metadata": { 1570 | "kernelspec": { 1571 | "display_name": "Python 3", 1572 | "language": "python", 1573 | "name": "python3" 1574 | }, 1575 | "language_info": { 1576 | "codemirror_mode": { 1577 | "name": "ipython", 1578 | "version": 3 1579 | }, 1580 | "file_extension": ".py", 1581 | "mimetype": "text/x-python", 1582 | "name": "python", 1583 | "nbconvert_exporter": "python", 1584 | "pygments_lexer": "ipython3", 1585 | "version": "3.7.3" 1586 | } 1587 | }, 1588 | "nbformat": 4, 1589 | "nbformat_minor": 2 1590 | } 1591 | --------------------------------------------------------------------------------