|
| 1 | +{ |
| 2 | +"cells": [ |
| 3 | + { |
| 4 | +"cell_type":"heading", |
| 5 | +"metadata": { |
| 6 | +"collapsed":true |
| 7 | + }, |
| 8 | +"level":1, |
| 9 | +"source": [ |
| 10 | +"PYTHON BASICS _ source-nerd" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | +"cell_type":"heading", |
| 15 | +"metadata": {}, |
| 16 | +"level":2, |
| 17 | +"source": [ |
| 18 | +"1. Data Structures in Python\n", |
| 19 | +"\ta. List\n", |
| 20 | +"\tb. Dictionary\n", |
| 21 | +"\tc. Tuples" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | +"cell_type":"heading", |
| 26 | +"metadata": {}, |
| 27 | +"level":3, |
| 28 | +"source": [ |
| 29 | +"1.a. List\n", |
| 30 | +"List literals are written within square brackets [ ]. Lists work similarly to strings -- use the len() function and square brackets [ ] to access data, with the first element at index 0. (See the official python.org list docs.)" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | +"cell_type":"code", |
| 35 | +"execution_count":46, |
| 36 | +"metadata": {}, |
| 37 | +"outputs": [ |
| 38 | + { |
| 39 | +"data": { |
| 40 | +"text/plain": [ |
| 41 | +"[1, 2, 3, 'xyz']" |
| 42 | + ] |
| 43 | + }, |
| 44 | +"execution_count":46, |
| 45 | +"metadata": {}, |
| 46 | +"output_type":"execute_result" |
| 47 | + } |
| 48 | + ], |
| 49 | +"source": [ |
| 50 | +"# A Sample List\n", |
| 51 | +"list_data = [1, 2, 3, 'xyz']\n", |
| 52 | +"list_data" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | +"cell_type":"code", |
| 57 | +"execution_count":48, |
| 58 | +"metadata": { |
| 59 | +"collapsed":true |
| 60 | + }, |
| 61 | +"outputs": [ |
| 62 | + { |
| 63 | +"data": { |
| 64 | +"text/plain": [ |
| 65 | +"1" |
| 66 | + ] |
| 67 | + }, |
| 68 | +"execution_count":48, |
| 69 | +"metadata": {}, |
| 70 | +"output_type":"execute_result" |
| 71 | + } |
| 72 | + ], |
| 73 | +"source": [ |
| 74 | +"# Elements in a list can be referred by its posotion\n", |
| 75 | +"list_data[0]" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | +"cell_type":"code", |
| 80 | +"execution_count":49, |
| 81 | +"metadata": {}, |
| 82 | +"outputs": [ |
| 83 | + { |
| 84 | +"data": { |
| 85 | +"text/plain": [ |
| 86 | +"4" |
| 87 | + ] |
| 88 | + }, |
| 89 | +"execution_count":49, |
| 90 | +"metadata": {}, |
| 91 | +"output_type":"execute_result" |
| 92 | + } |
| 93 | + ], |
| 94 | +"source": [ |
| 95 | +"# Length of the list\n", |
| 96 | +"# The length of the list can be retrieved by using len command followed by list variable name\n", |
| 97 | +"len(list_data)" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | +"cell_type":"code", |
| 102 | +"execution_count":50, |
| 103 | +"metadata": {}, |
| 104 | +"outputs": [ |
| 105 | + { |
| 106 | +"data": { |
| 107 | +"text/plain": [ |
| 108 | +"0" |
| 109 | + ] |
| 110 | + }, |
| 111 | +"execution_count":50, |
| 112 | +"metadata": {}, |
| 113 | +"output_type":"execute_result" |
| 114 | + } |
| 115 | + ], |
| 116 | +"source": [ |
| 117 | +"# Index\n", |
| 118 | +"# Index will return the position of the element in the list\n", |
| 119 | +"list_data.index(1)" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | +"cell_type":"code", |
| 124 | +"execution_count":51, |
| 125 | +"metadata": {}, |
| 126 | +"outputs": [ |
| 127 | + { |
| 128 | +"data": { |
| 129 | +"text/plain": [ |
| 130 | +"[1, 2, 3, 'xyz', 4, 'some_text']" |
| 131 | + ] |
| 132 | + }, |
| 133 | +"execution_count":51, |
| 134 | +"metadata": {}, |
| 135 | +"output_type":"execute_result" |
| 136 | + } |
| 137 | + ], |
| 138 | +"source": [ |
| 139 | +"# Adding new element\n", |
| 140 | +"# Append will append new element at the end of the list\n", |
| 141 | +"list_data.append(4)\n", |
| 142 | +"list_data\n", |
| 143 | +"\n", |
| 144 | +"# You can also use + sign to append elements to the list\n", |
| 145 | +"list_data = list_data + ['some_text']\n", |
| 146 | +"list_data" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | +"cell_type":"code", |
| 151 | +"execution_count":52, |
| 152 | +"metadata": {}, |
| 153 | +"outputs": [ |
| 154 | + { |
| 155 | +"data": { |
| 156 | +"text/plain": [ |
| 157 | +"[1, 2, 3, 'new_data', 4, 'some_text']" |
| 158 | + ] |
| 159 | + }, |
| 160 | +"execution_count":52, |
| 161 | +"metadata": {}, |
| 162 | +"output_type":"execute_result" |
| 163 | + } |
| 164 | + ], |
| 165 | +"source": [ |
| 166 | +"# Assigning/Replacing values\n", |
| 167 | +"# The position of the element can be used to replace the value of the element\n", |
| 168 | +"list_data[3] = 'new_data'\n", |
| 169 | +"list_data" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | +"cell_type":"code", |
| 174 | +"execution_count":53, |
| 175 | +"metadata": {}, |
| 176 | +"outputs": [ |
| 177 | + { |
| 178 | +"data": { |
| 179 | +"text/plain": [ |
| 180 | +"[5, 1, 2, 3, 'new_data', 4, 'some_text']" |
| 181 | + ] |
| 182 | + }, |
| 183 | +"execution_count":53, |
| 184 | +"metadata": {}, |
| 185 | +"output_type":"execute_result" |
| 186 | + } |
| 187 | + ], |
| 188 | +"source": [ |
| 189 | +"# Inserting elements at particular position in the list\n", |
| 190 | +"# Use the insert method to insert element at the particular position\n", |
| 191 | +"list_data.insert(0, 5)\n", |
| 192 | +"list_data" |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | +"cell_type":"code", |
| 197 | +"execution_count":54, |
| 198 | +"metadata": { |
| 199 | +"collapsed":false |
| 200 | + }, |
| 201 | +"outputs": [ |
| 202 | + { |
| 203 | +"data": { |
| 204 | +"text/plain": [ |
| 205 | +"[1, 3, 'new_data', 4]" |
| 206 | + ] |
| 207 | + }, |
| 208 | +"execution_count":54, |
| 209 | +"metadata": {}, |
| 210 | +"output_type":"execute_result" |
| 211 | + } |
| 212 | + ], |
| 213 | +"source": [ |
| 214 | +"# Removing elements from a list can either be done by pop, del or remove\n", |
| 215 | +"# pop\n", |
| 216 | +"list_data.pop(0)\n", |
| 217 | +"\n", |
| 218 | +"# del\n", |
| 219 | +"del list_data[1]\n", |
| 220 | +"\n", |
| 221 | +"# remove\n", |
| 222 | +"list_data.remove('some_text')\n", |
| 223 | +"list_data" |
| 224 | + ] |
| 225 | + }, |
| 226 | + { |
| 227 | +"cell_type":"code", |
| 228 | +"execution_count":55, |
| 229 | +"metadata": {}, |
| 230 | +"outputs": [ |
| 231 | + { |
| 232 | +"data": { |
| 233 | +"text/plain": [ |
| 234 | +"[1, 2, 3, 4, 5, 6]" |
| 235 | + ] |
| 236 | + }, |
| 237 | +"execution_count":55, |
| 238 | +"metadata": {}, |
| 239 | +"output_type":"execute_result" |
| 240 | + } |
| 241 | + ], |
| 242 | +"source": [ |
| 243 | +"# Addition of 2 lists\n", |
| 244 | +"list_1 = [1, 2, 3]\n", |
| 245 | +"list_2 = [4, 5, 6]\n", |
| 246 | +"res_list = list_1 + list_2\n", |
| 247 | +"res_list" |
| 248 | + ] |
| 249 | + }, |
| 250 | + { |
| 251 | +"cell_type":"code", |
| 252 | +"execution_count":56, |
| 253 | +"metadata": {}, |
| 254 | +"outputs": [ |
| 255 | + { |
| 256 | +"data": { |
| 257 | +"text/plain": [ |
| 258 | +"[1, 2, 3]" |
| 259 | + ] |
| 260 | + }, |
| 261 | +"execution_count":56, |
| 262 | +"metadata": {}, |
| 263 | +"output_type":"execute_result" |
| 264 | + } |
| 265 | + ], |
| 266 | +"source": [ |
| 267 | +"# Slicing lists\n", |
| 268 | +"# Returns all values before third element\n", |
| 269 | +"res_list[:3]" |
| 270 | + ] |
| 271 | + }, |
| 272 | + { |
| 273 | +"cell_type":"code", |
| 274 | +"execution_count":57, |
| 275 | +"metadata": {}, |
| 276 | +"outputs": [ |
| 277 | + { |
| 278 | +"data": { |
| 279 | +"text/plain": [ |
| 280 | +"[4, 5, 6]" |
| 281 | + ] |
| 282 | + }, |
| 283 | +"execution_count":57, |
| 284 | +"metadata": {}, |
| 285 | +"output_type":"execute_result" |
| 286 | + } |
| 287 | + ], |
| 288 | +"source": [ |
| 289 | +"# Returns all values after third element including the third element\n", |
| 290 | +"res_list[3:]" |
| 291 | + ] |
| 292 | + }, |
| 293 | + { |
| 294 | +"cell_type":"code", |
| 295 | +"execution_count":58, |
| 296 | +"metadata": {}, |
| 297 | +"outputs": [ |
| 298 | + { |
| 299 | +"data": { |
| 300 | +"text/plain": [ |
| 301 | +"[2, 3]" |
| 302 | + ] |
| 303 | + }, |
| 304 | +"execution_count":58, |
| 305 | +"metadata": {}, |
| 306 | +"output_type":"execute_result" |
| 307 | + } |
| 308 | + ], |
| 309 | +"source": [ |
| 310 | +"# Returns all values from 1 to 3 including 1st element and excluding 3 element\n", |
| 311 | +"res_list[1:3]" |
| 312 | + ] |
| 313 | + }, |
| 314 | + { |
| 315 | +"cell_type":"code", |
| 316 | +"execution_count":59, |
| 317 | +"metadata": {}, |
| 318 | +"outputs": [ |
| 319 | + { |
| 320 | +"data": { |
| 321 | +"text/plain": [ |
| 322 | +"[1, 3, 5]" |
| 323 | + ] |
| 324 | + }, |
| 325 | +"execution_count":59, |
| 326 | +"metadata": {}, |
| 327 | +"output_type":"execute_result" |
| 328 | + } |
| 329 | + ], |
| 330 | +"source": [ |
| 331 | +"# Returns every 2nd element starting from 0\n", |
| 332 | +"res_list[0::2]" |
| 333 | + ] |
| 334 | + }, |
| 335 | + { |
| 336 | +"cell_type":"code", |
| 337 | +"execution_count":44, |
| 338 | +"metadata": {}, |
| 339 | +"outputs": [ |
| 340 | + { |
| 341 | +"data": { |
| 342 | +"text/plain": [ |
| 343 | +"6" |
| 344 | + ] |
| 345 | + }, |
| 346 | +"execution_count":44, |
| 347 | +"metadata": {}, |
| 348 | +"output_type":"execute_result" |
| 349 | + } |
| 350 | + ], |
| 351 | +"source": [ |
| 352 | +"# Returns the last element\n", |
| 353 | +"res_list[-1]" |
| 354 | + ] |
| 355 | + }, |
| 356 | + { |
| 357 | +"cell_type":"code", |
| 358 | +"execution_count":null, |
| 359 | +"metadata": {}, |
| 360 | +"outputs": [], |
| 361 | +"source": [] |
| 362 | + } |
| 363 | + ], |
| 364 | +"metadata": { |
| 365 | +"kernelspec": { |
| 366 | +"display_name":"Python 2", |
| 367 | +"language":"python", |
| 368 | +"name":"python2" |
| 369 | + }, |
| 370 | +"language_info": { |
| 371 | +"codemirror_mode": { |
| 372 | +"name":"ipython", |
| 373 | +"version":2 |
| 374 | + }, |
| 375 | +"file_extension":".py", |
| 376 | +"mimetype":"text/x-python", |
| 377 | +"name":"python", |
| 378 | +"nbconvert_exporter":"python", |
| 379 | +"pygments_lexer":"ipython2", |
| 380 | +"version":"2.7.6" |
| 381 | + } |
| 382 | + }, |
| 383 | +"nbformat":4, |
| 384 | +"nbformat_minor":0 |
| 385 | +} |