├── Lab01----------Introduction----------.txt ├── Lab01-1_Say-Hello-World-With-Python.ipynb ├── Lab01-2_Reading-Raw-Input.ipynb ├── Lab01-3_Arithmetic-Operators.ipynb ├── Lab01-4_Python-Division.ipynb ├── Lab01-5_Python-If-Else.ipynb ├── Lab01-6_Write-a-Function.ipynb ├── Lab01-7_Loops.ipynb ├── Lab01-8_Print-Function.ipynb ├── Lab02----------Basic-DataTypes----------.txt ├── Lab02-1_Lists.ipynb ├── Lab02-2_Tuples.ipynb ├── Lab02-3_List-Comprehensions.ipynb ├── Lab02-4_Find-the-Second-Largest-Number.ipynb ├── Lab02-5_Nested-Lists.ipynb ├── Lab02-6_Finding-the-Percentage.ipynb ├── Lab03----------Strings----------.txt ├── Lab03-1_What's-Your-Name.ipynb ├── Lab03-2_sWAP-cASE.ipynb ├── Lab04----------Functions----------.txt ├── Lab05----------Class----------.txt ├── Lab06----------Advanced-Topics----------.txt ├── Lab07----------Numpy----------.txt ├── README.md ├── image ├── Lab01-5_1_RelationalOperator.png ├── Lab01-5_2_IfElseDiagram.png ├── Lab01-6_1_Function.png ├── Lab01-7_1_ForLoopDiagram.png ├── Lab01-7_2_WhileLoopDiagram.png └── Lab01-7_3_AssignmentOperator.png └── lab-01-1-pytorch_basics.ipynb /Lab01----------Introduction----------.txt: -------------------------------------------------------------------------------- 1 | Lab1 Introduction 2 | 3 | > Datatypes, I/O, Import, Operators 4 | 5 | > Flow Control: if-else, for Loop, while Loop -------------------------------------------------------------------------------- /Lab01-1_Say-Hello-World-With-Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab01-1: Say \"Hello, World!\" With Python\n", 8 | "https://www.hackerrank.com/challenges/py-hello-world" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "source": [ 17 | "## 1. Description of Problem" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "If we want to print something, how do we say in Python?\n", 25 | "\n", 26 | "This problem is to print out \"Hello, World!\".\n", 27 | ">Output: Hello, World!" 28 | ] 29 | }, 30 | { 31 | "cell_type": "markdown", 32 | "metadata": { 33 | "collapsed": true 34 | }, 35 | "source": [ 36 | "## 2. Concept & Short Examples" 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": { 42 | "collapsed": false 43 | }, 44 | "source": [ 45 | "### Concept [1] print(\" \")" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "You can print something with a line of code, print(\" \")\n", 53 | "\n", 54 | "Ex1) We want to print \"I am a genius.\"" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 22, 60 | "metadata": { 61 | "collapsed": false 62 | }, 63 | "outputs": [ 64 | { 65 | "name": "stdout", 66 | "output_type": "stream", 67 | "text": [ 68 | "I am a genius.\n" 69 | ] 70 | } 71 | ], 72 | "source": [ 73 | "print(\"I am a genius.\")" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": { 79 | "collapsed": true 80 | }, 81 | "source": [ 82 | "### Concept [2] print(variable)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "You can just as easily store a string as a variable and then print it to stdout.\n", 90 | "\n", 91 | "Ex2) We want to store a string \"Are you smart?\" into a variable 'my_var' and then print it." 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 23, 97 | "metadata": { 98 | "collapsed": false 99 | }, 100 | "outputs": [ 101 | { 102 | "name": "stdout", 103 | "output_type": "stream", 104 | "text": [ 105 | "Are you smart?\n" 106 | ] 107 | } 108 | ], 109 | "source": [ 110 | "my_var = \"Are you smart?\"\n", 111 | "print(my_var)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "## 3. Practice Problem" 119 | ] 120 | }, 121 | { 122 | "cell_type": "markdown", 123 | "metadata": {}, 124 | "source": [ 125 | "Print out \"Let's play!\" with Python.\n", 126 | "\n", 127 | ">Output: Let's play!" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 2, 133 | "metadata": { 134 | "collapsed": false 135 | }, 136 | "outputs": [ 137 | { 138 | "name": "stdout", 139 | "output_type": "stream", 140 | "text": [ 141 | "Let's play!\n" 142 | ] 143 | } 144 | ], 145 | "source": [ 146 | "print(\"Let's play!\")" 147 | ] 148 | }, 149 | { 150 | "cell_type": "code", 151 | "execution_count": 3, 152 | "metadata": { 153 | "collapsed": false 154 | }, 155 | "outputs": [ 156 | { 157 | "name": "stdout", 158 | "output_type": "stream", 159 | "text": [ 160 | "Let's play!\n" 161 | ] 162 | } 163 | ], 164 | "source": [ 165 | "variable_a = \"Let's play!\"\n", 166 | "print(variable_a)" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "## 4. Are you ready? \n", 174 | "### DIY at https://www.hackerrank.com/challenges/py-hello-world " 175 | ] 176 | } 177 | ], 178 | "metadata": { 179 | "anaconda-cloud": {}, 180 | "kernelspec": { 181 | "display_name": "Python [default]", 182 | "language": "python", 183 | "name": "python2" 184 | }, 185 | "language_info": { 186 | "codemirror_mode": { 187 | "name": "ipython", 188 | "version": 2 189 | }, 190 | "file_extension": ".py", 191 | "mimetype": "text/x-python", 192 | "name": "python", 193 | "nbconvert_exporter": "python", 194 | "pygments_lexer": "ipython2", 195 | "version": "2.7.12" 196 | } 197 | }, 198 | "nbformat": 4, 199 | "nbformat_minor": 1 200 | } 201 | -------------------------------------------------------------------------------- /Lab01-2_Reading-Raw-Input.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab01-2: Reading Raw Input\n", 8 | "https://www.hackerrank.com/challenges/python-raw-input" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "## 1. Description of Problem" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "If we want to print something, we say, 'print(\"something\")' with Python.\n", 23 | "\n", 24 | "Then, if we want to read input, how do we say with Python?\n", 25 | "\n", 26 | "This problem is to read input from a person and print out the input.\n", 27 | "\n", 28 | ">Input: How many chickens does it take to cross the road?\n", 29 | "\n", 30 | ">Output: input number from a person (ex. if you input 5, the output will be 5.)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": {}, 36 | "source": [ 37 | "## 2. Concept & Short Examples" 38 | ] 39 | }, 40 | { 41 | "cell_type": "markdown", 42 | "metadata": {}, 43 | "source": [ 44 | "### Concept [1] raw_input( ), input( )" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "You can read input with a line of code, raw_input( ) or input( ).\n", 52 | "\n", 53 | "->Python 2 syntax: raw_input( )\n", 54 | "\n", 55 | "->Python 3 syntax: input( )\n", 56 | "\n", 57 | "If you input something, output will be your input data.\n", 58 | "\n", 59 | "Ex1) We want to read input something from a person." 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 26, 65 | "metadata": { 66 | "collapsed": false 67 | }, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "2\n" 74 | ] 75 | }, 76 | { 77 | "data": { 78 | "text/plain": [ 79 | "'2'" 80 | ] 81 | }, 82 | "execution_count": 26, 83 | "metadata": {}, 84 | "output_type": "execute_result" 85 | } 86 | ], 87 | "source": [ 88 | "raw_input()" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 28, 94 | "metadata": { 95 | "collapsed": false 96 | }, 97 | "outputs": [ 98 | { 99 | "name": "stdout", 100 | "output_type": "stream", 101 | "text": [ 102 | "2\n" 103 | ] 104 | }, 105 | { 106 | "data": { 107 | "text/plain": [ 108 | "2" 109 | ] 110 | }, 111 | "execution_count": 28, 112 | "metadata": {}, 113 | "output_type": "execute_result" 114 | } 115 | ], 116 | "source": [ 117 | "input()" 118 | ] 119 | }, 120 | { 121 | "cell_type": "markdown", 122 | "metadata": {}, 123 | "source": [ 124 | "### Concept [2] variable = raw_input(\" \"), variable = input(\" \")" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "You can just as easily store your input as a variable and then print it to stdout.\n", 132 | "\n", 133 | "Ex2) We want to store your input into a variable 'my_var' and then print it." 134 | ] 135 | }, 136 | { 137 | "cell_type": "code", 138 | "execution_count": 29, 139 | "metadata": { 140 | "collapsed": false 141 | }, 142 | "outputs": [ 143 | { 144 | "name": "stdout", 145 | "output_type": "stream", 146 | "text": [ 147 | "Hey, what's your name? kim sung\n", 148 | "kim sung\n" 149 | ] 150 | } 151 | ], 152 | "source": [ 153 | "my_var = raw_input(\"Hey, what's your name? \")\n", 154 | "print(my_var)" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 30, 160 | "metadata": { 161 | "collapsed": false 162 | }, 163 | "outputs": [ 164 | { 165 | "name": "stdout", 166 | "output_type": "stream", 167 | "text": [ 168 | "Hey, what's your name? 1\n", 169 | "1\n" 170 | ] 171 | } 172 | ], 173 | "source": [ 174 | "my_var = input(\"Hey, what's your name? \")\n", 175 | "print(my_var)" 176 | ] 177 | }, 178 | { 179 | "cell_type": "markdown", 180 | "metadata": {}, 181 | "source": [ 182 | "## 3. Practice Problem" 183 | ] 184 | }, 185 | { 186 | "cell_type": "markdown", 187 | "metadata": {}, 188 | "source": [ 189 | "Read input from a person and print out the input.\n", 190 | "\n", 191 | ">Input: How old are you?\n", 192 | "\n", 193 | ">Output: input number from a person (ex. if you input 17, the output will be 17.)" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": 31, 199 | "metadata": { 200 | "collapsed": false 201 | }, 202 | "outputs": [ 203 | { 204 | "name": "stdout", 205 | "output_type": "stream", 206 | "text": [ 207 | "How old are you? 17\n" 208 | ] 209 | }, 210 | { 211 | "data": { 212 | "text/plain": [ 213 | "'17'" 214 | ] 215 | }, 216 | "execution_count": 31, 217 | "metadata": {}, 218 | "output_type": "execute_result" 219 | } 220 | ], 221 | "source": [ 222 | "raw_input(\"How old are you? \")" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": 32, 228 | "metadata": { 229 | "collapsed": false 230 | }, 231 | "outputs": [ 232 | { 233 | "name": "stdout", 234 | "output_type": "stream", 235 | "text": [ 236 | "How old are you? 17\n" 237 | ] 238 | }, 239 | { 240 | "data": { 241 | "text/plain": [ 242 | "17" 243 | ] 244 | }, 245 | "execution_count": 32, 246 | "metadata": {}, 247 | "output_type": "execute_result" 248 | } 249 | ], 250 | "source": [ 251 | "input(\"How old are you? \")" 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "## 4. Are you ready? \n", 259 | "### DIY at https://www.hackerrank.com/challenges/python-raw-input" 260 | ] 261 | } 262 | ], 263 | "metadata": { 264 | "anaconda-cloud": {}, 265 | "kernelspec": { 266 | "display_name": "Python [default]", 267 | "language": "python", 268 | "name": "python2" 269 | }, 270 | "language_info": { 271 | "codemirror_mode": { 272 | "name": "ipython", 273 | "version": 2 274 | }, 275 | "file_extension": ".py", 276 | "mimetype": "text/x-python", 277 | "name": "python", 278 | "nbconvert_exporter": "python", 279 | "pygments_lexer": "ipython2", 280 | "version": "2.7.12" 281 | } 282 | }, 283 | "nbformat": 4, 284 | "nbformat_minor": 1 285 | } 286 | -------------------------------------------------------------------------------- /Lab01-3_Arithmetic-Operators.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# Lab01-3: Arithmetic Operators\n", 10 | "https://www.hackerrank.com/challenges/python-arithmetic-operators" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "## 1. Description of Problem" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": { 23 | "collapsed": false 24 | }, 25 | "source": [ 26 | "If we want to do an operation, how do we say with Python?\n", 27 | "\n", 28 | "This problem is to read two integers from a person and print three results of operation.\n", 29 | "\n", 30 | ">Input: two integers (ex. 3, 7)\n", 31 | "\n", 32 | ">Output:\n", 33 | "1. Sum of the two numbers. (ex. 10)\n", 34 | "2. Difference of the two numbers (first - second). (ex. -4)\n", 35 | "3. Product of the two numbers. (ex. 21)" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "## 2. Concept & Short Examples" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "### Concept [1] Data Type: int, float, str" 50 | ] 51 | }, 52 | { 53 | "cell_type": "markdown", 54 | "metadata": {}, 55 | "source": [ 56 | "There are many kinds of data, and the data requires space to be stored.\n", 57 | "\n", 58 | "The following three data types vary in size depending on the type.\n", 59 | "\n", 60 | "> int: variable whose number is to be an integer\n", 61 | "\n", 62 | "> float: variable whose number is to be a real number\n", 63 | "\n", 64 | "> str: variable that makes the data a string\n", 65 | "\n", 66 | "Ex1) We want to store your input into a int variable 'int_a' and then print it." 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 1, 72 | "metadata": { 73 | "collapsed": false 74 | }, 75 | "outputs": [ 76 | { 77 | "name": "stdout", 78 | "output_type": "stream", 79 | "text": [ 80 | "3\n", 81 | "3\n" 82 | ] 83 | } 84 | ], 85 | "source": [ 86 | "int_a = int(input())\n", 87 | "print int_a" 88 | ] 89 | }, 90 | { 91 | "cell_type": "markdown", 92 | "metadata": {}, 93 | "source": [ 94 | "Ex2) We want to store your input into a float variable 'float_b' and then print it." 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 2, 100 | "metadata": { 101 | "collapsed": false 102 | }, 103 | "outputs": [ 104 | { 105 | "name": "stdout", 106 | "output_type": "stream", 107 | "text": [ 108 | "3\n", 109 | "3.0\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "float_b = float(input())\n", 115 | "print float_b" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "Ex3) We want to store your input into a string variable 'str_c' and then print it." 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 3, 128 | "metadata": { 129 | "collapsed": false 130 | }, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "abc\n", 137 | "abc\n" 138 | ] 139 | } 140 | ], 141 | "source": [ 142 | "str_c = str(raw_input())\n", 143 | "print str_c" 144 | ] 145 | }, 146 | { 147 | "cell_type": "markdown", 148 | "metadata": {}, 149 | "source": [ 150 | "### Concept [2] Sum(+), Difference(-), Product(*), Exponent(**)" 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | "You can use it as easily as a calculator.\n", 158 | "\n", 159 | "Ex4) We want to store two numbers into variables 'd', 'e' and print the output after calculation." 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 1, 165 | "metadata": { 166 | "collapsed": false 167 | }, 168 | "outputs": [ 169 | { 170 | "name": "stdout", 171 | "output_type": "stream", 172 | "text": [ 173 | "12.5\n", 174 | "7.5\n", 175 | "25.0\n", 176 | "316.227766017\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "d = 10\n", 182 | "e = 2.5\n", 183 | "print(d + e)\n", 184 | "print(d - e)\n", 185 | "print(d * e)\n", 186 | "print(d ** e)" 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "## 3. Practice Problem" 194 | ] 195 | }, 196 | { 197 | "cell_type": "markdown", 198 | "metadata": {}, 199 | "source": [ 200 | "Read three integers from a person and print three results of operation.\n", 201 | "\n", 202 | ">Input: three integers (ex. 30, 20, 10)\n", 203 | "\n", 204 | ">Output:\n", 205 | "1. Sum of the three numbers. (ex. 60)\n", 206 | "2. Difference of the three numbers (first - second - third). (ex. 0)\n", 207 | "3. Product of the three numbers. (ex. 6000)" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 5, 213 | "metadata": { 214 | "collapsed": false 215 | }, 216 | "outputs": [ 217 | { 218 | "name": "stdout", 219 | "output_type": "stream", 220 | "text": [ 221 | "30\n", 222 | "20\n", 223 | "10\n", 224 | "60\n", 225 | "0\n", 226 | "6000\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "a = int(input())\n", 232 | "b = int(input())\n", 233 | "c = int(input()) \n", 234 | "\n", 235 | "print a + b + c\n", 236 | "print a - b - c\n", 237 | "print a * b * c" 238 | ] 239 | }, 240 | { 241 | "cell_type": "markdown", 242 | "metadata": {}, 243 | "source": [ 244 | "## 4. Are you ready? \n", 245 | "### DIY at https://www.hackerrank.com/challenges/python-arithmetic-operators" 246 | ] 247 | } 248 | ], 249 | "metadata": { 250 | "anaconda-cloud": {}, 251 | "kernelspec": { 252 | "display_name": "Python [default]", 253 | "language": "python", 254 | "name": "python2" 255 | }, 256 | "language_info": { 257 | "codemirror_mode": { 258 | "name": "ipython", 259 | "version": 2 260 | }, 261 | "file_extension": ".py", 262 | "mimetype": "text/x-python", 263 | "name": "python", 264 | "nbconvert_exporter": "python", 265 | "pygments_lexer": "ipython2", 266 | "version": "2.7.12" 267 | } 268 | }, 269 | "nbformat": 4, 270 | "nbformat_minor": 1 271 | } 272 | -------------------------------------------------------------------------------- /Lab01-4_Python-Division.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab01-4: Python: Division\n", 8 | "https://www.hackerrank.com/challenges/python-division" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "## 1. Description of Problem" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "If we want to do an operation of division, how do we say with Python?\n", 23 | "\n", 24 | "This problem is to read two integers from a person and print two results of division.\n", 25 | "\n", 26 | ">Input: two integers (ex. 23, 4)\n", 27 | "\n", 28 | ">Output:\n", 29 | "1. Quotient after integer division of the two integers. (ex. 5)\n", 30 | "2. Quotient after float division of the two integers. (ex. 5.75)" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": { 36 | "collapsed": true 37 | }, 38 | "source": [ 39 | "## 2. Concept & Short Examples" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "### Concept [1] Quotient of integer division(//), Quotient of float division(/)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "Quotient can be integer or real number(float) after division of two integers.\n", 54 | "\n", 55 | "> integer division: we use '//' as an operator in Python.\n", 56 | "\n", 57 | "> float division: we use '/' as an operator in Python. \n", 58 | "\n", 59 | "But, syntax depends on the Python version. Unlike Python 3, you have to do the following in Python 2.\n", 60 | "\n", 61 | "In Python 2, we will import an attribute called 'division' from the module 'future'.\n", 62 | "\n", 63 | "> Python 2 syntax: should write 'from __future__ import division'\n", 64 | "\n", 65 | "> Python 3 syntax: just use '//', '/'\n", 66 | "\n", 67 | "Ex1) We want to print the quotients after integer division and float division of 5 and 3. (Python 2)" 68 | ] 69 | }, 70 | { 71 | "cell_type": "code", 72 | "execution_count": 1, 73 | "metadata": { 74 | "collapsed": false 75 | }, 76 | "outputs": [ 77 | { 78 | "name": "stdout", 79 | "output_type": "stream", 80 | "text": [ 81 | "1\n", 82 | "1.66666666667\n" 83 | ] 84 | } 85 | ], 86 | "source": [ 87 | "# Python 2 syntax\n", 88 | "from __future__ import division\n", 89 | "\n", 90 | "# integer division \n", 91 | "print (5 // 3)\n", 92 | "\n", 93 | "# float division\n", 94 | "print (5 / 3)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "markdown", 99 | "metadata": {}, 100 | "source": [ 101 | "Ex2) We want to print the quotients after integer division and float division of 5 and 3. (Python 3)" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 2, 107 | "metadata": { 108 | "collapsed": false 109 | }, 110 | "outputs": [ 111 | { 112 | "name": "stdout", 113 | "output_type": "stream", 114 | "text": [ 115 | "1\n", 116 | "1.66666666667\n" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "# Python 3 syntax\n", 122 | "\n", 123 | "# integer division \n", 124 | "print (5 // 3)\n", 125 | "\n", 126 | "# float division\n", 127 | "print (5 / 3)" 128 | ] 129 | }, 130 | { 131 | "cell_type": "markdown", 132 | "metadata": {}, 133 | "source": [ 134 | "### Concept [2] Remainder of integer division(%)" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "We use '%' as an operator of remainder after integer division in Python.\n", 142 | "\n", 143 | "Ex3) We want to print the remainder after integer division of 5 and 3. " 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": 3, 149 | "metadata": { 150 | "collapsed": false 151 | }, 152 | "outputs": [ 153 | { 154 | "name": "stdout", 155 | "output_type": "stream", 156 | "text": [ 157 | "2\n" 158 | ] 159 | } 160 | ], 161 | "source": [ 162 | "print (5 % 3)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "## 3. Practice Problem" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "Read two integers from a person and print three results of division.\n", 177 | "\n", 178 | ">Input: two integers (ex. 21, 2)\n", 179 | "\n", 180 | ">Output:\n", 181 | "1. Quotient after integer division of the two integers. (ex. 10)\n", 182 | "2. Remainder after integer division of the two integers. (ex. 1) \n", 183 | "3. Quotient after float division of the two integers. (ex. 10.5)" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 4, 189 | "metadata": { 190 | "collapsed": false 191 | }, 192 | "outputs": [ 193 | { 194 | "name": "stdout", 195 | "output_type": "stream", 196 | "text": [ 197 | "23\n", 198 | "2\n", 199 | "11\n", 200 | "1\n", 201 | "11.5\n" 202 | ] 203 | } 204 | ], 205 | "source": [ 206 | "# Python 2 \n", 207 | "from __future__ import division\n", 208 | "\n", 209 | "a = int(raw_input())\n", 210 | "b = int(raw_input())\n", 211 | "\n", 212 | "print(a // b)\n", 213 | "print(a % b)\n", 214 | "print(a / b)" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 5, 220 | "metadata": { 221 | "collapsed": false 222 | }, 223 | "outputs": [ 224 | { 225 | "name": "stdout", 226 | "output_type": "stream", 227 | "text": [ 228 | "23\n", 229 | "2\n", 230 | "11\n", 231 | "1\n", 232 | "11.5\n" 233 | ] 234 | } 235 | ], 236 | "source": [ 237 | "# Python 3\n", 238 | "\n", 239 | "a = int(input())\n", 240 | "b = int(input())\n", 241 | "\n", 242 | "print(a // b)\n", 243 | "print(a % b)\n", 244 | "print(a / b)" 245 | ] 246 | }, 247 | { 248 | "cell_type": "markdown", 249 | "metadata": {}, 250 | "source": [ 251 | "## 4. Are you ready? \n", 252 | "### DIY at https://www.hackerrank.com/challenges/python-division" 253 | ] 254 | } 255 | ], 256 | "metadata": { 257 | "anaconda-cloud": {}, 258 | "kernelspec": { 259 | "display_name": "Python [default]", 260 | "language": "python", 261 | "name": "python2" 262 | }, 263 | "language_info": { 264 | "codemirror_mode": { 265 | "name": "ipython", 266 | "version": 2 267 | }, 268 | "file_extension": ".py", 269 | "mimetype": "text/x-python", 270 | "name": "python", 271 | "nbconvert_exporter": "python", 272 | "pygments_lexer": "ipython2", 273 | "version": "2.7.12" 274 | } 275 | }, 276 | "nbformat": 4, 277 | "nbformat_minor": 1 278 | } 279 | -------------------------------------------------------------------------------- /Lab01-5_Python-If-Else.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# Lab01-5: Python If-Else\n", 10 | "https://www.hackerrank.com/challenges/py-if-else" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "## 1. Description of Problem" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "If we want to print the different output according to the different input, how do we say with Python?\n", 25 | "\n", 26 | "This problem is to read input from a person and print the different result based on the input data.\n", 27 | "\n", 28 | ">Input: positive integer, n\n", 29 | "\n", 30 | ">Output: Print \"Weird\" if the number is weird; otherwise, print \"Not Weird\"" 31 | ] 32 | }, 33 | { 34 | "cell_type": "markdown", 35 | "metadata": { 36 | "collapsed": true 37 | }, 38 | "source": [ 39 | "## 2. Concept & Short Examples" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "### Concept [1] Data Type: Boolean (True or False)" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "A proposition is a sentence or expression that can clearly identify whether the content is true or false. \n", 54 | "\n", 55 | "All propositions are true or false.\n", 56 | "\n", 57 | "Boolean is a data type with True and False values.\n", 58 | "\n", 59 | "Ex1) We want to print whether the following proposition is true or false.\n", 60 | "\n", 61 | "> '3 is greater than 6'\n", 62 | "\n", 63 | "> '99 is less than 100'" 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": 1, 69 | "metadata": { 70 | "collapsed": false 71 | }, 72 | "outputs": [ 73 | { 74 | "name": "stdout", 75 | "output_type": "stream", 76 | "text": [ 77 | "False\n", 78 | "True\n" 79 | ] 80 | } 81 | ], 82 | "source": [ 83 | "print(3 > 6)\n", 84 | "print(99 < 100)" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "### Concept [2] Relational Operators: ==, !=, >, <, >=, <=" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 4, 97 | "metadata": { 98 | "collapsed": false 99 | }, 100 | "outputs": [ 101 | { 102 | "data": { 103 | "image/png": "iVBORw0KGgoAAAANSUhEUgAABTgAAADbCAIAAADGY2knAAAAAXNSR0IArs4c6QAAAARnQU1BAACx\njwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAEUySURBVHhe7d19kFTlnejxHhXwbmVv+Ycx/1hb\ng+7GK0TWsXDwboYyxl2DxnGJMYQVIUpMgNVY4SVVoqJrUElVUFMkuUBi0IAYfEPjJIruGmIx2Qsj\nBYoBV2uFqa38ZShkE+8uDJi5z/t5znu/Tp/u/n6qq2a6+/Tp0+d5nnPO73k7XaOjoyUAAAAAAFAM\np5i/AAAAAACgAAjUAQAAAAAoEAJ1AAAAAAAKhEAdAAAAAIACIVAHADTS9sVdvr5H3jNvIPDeI31q\n7yzebl7IJXdrsLTZyWO1cyvf3CKQW032AwC0CAJ1AECDqHjus98zz7TffO0viZZqJPdrZLcix/bF\nXX/5td+YJwAAFB6BOgCgIbYvTouMRLDeWm2xhXfZw6PS4M3nmhcAAEBLI1AHADTAe4+sMG2+n/7x\nv6soUvrVN/Vrpe+9EIrUQ/3jQw3uQadu299a0GG+96Eg7vd6ZQfv6zV6K4i16XvvCeFqhMRNSO/2\n7Rbfbpe23xb6ktTPG96vk+zich22AuR7nxVvqJUHm6je0EJrSHwr+yelbEGZMvZo8O3eV4Q3UEjd\nW/aNxdvdx83bKVssXzY9EGSHjtDaQh8JbYN5J5aO4R8W324AAOrCXD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NkWcL79X\nNsaiuKrNHrWeGhqSPWhRB5Cq8S3qqv7S/C8nna6ulaYJZItr+IbwPnE1GRphNSWvBSlVKzcNqbYL\nd8LKq6UuErnlT85OyY2qsd0/D18funV8ZapoeKHdr0YD88d/KXzXX0/oiCTUclCiVS2Kw2ZbUN0i\ngiP701W2XjaB3PIts1OaysMnLKGGc1Ynl/3WzR4FOTVEWtQJ1AGkGquu70jFFWd7q+J6jsRtFZ18\nsd5cHDbRXJR9ZMjOHnR9BwAAAACguAjUAVQv2iEH9TY6Wurq6jJPKkHStITq0pfEbQkU3mZhz6O5\nqsuB4iPkwE5QUfag6zuAVNld3//7v49/9KfRCRPGnVLNFRHyicPz8ZETp556yukTxpuXPBmd6Eia\nlpCRviRuq6sucQXSt0YcNtFcVZf948dPnDj5ETmwveVmD8aoAyhXdqAujh4jIydHTpyMH0ZQF11d\nXePGnTph/LjEyteMUz5J0xIy0pfEbXXVJa5A+tYoY88LlCw0GmUfGXKzB4E6gHJlB+poruxTPloa\nidvGSNwmYuejich+yKCzB5PJAQAAAABQXATqAAAAAAAUCIE6AAAAAAAFQqAOAAAAAECBJE8md/4n\nz/3zj33MPAfQqf744Ydvv/veZX3TzHMUyQf/+Yc33nqb1GlLJG4bI3GbiJ2PJiL7IYPOHmXN+m7+\nAwAAAAAADZYfqAMAAAAAgGZhjDoAAAAAAAVCoA4AAAAAQIEQqAMAAAAAUCAE6gAAAAAAFAiBOgAA\nAAAABUKgDgAAAABAYZRK/x8gICZJI4K2dAAAAABJRU5ErkJggg==\n", 104 | "text/plain": [ 105 | "" 106 | ] 107 | }, 108 | "execution_count": 4, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "# Library for image output (Relational Operators) by WIKIPEDIA)\n", 115 | "from IPython.display import Image \n", 116 | "Image(filename=\"image/Lab01-5_1_RelationalOperator.png\")" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "In computer science, a relational operator tests or defines some kind of relation between two entities. \n", 124 | "\n", 125 | "These include numerical equality (ex. 5 = 5) and inequalities (ex. 4 ≥ 3)." 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "### Difference between \"=\" and \"==\"\n", 133 | "\n", 134 | "\"=\" is an assignment operator which is used to assign value to the variable (ex. int a = 3).\n", 135 | "\n", 136 | "\"==\" is a relational operator which is used to find the relation between two operands \n", 137 | "\n", 138 | "and returns in boolen value either 'true' or 'false' (ex. 5 == 5).\n", 139 | "\n", 140 | "Ex2) ==, !=, >, <, >=, <= can be used to determine whether a given proposition is True or False." 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 3, 146 | "metadata": { 147 | "collapsed": false 148 | }, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "False\n", 155 | "True\n", 156 | "False\n", 157 | "True\n", 158 | "False\n", 159 | "True\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "a = 1\n", 165 | "b = 2\n", 166 | "print (a == b)\n", 167 | "print (a != b)\n", 168 | "print (a > b)\n", 169 | "print (a < b)\n", 170 | "print (a >= b)\n", 171 | "print (a <= b)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "markdown", 176 | "metadata": {}, 177 | "source": [ 178 | "### Concept [3] Command: if ~ else" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 3, 184 | "metadata": { 185 | "collapsed": false 186 | }, 187 | "outputs": [ 188 | { 189 | "data": { 190 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAUwAAAFECAQAAADmNM+5AAAdzUlEQVR42u3deXxU5dXA8d9kISGE\nTdlVBGRTUREpLogiVXGhKNYNqcvrjruoiGirVq1VXOqGVqtoKdCKItQqCoqiCCIom2UHpYBIZAuB\n7Jnz/pHJZJLMTGa9z3Mz5zufj14mdznPzcmdO/c+z7mglFJKKaWUm93I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nyTRXGuqvp4mwlo6a3wNDHDK4VC32S83YKqd3UZQInpptlNEzO8/Orn\nifaOc1XViV0GZQGf7CoITczwfoRNvskece6sDv6pEmGD6YbZThMzvDWwzf+PlnGtaqDv/0sQD2tM\nN8x2mpjh7WFVvlRVexkQx4o8jPJNTQCYb7phyu1eTMq98r6mm6Xcrkbvol4xJ+YL2rtIJVSN/pix\nHjO1P6ZKvLHIJf7EOiemHuxfaA92lXAtyE/omJ8JphukGoo/1Bwl2SSqtPyNjpJUSVJnXHmbiLsH\n1xlX/ojpxqiG5OQEVeJYqpU4VGKNqV276G/SNOyxspf8GDD377V2kUqSKKq9Dag1n1Z7U8mTzlvB\n62P+Q66RgdJHjpfBcq/Wx1SO8/BnrSis7HQ+O5HG8kwESblXztEa7MoxrXiDCqR52KdWzKt+asW1\n2odLOeVIJlGGeLwtZaDcJe/KStkt/5Nv5FW5So6U7MqvQXncS1PToapUE+7JaPuYxnk0Mh2ie2n3\nq3i1YgBH0J0DyMHLXn5mFUtZqM+SVEop5Yz/BzGPVGKvNdbDAAAAAElFTkSuQmCC\n", 191 | "text/plain": [ 192 | "" 193 | ] 194 | }, 195 | "execution_count": 3, 196 | "metadata": {}, 197 | "output_type": "execute_result" 198 | } 199 | ], 200 | "source": [ 201 | "# Library for image output (If-Else-Diagram by WIKIMEDIA COMMONS)\n", 202 | "from IPython.display import Image \n", 203 | "Image(filename=\"image/Lab01-5_2_IfElseDiagram.png\")" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "We can use the if~else command to execute different command depending on the condition.\n", 211 | "\n", 212 | "The if~else command should look something like this:\n", 213 | "\n", 214 | ">if condition(true or false):\n", 215 | "\n", 216 | ">    Commands to be executed when the condition is true\n", 217 | "\n", 218 | ">else:\n", 219 | "\n", 220 | ">    Commands to be executed when the condition is false\n", 221 | "\n", 222 | "Ex3) We want to print \"odd number\" if the input number is odd, and print \"even number\" if it is even.\n", 223 | "\n", 224 | " (Hint: In order to know which number is even or odd, you can see the remainder by dividing by 2.\n", 225 | "\n", 226 | " If the remainder divided by 2 is 0, it is even.)" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 5, 232 | "metadata": { 233 | "collapsed": false 234 | }, 235 | "outputs": [ 236 | { 237 | "name": "stdout", 238 | "output_type": "stream", 239 | "text": [ 240 | "8\n", 241 | "even number\n" 242 | ] 243 | } 244 | ], 245 | "source": [ 246 | "# Python 2 \n", 247 | "n = int(raw_input())\n", 248 | "if n % 2 == 0:\n", 249 | " print \"even number\"\n", 250 | "else:\n", 251 | " print \"odd number\"" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 6, 257 | "metadata": { 258 | "collapsed": false 259 | }, 260 | "outputs": [ 261 | { 262 | "name": "stdout", 263 | "output_type": "stream", 264 | "text": [ 265 | "9\n", 266 | "odd number\n" 267 | ] 268 | } 269 | ], 270 | "source": [ 271 | "# Python 3\n", 272 | "n = int(input())\n", 273 | "if n % 2 == 0:\n", 274 | " print \"even number\"\n", 275 | "else:\n", 276 | " print \"odd number\"" 277 | ] 278 | }, 279 | { 280 | "cell_type": "markdown", 281 | "metadata": {}, 282 | "source": [ 283 | "### Concept [4] Command: if ~ elif ~ else" 284 | ] 285 | }, 286 | { 287 | "cell_type": "markdown", 288 | "metadata": {}, 289 | "source": [ 290 | "If there are more than 2 cases to divide by condition, you can use if~elif~else command as follows.\n", 291 | "\n", 292 | ">if Condition (1):\n", 293 | "\n", 294 | "> Command executed when condition (1) is true\n", 295 | "\n", 296 | ">elif condition (2):\n", 297 | "\n", 298 | "> Command executed when condition (1) is false and condition (2) is true\n", 299 | "\n", 300 | ">elif condition (3):\n", 301 | "\n", 302 | "> Command executed when condition (1) and condition (2) are false and condition (3) is true\n", 303 | "\n", 304 | ">...\n", 305 | "\n", 306 | ">else:\n", 307 | "\n", 308 | "> Command executed if any of the preceding conditions are not true\n", 309 | "\n", 310 | "Ex4) We want to output different results according to the answer to the question, \n", 311 | "\n", 312 | "\"How many times a week do you exercise?\".\n", 313 | "> 0~2: Cheer up!\n", 314 | "\n", 315 | "> 3~4: Oh, you are doing well. \n", 316 | "\n", 317 | "> 5~7: Wow, you like exercise!\n", 318 | "\n", 319 | "> 8 or more: Hey, calm down~" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 7, 325 | "metadata": { 326 | "collapsed": false 327 | }, 328 | "outputs": [ 329 | { 330 | "name": "stdout", 331 | "output_type": "stream", 332 | "text": [ 333 | "How many times a week do you exercise? 1\n", 334 | "Cheer up!\n" 335 | ] 336 | } 337 | ], 338 | "source": [ 339 | "# Python 3\n", 340 | "n = int(input(\"How many times a week do you exercise? \"))\n", 341 | "if 0 <= n <= 2:\n", 342 | " print(\"Cheer up!\")\n", 343 | "elif 3 <= n <= 4:\n", 344 | " print(\"Oh, you are doing well.\")\n", 345 | "elif 5 <= n <= 7:\n", 346 | " print(\"Wow, you like exercise!\")\n", 347 | "else:\n", 348 | " print(\"Hey, calm down~\")" 349 | ] 350 | }, 351 | { 352 | "cell_type": "markdown", 353 | "metadata": {}, 354 | "source": [ 355 | "## 3. Practice Problem" 356 | ] 357 | }, 358 | { 359 | "cell_type": "markdown", 360 | "metadata": {}, 361 | "source": [ 362 | "Read input from a person and print the different result based on the input data.\n", 363 | "\n", 364 | ">Input: positive integer, n\n", 365 | "\n", 366 | ">Output: Print \"OK\" if the number is OK; otherwise, print \"Try again!\" \n", 367 | "\n", 368 | ">>1) If n is a multiple of 3, print \"OK\"\n", 369 | "\n", 370 | ">>2) If n is not a multiple of 3 and in the inclusive range of 1 to 10, print \"Try again!\"\n", 371 | "\n", 372 | ">>3) If n is not a multiple of 3 and in the inclusive range of 11 to 50, print \"OK\"\n", 373 | "\n", 374 | ">>4) else, print \"Try again!\"" 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": 8, 380 | "metadata": { 381 | "collapsed": false 382 | }, 383 | "outputs": [ 384 | { 385 | "name": "stdout", 386 | "output_type": "stream", 387 | "text": [ 388 | "10\n", 389 | "Try again!\n" 390 | ] 391 | } 392 | ], 393 | "source": [ 394 | "# Python 2 \n", 395 | "n = int(raw_input())\n", 396 | "if n % 3 == 0:\n", 397 | " print \"OK\"\n", 398 | "elif n % 3 != 0 and 1 <= n <= 10:\n", 399 | " print \"Try again!\"\n", 400 | "elif n % 3 != 0 and 11 <= n <= 50:\n", 401 | " print \"OK\"\n", 402 | "else:\n", 403 | " print \"Try again!\"" 404 | ] 405 | }, 406 | { 407 | "cell_type": "code", 408 | "execution_count": 9, 409 | "metadata": { 410 | "collapsed": false 411 | }, 412 | "outputs": [ 413 | { 414 | "name": "stdout", 415 | "output_type": "stream", 416 | "text": [ 417 | "66\n", 418 | "OK\n" 419 | ] 420 | } 421 | ], 422 | "source": [ 423 | "# Python 3\n", 424 | "n = int(input())\n", 425 | "if n % 3 == 0:\n", 426 | " print \"OK\"\n", 427 | "elif n % 3 != 0 and 1 <= n <= 10:\n", 428 | " print \"Try again!\"\n", 429 | "elif n % 3 != 0 and 11 <= n <= 50:\n", 430 | " print \"OK\"\n", 431 | "else:\n", 432 | " print \"Try again!\"" 433 | ] 434 | }, 435 | { 436 | "cell_type": "markdown", 437 | "metadata": {}, 438 | "source": [ 439 | "## 4. Are you ready? \n", 440 | "### DIY at https://www.hackerrank.com/challenges/py-if-else" 441 | ] 442 | } 443 | ], 444 | "metadata": { 445 | "anaconda-cloud": {}, 446 | "kernelspec": { 447 | "display_name": "Python [default]", 448 | "language": "python", 449 | "name": "python2" 450 | }, 451 | "language_info": { 452 | "codemirror_mode": { 453 | "name": "ipython", 454 | "version": 2 455 | }, 456 | "file_extension": ".py", 457 | "mimetype": "text/x-python", 458 | "name": "python", 459 | "nbconvert_exporter": "python", 460 | "pygments_lexer": "ipython2", 461 | "version": "2.7.12" 462 | } 463 | }, 464 | "nbformat": 4, 465 | "nbformat_minor": 1 466 | } 467 | -------------------------------------------------------------------------------- /Lab01-6_Write-a-Function.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab01-6: Write a function\n", 8 | "https://www.hackerrank.com/challenges/write-a-function" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "## 1. Description of Problem" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "If we want to write a function, how do we say with Python?\n", 23 | "\n", 24 | "This problem is to write a function to check if the given year is leap or not.\n", 25 | "\n", 26 | ">Input: year (1900 ≤ year ≤ 100000)\n", 27 | "\n", 28 | ">Output: boolean value (True / False)" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "## 2. Concept & Short Examples" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "### Concept [1] Function" 43 | ] 44 | }, 45 | { 46 | "cell_type": "code", 47 | "execution_count": 8, 48 | "metadata": { 49 | "collapsed": false 50 | }, 51 | "outputs": [ 52 | { 53 | "data": { 54 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAL8AAAC9CAYAAADiIqwNAAAABmJLR0QA/wD/AP+gvaeTAAAOrklE\nQVR4nO3deZAU5RnH8e+u3CAYNawhWi6YlOaPgMFUkWhFBTHxqjJGUYgmJkVFJYIHEUWjVSomRrzQ\nSIFX4hWvSllWaWKVUZCIZwURj8KIHIpFVo7IKQsCmz+ensw7vT09Pbs9R8/7+1RNzWz322+/PfvM\nO++83f2+ICIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIpIxk4EO4LCIZZ8B/UPpW4N1F0Skzz22AIuD\nNHs56e4DdhUpx9hg2zOAAaH8ij3eS3qQ0lh6VGEfg4GLgBsTpp8EfAoMAiYAc7APy/Qy97sdGBda\ndi+wDJjpLNtUZr4iBeJq/ueBDVgw57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0rm5U+n9J9ETu7YoSgfWi3MidkPOi0XSfUkXrklSmz8dHdgMhkdTfISz\nXFfhPGfZWizwhkSkz9VkbRHrknoL+6G5hOh2fqX3n8Q6rCs2KhYPAO7AjmMEcHGRPPYN8imLgj89\nt2C3D86m8/s6DLgUa5u6J3LmB88TQumbsDuxNtL9WxDj1Hr/YO9JM9YdHC7Dg1hzaCzWBLoJ6zEL\nO5QudndKei4nP0zIecBPgGuxHqD/AN+K2Obv2I+2OcBPgV9hX+8dxLfPy+3nL6YS+49ybJBn+Gb6\nFqz78pzQ8t8E5Tom+LsX9g3wPoUTmLcG+WpEiTpwHBZQG7BaawX21V3sqsne2AfkgyD9VuzDc2aJ\n/aQV/JXYf5RjiQ5+sG/Dp5y/RwZluSGU7lCsG3SOs2wq1uTpjUgG/QgL9pYubLsE+H26xRGprn9g\nZ8HL8WPyP5hFMuvrWJOxHIcT/QNYRERERERERERERERERERERERERERERERERETqwv8A0j9R0ntp\nPYIAAAAASUVORK5CYII=\n", 55 | "text/plain": [ 56 | "" 57 | ] 58 | }, 59 | "execution_count": 8, 60 | "metadata": {}, 61 | "output_type": "execute_result" 62 | } 63 | ], 64 | "source": [ 65 | "# Library for image output (Lab01-6_1_Function by WIKIPEDIA)\n", 66 | "from IPython.display import Image \n", 67 | "Image(filename=\"image/Lab01-6_1_Function.png\")" 68 | ] 69 | }, 70 | { 71 | "cell_type": "markdown", 72 | "metadata": {}, 73 | "source": [ 74 | "![image](https://cloud.githubusercontent.com/assets/26020722/24488780/44312eb4-1554-11e7-92fc-fdc12c820a96.png)\n", 75 | "\n", 76 | "In mathematics, a function is a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output.\n", 77 | "\n", 78 | "> #### Example: Function that relates each real number x to its square x*x. \n", 79 | "\n", 80 | "> The output of a function f corresponding to an input x is denoted by f(x) (read \"f of x\").\n", 81 | "\n", 82 | "> If the input is −3, then the output is 9, and we may write f(−3) = 9. \n", 83 | "\n", 84 | "> Likewise, if the input is 3, then the output is also 9, and we may write f(3) = 9. \n", 85 | "\n", 86 | "> (The same output may be produced by more than one input, but each input gives only one output.) \n", 87 | "\n", 88 | "In Python, you can use the keyword \"def\" to write a function like this:\n", 89 | "\n", 90 | "(The keyword \"def\" was created from \"define\".)\n", 91 | "\n", 92 | ">def function name(input variable name):\n", 93 | "\n", 94 | "> Commands\n", 95 | "\n", 96 | "> return output variable name\n", 97 | "\n", 98 | "Ex1) We want to write the square function of the example above." 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 9, 104 | "metadata": { 105 | "collapsed": true 106 | }, 107 | "outputs": [], 108 | "source": [ 109 | "def square(x):\n", 110 | " y = x * x\n", 111 | " return y" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "Ex2) To use this square function, we put the function name followed by the input value in parentheses." 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 10, 124 | "metadata": { 125 | "collapsed": false 126 | }, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "9\n" 133 | ] 134 | } 135 | ], 136 | "source": [ 137 | "print(square(-3))" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": 11, 143 | "metadata": { 144 | "collapsed": false 145 | }, 146 | "outputs": [ 147 | { 148 | "name": "stdout", 149 | "output_type": "stream", 150 | "text": [ 151 | "9\n" 152 | ] 153 | } 154 | ], 155 | "source": [ 156 | "print(square(3))" 157 | ] 158 | }, 159 | { 160 | "cell_type": "markdown", 161 | "metadata": {}, 162 | "source": [ 163 | "Function does not need to have input or output, as follows.\n", 164 | "\n", 165 | ">def function name( ):\n", 166 | "\n", 167 | "> Commands\n", 168 | "\n", 169 | "Ex3) We want to write a function that prints \"Nice to meet you.\" with no input." 170 | ] 171 | }, 172 | { 173 | "cell_type": "code", 174 | "execution_count": 26, 175 | "metadata": { 176 | "collapsed": false 177 | }, 178 | "outputs": [ 179 | { 180 | "name": "stdout", 181 | "output_type": "stream", 182 | "text": [ 183 | "Nice to meet you.\n" 184 | ] 185 | } 186 | ], 187 | "source": [ 188 | "# no input\n", 189 | "def message_1():\n", 190 | " return(\"Nice to meet you.\")\n", 191 | "m = message_1()\n", 192 | "print(m)" 193 | ] 194 | }, 195 | { 196 | "cell_type": "code", 197 | "execution_count": 27, 198 | "metadata": { 199 | "collapsed": false 200 | }, 201 | "outputs": [ 202 | { 203 | "name": "stdout", 204 | "output_type": "stream", 205 | "text": [ 206 | "Nice to meet you.\n" 207 | ] 208 | } 209 | ], 210 | "source": [ 211 | "# no input, no output\n", 212 | "def message_2():\n", 213 | " print(\"Nice to meet you.\")\n", 214 | "message_2()" 215 | ] 216 | }, 217 | { 218 | "cell_type": "markdown", 219 | "metadata": {}, 220 | "source": [ 221 | "### Concept [2] Function with multiple inputs" 222 | ] 223 | }, 224 | { 225 | "cell_type": "markdown", 226 | "metadata": {}, 227 | "source": [ 228 | "Input of function may be multiple, not just one.\n", 229 | "\n", 230 | "If there are multiple input variables, we can define the function using comma like this.\n", 231 | "\n", 232 | ">def function name(input variable name 1, input variable name 2, input variable name 3, ...):\n", 233 | "\n", 234 | "> Commands\n", 235 | "\n", 236 | "> return output variable name\n", 237 | "\n", 238 | "We can make inputs as many as we want.\n", 239 | "\n", 240 | "Ex4) We want to write function of sum from two inputs and three inputs." 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 12, 246 | "metadata": { 247 | "collapsed": false 248 | }, 249 | "outputs": [ 250 | { 251 | "name": "stdout", 252 | "output_type": "stream", 253 | "text": [ 254 | "7\n" 255 | ] 256 | } 257 | ], 258 | "source": [ 259 | "def sum(x, y):\n", 260 | " result = x + y \n", 261 | " return result\n", 262 | "print(sum(3, 4))" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": 13, 268 | "metadata": { 269 | "collapsed": false 270 | }, 271 | "outputs": [ 272 | { 273 | "name": "stdout", 274 | "output_type": "stream", 275 | "text": [ 276 | "12\n" 277 | ] 278 | } 279 | ], 280 | "source": [ 281 | "def sum(x, y, z):\n", 282 | " result = x + y + z \n", 283 | " return result\n", 284 | "print(sum(3, 4, 5))" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": {}, 290 | "source": [ 291 | "## 3. Practice Problem" 292 | ] 293 | }, 294 | { 295 | "cell_type": "markdown", 296 | "metadata": {}, 297 | "source": [ 298 | "Write a function to check whether the given number is a multiple of 2 or 3 or 5.\n", 299 | "\n", 300 | ">Input: positve integer, n\n", 301 | "\n", 302 | ">Output: boolean value (True / False)\n", 303 | "\n", 304 | ">>True: the number is a multiple of 2 or a multiple of 3 or a multiple of 5\n", 305 | "\n", 306 | ">>False: else " 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 14, 312 | "metadata": { 313 | "collapsed": false 314 | }, 315 | "outputs": [ 316 | { 317 | "name": "stdout", 318 | "output_type": "stream", 319 | "text": [ 320 | "29\n", 321 | "False\n" 322 | ] 323 | } 324 | ], 325 | "source": [ 326 | "def multiple(n):\n", 327 | " if n % 2 == 0:\n", 328 | " return True\n", 329 | " if n % 3 == 0:\n", 330 | " return True\n", 331 | " if n % 5 == 0:\n", 332 | " return True\n", 333 | " return False\n", 334 | "\n", 335 | "print multiple(input())" 336 | ] 337 | }, 338 | { 339 | "cell_type": "code", 340 | "execution_count": 15, 341 | "metadata": { 342 | "collapsed": false 343 | }, 344 | "outputs": [ 345 | { 346 | "name": "stdout", 347 | "output_type": "stream", 348 | "text": [ 349 | "10\n", 350 | "True\n" 351 | ] 352 | } 353 | ], 354 | "source": [ 355 | "def multiple(n):\n", 356 | " result = False\n", 357 | " if n % 2 == 0:\n", 358 | " result = True\n", 359 | " if n % 3 == 0:\n", 360 | " result = True\n", 361 | " if n % 5 == 0:\n", 362 | " result = True\n", 363 | " return result\n", 364 | "\n", 365 | "print multiple(input())" 366 | ] 367 | }, 368 | { 369 | "cell_type": "markdown", 370 | "metadata": {}, 371 | "source": [ 372 | "## 4. Are you ready? \n", 373 | "### DIY at https://www.hackerrank.com/challenges/write-a-function" 374 | ] 375 | } 376 | ], 377 | "metadata": { 378 | "anaconda-cloud": {}, 379 | "kernelspec": { 380 | "display_name": "Python [default]", 381 | "language": "python", 382 | "name": "python2" 383 | }, 384 | "language_info": { 385 | "codemirror_mode": { 386 | "name": "ipython", 387 | "version": 2 388 | }, 389 | "file_extension": ".py", 390 | "mimetype": "text/x-python", 391 | "name": "python", 392 | "nbconvert_exporter": "python", 393 | "pygments_lexer": "ipython2", 394 | "version": "2.7.12" 395 | } 396 | }, 397 | "nbformat": 4, 398 | "nbformat_minor": 1 399 | } 400 | -------------------------------------------------------------------------------- /Lab01-8_Print-Function.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab01-8: Print Function\n", 8 | "https://www.hackerrank.com/challenges/python-print" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "## 1. Description of Problem" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "If we want to print something without using any string methods, how do we say in Python?\n", 23 | "\n", 24 | "This problem is to print the following using print function, for the input integer N.\n", 25 | "\n", 26 | ">Input: integer N\n", 27 | "\n", 28 | ">> (ex. 7)\n", 29 | "\n", 30 | ">Output: 123...N (\"...\" represents the values in between)\n", 31 | "\n", 32 | ">> (ex. 1234567)" 33 | ] 34 | }, 35 | { 36 | "cell_type": "markdown", 37 | "metadata": {}, 38 | "source": [ 39 | "## 2. Concept & Short Examples" 40 | ] 41 | }, 42 | { 43 | "cell_type": "markdown", 44 | "metadata": {}, 45 | "source": [ 46 | "### Concept [1] Types of Functions" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": { 52 | "collapsed": false 53 | }, 54 | "source": [ 55 | "Basically, we can divide functions into the following two types:\n", 56 | "\n", 57 | "> Built-in functions: functions that are built into Python.\n", 58 | "\n", 59 | "> User-defined functions: functions defined by the users themselves.\n", 60 | "\n", 61 | "Functions like print(), input(), eval() etc. that we have been using, are some examples of the built-in function.\n", 62 | "\n", 63 | "There are 68 built-in functions defined in Python 3.5.2.\n", 64 | "\n", 65 | "They are listed on the following site alphabetically along with a brief description.\n", 66 | "\n", 67 | "https://www.programiz.com/python-programming/built-in-function\n", 68 | "\n", 69 | "Ex1) Let's use a few built-in functions." 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 80, 75 | "metadata": { 76 | "collapsed": false 77 | }, 78 | "outputs": [ 79 | { 80 | "data": { 81 | "text/plain": [ 82 | "10" 83 | ] 84 | }, 85 | "execution_count": 80, 86 | "metadata": {}, 87 | "output_type": "execute_result" 88 | } 89 | ], 90 | "source": [ 91 | "# abs(): Return the absolute value of a number.\n", 92 | "abs(-10)" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 81, 98 | "metadata": { 99 | "collapsed": false 100 | }, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "1000" 106 | ] 107 | }, 108 | "execution_count": 81, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "# max(): Return the largest item in an iterable.\n", 115 | "max(0, -2, 4, 1000, -200)" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 82, 121 | "metadata": { 122 | "collapsed": false 123 | }, 124 | "outputs": [ 125 | { 126 | "data": { 127 | "text/plain": [ 128 | "-200" 129 | ] 130 | }, 131 | "execution_count": 82, 132 | "metadata": {}, 133 | "output_type": "execute_result" 134 | } 135 | ], 136 | "source": [ 137 | "# min(): Return the smallest item in an iterable.\n", 138 | "min(0, -2, 4, 1000, -200)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 83, 144 | "metadata": { 145 | "collapsed": false 146 | }, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "int" 152 | ] 153 | }, 154 | "execution_count": 83, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "# type(): Return the type of an object.\n", 161 | "type(-200)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 84, 167 | "metadata": { 168 | "collapsed": false 169 | }, 170 | "outputs": [ 171 | { 172 | "data": { 173 | "text/plain": [ 174 | "float" 175 | ] 176 | }, 177 | "execution_count": 84, 178 | "metadata": {}, 179 | "output_type": "execute_result" 180 | } 181 | ], 182 | "source": [ 183 | "type(3.2)" 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "execution_count": 85, 189 | "metadata": { 190 | "collapsed": false 191 | }, 192 | "outputs": [ 193 | { 194 | "data": { 195 | "text/plain": [ 196 | "str" 197 | ] 198 | }, 199 | "execution_count": 85, 200 | "metadata": {}, 201 | "output_type": "execute_result" 202 | } 203 | ], 204 | "source": [ 205 | "type(\"hello\")" 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 86, 211 | "metadata": { 212 | "collapsed": false 213 | }, 214 | "outputs": [ 215 | { 216 | "data": { 217 | "text/plain": [ 218 | "bool" 219 | ] 220 | }, 221 | "execution_count": 86, 222 | "metadata": {}, 223 | "output_type": "execute_result" 224 | } 225 | ], 226 | "source": [ 227 | "type(False)" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": {}, 233 | "source": [ 234 | "### Concept [2] Function with inputs; how to set the number of inputs freely" 235 | ] 236 | }, 237 | { 238 | "cell_type": "markdown", 239 | "metadata": {}, 240 | "source": [ 241 | "When we define function, we use * as below, if we want to set the number of inputs freely.\n", 242 | "\n", 243 | ">def function name(*input variable name):\n", 244 | "\n", 245 | "> Commands\n", 246 | "\n", 247 | "> return output variable name\n", 248 | "\n", 249 | "Ex2) We want to write function of sum from any number of inputs." 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": 87, 255 | "metadata": { 256 | "collapsed": false 257 | }, 258 | "outputs": [ 259 | { 260 | "name": "stdout", 261 | "output_type": "stream", 262 | "text": [ 263 | "-63\n" 264 | ] 265 | } 266 | ], 267 | "source": [ 268 | "def anysum(*x):\n", 269 | " sum = 0\n", 270 | " for num in x:\n", 271 | " sum += num \n", 272 | " return sum\n", 273 | "\n", 274 | "y = anysum(10, 20, 3, 4, -100)\n", 275 | "print(y)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "### Concept [3] print( ) function" 283 | ] 284 | }, 285 | { 286 | "cell_type": "markdown", 287 | "metadata": {}, 288 | "source": [ 289 | "In Python 3, print is a function, not a statement.\n", 290 | "\n", 291 | "To make it easier to convert your programs to the new syntax, first use this import statement (introduced in Python 2.6): \n", 292 | "\n", 293 | "from __future__ import print_function\n", 294 | "\n", 295 | "Here is the interface to this function: \n", 296 | "\n", 297 | "> print(*args, sep=\" \", end=\"\\n\", file=None)\n", 298 | "\n", 299 | ">> args: One or more positional arguments whose values are to be printed.\n", 300 | "\n", 301 | ">> sep: By default, consecutive values are separated by one space. You may specify a different separator string using this keyword argument. (ex. if you want to use tab, (sep = \"\\t\"))\n", 302 | "\n", 303 | ">> end: By default, a newline (\"\\n\") is written after the last value in args. You may use this keywoard argument to specify a different line terminator, or no terminator at all. \n", 304 | "\n", 305 | ">> file: Output normally goes to the standard output stream (sys.stdout). To divert the output to another writeable file, use this keyword argument.\n", 306 | "\n", 307 | "reference: https://infohost.nmt.edu/tcc/help/pubs/python/web/print-as-function.html\n", 308 | "\n", 309 | "Ex3) Let's use print( ) function." 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": 88, 315 | "metadata": { 316 | "collapsed": false 317 | }, 318 | "outputs": [ 319 | { 320 | "name": "stdout", 321 | "output_type": "stream", 322 | "text": [ 323 | "1 2 3\n", 324 | "1 2 3\n", 325 | " \n", 326 | "1\t2\t3\n", 327 | "123456789\n", 328 | "1 2 3 4 5 6 7 8 9\n", 329 | "1\t2\t3\t4\t5\t6\t7\t8\t9\n" 330 | ] 331 | } 332 | ], 333 | "source": [ 334 | "from __future__ import print_function\n", 335 | "print(1,2,3)\n", 336 | "print(1,2,3, end=\"\\n \\n\")\n", 337 | "print(1,2,3, sep=\"\\t\")\n", 338 | "print(*range(1, 10), sep=\"\")\n", 339 | "print(*range(1, 10), sep=\" \")\n", 340 | "print(*range(1, 10), sep=\"\\t\")" 341 | ] 342 | }, 343 | { 344 | "cell_type": "markdown", 345 | "metadata": {}, 346 | "source": [ 347 | "## 3. Practice Problem" 348 | ] 349 | }, 350 | { 351 | "cell_type": "markdown", 352 | "metadata": {}, 353 | "source": [ 354 | "print the following using print function, for the input integer N.\n", 355 | "\n", 356 | ">Input: integer N\n", 357 | "\n", 358 | ">> (ex. 7)\n", 359 | "\n", 360 | ">Output: 1 2 3 4 . . . 2*N (\". . .\" represents the values in between)\n", 361 | "\n", 362 | ">> (ex. 1 2 3 4 5 6 7 8 9 10 11 12 13 14)" 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": 89, 368 | "metadata": { 369 | "collapsed": false 370 | }, 371 | "outputs": [ 372 | { 373 | "name": "stdout", 374 | "output_type": "stream", 375 | "text": [ 376 | "7\n", 377 | "1 2 3 4 5 6 7 8 9 10 11 12 13 14\n" 378 | ] 379 | } 380 | ], 381 | "source": [ 382 | "# Python 2\n", 383 | "from __future__ import print_function\n", 384 | "print(*range(1, input()*2 +1 )) " 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 90, 390 | "metadata": { 391 | "collapsed": false 392 | }, 393 | "outputs": [ 394 | { 395 | "name": "stdout", 396 | "output_type": "stream", 397 | "text": [ 398 | "10\n", 399 | "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20\n" 400 | ] 401 | } 402 | ], 403 | "source": [ 404 | "# Python 3\n", 405 | "print(*range(1, int(input())*2 + 1))" 406 | ] 407 | }, 408 | { 409 | "cell_type": "markdown", 410 | "metadata": {}, 411 | "source": [ 412 | "## 4. Are you ready? \n", 413 | "### DIY at https://www.hackerrank.com/challenges/python-print" 414 | ] 415 | } 416 | ], 417 | "metadata": { 418 | "anaconda-cloud": {}, 419 | "kernelspec": { 420 | "display_name": "Python [default]", 421 | "language": "python", 422 | "name": "python2" 423 | }, 424 | "language_info": { 425 | "codemirror_mode": { 426 | "name": "ipython", 427 | "version": 2 428 | }, 429 | "file_extension": ".py", 430 | "mimetype": "text/x-python", 431 | "name": "python", 432 | "nbconvert_exporter": "python", 433 | "pygments_lexer": "ipython2", 434 | "version": "2.7.12" 435 | } 436 | }, 437 | "nbformat": 4, 438 | "nbformat_minor": 1 439 | } 440 | -------------------------------------------------------------------------------- /Lab02----------Basic-DataTypes----------.txt: -------------------------------------------------------------------------------- 1 | Lab2 Basic DataTypes 2 | 3 | > List, Tuple, Dictionary, Set, etc. -------------------------------------------------------------------------------- /Lab02-1_Lists.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab02-1: Lists\n", 8 | "https://www.hackerrank.com/challenges/python-lists" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "## 1. Description of Problem" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "If we want to store multiple values in a container-like data structure, how do we say in Python?\n", 23 | "\n", 24 | "The goal is to solve a problem using list methods and conditionals.\n", 25 | "\n", 26 | ">Input: Integer n, denoting the number of commands.\n", 27 | "\n", 28 | "> Each line of the subsequent lines contains one of the commands described below.\n", 29 | "\n", 30 | ">> Consider a list (list = [ ]). You can perform the following commands: \n", 31 | "\n", 32 | ">> (1) insert i e: Insert integer at position.\n", 33 | "\n", 34 | ">> (2) print: Print the list.\n", 35 | "\n", 36 | ">> (3) remove e: Delete the first occurrence of integer.\n", 37 | "\n", 38 | ">> (4) append e: Insert integer at the end of the list. \n", 39 | "\n", 40 | ">> (5) sort: Sort the list.\n", 41 | "\n", 42 | ">> (6) pop: Pop the last element from the list.\n", 43 | "\n", 44 | ">> (7) reverse: Reverse the list.\n", 45 | "\n", 46 | ">Output: For each command of type print, print the list on a new line." 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "## 2. Concept & Short Examples" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "### Concept [1] How to create a list?" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "In Python programming, a list is created by placing all the items (elements) inside a square bracket [ ], separated by commas.\n", 68 | "\n", 69 | "It can have any number of items and they may be of different types (integer, float, string, etc.).\n", 70 | "\n", 71 | "Ex1) Let's make a list and print it." 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 32, 77 | "metadata": { 78 | "collapsed": false 79 | }, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "[]\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "# empty list\n", 91 | "my_list = []\n", 92 | "print my_list" 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 16, 98 | "metadata": { 99 | "collapsed": false 100 | }, 101 | "outputs": [ 102 | { 103 | "name": "stdout", 104 | "output_type": "stream", 105 | "text": [ 106 | "[1, 2, 3]\n" 107 | ] 108 | } 109 | ], 110 | "source": [ 111 | "# list of integers\n", 112 | "my_list = [1, 2, 3]\n", 113 | "print my_list" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 17, 119 | "metadata": { 120 | "collapsed": false 121 | }, 122 | "outputs": [ 123 | { 124 | "name": "stdout", 125 | "output_type": "stream", 126 | "text": [ 127 | "[1, 'Hello', 3.4]\n" 128 | ] 129 | } 130 | ], 131 | "source": [ 132 | "# list with mixed datatypes\n", 133 | "my_list = [1, \"Hello\", 3.4]\n", 134 | "print my_list" 135 | ] 136 | }, 137 | { 138 | "cell_type": "markdown", 139 | "metadata": {}, 140 | "source": [ 141 | "Also, a list can even have another list as an item. \n", 142 | "\n", 143 | "This is called nested list. (We deal with this concept in Lab-02-5.)\n", 144 | "\n", 145 | "Ex2) Let's make a nested list and print it." 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 21, 151 | "metadata": { 152 | "collapsed": false 153 | }, 154 | "outputs": [ 155 | { 156 | "name": "stdout", 157 | "output_type": "stream", 158 | "text": [ 159 | "['your list', [1, 2, 3.5], ['b', [2, 'a']]]\n" 160 | ] 161 | } 162 | ], 163 | "source": [ 164 | "# nested list\n", 165 | "my_list = [\"your list\", [1, 2, 3.5], [\"b\", [2, \"a\"]]]\n", 166 | "print my_list" 167 | ] 168 | }, 169 | { 170 | "cell_type": "markdown", 171 | "metadata": {}, 172 | "source": [ 173 | "### Concept [2] How to access elements from a list?" 174 | ] 175 | }, 176 | { 177 | "cell_type": "markdown", 178 | "metadata": {}, 179 | "source": [ 180 | "We can use the index operator [ ] to access an item in a list. \n", 181 | "\n", 182 | "Index starts from 0. So, a list having 5 elements will have index from 0 to 4.\n", 183 | "\n", 184 | "Trying to access an element other that this will raise an IndexError.\n", 185 | "\n", 186 | "The index must be an integer. We can't use float or other types, this will result into TypeError.\n", 187 | "\n", 188 | "Ex3) We want to access elements from a list using indexing." 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 34, 194 | "metadata": { 195 | "collapsed": false 196 | }, 197 | "outputs": [ 198 | { 199 | "name": "stdout", 200 | "output_type": "stream", 201 | "text": [ 202 | "b\n", 203 | "2\n", 204 | "apple\n", 205 | "z\n" 206 | ] 207 | } 208 | ], 209 | "source": [ 210 | "list_a = [\"b\", 2, \"apple\", \"z\"]\n", 211 | "\n", 212 | "# Output: b\n", 213 | "print(list_a[0])\n", 214 | "# Output: 2\n", 215 | "print(list_a[1])\n", 216 | "# Output: apple\n", 217 | "print(list_a[2])\n", 218 | "# Output: z\n", 219 | "print(list_a[3])" 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 35, 225 | "metadata": { 226 | "collapsed": false 227 | }, 228 | "outputs": [ 229 | { 230 | "ename": "IndexError", 231 | "evalue": "list index out of range", 232 | "output_type": "error", 233 | "traceback": [ 234 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 235 | "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", 236 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[1;31m# Index Error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlist_a\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 237 | "\u001b[0;31mIndexError\u001b[0m: list index out of range" 238 | ] 239 | } 240 | ], 241 | "source": [ 242 | "# Index Error\n", 243 | "print(list_a[4])" 244 | ] 245 | }, 246 | { 247 | "cell_type": "code", 248 | "execution_count": 36, 249 | "metadata": { 250 | "collapsed": false 251 | }, 252 | "outputs": [ 253 | { 254 | "ename": "TypeError", 255 | "evalue": "list indices must be integers, not float", 256 | "output_type": "error", 257 | "traceback": [ 258 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 259 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 260 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[1;31m# Type Error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlist_a\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m3.0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 261 | "\u001b[0;31mTypeError\u001b[0m: list indices must be integers, not float" 262 | ] 263 | } 264 | ], 265 | "source": [ 266 | "# Type Error\n", 267 | "print(list_a[3.0])" 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "Python allows negative indexing for its sequences.\n", 275 | "\n", 276 | "The index of -1 refers to the last item, -2 to the second last item and so on.\n", 277 | "\n", 278 | "Ex4) We want to access elements from a list using negative indexing." 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": 45, 284 | "metadata": { 285 | "collapsed": false 286 | }, 287 | "outputs": [ 288 | { 289 | "name": "stdout", 290 | "output_type": "stream", 291 | "text": [ 292 | "l\n", 293 | "f\n", 294 | "b\n" 295 | ] 296 | } 297 | ], 298 | "source": [ 299 | "list_b = [\"b\", \"e\", \"a\", \"u\", \"t\", \"i\", \"f\", \"u\", \"l\"]\n", 300 | "\n", 301 | "# Output: l\n", 302 | "print(list_b[-1])\n", 303 | "# Output: f\n", 304 | "print(list_b[-3])\n", 305 | "# Output: b\n", 306 | "print(list_b[-9])" 307 | ] 308 | }, 309 | { 310 | "cell_type": "markdown", 311 | "metadata": {}, 312 | "source": [ 313 | "Ex5) We can access a range of items in a list by using the slicing operator (colon)." 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 46, 319 | "metadata": { 320 | "collapsed": false 321 | }, 322 | "outputs": [ 323 | { 324 | "name": "stdout", 325 | "output_type": "stream", 326 | "text": [ 327 | "['a', 'u', 't']\n", 328 | "['b', 'e', 'a', 'u']\n", 329 | "['i', 'f', 'u', 'l']\n", 330 | "['b', 'e', 'a', 'u', 't', 'i', 'f', 'u', 'l']\n" 331 | ] 332 | } 333 | ], 334 | "source": [ 335 | "list_b = [\"b\", \"e\", \"a\", \"u\", \"t\", \"i\", \"f\", \"u\", \"l\"]\n", 336 | "\n", 337 | "# elements 3rd to 5th\n", 338 | "print(list_b[2:5])\n", 339 | "# elements beginning to 4th\n", 340 | "print(list_b[:-5])\n", 341 | "# elements 6th to end\n", 342 | "print(list_b[5:])\n", 343 | "# elements beginning to end\n", 344 | "print(list_b[:])" 345 | ] 346 | }, 347 | { 348 | "cell_type": "markdown", 349 | "metadata": {}, 350 | "source": [ 351 | "### Concept [3] Python List Methods" 352 | ] 353 | }, 354 | { 355 | "cell_type": "markdown", 356 | "metadata": {}, 357 | "source": [ 358 | "Methods are available with list object in Python programming.\n", 359 | "\n", 360 | "They are accessed as list.method().\n", 361 | "\n", 362 | "Ex6) Let's look at some of the methods we can use on list." 363 | ] 364 | }, 365 | { 366 | "cell_type": "code", 367 | "execution_count": 47, 368 | "metadata": { 369 | "collapsed": false 370 | }, 371 | "outputs": [ 372 | { 373 | "name": "stdout", 374 | "output_type": "stream", 375 | "text": [ 376 | "[1, 2, 3, 9]\n" 377 | ] 378 | } 379 | ], 380 | "source": [ 381 | "# append(x): Adds a single element x to the end of a list.\n", 382 | "arr = [1, 2, 3]\n", 383 | "arr.append(9) \n", 384 | "print arr \n", 385 | "# prints [1, 2, 3, 9]" 386 | ] 387 | }, 388 | { 389 | "cell_type": "code", 390 | "execution_count": 48, 391 | "metadata": { 392 | "collapsed": false 393 | }, 394 | "outputs": [ 395 | { 396 | "name": "stdout", 397 | "output_type": "stream", 398 | "text": [ 399 | "[1, 2, 3, 10, 11]\n" 400 | ] 401 | } 402 | ], 403 | "source": [ 404 | "# extend(L): Merges another list L to the end.\n", 405 | "arr = [1, 2, 3]\n", 406 | "arr.extend([10,11])\n", 407 | "print arr\n", 408 | "# prints [1, 2, 3, 10, 11]" 409 | ] 410 | }, 411 | { 412 | "cell_type": "code", 413 | "execution_count": 50, 414 | "metadata": { 415 | "collapsed": false 416 | }, 417 | "outputs": [ 418 | { 419 | "name": "stdout", 420 | "output_type": "stream", 421 | "text": [ 422 | "[1, 2, 7, 3]\n" 423 | ] 424 | } 425 | ], 426 | "source": [ 427 | "# insert(i, x): Inserts element x at position i.\n", 428 | "arr = [1, 2, 3]\n", 429 | "arr.insert(2,7)\n", 430 | "print arr\n", 431 | "# prints [1, 2, 7, 3]" 432 | ] 433 | }, 434 | { 435 | "cell_type": "code", 436 | "execution_count": 56, 437 | "metadata": { 438 | "collapsed": false 439 | }, 440 | "outputs": [ 441 | { 442 | "name": "stdout", 443 | "output_type": "stream", 444 | "text": [ 445 | "[2, 3]\n" 446 | ] 447 | } 448 | ], 449 | "source": [ 450 | "# remove(x): Removes the first occurrence of element x.\n", 451 | "arr = [1, 2, 3]\n", 452 | "arr.remove(1)\n", 453 | "print arr\n", 454 | "# prints [2, 3]" 455 | ] 456 | }, 457 | { 458 | "cell_type": "code", 459 | "execution_count": 58, 460 | "metadata": { 461 | "collapsed": false 462 | }, 463 | "outputs": [ 464 | { 465 | "name": "stdout", 466 | "output_type": "stream", 467 | "text": [ 468 | "[1, 2]\n" 469 | ] 470 | } 471 | ], 472 | "source": [ 473 | "# pop(): Removes the last element of a list.\n", 474 | "# If an argument is passed, that index item is popped out. \n", 475 | "arr = [1, 2, 3]\n", 476 | "arr.pop()\n", 477 | "print arr\n", 478 | "# prints [1, 2]" 479 | ] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": 69, 484 | "metadata": { 485 | "collapsed": false 486 | }, 487 | "outputs": [ 488 | { 489 | "name": "stdout", 490 | "output_type": "stream", 491 | "text": [ 492 | "0\n" 493 | ] 494 | } 495 | ], 496 | "source": [ 497 | "# index(x): Returns the first index of a value in the list. \n", 498 | "# Throws an error if it's not found. \n", 499 | "arr = [1, 2, 3]\n", 500 | "index_of_item = arr.index(1)\n", 501 | "print index_of_item \n", 502 | "# prints 0" 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": 72, 508 | "metadata": { 509 | "collapsed": false 510 | }, 511 | "outputs": [ 512 | { 513 | "name": "stdout", 514 | "output_type": "stream", 515 | "text": [ 516 | "4\n" 517 | ] 518 | } 519 | ], 520 | "source": [ 521 | "# count(x): Counts the number of occurrences of an element x.\n", 522 | "arr = [1, 2, 3, 1, 1, 1]\n", 523 | "cnt_of_item = arr.count(1)\n", 524 | "print cnt_of_item \n", 525 | "# prints 4" 526 | ] 527 | }, 528 | { 529 | "cell_type": "code", 530 | "execution_count": 74, 531 | "metadata": { 532 | "collapsed": false 533 | }, 534 | "outputs": [ 535 | { 536 | "name": "stdout", 537 | "output_type": "stream", 538 | "text": [ 539 | "[1, 1, 1, 1, 2, 3]\n" 540 | ] 541 | } 542 | ], 543 | "source": [ 544 | "# sort(): Sorts the list.\n", 545 | "arr = [1, 2, 3, 1, 1, 1]\n", 546 | "arr.sort()\n", 547 | "print arr \n", 548 | "# prints [1, 1, 1, 1, 2, 3]" 549 | ] 550 | }, 551 | { 552 | "cell_type": "code", 553 | "execution_count": 76, 554 | "metadata": { 555 | "collapsed": false 556 | }, 557 | "outputs": [ 558 | { 559 | "name": "stdout", 560 | "output_type": "stream", 561 | "text": [ 562 | "[1, 1, 1, 3, 2, 1]\n" 563 | ] 564 | } 565 | ], 566 | "source": [ 567 | "# reverse(): Reverses the list.\n", 568 | "arr = [1, 2, 3, 1, 1, 1]\n", 569 | "arr.reverse()\n", 570 | "print arr \n", 571 | "# prints [1, 1, 1, 3, 2, 1]" 572 | ] 573 | }, 574 | { 575 | "cell_type": "markdown", 576 | "metadata": {}, 577 | "source": [ 578 | "## 3. Practice Problem" 579 | ] 580 | }, 581 | { 582 | "cell_type": "markdown", 583 | "metadata": {}, 584 | "source": [ 585 | "Let's solve a problem using list methods and conditionals.\n", 586 | "\n", 587 | ">Input: Integer n, denoting the number of commands.\n", 588 | "\n", 589 | "> Each line of the subsequent lines contains one of the commands described below.\n", 590 | "\n", 591 | ">> Consider a list (list = [ ]). You can perform the following commands: \n", 592 | "\n", 593 | ">> (1) insert i e: Insert integer e at position i.\n", 594 | "\n", 595 | ">> (2) print: Print the list.\n", 596 | "\n", 597 | ">> (3) append e: Insert integer e at the end of the list.\n", 598 | "\n", 599 | ">> (4) index e: Returns the first index of a value e in the list.\n", 600 | "\n", 601 | ">Output: For each command of type print, print the list on a new line." 602 | ] 603 | }, 604 | { 605 | "cell_type": "markdown", 606 | "metadata": {}, 607 | "source": [ 608 | "*Hint 1. The len() function returns the number of items of an object." 609 | ] 610 | }, 611 | { 612 | "cell_type": "code", 613 | "execution_count": 2, 614 | "metadata": { 615 | "collapsed": false 616 | }, 617 | "outputs": [ 618 | { 619 | "name": "stdout", 620 | "output_type": "stream", 621 | "text": [ 622 | "5\n", 623 | "6\n", 624 | "3\n" 625 | ] 626 | } 627 | ], 628 | "source": [ 629 | "print(len(\"apple\"))\n", 630 | "print(len([1,3,2,4,2,3]))\n", 631 | "print(len(range(6,9)))" 632 | ] 633 | }, 634 | { 635 | "cell_type": "markdown", 636 | "metadata": {}, 637 | "source": [ 638 | "*Hint 2. The split() breaks the string at the separator and returns a list of strings." 639 | ] 640 | }, 641 | { 642 | "cell_type": "code", 643 | "execution_count": 21, 644 | "metadata": { 645 | "collapsed": false 646 | }, 647 | "outputs": [ 648 | { 649 | "name": "stdout", 650 | "output_type": "stream", 651 | "text": [ 652 | "['Start', 'where', 'you', 'are']\n", 653 | "['one', 'two', 'three']\n" 654 | ] 655 | } 656 | ], 657 | "source": [ 658 | "text = \"Start where you are\"\n", 659 | "# splits at space\n", 660 | "print(text.split())\n", 661 | "\n", 662 | "number = \"one, two, three\"\n", 663 | "# splits at ','\n", 664 | "print(number.split(\", \"))" 665 | ] 666 | }, 667 | { 668 | "cell_type": "code", 669 | "execution_count": 1, 670 | "metadata": { 671 | "collapsed": false 672 | }, 673 | "outputs": [ 674 | { 675 | "name": "stdout", 676 | "output_type": "stream", 677 | "text": [ 678 | "10\n", 679 | "insert 0 4\n", 680 | "print\n", 681 | "[4]\n", 682 | "append 7\n", 683 | "print\n", 684 | "[4, 7]\n", 685 | "index 4\n", 686 | "0\n", 687 | "index 7\n", 688 | "1\n", 689 | "insert 1 8\n", 690 | "print\n", 691 | "[4, 8, 7]\n", 692 | "insert 0 3\n", 693 | "print\n", 694 | "[3, 4, 8, 7]\n" 695 | ] 696 | } 697 | ], 698 | "source": [ 699 | "# solution\n", 700 | "arr = []\n", 701 | "for i in range(int(raw_input())):\n", 702 | " s = raw_input().split()\n", 703 | " for i in range(1,len(s)):\n", 704 | " s[i] = int(s[i])\n", 705 | " if s[0] == \"insert\":\n", 706 | " arr.insert(s[1],s[2])\n", 707 | " elif s[0] == \"print\":\n", 708 | " print arr\n", 709 | " elif s[0] == \"append\":\n", 710 | " arr.append(s[1])\n", 711 | " elif s[0] == \"index\":\n", 712 | " print arr.index(s[1])" 713 | ] 714 | }, 715 | { 716 | "cell_type": "markdown", 717 | "metadata": {}, 718 | "source": [ 719 | "## 4. Are you ready? \n", 720 | "### DIY at https://www.hackerrank.com/challenges/python-lists" 721 | ] 722 | } 723 | ], 724 | "metadata": { 725 | "anaconda-cloud": {}, 726 | "kernelspec": { 727 | "display_name": "Python [default]", 728 | "language": "python", 729 | "name": "python2" 730 | }, 731 | "language_info": { 732 | "codemirror_mode": { 733 | "name": "ipython", 734 | "version": 2 735 | }, 736 | "file_extension": ".py", 737 | "mimetype": "text/x-python", 738 | "name": "python", 739 | "nbconvert_exporter": "python", 740 | "pygments_lexer": "ipython2", 741 | "version": "2.7.12" 742 | } 743 | }, 744 | "nbformat": 4, 745 | "nbformat_minor": 1 746 | } 747 | -------------------------------------------------------------------------------- /Lab02-2_Tuples.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab02-2: Tuples\n", 8 | "https://www.hackerrank.com/challenges/python-tuples" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "## 1. Description of Problem" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "If we want to store multiple values in a container-like data structure, how do we say in Python?\n", 23 | "\n", 24 | "The goal is to solve the problem using Tuple, similar datatype with List.\n", 25 | "\n", 26 | "> Task: \n", 27 | "\n", 28 | ">> Given an integer, n, and n space-separated integers as input, \n", 29 | "\n", 30 | ">> create a tuple, t, of those n integers. Then compute and print the result of hash(t).\n", 31 | "\n", 32 | "> Input:\n", 33 | "\n", 34 | ">> The first line contains an integer, n, denoting the number of elements in the tuple. \n", 35 | "\n", 36 | ">> The second line contains n space-separated integers describing the elements in tuple t.\n", 37 | "\n", 38 | "> Output:\n", 39 | "\n", 40 | ">> Print the result of hash(t)" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "## 2. Concept & Short Examples" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "### Concept [1] Creating a Tuple" 55 | ] 56 | }, 57 | { 58 | "cell_type": "markdown", 59 | "metadata": {}, 60 | "source": [ 61 | "In Python programming, a tuple is similar to a list.\n", 62 | "\n", 63 | "The difference between the two is that we cannot change the elements of a tuple once it is assigned whereas in a list, elements can be changed.\n", 64 | "\n", 65 | "However, there are certain advantages of implementing a tuple over a list.\n", 66 | "\n", 67 | "> (1) Since tuple are immutable, iterating through tuple is faster than with list. So there is a slight performance boost.\n", 68 | "\n", 69 | "> (2) Tuples that contain immutable elements can be used as key for a dictionary. With list, this is not possible.\n", 70 | "\n", 71 | "A tuple is created by placing all the items (elements) inside a parentheses ( ), separated by comma.\n", 72 | "\n", 73 | "The parentheses are optional but is a good practice to write it.\n", 74 | "\n", 75 | "A tuple can have any number of items and they may be of different types (integer, float, list, string, etc.).\n", 76 | "\n", 77 | "Ex1) Let's create some tuples and print them." 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": 58, 83 | "metadata": { 84 | "collapsed": false 85 | }, 86 | "outputs": [ 87 | { 88 | "name": "stdout", 89 | "output_type": "stream", 90 | "text": [ 91 | "()\n" 92 | ] 93 | } 94 | ], 95 | "source": [ 96 | "# empty tuple\n", 97 | "tuple_a = ()\n", 98 | "print(tuple_a)" 99 | ] 100 | }, 101 | { 102 | "cell_type": "code", 103 | "execution_count": 59, 104 | "metadata": { 105 | "collapsed": false 106 | }, 107 | "outputs": [ 108 | { 109 | "name": "stdout", 110 | "output_type": "stream", 111 | "text": [ 112 | "(1, 3, 4)\n" 113 | ] 114 | } 115 | ], 116 | "source": [ 117 | "# tuple having integers\n", 118 | "tuple_b = (1, 3, 4)\n", 119 | "print(tuple_b)" 120 | ] 121 | }, 122 | { 123 | "cell_type": "code", 124 | "execution_count": 60, 125 | "metadata": { 126 | "collapsed": false 127 | }, 128 | "outputs": [ 129 | { 130 | "name": "stdout", 131 | "output_type": "stream", 132 | "text": [ 133 | "(2, 3.7, 'piano')\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "# tuple with mixed datatypes\n", 139 | "tuple_c = (2, 3.7, \"piano\")\n", 140 | "print(tuple_c)" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 61, 146 | "metadata": { 147 | "collapsed": false 148 | }, 149 | "outputs": [ 150 | { 151 | "name": "stdout", 152 | "output_type": "stream", 153 | "text": [ 154 | "('guitar', (2, 3), [5, 7, 0])\n" 155 | ] 156 | } 157 | ], 158 | "source": [ 159 | "# nested tuple\n", 160 | "tuple_d = (\"guitar\", (2, 3), [5, 7, 0])\n", 161 | "print(tuple_d)" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 62, 167 | "metadata": { 168 | "collapsed": false 169 | }, 170 | "outputs": [ 171 | { 172 | "name": "stdout", 173 | "output_type": "stream", 174 | "text": [ 175 | "(1, 2.7, 'music')\n", 176 | "1\n", 177 | "2.7\n", 178 | "music\n" 179 | ] 180 | } 181 | ], 182 | "source": [ 183 | "# tuple can be created without parentheses\n", 184 | "# also called tuple packing\n", 185 | "tuple_e = 1, 2.7, \"music\"\n", 186 | "print(tuple_e)\n", 187 | "\n", 188 | "# tuple unpacking is also possible\n", 189 | "one, two, three = tuple_e\n", 190 | "print(one)\n", 191 | "print(two)\n", 192 | "print(three)" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "### Concept [2] Accessing Elements in a Tuple" 200 | ] 201 | }, 202 | { 203 | "cell_type": "markdown", 204 | "metadata": {}, 205 | "source": [ 206 | "We can use the index operator [ ] to access an item in a tuple where the index starts from 0.\n", 207 | "\n", 208 | "So, a tuple having 6 elements will have index from 0 to 5. \n", 209 | "\n", 210 | "Trying to access an element other that (6, 7,...) will raise an IndexError.\n", 211 | "\n", 212 | "The index must be an integer, so we cannot use float or other types. This will result into TypeError.\n", 213 | "\n", 214 | "Ex2) Let's access elements in a tuple using indexing." 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 63, 220 | "metadata": { 221 | "collapsed": false 222 | }, 223 | "outputs": [ 224 | { 225 | "name": "stdout", 226 | "output_type": "stream", 227 | "text": [ 228 | "red\n", 229 | "orange\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "tuple_color = (\"red\", \"yellow\", \"orange\", \"blue\", \"green\")\n", 235 | "print(tuple_color[0])\n", 236 | "print(tuple_color[2])" 237 | ] 238 | }, 239 | { 240 | "cell_type": "markdown", 241 | "metadata": {}, 242 | "source": [ 243 | "Python allows negative indexing for its sequences.\n", 244 | "\n", 245 | "The index of -1 refers to the last item, -2 to the second last item and so on.\n", 246 | "\n", 247 | "Ex3) Let's access elements in a tuple using negative indexing." 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 21, 253 | "metadata": { 254 | "collapsed": false 255 | }, 256 | "outputs": [ 257 | { 258 | "name": "stdout", 259 | "output_type": "stream", 260 | "text": [ 261 | "green\n", 262 | "red\n" 263 | ] 264 | } 265 | ], 266 | "source": [ 267 | "tuple_color = (\"red\", \"yellow\", \"orange\", \"blue\", \"green\")\n", 268 | "print(tuple_color[-1])\n", 269 | "print(tuple_color[-5])" 270 | ] 271 | }, 272 | { 273 | "cell_type": "markdown", 274 | "metadata": {}, 275 | "source": [ 276 | "Ex4) We can access a range of items in a tuple by using the slicing operator, colon \":\"." 277 | ] 278 | }, 279 | { 280 | "cell_type": "code", 281 | "execution_count": 64, 282 | "metadata": { 283 | "collapsed": false 284 | }, 285 | "outputs": [ 286 | { 287 | "name": "stdout", 288 | "output_type": "stream", 289 | "text": [ 290 | "('yellow', 'orange', 'blue')\n", 291 | "('red', 'yellow', 'orange')\n", 292 | "('blue', 'green')\n", 293 | "('red', 'yellow', 'orange', 'blue', 'green')\n" 294 | ] 295 | } 296 | ], 297 | "source": [ 298 | "tuple_color = (\"red\", \"yellow\", \"orange\", \"blue\", \"green\")\n", 299 | "print(tuple_color[1:4])\n", 300 | "print(tuple_color[:3])\n", 301 | "print(tuple_color[-2:])\n", 302 | "print(tuple_color[:])" 303 | ] 304 | }, 305 | { 306 | "cell_type": "markdown", 307 | "metadata": {}, 308 | "source": [ 309 | "### Concept [3] Changing and Deleting a Tuple" 310 | ] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "metadata": {}, 315 | "source": [ 316 | "We can use + operator to combine two tuples. This is also called concatenation.\n", 317 | "\n", 318 | "We can also repeat the elements in a tuple for a given number of times using the * operator.\n", 319 | "\n", 320 | "Ex5) Both + and * operations result into a new tuple." 321 | ] 322 | }, 323 | { 324 | "cell_type": "code", 325 | "execution_count": 65, 326 | "metadata": { 327 | "collapsed": false 328 | }, 329 | "outputs": [ 330 | { 331 | "name": "stdout", 332 | "output_type": "stream", 333 | "text": [ 334 | "(2, 3, 4, 4, 5, 6)\n", 335 | "('again', 'again2', 'again', 'again2', 'again', 'again2')\n" 336 | ] 337 | } 338 | ], 339 | "source": [ 340 | "# Concatenation\n", 341 | "print((2,3,4)+(4,5,6))\n", 342 | "# Repeat\n", 343 | "print((\"again\", \"again2\")*3)" 344 | ] 345 | }, 346 | { 347 | "cell_type": "markdown", 348 | "metadata": {}, 349 | "source": [ 350 | "As discussed above, we cannot change the elements in a tuple. \n", 351 | "\n", 352 | "That also means we cannot delete or remove items from a tuple.\n", 353 | "\n", 354 | "But deleting a tuple entirely is possible using the keyword del.\n", 355 | "\n", 356 | "Ex6) Let's delete a tuple using the keyword del." 357 | ] 358 | }, 359 | { 360 | "cell_type": "code", 361 | "execution_count": 66, 362 | "metadata": { 363 | "collapsed": false 364 | }, 365 | "outputs": [ 366 | { 367 | "name": "stdout", 368 | "output_type": "stream", 369 | "text": [ 370 | "('w', 'a', 't', 'e', 'r')\n" 371 | ] 372 | }, 373 | { 374 | "ename": "NameError", 375 | "evalue": "name 'tuple_f' is not defined", 376 | "output_type": "error", 377 | "traceback": [ 378 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 379 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 380 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mtuple_f\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[1;31m# NameError: name 'tuple_f' is not defined\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtuple_f\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 381 | "\u001b[0;31mNameError\u001b[0m: name 'tuple_f' is not defined" 382 | ] 383 | } 384 | ], 385 | "source": [ 386 | "tuple_f = (\"w\", \"a\", \"t\", \"e\", \"r\")\n", 387 | "print(tuple_f)\n", 388 | "# can delete entire tuple\n", 389 | "del tuple_f\n", 390 | "# NameError: name 'tuple_f' is not defined\n", 391 | "print(tuple_f)" 392 | ] 393 | }, 394 | { 395 | "cell_type": "markdown", 396 | "metadata": {}, 397 | "source": [ 398 | "### Concept [4] Python Tuple Methods" 399 | ] 400 | }, 401 | { 402 | "cell_type": "markdown", 403 | "metadata": {}, 404 | "source": [ 405 | "Methods that add items or remove items are not available with tuple.\n", 406 | "\n", 407 | "Only the following two methods are available.\n", 408 | "\n", 409 | "> count(x): Return the number of items that is equal to x\n", 410 | "\n", 411 | "> index(x): Return index of first item that is equal to x\n", 412 | "\n", 413 | "Ex7) Let's use Python tuple methods." 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": 67, 419 | "metadata": { 420 | "collapsed": false 421 | }, 422 | "outputs": [ 423 | { 424 | "name": "stdout", 425 | "output_type": "stream", 426 | "text": [ 427 | "3\n", 428 | "2\n", 429 | "1\n", 430 | "0\n", 431 | "1\n", 432 | "5\n" 433 | ] 434 | } 435 | ], 436 | "source": [ 437 | "tuple_g = (\"p\",\"e\",\"p\",\"p\",\"e\",\"r\")\n", 438 | "print(tuple_g.count(\"p\"))\n", 439 | "print(tuple_g.count(\"e\"))\n", 440 | "print(tuple_g.count(\"r\"))\n", 441 | "print(tuple_g.index(\"p\"))\n", 442 | "print(tuple_g.index(\"e\"))\n", 443 | "print(tuple_g.index(\"r\"))" 444 | ] 445 | }, 446 | { 447 | "cell_type": "markdown", 448 | "metadata": {}, 449 | "source": [ 450 | "### Concept [5] hash( )" 451 | ] 452 | }, 453 | { 454 | "cell_type": "markdown", 455 | "metadata": { 456 | "collapsed": true 457 | }, 458 | "source": [ 459 | "#### Meaning of 'hashable'\n", 460 | "\n", 461 | "hash( ) is a method that takes an object(integer, string, float, etc) and returns an integer.\n", 462 | "\n", 463 | "The dictionary(datatype) uses this integer, called a hash value to store and retrieve key-value pairs.\n", 464 | "\n", 465 | "> hash value = hash(object)\n", 466 | "\n", 467 | "This system works well if the key can not be modified. \n", 468 | "\n", 469 | "But if the key, like a list, is correctable, something bad happens. \n", 470 | "\n", 471 | "For example, when you create a key-value pair, Python hashes the key and stores it in a location that corresponds to the value.\n", 472 | "\n", 473 | "If you modify the key and then hash it again, you will go to another location. \n", 474 | "\n", 475 | "In that case, you will have two items with the same key, or you will not be able to find the key.\n", 476 | "\n", 477 | "In both cases, the dictionary will not work correctly.\n", 478 | "\n", 479 | "This is why the key must be hashable, and modifiable datatypes such as lists are not hashable. \n", 480 | "\n", 481 | "The simplest way to avoid this limitation is to use a tuple.\n", 482 | "\n", 483 | "Ex8) Let's use hash()." 484 | ] 485 | }, 486 | { 487 | "cell_type": "code", 488 | "execution_count": 68, 489 | "metadata": { 490 | "collapsed": false 491 | }, 492 | "outputs": [ 493 | { 494 | "name": "stdout", 495 | "output_type": "stream", 496 | "text": [ 497 | "321\n", 498 | "0\n", 499 | "-728237282\n", 500 | "2126309038\n" 501 | ] 502 | } 503 | ], 504 | "source": [ 505 | "# hash for integer unchanged\n", 506 | "print(hash(321))\n", 507 | "print(hash(0))\n", 508 | "# hash for float\n", 509 | "print(hash(120.42))\n", 510 | "# hash for string\n", 511 | "print(hash(\"ice\"))" 512 | ] 513 | }, 514 | { 515 | "cell_type": "markdown", 516 | "metadata": {}, 517 | "source": [ 518 | "## 3. Practice Problem" 519 | ] 520 | }, 521 | { 522 | "cell_type": "markdown", 523 | "metadata": {}, 524 | "source": [ 525 | "> Given an integer, n, and n space-separated integers as input => store them in variables.\n", 526 | "\n", 527 | "> Create a tuple, t, of those n integers. Then print the tuple t and the result of hash(t).\n", 528 | "\n", 529 | "> Input:\n", 530 | "\n", 531 | ">> The first line: input integer, n, denoting the number of elements in the tuple. \n", 532 | "\n", 533 | ">> The second line: n space-separated input integers describing the elements in tuple t.\n", 534 | "\n", 535 | "> Output:\n", 536 | "\n", 537 | ">> Print the tuple t and the result of hash(t)" 538 | ] 539 | }, 540 | { 541 | "cell_type": "code", 542 | "execution_count": 70, 543 | "metadata": { 544 | "collapsed": false 545 | }, 546 | "outputs": [ 547 | { 548 | "name": "stdout", 549 | "output_type": "stream", 550 | "text": [ 551 | "4\n", 552 | "1 3 0 -5\n", 553 | "(1, 3, 0, -5)\n", 554 | "1724495248\n" 555 | ] 556 | } 557 | ], 558 | "source": [ 559 | "n = raw_input()\n", 560 | "x = raw_input().split()\n", 561 | "t = tuple([int(i) for i in x]) \n", 562 | "print(t)\n", 563 | "print(hash(t))" 564 | ] 565 | }, 566 | { 567 | "cell_type": "markdown", 568 | "metadata": {}, 569 | "source": [ 570 | "## 4. Are you ready? \n", 571 | "### DIY at https://www.hackerrank.com/challenges/python-tuples" 572 | ] 573 | } 574 | ], 575 | "metadata": { 576 | "anaconda-cloud": {}, 577 | "kernelspec": { 578 | "display_name": "Python [default]", 579 | "language": "python", 580 | "name": "python2" 581 | }, 582 | "language_info": { 583 | "codemirror_mode": { 584 | "name": "ipython", 585 | "version": 2 586 | }, 587 | "file_extension": ".py", 588 | "mimetype": "text/x-python", 589 | "name": "python", 590 | "nbconvert_exporter": "python", 591 | "pygments_lexer": "ipython2", 592 | "version": "2.7.12" 593 | } 594 | }, 595 | "nbformat": 4, 596 | "nbformat_minor": 1 597 | } 598 | -------------------------------------------------------------------------------- /Lab02-3_List-Comprehensions.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# Lab02-3: List Comprehensions\n", 10 | "https://www.hackerrank.com/challenges/list-comprehensions" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "## 1. Description of Problem" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "If we want to make a list without using different for-loops, how do we say in Python?\n", 25 | "\n", 26 | "This problem is to use list comprehension.\n", 27 | "\n", 28 | "You are given three integers X, Y, Z representing the dimensions of a cuboid along with an integer N. \n", 29 | "\n", 30 | "You have to print a list of all possible coordinates given by (i, j, k) on a 3D grid where the sum of i+j+k is not equal to N. Here, 0 ≤ i ≤ X, 0 ≤ j ≤ Y, 0 ≤ k ≤ Z\n", 31 | "\n", 32 | "> Input: Four integers; X, Y, Z, N each on four separate lines, respectively. \n", 33 | "\n", 34 | ">> ex. 2 2 2 4\n", 35 | "\n", 36 | "> Output: Print the list in lexicographic increasing order.\n", 37 | "\n", 38 | ">> ex. [[0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 2, 0], [0, 2, 1], [1, 0, 0], [1, 0, 1], [1, 0, 2], [1, 1, 0], [1, 1, 1], [1, 2, 0], [1, 2, 2], [2, 0, 0], [2, 0, 1], [2, 1, 0], [2, 1, 2], [2, 2, 1], [2, 2, 2]]" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "## 2. Concept & Short Examples" 46 | ] 47 | }, 48 | { 49 | "cell_type": "markdown", 50 | "metadata": {}, 51 | "source": [ 52 | "### Concept [1] List Comprehension" 53 | ] 54 | }, 55 | { 56 | "cell_type": "markdown", 57 | "metadata": {}, 58 | "source": [ 59 | "List comprehension is an elegant and concise way to create new list from an existing list in Python.\n", 60 | "\n", 61 | "List comprehension consists of an expression followed by for-Loop statement inside square brackets.\n", 62 | "\n", 63 | "Ex1) Let's make a list using List Comprehension.\n", 64 | "\n", 65 | "We want to make a list with each item being increasing power of 2 in range(5)." 66 | ] 67 | }, 68 | { 69 | "cell_type": "code", 70 | "execution_count": 20, 71 | "metadata": { 72 | "collapsed": false 73 | }, 74 | "outputs": [ 75 | { 76 | "name": "stdout", 77 | "output_type": "stream", 78 | "text": [ 79 | "[1, 2, 4, 8, 16]\n" 80 | ] 81 | } 82 | ], 83 | "source": [ 84 | "# using List Comprehension\n", 85 | "power2 = [2**x for x in range(5)]\n", 86 | "print(power2)" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 21, 92 | "metadata": { 93 | "collapsed": false 94 | }, 95 | "outputs": [ 96 | { 97 | "name": "stdout", 98 | "output_type": "stream", 99 | "text": [ 100 | "[1, 2, 4, 8, 16]\n" 101 | ] 102 | } 103 | ], 104 | "source": [ 105 | "# The above code is equivalent to\n", 106 | "power2 = []\n", 107 | "for x in range(5):\n", 108 | " power2.append(2**x)\n", 109 | "print(power2)" 110 | ] 111 | }, 112 | { 113 | "cell_type": "markdown", 114 | "metadata": {}, 115 | "source": [ 116 | "A list comprehension can optionally contain more for-statements or if-statements.\n", 117 | "\n", 118 | "An optional if-statement can filter out items for the new list.\n", 119 | "\n", 120 | "Ex2) We want to make a list with each item being increasing power of 2 in range(5), \n", 121 | "\n", 122 | "and the item must be greater than 1." 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 22, 128 | "metadata": { 129 | "collapsed": false 130 | }, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "[4, 8, 16]\n" 137 | ] 138 | } 139 | ], 140 | "source": [ 141 | "power2 = [2**x for x in range(5) if x>1]\n", 142 | "print(power2)" 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "Ex3) We want to create a list of odd numbers less than 10 for each item." 150 | ] 151 | }, 152 | { 153 | "cell_type": "code", 154 | "execution_count": 23, 155 | "metadata": { 156 | "collapsed": false 157 | }, 158 | "outputs": [ 159 | { 160 | "name": "stdout", 161 | "output_type": "stream", 162 | "text": [ 163 | "[1, 3, 5, 7, 9]\n" 164 | ] 165 | } 166 | ], 167 | "source": [ 168 | "odd_n = [x for x in range(10) if x % 2 == 1]\n", 169 | "print(odd_n)" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "Ex4) We want to make a list of combinations of words." 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 24, 182 | "metadata": { 183 | "collapsed": false 184 | }, 185 | "outputs": [ 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "['cute dog', 'cute cat', 'smart dog', 'smart cat']\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "list_str = [a+b for a in [\"cute \", \"smart \"] for b in [\"dog\", \"cat\"]]\n", 196 | "print list_str" 197 | ] 198 | }, 199 | { 200 | "cell_type": "markdown", 201 | "metadata": {}, 202 | "source": [ 203 | "## 3. Practice Problem" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "You are given three integers X, Y, Z representing the dimensions of a cuboid along with an integer N. \n", 211 | "\n", 212 | "You have to print a list of all possible coordinates given by (i, j, k) on a 3D grid where the sum of i+j+k is equal to N. Here, 0 ≤ i ≤ X, 0 ≤ j ≤ Y, 0 ≤ k ≤ Z\n", 213 | "\n", 214 | "> Input: Four integers; X, Y, Z, N each on four separate lines, respectively. \n", 215 | "\n", 216 | ">> ex. 2 2 2 4\n", 217 | "\n", 218 | "> Output: Print the list in lexicographic increasing order.\n", 219 | "\n", 220 | ">> ex. [[0, 2, 2], [1, 1, 2], [1, 2, 1], [2, 0, 2], [2, 1, 1], [2, 2, 0]]" 221 | ] 222 | }, 223 | { 224 | "cell_type": "code", 225 | "execution_count": 25, 226 | "metadata": { 227 | "collapsed": false 228 | }, 229 | "outputs": [ 230 | { 231 | "name": "stdout", 232 | "output_type": "stream", 233 | "text": [ 234 | "2\n", 235 | "2\n", 236 | "2\n", 237 | "4\n", 238 | "[[0, 2, 2], [1, 1, 2], [1, 2, 1], [2, 0, 2], [2, 1, 1], [2, 2, 0]]\n" 239 | ] 240 | } 241 | ], 242 | "source": [ 243 | "a, b, c, n = [int(raw_input()) for _ in xrange(4)]\n", 244 | "print [[x,y,z] for x in xrange(a + 1) for y in xrange(b + 1) for z in xrange(c + 1) if x + y + z == n]" 245 | ] 246 | }, 247 | { 248 | "cell_type": "markdown", 249 | "metadata": {}, 250 | "source": [ 251 | "## 4. Are you ready? \n", 252 | "### DIY at https://www.hackerrank.com/challenges/list-comprehensions" 253 | ] 254 | } 255 | ], 256 | "metadata": { 257 | "anaconda-cloud": {}, 258 | "kernelspec": { 259 | "display_name": "Python [default]", 260 | "language": "python", 261 | "name": "python2" 262 | }, 263 | "language_info": { 264 | "codemirror_mode": { 265 | "name": "ipython", 266 | "version": 2 267 | }, 268 | "file_extension": ".py", 269 | "mimetype": "text/x-python", 270 | "name": "python", 271 | "nbconvert_exporter": "python", 272 | "pygments_lexer": "ipython2", 273 | "version": "2.7.12" 274 | } 275 | }, 276 | "nbformat": 4, 277 | "nbformat_minor": 1 278 | } 279 | -------------------------------------------------------------------------------- /Lab02-4_Find-the-Second-Largest-Number.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# Lab02-4: Find the Second Largest Number\n", 10 | "https://www.hackerrank.com/challenges/find-second-maximum-number-in-a-list" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "## 1. Description of Problem" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "The Python built-in function includes a max() function and a min() function.\n", 25 | "\n", 26 | "Then, if we want to find the largest or smallest item among several numbers \n", 27 | "\n", 28 | "without using these max() and min() functions, how do we say in Python?\n", 29 | "\n", 30 | "This problem is to store N numbers in a list and find the second largest number.\n", 31 | "\n", 32 | ">Input: The first line contains N.\n", 33 | "\n", 34 | ">The second line contains N integers, each separated by a space.\n", 35 | "\n", 36 | ">> ex. \n", 37 | "\n", 38 | ">> 5\n", 39 | "\n", 40 | ">> 1 3 10 2 -2\n", 41 | "\n", 42 | ">Output: the value of the second largest number\n", 43 | "\n", 44 | ">> ex.\n", 45 | "\n", 46 | ">> 3" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "## 2. Concept & Short Examples" 54 | ] 55 | }, 56 | { 57 | "cell_type": "markdown", 58 | "metadata": {}, 59 | "source": [ 60 | "### Concept [1] find Max and Min with for-loop" 61 | ] 62 | }, 63 | { 64 | "cell_type": "markdown", 65 | "metadata": {}, 66 | "source": [ 67 | "We can use the for-loop statement to find the maximum and minimum values in a list.\n", 68 | "\n", 69 | "Ex1) Let's find the largest number in a list using the for-loop statement. " 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 20, 75 | "metadata": { 76 | "collapsed": false 77 | }, 78 | "outputs": [ 79 | { 80 | "name": "stdout", 81 | "output_type": "stream", 82 | "text": [ 83 | "52\n" 84 | ] 85 | } 86 | ], 87 | "source": [ 88 | "list_a = [1, 2, -10, 5, -3, 0, 52, 30, 45]\n", 89 | "max_a = -1000\n", 90 | "for i in list_a:\n", 91 | " if (i > max_a):\n", 92 | " max_a = i\n", 93 | "print (max_a)" 94 | ] 95 | }, 96 | { 97 | "cell_type": "markdown", 98 | "metadata": {}, 99 | "source": [ 100 | "Ex2) Let's find the smallest number in a list using the for-loop statement." 101 | ] 102 | }, 103 | { 104 | "cell_type": "code", 105 | "execution_count": 21, 106 | "metadata": { 107 | "collapsed": false 108 | }, 109 | "outputs": [ 110 | { 111 | "name": "stdout", 112 | "output_type": "stream", 113 | "text": [ 114 | "-10\n" 115 | ] 116 | } 117 | ], 118 | "source": [ 119 | "list_a = [1, 2, -10, 5, -3, 0, 52, 30, 45]\n", 120 | "min_a = 1000\n", 121 | "for i in list_a:\n", 122 | " if (i < min_a):\n", 123 | " min_a = i\n", 124 | "print (min_a)" 125 | ] 126 | }, 127 | { 128 | "cell_type": "markdown", 129 | "metadata": {}, 130 | "source": [ 131 | "### Concept [2] map( )" 132 | ] 133 | }, 134 | { 135 | "cell_type": "markdown", 136 | "metadata": { 137 | "collapsed": true 138 | }, 139 | "source": [ 140 | "The map() function applies a given function to each item of an iterable (list, tuple, etc.) and returns a list of the results.\n", 141 | "\n", 142 | "The syntax of map() is\n", 143 | "\n", 144 | "> map(function, iterable, ...)\n", 145 | "\n", 146 | ">>- function: map() passes each item of the iterable to this function.\n", 147 | "\n", 148 | ">>- iterable: iterable which is to be mapped.\n", 149 | "\n", 150 | "> You can pass more than one iterable to the map() function.\n", 151 | "\n", 152 | "The returned value from map() (map object) then can be passed to functions like list() (to create a list), set() (to create a set) and so on.\n", 153 | "\n", 154 | "Ex3) Create a function that returns a multiple of 3 for an integer n, \n", 155 | "\n", 156 | "and use map() to create and print a new list, list_3n of items \n", 157 | "\n", 158 | "from list_n that have passed through a multiple of 3 function." 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 22, 164 | "metadata": { 165 | "collapsed": false 166 | }, 167 | "outputs": [ 168 | { 169 | "name": "stdout", 170 | "output_type": "stream", 171 | "text": [ 172 | "[3, 6, 0, 12, 15]\n", 173 | "[3, 6, 0, 12, 15]\n" 174 | ] 175 | } 176 | ], 177 | "source": [ 178 | "def multiple_three(n):\n", 179 | " return 3*n\n", 180 | "\n", 181 | "list_n = [1, 2, 0, 4, 5]\n", 182 | "result = map(multiple_three, list_n)\n", 183 | "print(result)\n", 184 | "\n", 185 | "# converting map object to list\n", 186 | "list_3n = list(result)\n", 187 | "print(list_3n)" 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": {}, 193 | "source": [ 194 | "### Concept [3] int( )" 195 | ] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": {}, 200 | "source": [ 201 | "The syntax of int() method is:\n", 202 | "\n", 203 | "> int(x=0, base=10)\n", 204 | "\n", 205 | "The int() method takes two arguments:\n", 206 | "\n", 207 | "> - x: Number or string to be converted to integer object. Default argument is zero.\n", 208 | "\n", 209 | "> - base: Base of the number in x. Can be 0 (code literal) or 2-36.\n", 210 | "\n", 211 | "The int() method returns\n", 212 | "\n", 213 | "> - an integer object from the given number or string, treats default base as 10\n", 214 | "\n", 215 | "> - (No parameters) returns 0\n", 216 | "\n", 217 | "> - (If base given) treats the string in the given base (0, 2, 8, 10, 16)\n", 218 | "\n", 219 | "Ex4) Let's use int( )." 220 | ] 221 | }, 222 | { 223 | "cell_type": "code", 224 | "execution_count": 26, 225 | "metadata": { 226 | "collapsed": false 227 | }, 228 | "outputs": [ 229 | { 230 | "name": "stdout", 231 | "output_type": "stream", 232 | "text": [ 233 | "123\n", 234 | "123\n", 235 | "123\n" 236 | ] 237 | } 238 | ], 239 | "source": [ 240 | "# integer\n", 241 | "print(int(123))\n", 242 | "\n", 243 | "# float\n", 244 | "print(int(123.23))\n", 245 | "\n", 246 | "# string\n", 247 | "print(int('123'))" 248 | ] 249 | }, 250 | { 251 | "cell_type": "markdown", 252 | "metadata": { 253 | "collapsed": true 254 | }, 255 | "source": [ 256 | "### Concept [4] list( )" 257 | ] 258 | }, 259 | { 260 | "cell_type": "markdown", 261 | "metadata": {}, 262 | "source": [ 263 | "The list() constructor creates a list in Python.\n", 264 | "\n", 265 | "The syntax of list() constructor is:\n", 266 | "\n", 267 | "> list([iterable])\n", 268 | "\n", 269 | "list() constructor takes a single argument:\n", 270 | "\n", 271 | "> iterable(Optional): an object that could be a sequence (string, tuples) or collection (set, dictionary) or iterator object\n", 272 | "\n", 273 | "The list() constructor returns a mutable sequence list of elements.\n", 274 | "\n", 275 | "> - If no parameters are passed, it creates an empty list\n", 276 | "> - If iterable is passed as parameter, it creates a list of elements in the iterable\n", 277 | "\n", 278 | "Ex5) Let's use list( )." 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": 27, 284 | "metadata": { 285 | "collapsed": false 286 | }, 287 | "outputs": [ 288 | { 289 | "name": "stdout", 290 | "output_type": "stream", 291 | "text": [ 292 | "[]\n", 293 | "['d', 'a', 'n', 'c', 'e']\n", 294 | "['d', 'a', 'n', 'c', 'e']\n", 295 | "['d', 'a', 'n', 'c', 'e']\n" 296 | ] 297 | } 298 | ], 299 | "source": [ 300 | "# print empty list\n", 301 | "print(list())\n", 302 | "\n", 303 | "# create list from string\n", 304 | "string_a = 'dance'\n", 305 | "print(list(string_a))\n", 306 | "\n", 307 | "# create list from tuple\n", 308 | "tuple_a = ('d', 'a', 'n', 'c', 'e')\n", 309 | "print(list(tuple_a))\n", 310 | "\n", 311 | "# create list from list\n", 312 | "list_a = ['d', 'a', 'n', 'c', 'e']\n", 313 | "print(list(list_a))" 314 | ] 315 | }, 316 | { 317 | "cell_type": "markdown", 318 | "metadata": {}, 319 | "source": [ 320 | "## 3. Practice Problem" 321 | ] 322 | }, 323 | { 324 | "cell_type": "markdown", 325 | "metadata": {}, 326 | "source": [ 327 | "store N numbers in a list and find the second smallest number.\n", 328 | "\n", 329 | ">Input: The first line contains N.\n", 330 | "\n", 331 | ">The second line contains N integers, each separated by a space.\n", 332 | "\n", 333 | ">> ex. \n", 334 | "\n", 335 | ">> 5\n", 336 | "\n", 337 | ">> 1 3 10 2 -2\n", 338 | "\n", 339 | ">Output: the value of the second smallest number\n", 340 | "\n", 341 | ">> ex.\n", 342 | "\n", 343 | ">> 1" 344 | ] 345 | }, 346 | { 347 | "cell_type": "code", 348 | "execution_count": 28, 349 | "metadata": { 350 | "collapsed": false 351 | }, 352 | "outputs": [ 353 | { 354 | "name": "stdout", 355 | "output_type": "stream", 356 | "text": [ 357 | "5\n", 358 | "1 3 10 2 -2\n", 359 | "1\n" 360 | ] 361 | } 362 | ], 363 | "source": [ 364 | "# solution\n", 365 | "n = int(raw_input())\n", 366 | "numb = raw_input()\n", 367 | "list_a = list(map(int, numb.split()))\n", 368 | "small_first, small_second = 1000, 1000\n", 369 | "for i in list_a:\n", 370 | " if (i < small_first):\n", 371 | " small_first, small_second = i, small_first\n", 372 | " elif (i > small_first and i < small_second):\n", 373 | " small_second = i\n", 374 | "print (small_second)" 375 | ] 376 | }, 377 | { 378 | "cell_type": "markdown", 379 | "metadata": {}, 380 | "source": [ 381 | "## 4. Are you ready? \n", 382 | "### DIY at https://www.hackerrank.com/challenges/find-second-maximum-number-in-a-list" 383 | ] 384 | } 385 | ], 386 | "metadata": { 387 | "anaconda-cloud": {}, 388 | "kernelspec": { 389 | "display_name": "Python [default]", 390 | "language": "python", 391 | "name": "python2" 392 | }, 393 | "language_info": { 394 | "codemirror_mode": { 395 | "name": "ipython", 396 | "version": 2 397 | }, 398 | "file_extension": ".py", 399 | "mimetype": "text/x-python", 400 | "name": "python", 401 | "nbconvert_exporter": "python", 402 | "pygments_lexer": "ipython2", 403 | "version": "2.7.12" 404 | } 405 | }, 406 | "nbformat": 4, 407 | "nbformat_minor": 1 408 | } 409 | -------------------------------------------------------------------------------- /Lab02-5_Nested-Lists.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# Lab02-5: Nested Lists\n", 10 | "https://www.hackerrank.com/challenges/nested-list" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "## 1. Description of Problem" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "If we want to put multiple items in each container \n", 25 | "\n", 26 | "and put multiple containers in another container, what should we say in Python?\n", 27 | "\n", 28 | "This problem is to use Nested List.\n", 29 | "\n", 30 | "*Task: \n", 31 | "\n", 32 | ">Given the names and grades for each student in a Physics class of N students,\n", 33 | "\n", 34 | ">store them in a nested list and print the name(s) of any student(s) \n", 35 | "\n", 36 | ">having the second lowest grade.\n", 37 | "\n", 38 | "*Input\n", 39 | "> ex.\n", 40 | "\n", 41 | "> 3\n", 42 | "\n", 43 | "> Harry\n", 44 | "\n", 45 | "> 37.21\n", 46 | "\n", 47 | "> Tina\n", 48 | "\n", 49 | "> 37.2\n", 50 | "\n", 51 | "> Harsh\n", 52 | "\n", 53 | "> 39\n", 54 | "\n", 55 | "*Output\n", 56 | "> ex.\n", 57 | "\n", 58 | "> Harry" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "## 2. Concept & Short Examples" 66 | ] 67 | }, 68 | { 69 | "cell_type": "markdown", 70 | "metadata": {}, 71 | "source": [ 72 | "### Concept [1] Nested List" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | "A nested list is a list that contains another list (i.e.: a list of lists).\n", 80 | "\n", 81 | "Nested list are accessed using nested indexing.\n", 82 | "\n", 83 | "Ex1) Let's implement a nested list!" 84 | ] 85 | }, 86 | { 87 | "cell_type": "code", 88 | "execution_count": 4, 89 | "metadata": { 90 | "collapsed": false 91 | }, 92 | "outputs": [ 93 | { 94 | "name": "stdout", 95 | "output_type": "stream", 96 | "text": [ 97 | "3\n", 98 | "['red', 'black']\n", 99 | "red\n", 100 | "a\n", 101 | "1.2\n" 102 | ] 103 | } 104 | ], 105 | "source": [ 106 | "nested_list = [['blue', 'green'], ['red', 'black'], ['blue', 'white']]\n", 107 | "n_list = [\"happy\", [2, 0, 2.4, 1.2]]\n", 108 | "\n", 109 | "print len(nested_list)\n", 110 | "print nested_list[1]\n", 111 | "\n", 112 | "# Nested indexing\n", 113 | "print nested_list[1][0]\n", 114 | "print(n_list[0][1])\n", 115 | "print(n_list[1][3])" 116 | ] 117 | }, 118 | { 119 | "cell_type": "markdown", 120 | "metadata": {}, 121 | "source": [ 122 | "Ex2) To go through every element in a list, use a nested for-loop." 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 32, 128 | "metadata": { 129 | "collapsed": false 130 | }, 131 | "outputs": [ 132 | { 133 | "name": "stdout", 134 | "output_type": "stream", 135 | "text": [ 136 | "s\n", 137 | "smile\n", 138 | "a\n", 139 | "amazing\n", 140 | "b\n", 141 | "baby\n" 142 | ] 143 | } 144 | ], 145 | "source": [ 146 | "a = [[\"s\", \"smile\"], [\"a\", \"amazing\"], [\"b\", \"baby\"]]\n", 147 | "\n", 148 | "for x in range(3):\n", 149 | " for y in range(2):\n", 150 | " print(a[x][y])" 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": {}, 156 | "source": [ 157 | "### Concept [2] Datatype: Set" 158 | ] 159 | }, 160 | { 161 | "cell_type": "markdown", 162 | "metadata": {}, 163 | "source": [ 164 | "A set is an unordered collection of items. Every element is unique (no duplicates) and must be immutable (which cannot be changed).\n", 165 | "\n", 166 | "However, the set itself is mutable. We can add or remove items from it.\n", 167 | "\n", 168 | "A set is created by placing all the items (elements) inside curly braces {}, separated by comma or by using the built-in function set().\n", 169 | "\n", 170 | "We will learn more about Set in Lab4.\n", 171 | "\n", 172 | "Ex3) Let's try the following examples of Set." 173 | ] 174 | }, 175 | { 176 | "cell_type": "code", 177 | "execution_count": 40, 178 | "metadata": { 179 | "collapsed": false 180 | }, 181 | "outputs": [ 182 | { 183 | "name": "stdout", 184 | "output_type": "stream", 185 | "text": [ 186 | "set([1, 2, 3])\n", 187 | "set([1.0, 'Hello', (1, 2, 3)])\n", 188 | "set([1, 2, 3, 4])\n" 189 | ] 190 | } 191 | ], 192 | "source": [ 193 | "# set of integers\n", 194 | "my_set = {1, 2, 3}\n", 195 | "print(my_set)\n", 196 | "\n", 197 | "# set of mixed datatypes\n", 198 | "my_set = {1.0, \"Hello\", (1, 2, 3)}\n", 199 | "print(my_set)\n", 200 | "\n", 201 | "# set do not have duplicates\n", 202 | "my_set = {1,2,3,4,3,2}\n", 203 | "print(my_set)" 204 | ] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "Ex4) Let's try the following examples of set()." 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 41, 216 | "metadata": { 217 | "collapsed": false 218 | }, 219 | "outputs": [ 220 | { 221 | "name": "stdout", 222 | "output_type": "stream", 223 | "text": [ 224 | "set([])\n", 225 | "set(['a', 'i', 's', 'r', 't'])\n", 226 | "set(['e', 'g', 'i', 'n', 's', 'r'])\n", 227 | "set(['a', 'c', 'e', 'd', 'n', 'r'])\n", 228 | "set([0, 1, 2, 3, 4])\n" 229 | ] 230 | } 231 | ], 232 | "source": [ 233 | "# empty set\n", 234 | "print(set())\n", 235 | "# from string\n", 236 | "print(set('artist'))\n", 237 | "# from tuple\n", 238 | "print(set(('s', 'i', 'n', 'g', 'e', 'r')))\n", 239 | "# from list\n", 240 | "print(set(['d', 'a', 'n', 'c', 'e', 'r']))\n", 241 | "# from range\n", 242 | "print(set(range(5)))" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "### Concept [3] sorted( )" 250 | ] 251 | }, 252 | { 253 | "cell_type": "markdown", 254 | "metadata": {}, 255 | "source": [ 256 | "The sorted() method sorts the elements of a given iterable in a specific order - Ascending or Descending.\n", 257 | "\n", 258 | "sorted() method returns a sorted list from the given iterable.\n", 259 | "\n", 260 | "The syntax of sorted() method is:\n", 261 | "\n", 262 | "> sorted(iterable[, key][, reverse])\n", 263 | "\n", 264 | "sorted() takes the following parameters:\n", 265 | "\n", 266 | "> - iterable - sequence (string, tuple, list) or collection (set, dictionary, frozen set) or any iterator \n", 267 | "> - reverse (Optional) - If true, the sorted list is reversed (or sorted in Descending order)\n", 268 | "> - key (Optional) - function that serves as a key for the sort comparison\n", 269 | "\n", 270 | "Ex5) Let's use sorted( ).\n" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 53, 276 | "metadata": { 277 | "collapsed": false 278 | }, 279 | "outputs": [ 280 | { 281 | "name": "stdout", 282 | "output_type": "stream", 283 | "text": [ 284 | "['a', 'c', 'e', 'k']\n", 285 | "['C', 'a', 'e', 'k']\n", 286 | "['k', 'e', 'a', 'C']\n", 287 | "['k', 'e', 'c', 'a']\n" 288 | ] 289 | } 290 | ], 291 | "source": [ 292 | "# list\n", 293 | "List_a = ['c', 'a', 'k', 'e']\n", 294 | "print(sorted(List_a))\n", 295 | "\n", 296 | "# string \n", 297 | "String_a = 'Cake'\n", 298 | "print(sorted(String_a, reverse=False))\n", 299 | "\n", 300 | "# tuple\n", 301 | "Tuple_a = ('C', 'a', 'k', 'e')\n", 302 | "print(sorted(Tuple_a, reverse=True))\n", 303 | "\n", 304 | "# set\n", 305 | "Set_a = {'c', 'a', 'k', 'e'}\n", 306 | "print(sorted(Set_a, reverse=True))" 307 | ] 308 | }, 309 | { 310 | "cell_type": "markdown", 311 | "metadata": {}, 312 | "source": [ 313 | "## 3. Practice Problem" 314 | ] 315 | }, 316 | { 317 | "cell_type": "markdown", 318 | "metadata": {}, 319 | "source": [ 320 | "*Task: \n", 321 | "\n", 322 | ">Given the names and grades for each student in a Physics class of N students,\n", 323 | "\n", 324 | ">store them in a nested list, L and print the name(s) of any student(s) \n", 325 | "\n", 326 | ">having the second highest grade.\n", 327 | "\n", 328 | ">And print the nested list, L.\n", 329 | "\n", 330 | "> (If there are multiple students with the same grade, \n", 331 | "\n", 332 | "> order their names alphabetically and print each name on a new line)\n", 333 | "\n", 334 | "*Input\n", 335 | "> ex.\n", 336 | "\n", 337 | "> 5\n", 338 | "\n", 339 | "> Tony\n", 340 | "\n", 341 | "> 30.5\n", 342 | "\n", 343 | "> Sam\n", 344 | "\n", 345 | "> 20.5\n", 346 | "\n", 347 | "> Bob\n", 348 | "\n", 349 | "> 20.5\n", 350 | "\n", 351 | "> Peter\n", 352 | "\n", 353 | "> 10\n", 354 | "\n", 355 | "> Doris\n", 356 | "\n", 357 | "> 5\n", 358 | "\n", 359 | "*Output\n", 360 | "> ex.\n", 361 | "\n", 362 | "> Bob\n", 363 | "\n", 364 | "> Sam" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 2, 370 | "metadata": { 371 | "collapsed": false 372 | }, 373 | "outputs": [ 374 | { 375 | "name": "stdout", 376 | "output_type": "stream", 377 | "text": [ 378 | "5\n", 379 | "Tony\n", 380 | "30.5\n", 381 | "Sam\n", 382 | "20.5\n", 383 | "Bob\n", 384 | "20.5\n", 385 | "Peter\n", 386 | "10\n", 387 | "Doris\n", 388 | "5\n", 389 | "Bob\n", 390 | "Sam\n", 391 | "[['Tony', 30.5], ['Sam', 20.5], ['Bob', 20.5], ['Peter', 10.0], ['Doris', 5.0]]\n" 392 | ] 393 | } 394 | ], 395 | "source": [ 396 | "L = [[raw_input(), float(raw_input())] for i in xrange(int(raw_input()))]\n", 397 | "s = sorted(set([x[1] for x in a]), reverse = True)\n", 398 | "for name in sorted(x[0] for x in a if x[1] == s[1]):\n", 399 | " print name\n", 400 | " \n", 401 | "print(L)" 402 | ] 403 | }, 404 | { 405 | "cell_type": "markdown", 406 | "metadata": {}, 407 | "source": [ 408 | "## 4. Are you ready? \n", 409 | "### DIY at https://www.hackerrank.com/challenges/nested-list" 410 | ] 411 | } 412 | ], 413 | "metadata": { 414 | "anaconda-cloud": {}, 415 | "kernelspec": { 416 | "display_name": "Python [default]", 417 | "language": "python", 418 | "name": "python2" 419 | }, 420 | "language_info": { 421 | "codemirror_mode": { 422 | "name": "ipython", 423 | "version": 2 424 | }, 425 | "file_extension": ".py", 426 | "mimetype": "text/x-python", 427 | "name": "python", 428 | "nbconvert_exporter": "python", 429 | "pygments_lexer": "ipython2", 430 | "version": "2.7.12" 431 | } 432 | }, 433 | "nbformat": 4, 434 | "nbformat_minor": 1 435 | } 436 | -------------------------------------------------------------------------------- /Lab02-6_Finding-the-Percentage.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab02-6: Finding the percentage\n", 8 | "https://www.hackerrank.com/challenges/finding-the-percentage" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "## 1. Description of Problem" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "If we want to store values in pairs, how do we say in Python?\n", 23 | "\n", 24 | "This problem is to use a dictionary.\n", 25 | "\n", 26 | "*Task:\n", 27 | "\n", 28 | "> You have a record of N students. Each record contains the student's name, and their percent marks in Maths, Physics and Chemistry. The marks can be floating values.\n", 29 | "\n", 30 | "> The user enters some integer N followed by the names and marks for N students. You are required to save the record in a dictionary data type. The user then enters a student's name.\n", 31 | "\n", 32 | "> Output the average percentage marks obtained by that student, correct to two decimal places.\n", 33 | "\n", 34 | "*Input: ex.\n", 35 | "\n", 36 | "> 3\n", 37 | "\n", 38 | "> Tom 67 68 69\n", 39 | "\n", 40 | "> Joo 70 98 63\n", 41 | "\n", 42 | "> Andy 52 56 60\n", 43 | "\n", 44 | "> Andy\n", 45 | "\n", 46 | "*Output: ex.\n", 47 | "\n", 48 | "> 56.00" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "## 2. Concept & Short Examples" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "### Concept [1] How to create a dictionary?" 63 | ] 64 | }, 65 | { 66 | "cell_type": "markdown", 67 | "metadata": {}, 68 | "source": [ 69 | "Python dictionary is an unordered collection of items.\n", 70 | "\n", 71 | "While other compound data types have only value as an element, a dictionary has a key: value pair.\n", 72 | "\n", 73 | "Dictionaries are optimized to retrieve values when the key is known.\n", 74 | "\n", 75 | "Creating a dictionary is as simple as placing items inside curly braces {} separated by comma.\n", 76 | "\n", 77 | "An item has a key and the corresponding value expressed as a pair, key: value.\n", 78 | "\n", 79 | "While values can be of any data type and can repeat, keys must be of immutable type (string, number or tuple with immutable elements) and must be unique.\n", 80 | "\n", 81 | "Ex1) Let's take a look at some examples of creating a dictionary." 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 28, 87 | "metadata": { 88 | "collapsed": false 89 | }, 90 | "outputs": [ 91 | { 92 | "name": "stdout", 93 | "output_type": "stream", 94 | "text": [ 95 | "{}\n", 96 | "{1: 'milk', 2: 'cow'}\n", 97 | "{1: [7, 3, 4], 'name': 'Peter'}\n", 98 | "{1: 'milk', 2: 'cow'}\n", 99 | "{1: 'milk', 2: 'cow'}\n" 100 | ] 101 | } 102 | ], 103 | "source": [ 104 | "# empty dictionary\n", 105 | "my_d = {}\n", 106 | "print(my_d)\n", 107 | "\n", 108 | "# dictionary with integer keys\n", 109 | "my_d = {1: 'milk', 2: 'cow'}\n", 110 | "print(my_d)\n", 111 | "\n", 112 | "# dictionary with mixed keys\n", 113 | "my_d = {'name': 'Peter', 1: [7, 3, 4]}\n", 114 | "print(my_d)\n", 115 | "\n", 116 | "# using dict()\n", 117 | "my_d = dict({1:'milk', 2:'cow'})\n", 118 | "print(my_d)\n", 119 | "\n", 120 | "# from sequence having each item as a pair\n", 121 | "my_d = dict([(1,'milk'), (2,'cow')])\n", 122 | "print(my_d)" 123 | ] 124 | }, 125 | { 126 | "cell_type": "markdown", 127 | "metadata": {}, 128 | "source": [ 129 | "### Concept [2] How to access elements from a dictionary?" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "While indexing is used with other container types to access values, dictionary uses keys.\n", 137 | "\n", 138 | "Key can be used either inside square brackets or with the get() method.\n", 139 | "\n", 140 | "The difference while using get() is that it returns None instead of KeyError, if the key is not found.\n", 141 | "\n", 142 | "Ex2) Let's access elements from a dictionary." 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 29, 148 | "metadata": { 149 | "collapsed": false 150 | }, 151 | "outputs": [ 152 | { 153 | "name": "stdout", 154 | "output_type": "stream", 155 | "text": [ 156 | "EJ\n", 157 | "22\n" 158 | ] 159 | }, 160 | { 161 | "ename": "KeyError", 162 | "evalue": "'address'", 163 | "output_type": "error", 164 | "traceback": [ 165 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 166 | "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", 167 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[1;31m# Trying to access keys which doesn't exist throws Keyerror\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0mmy_d\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'address'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 168 | "\u001b[0;31mKeyError\u001b[0m: 'address'" 169 | ] 170 | } 171 | ], 172 | "source": [ 173 | "my_d = {'name':'EJ', 'age': 22}\n", 174 | "\n", 175 | "# Output: EJ\n", 176 | "print(my_d['name'])\n", 177 | "\n", 178 | "# Output: 22\n", 179 | "print(my_d.get('age'))\n", 180 | "\n", 181 | "# using get(): returns None instead of KeyError\n", 182 | "my_d.get('address')\n", 183 | "\n", 184 | "# Trying to access keys which doesn't exist throws Keyerror\n", 185 | "my_d['address']" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "### Concept [3] How to change or add elements in a dictionary?" 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "Dictionary are mutable. We can add new items or change the value of existing items using assignment operator.\n", 200 | "\n", 201 | "If the key is already present, value gets updated, else a new key: value pair is added to the dictionary.\n", 202 | "\n", 203 | "Ex3) Let's change or add elements in a dictionary." 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": 30, 209 | "metadata": { 210 | "collapsed": false 211 | }, 212 | "outputs": [ 213 | { 214 | "name": "stdout", 215 | "output_type": "stream", 216 | "text": [ 217 | "{'age': 27, 'name': 'EJ'}\n", 218 | "{'age': 27, 'name': 'EJ', 'address': 'Central'}\n" 219 | ] 220 | } 221 | ], 222 | "source": [ 223 | "my_d = {'name':'EJ', 'age': 22}\n", 224 | "\n", 225 | "# update value\n", 226 | "my_d['age'] = 27\n", 227 | "\n", 228 | "# Output: {'age': 27, 'name': 'EJ'}\n", 229 | "print(my_d)\n", 230 | "\n", 231 | "# add item\n", 232 | "my_d['address'] = 'Central' \n", 233 | "\n", 234 | "# Output: {'age': 27, 'name': 'EJ', 'address': 'Central'}\n", 235 | "print(my_d)" 236 | ] 237 | }, 238 | { 239 | "cell_type": "markdown", 240 | "metadata": {}, 241 | "source": [ 242 | "### Concept [4] How to delete or remove elements from a dictionary?" 243 | ] 244 | }, 245 | { 246 | "cell_type": "markdown", 247 | "metadata": {}, 248 | "source": [ 249 | "We can remove a particular item in a dictionary by using the method pop(). This method removes as item with the provided key and returns the value.\n", 250 | "\n", 251 | "The method, popitem() can be used to remove and return an arbitrary item (key, value) from the dictionary. All the items can be removed at once using the clear() method.\n", 252 | "\n", 253 | "We can also use the del keyword to remove individual items or the entire dictionary itself.\n", 254 | "\n", 255 | "Ex4) Let's delete or remove elements from a dictionary." 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 31, 261 | "metadata": { 262 | "collapsed": false 263 | }, 264 | "outputs": [ 265 | { 266 | "name": "stdout", 267 | "output_type": "stream", 268 | "text": [ 269 | "16\n", 270 | "{1: 1, 2: 4, 3: 9, 5: 25}\n", 271 | "(1, 1)\n", 272 | "{2: 4, 3: 9, 5: 25}\n", 273 | "{2: 4, 3: 9}\n", 274 | "{}\n" 275 | ] 276 | }, 277 | { 278 | "ename": "NameError", 279 | "evalue": "name 'squares' is not defined", 280 | "output_type": "error", 281 | "traceback": [ 282 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 283 | "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", 284 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 32\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 33\u001b[0m \u001b[1;31m# Throws Error\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m---> 34\u001b[0;31m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msquares\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 285 | "\u001b[0;31mNameError\u001b[0m: name 'squares' is not defined" 286 | ] 287 | } 288 | ], 289 | "source": [ 290 | "# create a dictionary\n", 291 | "squares = {1:1, 2:4, 3:9, 4:16, 5:25} \n", 292 | "\n", 293 | "# remove a particular item\n", 294 | "# Output: 16\n", 295 | "print(squares.pop(4)) \n", 296 | "\n", 297 | "# Output: {1: 1, 2: 4, 3: 9, 5: 25}\n", 298 | "print(squares)\n", 299 | "\n", 300 | "# remove an arbitrary item\n", 301 | "# Output: (1, 1)\n", 302 | "print(squares.popitem())\n", 303 | "\n", 304 | "# Output: {2: 4, 3: 9, 5: 25}\n", 305 | "print(squares)\n", 306 | "\n", 307 | "# delete a particular item\n", 308 | "del squares[5] \n", 309 | "\n", 310 | "# Output: {2: 4, 3: 9}\n", 311 | "print(squares)\n", 312 | "\n", 313 | "# remove all items\n", 314 | "squares.clear()\n", 315 | "\n", 316 | "# Output: {}\n", 317 | "print(squares)\n", 318 | "\n", 319 | "# delete the dictionary itself\n", 320 | "del squares\n", 321 | "\n", 322 | "# Throws Error\n", 323 | "print(squares)" 324 | ] 325 | }, 326 | { 327 | "cell_type": "markdown", 328 | "metadata": {}, 329 | "source": [ 330 | "## 3. Practice Problem" 331 | ] 332 | }, 333 | { 334 | "cell_type": "markdown", 335 | "metadata": {}, 336 | "source": [ 337 | "You have a record of N students. Each record contains the student's name, and their percent marks in Maths and Physics. The marks can be floating values.\n", 338 | "\n", 339 | "The user enters some integer N followed by the names and marks for N students. You are required to save the record in a dictionary data type. The user then enters a student's name.\n", 340 | "\n", 341 | "Output the average percentage marks obtained by that student, correct to three decimal places.\n", 342 | "\n", 343 | "*Input: ex.\n", 344 | "\n", 345 | "> 3\n", 346 | "\n", 347 | "> Tom 67 68\n", 348 | "\n", 349 | "> Joo 70 98\n", 350 | "\n", 351 | "> Andy 52 56\n", 352 | "\n", 353 | "> Andy\n", 354 | "\n", 355 | "*Output: ex.\n", 356 | "\n", 357 | "> 54.000" 358 | ] 359 | }, 360 | { 361 | "cell_type": "markdown", 362 | "metadata": { 363 | "collapsed": true 364 | }, 365 | "source": [ 366 | "*Hint 1. \"print\" treats the % as a special character you need to add. When you type \"f\", the number (result) that will be printed will be a floating point type. \".3\" tells your \"print\" to print only the first 3 digits after the point." 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 32, 372 | "metadata": { 373 | "collapsed": false 374 | }, 375 | "outputs": [ 376 | { 377 | "name": "stdout", 378 | "output_type": "stream", 379 | "text": [ 380 | "My height is 180.574 cm. Your height is 160.38 cm.\n" 381 | ] 382 | } 383 | ], 384 | "source": [ 385 | "height_m = 180.573567\n", 386 | "height_y = 160.382034\n", 387 | "print('My height is %.3f cm. Your height is %.2f cm.' % (height_m, height_y))" 388 | ] 389 | }, 390 | { 391 | "cell_type": "code", 392 | "execution_count": 27, 393 | "metadata": { 394 | "collapsed": false 395 | }, 396 | "outputs": [ 397 | { 398 | "name": "stdout", 399 | "output_type": "stream", 400 | "text": [ 401 | "3\n", 402 | "Tom 67 68\n", 403 | "Joo 70 98\n", 404 | "Andy 52 56\n", 405 | "Andy\n", 406 | "54.000\n" 407 | ] 408 | } 409 | ], 410 | "source": [ 411 | "d={}\n", 412 | "for i in range(int(raw_input())):\n", 413 | "\tline= raw_input().split()\n", 414 | "\td[line[0]] = sum(map(float,line[1:])) / 2\n", 415 | "\n", 416 | "print '%.3f' % d[raw_input()]" 417 | ] 418 | }, 419 | { 420 | "cell_type": "markdown", 421 | "metadata": {}, 422 | "source": [ 423 | "## 4. Are you ready? \n", 424 | "### DIY at https://www.hackerrank.com/challenges/finding-the-percentage" 425 | ] 426 | } 427 | ], 428 | "metadata": { 429 | "anaconda-cloud": {}, 430 | "kernelspec": { 431 | "display_name": "Python [default]", 432 | "language": "python", 433 | "name": "python2" 434 | }, 435 | "language_info": { 436 | "codemirror_mode": { 437 | "name": "ipython", 438 | "version": 2 439 | }, 440 | "file_extension": ".py", 441 | "mimetype": "text/x-python", 442 | "name": "python", 443 | "nbconvert_exporter": "python", 444 | "pygments_lexer": "ipython2", 445 | "version": "2.7.12" 446 | } 447 | }, 448 | "nbformat": 4, 449 | "nbformat_minor": 1 450 | } 451 | -------------------------------------------------------------------------------- /Lab03----------Strings----------.txt: -------------------------------------------------------------------------------- 1 | Lab3 Strings -------------------------------------------------------------------------------- /Lab03-1_What's-Your-Name.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# Lab03-1: What's Your Name?\n", 10 | "https://www.hackerrank.com/challenges/whats-your-name" 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "## 1. Description of Problem" 18 | ] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "If we want to print some of the contents differently depending on the input value and the rest of the output equally, how do we say in Python?\n", 25 | "\n", 26 | "This problem is formatting Strings.\n", 27 | "\n", 28 | "*Task: You are given the firstname and lastname of a person on two different lines. Your task is to read them and print the following.\n", 29 | "\n", 30 | "> Hello (firstname) (lastname)! You just delved into python.\n", 31 | "\n", 32 | "*Input: The first line contains the first name, and the second line contains the last name.\n", 33 | "\n", 34 | "> ex.\n", 35 | "\n", 36 | "> Top\n", 37 | "\n", 38 | "> Choi\n", 39 | "\n", 40 | "*Output: Print the output as mentioned above.\n", 41 | "\n", 42 | "> ex.\n", 43 | "\n", 44 | "> Hello Top Choi! You just delved into python." 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "## 2. Concept & Short Examples" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "### Concept [1] The basics of String" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "#### *How to create a string?\n", 66 | "Strings can be created by enclosing characters inside a single quote or double quotes. Even triple quotes can be used in Python but generally used to represent multiline strings and docstrings.\n", 67 | "\n", 68 | "Ex1) Let's create some strings." 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 52, 74 | "metadata": { 75 | "collapsed": false 76 | }, 77 | "outputs": [ 78 | { 79 | "name": "stdout", 80 | "output_type": "stream", 81 | "text": [ 82 | "Hello\n", 83 | "Hello\n", 84 | "Hello\n", 85 | "Hello, welcome to\n", 86 | " the world of Python\n" 87 | ] 88 | } 89 | ], 90 | "source": [ 91 | "# all of the following are equivalent\n", 92 | "my_str = 'Hello'\n", 93 | "print(my_str)\n", 94 | "\n", 95 | "my_str = \"Hello\"\n", 96 | "print(my_str)\n", 97 | "\n", 98 | "my_str = '''Hello'''\n", 99 | "print(my_str)\n", 100 | "\n", 101 | "# triple quotes string can extend multiple lines\n", 102 | "my_str = \"\"\"Hello, welcome to\n", 103 | " the world of Python\"\"\"\n", 104 | "print(my_str)" 105 | ] 106 | }, 107 | { 108 | "cell_type": "markdown", 109 | "metadata": {}, 110 | "source": [ 111 | "#### *How to access characters in a string?\n", 112 | "We can access individual characters using indexing and a range of characters using slicing. Index starts from 0. Trying to access a character out of index range will raise an IndexError. The index must be an integer. We can't use float or other types, this will result into TypeError.\n", 113 | "\n", 114 | "Python allows negative indexing for its sequences.\n", 115 | "\n", 116 | "The index of -1 refers to the last item, -2 to the second last item and so on. We can access a range of items in a string by using the slicing operator (colon).\n", 117 | "\n", 118 | "Ex2) Let's access characters in a string." 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 53, 124 | "metadata": { 125 | "collapsed": false 126 | }, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "('str = ', 'chocolate')\n", 133 | "('str[0] = ', 'c')\n", 134 | "('str[-1] = ', 'e')\n", 135 | "('str[1:5] = ', 'hoco')\n", 136 | "('str[5:-2] = ', 'la')\n" 137 | ] 138 | } 139 | ], 140 | "source": [ 141 | "str = \"chocolate\"\n", 142 | "print('str = ', str)\n", 143 | "\n", 144 | "#first character\n", 145 | "print('str[0] = ', str[0])\n", 146 | "\n", 147 | "#last character\n", 148 | "print('str[-1] = ', str[-1])\n", 149 | "\n", 150 | "#slicing 2nd to 5th character\n", 151 | "print('str[1:5] = ', str[1:5])\n", 152 | "\n", 153 | "#slicing 6th to 2nd last character\n", 154 | "print('str[5:-2] = ', str[5:-2])" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "#### *How to change or delete a string?\n", 162 | "\n", 163 | "Strings are immutable. This means that elements of a string cannot be changed once it has been assigned. We can simply reassign different strings to the same name.\n", 164 | "\n", 165 | "We cannot delete or remove characters from a string. But deleting the string entirely is possible using the keyword del.\n", 166 | "\n", 167 | "Ex3) Let's change or delete a string." 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 54, 173 | "metadata": { 174 | "collapsed": false 175 | }, 176 | "outputs": [ 177 | { 178 | "name": "stdout", 179 | "output_type": "stream", 180 | "text": [ 181 | "airplane\n", 182 | "helicopter\n" 183 | ] 184 | }, 185 | { 186 | "ename": "TypeError", 187 | "evalue": "'str' object does not support item assignment", 188 | "output_type": "error", 189 | "traceback": [ 190 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 191 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 192 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[1;31m#TypeError: 'str' object does not support item assignment\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 8\u001b[0;31m \u001b[0mmy_string\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'e'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", 193 | "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" 194 | ] 195 | } 196 | ], 197 | "source": [ 198 | "my_string = 'airplane'\n", 199 | "print(my_string)\n", 200 | "\n", 201 | "my_string = 'helicopter'\n", 202 | "print(my_string)\n", 203 | "\n", 204 | "#TypeError: 'str' object does not support item assignment\n", 205 | "my_string[5] = 'e'" 206 | ] 207 | }, 208 | { 209 | "cell_type": "markdown", 210 | "metadata": {}, 211 | "source": [ 212 | "### Concept [2] Concatenation of Two or More Strings" 213 | ] 214 | }, 215 | { 216 | "cell_type": "markdown", 217 | "metadata": {}, 218 | "source": [ 219 | "Joining of two or more strings into a single one is called concatenation.\n", 220 | "\n", 221 | "The + operator does this in Python. Simply writing two string literals together also concatenates them.\n", 222 | "\n", 223 | "The * operator can be used to repeat the string for a given number of times.\n", 224 | "\n", 225 | "Ex4) Let's join two or more strings into a single one." 226 | ] 227 | }, 228 | { 229 | "cell_type": "code", 230 | "execution_count": 55, 231 | "metadata": { 232 | "collapsed": false 233 | }, 234 | "outputs": [ 235 | { 236 | "name": "stdout", 237 | "output_type": "stream", 238 | "text": [ 239 | "('str1 + str2 = ', 'Sunday')\n", 240 | "('str1 * 3 =', 'SunSunSun')\n" 241 | ] 242 | } 243 | ], 244 | "source": [ 245 | "str1 = 'Sun'\n", 246 | "str2 ='day'\n", 247 | "\n", 248 | "# using +\n", 249 | "print('str1 + str2 = ', str1 + str2)\n", 250 | "\n", 251 | "# using *\n", 252 | "print('str1 * 3 =', str1 * 3)" 253 | ] 254 | }, 255 | { 256 | "cell_type": "markdown", 257 | "metadata": {}, 258 | "source": [ 259 | "### Concept [3] format() Method for Formatting Strings" 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "The format() method that is available with the string object is very versatile and powerful in formatting strings. Format strings contains curly braces {} as placeholders or replacement fields which gets replaced.\n", 267 | "\n", 268 | "We can use positional arguments or keyword arguments to specify the order.\n", 269 | "\n", 270 | "Ex5) Let's use format()." 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 56, 276 | "metadata": { 277 | "collapsed": false 278 | }, 279 | "outputs": [ 280 | { 281 | "name": "stdout", 282 | "output_type": "stream", 283 | "text": [ 284 | " Default Order: \n", 285 | "One, Two and Three\n", 286 | "\n", 287 | " Positional Order: \n", 288 | "Two, One and Three\n", 289 | "\n", 290 | " Keyword Order: \n", 291 | "Three, Two and One\n" 292 | ] 293 | } 294 | ], 295 | "source": [ 296 | "# default(implicit) order\n", 297 | "default_order = \"{}, {} and {}\".format('One','Two','Three')\n", 298 | "print(' Default Order: ')\n", 299 | "print(default_order)\n", 300 | "\n", 301 | "# order using positional argument\n", 302 | "positional_order = \"{1}, {0} and {2}\".format('One','Two','Three')\n", 303 | "print('\\n Positional Order: ')\n", 304 | "print(positional_order)\n", 305 | "\n", 306 | "# order using keyword argument\n", 307 | "keyword_order = \"{c}, {b} and {a}\".format(a='One',b='Two',c='Three')\n", 308 | "print('\\n Keyword Order: ')\n", 309 | "print(keyword_order)" 310 | ] 311 | }, 312 | { 313 | "cell_type": "markdown", 314 | "metadata": {}, 315 | "source": [ 316 | "The format() method can have optional format specifications.\n", 317 | "\n", 318 | "They are separated from field name using colon. For example, we can left-justify <, right-justify > or center ^ a string in the given space. \n", 319 | "\n", 320 | "We can also format integers as binary, hexadecimal etc. and floats can be rounded or displayed in the exponent format.\n", 321 | "\n", 322 | "Ex6) Let's use optional format specifications." 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": 57, 328 | "metadata": { 329 | "collapsed": false 330 | }, 331 | "outputs": [ 332 | { 333 | "name": "stdout", 334 | "output_type": "stream", 335 | "text": [ 336 | "Binary representation of 12 is 1100\n", 337 | "Exponent representation: 1.566345e+03\n", 338 | "One third is: 0.333\n", 339 | "|butter | bread | ham|\n" 340 | ] 341 | } 342 | ], 343 | "source": [ 344 | "# formatting integers\n", 345 | "binary = \"Binary representation of {0} is {0:b}\".format(12)\n", 346 | "print(binary)\n", 347 | "\n", 348 | "# formatting floats\n", 349 | "exponent = \"Exponent representation: {0:e}\".format(1566.345)\n", 350 | "print(exponent)\n", 351 | "\n", 352 | "# round off\n", 353 | "round = \"One third is: {0:.3f}\".format(1.0/3)\n", 354 | "print(round)\n", 355 | "\n", 356 | "# string alignment\n", 357 | "alignment = \"|{:<10}|{:^10}|{:>10}|\".format('butter','bread','ham')\n", 358 | "print(alignment)" 359 | ] 360 | }, 361 | { 362 | "cell_type": "markdown", 363 | "metadata": {}, 364 | "source": [ 365 | "### Concept [4] Old style formatting" 366 | ] 367 | }, 368 | { 369 | "cell_type": "markdown", 370 | "metadata": {}, 371 | "source": [ 372 | "We can even format strings like the old sprintf() style used in C programming language. We use the % operator to accomplish this.\n", 373 | "\n", 374 | "Ex7) Let's use % operator for formatting." 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "execution_count": 58, 380 | "metadata": { 381 | "collapsed": false 382 | }, 383 | "outputs": [ 384 | { 385 | "name": "stdout", 386 | "output_type": "stream", 387 | "text": [ 388 | "The value of num is 12.35\n", 389 | "The value of num is 12.3457\n", 390 | "name: Vicky. age: 25. height: 170.143600cm.\n", 391 | "name: Vicky. age: 25. height: 170cm.\n", 392 | "name: Vicky. age: 25.00. height: 170.144cm.\n" 393 | ] 394 | } 395 | ], 396 | "source": [ 397 | "num = 12.3456789\n", 398 | "print('The value of num is %3.2f' %num)\n", 399 | "print('The value of num is %3.4f' %num)\n", 400 | "\n", 401 | "name = 'Vicky'\n", 402 | "age = 25\n", 403 | "height = 170.1436\n", 404 | "print('name: %s. age: %d. height: %fcm.' % (name, age, height))\n", 405 | "print('name: %s. age: %d. height: %dcm.' % (name, age, height))\n", 406 | "print('name: %6s. age: %.2f. height: %.3fcm.'% (name, age, height))" 407 | ] 408 | }, 409 | { 410 | "cell_type": "markdown", 411 | "metadata": {}, 412 | "source": [ 413 | "## 3. Practice Problem" 414 | ] 415 | }, 416 | { 417 | "cell_type": "markdown", 418 | "metadata": {}, 419 | "source": [ 420 | "You are given the firstname and hobby of a person on two different lines. Your task is to read them and print the following.\n", 421 | "\n", 422 | "> Hello, (firstname)! You like (hobby). Let's talk about it.\n", 423 | "\n", 424 | "*Input: The first line contains the first name, and the second line contains the hobby.\n", 425 | "\n", 426 | "> ex.\n", 427 | "\n", 428 | "> Vivian\n", 429 | "\n", 430 | "> painting\n", 431 | "\n", 432 | "*Output: Print the output as mentioned above.\n", 433 | "\n", 434 | "> ex.\n", 435 | "\n", 436 | "> Hello, Vivian! You like painting. Let's talk about it." 437 | ] 438 | }, 439 | { 440 | "cell_type": "code", 441 | "execution_count": 49, 442 | "metadata": { 443 | "collapsed": false 444 | }, 445 | "outputs": [ 446 | { 447 | "name": "stdout", 448 | "output_type": "stream", 449 | "text": [ 450 | "Vivian\n", 451 | "painting\n", 452 | "Hello, Vivian! You like painting. Let's talk about it.\n" 453 | ] 454 | } 455 | ], 456 | "source": [ 457 | "a = raw_input()\n", 458 | "b = raw_input()\n", 459 | "print \"Hello, %s! You like %s. Let's talk about it.\" % (a, b)" 460 | ] 461 | }, 462 | { 463 | "cell_type": "code", 464 | "execution_count": 50, 465 | "metadata": { 466 | "collapsed": false 467 | }, 468 | "outputs": [ 469 | { 470 | "name": "stdout", 471 | "output_type": "stream", 472 | "text": [ 473 | "Doris\n", 474 | "walking\n", 475 | "Hello, Doris! You like walking. Let's talk about it.\n" 476 | ] 477 | } 478 | ], 479 | "source": [ 480 | "a = raw_input()\n", 481 | "b = raw_input()\n", 482 | "print \"Hello, \" + a + \"! You like \" + b + \". Let's talk about it.\"" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": 51, 488 | "metadata": { 489 | "collapsed": false 490 | }, 491 | "outputs": [ 492 | { 493 | "name": "stdout", 494 | "output_type": "stream", 495 | "text": [ 496 | "Aron\n", 497 | "cooking\n", 498 | "Hello, Aron! You like cooking. Let's talk about it.\n" 499 | ] 500 | } 501 | ], 502 | "source": [ 503 | "a = raw_input()\n", 504 | "b = raw_input()\n", 505 | "print(\"Hello, {0}! You like {1}. Let's talk about it.\".format(a, b))" 506 | ] 507 | }, 508 | { 509 | "cell_type": "markdown", 510 | "metadata": {}, 511 | "source": [ 512 | "## 4. Are you ready? \n", 513 | "### DIY at https://www.hackerrank.com/challenges/whats-your-name" 514 | ] 515 | } 516 | ], 517 | "metadata": { 518 | "anaconda-cloud": {}, 519 | "kernelspec": { 520 | "display_name": "Python [default]", 521 | "language": "python", 522 | "name": "python2" 523 | }, 524 | "language_info": { 525 | "codemirror_mode": { 526 | "name": "ipython", 527 | "version": 2 528 | }, 529 | "file_extension": ".py", 530 | "mimetype": "text/x-python", 531 | "name": "python", 532 | "nbconvert_exporter": "python", 533 | "pygments_lexer": "ipython2", 534 | "version": "2.7.12" 535 | } 536 | }, 537 | "nbformat": 4, 538 | "nbformat_minor": 1 539 | } 540 | -------------------------------------------------------------------------------- /Lab03-2_sWAP-cASE.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Lab03-2: sWAP cASE\n", 8 | "https://www.hackerrank.com/challenges/swap-case" 9 | ] 10 | }, 11 | { 12 | "cell_type": "markdown", 13 | "metadata": {}, 14 | "source": [ 15 | "## 1. Description of Problem" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "If we want to swap lower case letters to upper case letters and vice versa, how do we say in Python?\n", 23 | "\n", 24 | "This problem is to use the method string.swapcase.\n", 25 | "\n", 26 | "You are given a string S. Your task is to swap cases. In other words, convert all lowercase letters to uppercase letters and vice versa.\n", 27 | "\n", 28 | "*Input: A single line containing a string .\n", 29 | "\n", 30 | "> ex. HackerRank.com presents \"Pythonist 2\".\n", 31 | "\n", 32 | "*Output: Print the modified string S.\n", 33 | "\n", 34 | "> ex. hACKERrANK.COM PRESENTS \"pYTHONIST 2\"." 35 | ] 36 | }, 37 | { 38 | "cell_type": "markdown", 39 | "metadata": {}, 40 | "source": [ 41 | "## 2. Concept & Short Examples" 42 | ] 43 | }, 44 | { 45 | "cell_type": "markdown", 46 | "metadata": {}, 47 | "source": [ 48 | "### Concept [1] String swapcase( )" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | "The string swapcase() method converts all uppercase characters to lowercase and all lowercase characters to uppercase characters of the given string, and returns it.\n", 56 | "\n", 57 | "The format of swapcase() method is:\n", 58 | "\n", 59 | "> string.swapcase()\n", 60 | "\n", 61 | "String swapcase() Parameters():\n", 62 | "\n", 63 | "> The swapcase() method doesn't take any parameters.\n", 64 | "\n", 65 | "Return value from String swapcase():\n", 66 | "\n", 67 | "> The swapcase() method returns the string where all uppercase characters are converted to lowercase, and lowercase characters are converted to uppercase. \n", 68 | "\n", 69 | "Ex1) We want to swap lowercase to uppercase and vice versa using swapcase( )." 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": 5, 75 | "metadata": { 76 | "collapsed": false 77 | }, 78 | "outputs": [ 79 | { 80 | "name": "stdout", 81 | "output_type": "stream", 82 | "text": [ 83 | "nothing is stronger than habit.\n", 84 | "NOTHING IS STRONGER THAN HABIT.\n", 85 | "nOtHiNg iS sTrOnGeR tHaN hAbIt.\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "str_a = \"NOTHING IS STRONGER THAN HABIT.\"\n", 91 | "print(str_a.swapcase())\n", 92 | "\n", 93 | "str_b = \"nothing is stronger than habit.\"\n", 94 | "print(str_b.swapcase())\n", 95 | "\n", 96 | "str_c = \"NoThInG Is StRoNgEr ThAn HaBiT.\"\n", 97 | "print(str_c.swapcase())" 98 | ] 99 | }, 100 | { 101 | "cell_type": "markdown", 102 | "metadata": {}, 103 | "source": [ 104 | "Note: If you want to convert string to lowercase only, use lower(). Likewise, if you want to convert string to uppercase only, use upper().\n", 105 | "\n", 106 | "Ex2) Let's use lower(), upper()." 107 | ] 108 | }, 109 | { 110 | "cell_type": "code", 111 | "execution_count": 6, 112 | "metadata": { 113 | "collapsed": false 114 | }, 115 | "outputs": [ 116 | { 117 | "name": "stdout", 118 | "output_type": "stream", 119 | "text": [ 120 | "nothing is stronger than habit.\n", 121 | "nothing is stronger than habit.\n", 122 | "nothing is stronger than habit.\n", 123 | "NOTHING IS STRONGER THAN HABIT.\n", 124 | "NOTHING IS STRONGER THAN HABIT.\n", 125 | "NOTHING IS STRONGER THAN HABIT.\n" 126 | ] 127 | } 128 | ], 129 | "source": [ 130 | "str_a = \"NOTHING IS STRONGER THAN HABIT.\"\n", 131 | "print(str_a.lower())\n", 132 | "str_b = \"nothing is stronger than habit.\"\n", 133 | "print(str_b.lower())\n", 134 | "str_c = \"NoThInG Is StRoNgEr ThAn HaBiT.\"\n", 135 | "print(str_c.lower())\n", 136 | "\n", 137 | "str_a = \"NOTHING IS STRONGER THAN HABIT.\"\n", 138 | "print(str_a.upper())\n", 139 | "str_b = \"nothing is stronger than habit.\"\n", 140 | "print(str_b.upper())\n", 141 | "str_c = \"NoThInG Is StRoNgEr ThAn HaBiT.\"\n", 142 | "print(str_c.upper())" 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "## 3. Practice Problem" 150 | ] 151 | }, 152 | { 153 | "cell_type": "markdown", 154 | "metadata": {}, 155 | "source": [ 156 | "You are given a string S. Your task is to swap cases. In other words, convert all lowercase letters to uppercase letters and vice versa.\n", 157 | "\n", 158 | "*Input: A single line containing a string .\n", 159 | "\n", 160 | "> ex. Paradise is where I am.\n", 161 | "\n", 162 | "*Output: Print the modified string S.\n", 163 | "\n", 164 | "> ex. pARADISE IS WHERE i AM." 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 4, 170 | "metadata": { 171 | "collapsed": false 172 | }, 173 | "outputs": [ 174 | { 175 | "name": "stdout", 176 | "output_type": "stream", 177 | "text": [ 178 | "Paradise is where I am.\n", 179 | "pARADISE IS WHERE i AM.\n" 180 | ] 181 | } 182 | ], 183 | "source": [ 184 | "import string\n", 185 | "print string.swapcase(raw_input())" 186 | ] 187 | }, 188 | { 189 | "cell_type": "markdown", 190 | "metadata": {}, 191 | "source": [ 192 | "## 4. Are you ready? \n", 193 | "### DIY at https://www.hackerrank.com/challenges/swap-case" 194 | ] 195 | } 196 | ], 197 | "metadata": { 198 | "anaconda-cloud": {}, 199 | "kernelspec": { 200 | "display_name": "Python [default]", 201 | "language": "python", 202 | "name": "python2" 203 | }, 204 | "language_info": { 205 | "codemirror_mode": { 206 | "name": "ipython", 207 | "version": 2 208 | }, 209 | "file_extension": ".py", 210 | "mimetype": "text/x-python", 211 | "name": "python", 212 | "nbconvert_exporter": "python", 213 | "pygments_lexer": "ipython2", 214 | "version": "2.7.12" 215 | } 216 | }, 217 | "nbformat": 4, 218 | "nbformat_minor": 1 219 | } 220 | -------------------------------------------------------------------------------- /Lab04----------Functions----------.txt: -------------------------------------------------------------------------------- 1 | Lab4 Functions 2 | 3 | > Built-Ins, Python Functionals -------------------------------------------------------------------------------- /Lab05----------Class----------.txt: -------------------------------------------------------------------------------- 1 | Lab5 Class 2 | 3 | > Object, Class, Inheritance, Overloading -------------------------------------------------------------------------------- /Lab06----------Advanced-Topics----------.txt: -------------------------------------------------------------------------------- 1 | Lab6 Advanced Topics 2 | 3 | > Closures, Decorators, etc. -------------------------------------------------------------------------------- /Lab07----------Numpy----------.txt: -------------------------------------------------------------------------------- 1 | Lab7 Numpy -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PythonZeroToAll 2 | # 3 | ## Table Of Contents 4 | 5 | ### Lab1 Introduction 6 | ##### > Datatypes, I/O, Import, Operators 7 | ##### > Flow Control: if-else, for Loop, while Loop 8 | 9 | ### Lab2 Basic DataTypes 10 | ##### > List, Tuple, Dictionary, Set, etc. 11 | 12 | ### Lab3 Strings 13 | 14 | ### Lab4 Functions 15 | ##### > Built-Ins, Python Functionals 16 | 17 | ### Lab5 Class 18 | ##### > Object, Class, Inheritance, Overloading 19 | 20 | ### Lab6 Advanced Topics 21 | ##### > Closures, Decorators, etc. 22 | 23 | ### Lab7 Numpy 24 | 25 | # 26 | ## References 27 | ##### https://www.hackerrank.com/domains/python/ 28 | ##### https://www.programiz.com/python-programming/ 29 | ##### https://docs.python.org/3/tutorial/index.html 30 | ##### https://www.codecademy.com/learn/python 31 | ##### https://leetcode.com/ 32 | ##### https://www.datascienceschool.net/view-notebook/661128713b654edc928ecb455a826b1d/ 33 | ##### Learn to Program with Python (Irv Kalb) 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /image/Lab01-5_1_RelationalOperator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hunkim/PythonZeroToAll/4e094eaa9f4cd9c076d9ca2a5b0b7f23e8c6cbb4/image/Lab01-5_1_RelationalOperator.png -------------------------------------------------------------------------------- /image/Lab01-5_2_IfElseDiagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hunkim/PythonZeroToAll/4e094eaa9f4cd9c076d9ca2a5b0b7f23e8c6cbb4/image/Lab01-5_2_IfElseDiagram.png -------------------------------------------------------------------------------- /image/Lab01-6_1_Function.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hunkim/PythonZeroToAll/4e094eaa9f4cd9c076d9ca2a5b0b7f23e8c6cbb4/image/Lab01-6_1_Function.png -------------------------------------------------------------------------------- /image/Lab01-7_1_ForLoopDiagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hunkim/PythonZeroToAll/4e094eaa9f4cd9c076d9ca2a5b0b7f23e8c6cbb4/image/Lab01-7_1_ForLoopDiagram.png -------------------------------------------------------------------------------- /image/Lab01-7_2_WhileLoopDiagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hunkim/PythonZeroToAll/4e094eaa9f4cd9c076d9ca2a5b0b7f23e8c6cbb4/image/Lab01-7_2_WhileLoopDiagram.png -------------------------------------------------------------------------------- /image/Lab01-7_3_AssignmentOperator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hunkim/PythonZeroToAll/4e094eaa9f4cd9c076d9ca2a5b0b7f23e8c6cbb4/image/Lab01-7_3_AssignmentOperator.png -------------------------------------------------------------------------------- /lab-01-1-pytorch_basics.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": { 6 | "collapsed": true 7 | }, 8 | "source": [ 9 | "# What is PyTorch?" 10 | ] 11 | }, 12 | { 13 | "cell_type": "markdown", 14 | "metadata": {}, 15 | "source": [ 16 | "It's a Python based scientific computing package targeted at two sets of audiences:\n", 17 | "* A replacement for numpy to use the power of GPUs\n", 18 | "* A deep learning research platform that provides maximum flexibility and speed" 19 | ] 20 | }, 21 | { 22 | "cell_type": "markdown", 23 | "metadata": {}, 24 | "source": [ 25 | "## Tensors" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "Tensors are similar to numpy's ndarrays, with the addition being that Tensors can also be used on a GPU to accelerate computing." 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 3, 38 | "metadata": { 39 | "collapsed": true 40 | }, 41 | "outputs": [], 42 | "source": [ 43 | "from __future__ import print_function\n", 44 | "import torch" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "Construct a 5x3 matrix, uninitialized:" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 5, 57 | "metadata": { 58 | "collapsed": false 59 | }, 60 | "outputs": [ 61 | { 62 | "name": "stdout", 63 | "output_type": "stream", 64 | "text": [ 65 | "\n", 66 | "1.00000e-36 *\n", 67 | " 0.0000 0.0000 0.0000\n", 68 | " 0.0000 0.0000 0.0000\n", 69 | " 0.4113 0.0000 0.0000\n", 70 | " 0.0000 0.0001 0.0000\n", 71 | " 1.8967 0.0000 0.0000\n", 72 | "[torch.FloatTensor of size 5x3]\n", 73 | "\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "x = torch.Tensor(5,3)\n", 79 | "print(x)" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "Construct a randomly initialized matrix" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 9, 92 | "metadata": { 93 | "collapsed": false 94 | }, 95 | "outputs": [ 96 | { 97 | "name": "stdout", 98 | "output_type": "stream", 99 | "text": [ 100 | "\n", 101 | " 0.4381 0.1222 0.1948\n", 102 | " 0.6345 0.0023 0.4593\n", 103 | " 0.2548 0.3231 0.5043\n", 104 | " 0.2990 0.9189 0.7335\n", 105 | " 0.0187 0.8618 0.4062\n", 106 | "[torch.FloatTensor of size 5x3]\n", 107 | "\n" 108 | ] 109 | } 110 | ], 111 | "source": [ 112 | "x = torch.rand(5,3)\n", 113 | "print(x)" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "Get its size" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 11, 126 | "metadata": { 127 | "collapsed": false 128 | }, 129 | "outputs": [ 130 | { 131 | "name": "stdout", 132 | "output_type": "stream", 133 | "text": [ 134 | "torch.Size([5, 3])\n" 135 | ] 136 | } 137 | ], 138 | "source": [ 139 | "print(x.size())" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "* Note: torch.Size is in fact a tuple, so it supports the same operations" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "## Operations" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | "There are multiple syntaxes for operations. Let's see addition as an example" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "Addition: syntax 1" 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "execution_count": 13, 173 | "metadata": { 174 | "collapsed": false 175 | }, 176 | "outputs": [ 177 | { 178 | "name": "stdout", 179 | "output_type": "stream", 180 | "text": [ 181 | "\n", 182 | " 0.8535 0.6359 1.0222\n", 183 | " 0.6420 0.0091 0.6165\n", 184 | " 0.9360 0.5439 0.9757\n", 185 | " 1.1209 1.2988 1.6813\n", 186 | " 0.4131 1.4377 0.6229\n", 187 | "[torch.FloatTensor of size 5x3]\n", 188 | "\n" 189 | ] 190 | } 191 | ], 192 | "source": [ 193 | "y = torch.rand(5,3)\n", 194 | "print(x + y)" 195 | ] 196 | }, 197 | { 198 | "cell_type": "markdown", 199 | "metadata": {}, 200 | "source": [ 201 | "Addition: syntax2" 202 | ] 203 | }, 204 | { 205 | "cell_type": "code", 206 | "execution_count": 14, 207 | "metadata": { 208 | "collapsed": false 209 | }, 210 | "outputs": [ 211 | { 212 | "name": "stdout", 213 | "output_type": "stream", 214 | "text": [ 215 | "\n", 216 | " 0.8535 0.6359 1.0222\n", 217 | " 0.6420 0.0091 0.6165\n", 218 | " 0.9360 0.5439 0.9757\n", 219 | " 1.1209 1.2988 1.6813\n", 220 | " 0.4131 1.4377 0.6229\n", 221 | "[torch.FloatTensor of size 5x3]\n", 222 | "\n" 223 | ] 224 | } 225 | ], 226 | "source": [ 227 | "print(torch.add(x,y))" 228 | ] 229 | }, 230 | { 231 | "cell_type": "markdown", 232 | "metadata": {}, 233 | "source": [ 234 | "Addition: giving an output tensor" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 16, 240 | "metadata": { 241 | "collapsed": false 242 | }, 243 | "outputs": [ 244 | { 245 | "name": "stdout", 246 | "output_type": "stream", 247 | "text": [ 248 | "\n", 249 | " 0.8535 0.6359 1.0222\n", 250 | " 0.6420 0.0091 0.6165\n", 251 | " 0.9360 0.5439 0.9757\n", 252 | " 1.1209 1.2988 1.6813\n", 253 | " 0.4131 1.4377 0.6229\n", 254 | "[torch.FloatTensor of size 5x3]\n", 255 | "\n" 256 | ] 257 | } 258 | ], 259 | "source": [ 260 | "result = torch.Tensor(5, 3)\n", 261 | "torch.add(x, y, out=result)\n", 262 | "print(result)" 263 | ] 264 | }, 265 | { 266 | "cell_type": "markdown", 267 | "metadata": {}, 268 | "source": [ 269 | "Addition: in-place" 270 | ] 271 | }, 272 | { 273 | "cell_type": "code", 274 | "execution_count": 20, 275 | "metadata": { 276 | "collapsed": false 277 | }, 278 | "outputs": [ 279 | { 280 | "name": "stdout", 281 | "output_type": "stream", 282 | "text": [ 283 | "\n", 284 | " 1.2916 0.7581 1.2170\n", 285 | " 1.2765 0.0114 1.0757\n", 286 | " 1.1908 0.8670 1.4800\n", 287 | " 1.4199 2.2177 2.4148\n", 288 | " 0.4318 2.2995 1.0290\n", 289 | "[torch.FloatTensor of size 5x3]\n", 290 | "\n" 291 | ] 292 | } 293 | ], 294 | "source": [ 295 | "# adds x to y\n", 296 | "y.add_(x)\n", 297 | "print(y)" 298 | ] 299 | }, 300 | { 301 | "cell_type": "markdown", 302 | "metadata": {}, 303 | "source": [ 304 | "* Note\n", 305 | "Any operation that mutates a tensro in-place is post-fixed with an _.\n", 306 | "For example, x.copy_(y), x.t_() will change x." 307 | ] 308 | }, 309 | { 310 | "cell_type": "markdown", 311 | "metadata": {}, 312 | "source": [ 313 | "You can use standard numpy-like indexing with all bells and whistles!" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 22, 319 | "metadata": { 320 | "collapsed": false 321 | }, 322 | "outputs": [ 323 | { 324 | "name": "stdout", 325 | "output_type": "stream", 326 | "text": [ 327 | "\n", 328 | " 0.1222\n", 329 | " 0.0023\n", 330 | " 0.3231\n", 331 | " 0.9189\n", 332 | " 0.8618\n", 333 | "[torch.FloatTensor of size 5]\n", 334 | "\n" 335 | ] 336 | } 337 | ], 338 | "source": [ 339 | "print(x[:, 1])" 340 | ] 341 | }, 342 | { 343 | "cell_type": "markdown", 344 | "metadata": {}, 345 | "source": [ 346 | "* Read later: 100+ Tensor operations, including transposing, indexing, slicing, mathematical operations, linear algebra, random numbers, etc are described here " 347 | ] 348 | }, 349 | { 350 | "cell_type": "markdown", 351 | "metadata": {}, 352 | "source": [ 353 | "## Numpy Bridge" 354 | ] 355 | }, 356 | { 357 | "cell_type": "markdown", 358 | "metadata": {}, 359 | "source": [ 360 | "- Converting a torch Tensor to a numpy array and vice versa is a breeze.\n", 361 | "- The torch Tensor and numpy array will share their underlying memory locations, and changing one will change the other.\n", 362 | "\n", 363 | "Convertng torch Tensor to numpy Array" 364 | ] 365 | }, 366 | { 367 | "cell_type": "code", 368 | "execution_count": 23, 369 | "metadata": { 370 | "collapsed": false 371 | }, 372 | "outputs": [ 373 | { 374 | "name": "stdout", 375 | "output_type": "stream", 376 | "text": [ 377 | "\n", 378 | " 1\n", 379 | " 1\n", 380 | " 1\n", 381 | " 1\n", 382 | " 1\n", 383 | "[torch.FloatTensor of size 5]\n", 384 | "\n" 385 | ] 386 | } 387 | ], 388 | "source": [ 389 | "a = torch.ones(5)\n", 390 | "print(a)" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": 25, 396 | "metadata": { 397 | "collapsed": false 398 | }, 399 | "outputs": [ 400 | { 401 | "name": "stdout", 402 | "output_type": "stream", 403 | "text": [ 404 | "[ 1. 1. 1. 1. 1.]\n" 405 | ] 406 | } 407 | ], 408 | "source": [ 409 | "b = a.numpy()\n", 410 | "print(b)" 411 | ] 412 | }, 413 | { 414 | "cell_type": "markdown", 415 | "metadata": {}, 416 | "source": [ 417 | "See how the numpy array changed in value." 418 | ] 419 | }, 420 | { 421 | "cell_type": "code", 422 | "execution_count": 26, 423 | "metadata": { 424 | "collapsed": false 425 | }, 426 | "outputs": [ 427 | { 428 | "name": "stdout", 429 | "output_type": "stream", 430 | "text": [ 431 | "\n", 432 | " 2\n", 433 | " 2\n", 434 | " 2\n", 435 | " 2\n", 436 | " 2\n", 437 | "[torch.FloatTensor of size 5]\n", 438 | "\n", 439 | "[ 2. 2. 2. 2. 2.]\n" 440 | ] 441 | } 442 | ], 443 | "source": [ 444 | "a.add_(1)\n", 445 | "print(a)\n", 446 | "print(b)" 447 | ] 448 | }, 449 | { 450 | "cell_type": "markdown", 451 | "metadata": {}, 452 | "source": [ 453 | "Covering numpy Array to torch Tensor\n", 454 | "\n", 455 | "See how changing the np array changedthe torch Tensor atomatically" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": 27, 461 | "metadata": { 462 | "collapsed": false 463 | }, 464 | "outputs": [ 465 | { 466 | "name": "stdout", 467 | "output_type": "stream", 468 | "text": [ 469 | "[ 2. 2. 2. 2. 2.]\n", 470 | "\n", 471 | " 2\n", 472 | " 2\n", 473 | " 2\n", 474 | " 2\n", 475 | " 2\n", 476 | "[torch.DoubleTensor of size 5]\n", 477 | "\n" 478 | ] 479 | } 480 | ], 481 | "source": [ 482 | "import numpy as np\n", 483 | "a = np.ones(5)\n", 484 | "b = torch.from_numpy(a)\n", 485 | "np.add(a, 1, out=a)\n", 486 | "print(a)\n", 487 | "print(b)" 488 | ] 489 | }, 490 | { 491 | "cell_type": "markdown", 492 | "metadata": {}, 493 | "source": [ 494 | "All the Tensors on the CPU except a CharTensor support convering to Numpy and back." 495 | ] 496 | }, 497 | { 498 | "cell_type": "markdown", 499 | "metadata": {}, 500 | "source": [ 501 | "## CUDA Tensors" 502 | ] 503 | }, 504 | { 505 | "cell_type": "markdown", 506 | "metadata": {}, 507 | "source": [ 508 | "Tensors can be moved onto GPU using the .cude function." 509 | ] 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": 32, 514 | "metadata": { 515 | "collapsed": false 516 | }, 517 | "outputs": [ 518 | { 519 | "name": "stdout", 520 | "output_type": "stream", 521 | "text": [ 522 | "\n", 523 | " 1.7296 0.8803 1.4118\n", 524 | " 1.9110 0.0137 1.5350\n", 525 | " 1.4456 1.1901 1.9842\n", 526 | " 1.7189 3.1366 3.1483\n", 527 | " 0.4506 3.1613 1.4352\n", 528 | "[torch.cuda.FloatTensor of size 5x3 (GPU 0)]\n", 529 | "\n" 530 | ] 531 | } 532 | ], 533 | "source": [ 534 | "# let us run this cell only if CUDA is available\n", 535 | "if torch.cuda.is_available():\n", 536 | " x = x.cuda()\n", 537 | " y = y.cuda()\n", 538 | " x + y\n", 539 | " print(x + y)" 540 | ] 541 | }, 542 | { 543 | "cell_type": "markdown", 544 | "metadata": {}, 545 | "source": [ 546 | "* References: http://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html#tensors" 547 | ] 548 | } 549 | ], 550 | "metadata": { 551 | "anaconda-cloud": {}, 552 | "kernelspec": { 553 | "display_name": "Python [default]", 554 | "language": "python", 555 | "name": "python2" 556 | }, 557 | "language_info": { 558 | "codemirror_mode": { 559 | "name": "ipython", 560 | "version": 2 561 | }, 562 | "file_extension": ".py", 563 | "mimetype": "text/x-python", 564 | "name": "python", 565 | "nbconvert_exporter": "python", 566 | "pygments_lexer": "ipython2", 567 | "version": "2.7.12" 568 | } 569 | }, 570 | "nbformat": 4, 571 | "nbformat_minor": 2 572 | } 573 | --------------------------------------------------------------------------------