|
16 | 16 | "Python has a number of types. You need to be familiar with some of them as a start, then you will learn about more as you go. Let's quickly investigate some of these here:" |
17 | 17 | ] |
18 | 18 | }, |
| 19 | + { |
| 20 | + "cell_type": "markdown", |
| 21 | + "metadata": {}, |
| 22 | + "source": [ |
| 23 | + "#Integers and floats" |
| 24 | + ] |
| 25 | + }, |
19 | 26 | { |
20 | 27 | "cell_type": "code", |
21 | 28 | "execution_count": 5, |
|
81 | 88 | ] |
82 | 89 | }, |
83 | 90 | { |
84 | | - "cell_type": "code", |
85 | | - "execution_count": null, |
| 91 | + "cell_type": "markdown", |
86 | 92 | "metadata": { |
87 | 93 | "collapsed": true |
88 | 94 | }, |
89 | | - "outputs": [], |
90 | | - "source": [] |
| 95 | + "source": [ |
| 96 | + "# Strings" |
| 97 | + ] |
91 | 98 | }, |
92 | 99 | { |
93 | 100 | "cell_type": "code", |
|
112 | 119 | "print \"Hello world\"" |
113 | 120 | ] |
114 | 121 | }, |
115 | | - { |
116 | | - "cell_type": "code", |
117 | | - "execution_count": null, |
118 | | - "metadata": { |
119 | | - "collapsed": true |
120 | | - }, |
121 | | - "outputs": [], |
122 | | - "source": [] |
123 | | - }, |
124 | 122 | { |
125 | 123 | "cell_type": "code", |
126 | 124 | "execution_count": 2, |
|
174 | 172 | "print \"The type of the value: \", len(feeling)\n" |
175 | 173 | ] |
176 | 174 | }, |
| 175 | + { |
| 176 | + "cell_type": "markdown", |
| 177 | + "metadata": {}, |
| 178 | + "source": [ |
| 179 | + "# Lists" |
| 180 | + ] |
| 181 | + }, |
177 | 182 | { |
178 | 183 | "cell_type": "code", |
179 | 184 | "execution_count": 7, |
|
320 | 325 | ] |
321 | 326 | }, |
322 | 327 | { |
323 | | - "cell_type": "code", |
324 | | - "execution_count": null, |
325 | | - "metadata": { |
326 | | - "collapsed": true |
327 | | - }, |
328 | | - "outputs": [], |
329 | | - "source": [] |
330 | | - }, |
331 | | - { |
332 | | - "cell_type": "code", |
333 | | - "execution_count": null, |
| 328 | + "cell_type": "markdown", |
334 | 329 | "metadata": { |
335 | 330 | "collapsed": true |
336 | 331 | }, |
337 | | - "outputs": [], |
338 | | - "source": [] |
| 332 | + "source": [ |
| 333 | + "# Dictionaries" |
| 334 | + ] |
339 | 335 | }, |
340 | 336 | { |
341 | 337 | "cell_type": "code", |
342 | | - "execution_count": null, |
| 338 | + "execution_count": 3, |
343 | 339 | "metadata": { |
344 | | - "collapsed": true |
| 340 | + "collapsed": false |
345 | 341 | }, |
346 | | - "outputs": [], |
347 | | - "source": [] |
| 342 | + "outputs": [ |
| 343 | + { |
| 344 | + "name": "stdout", |
| 345 | + "output_type": "stream", |
| 346 | + "text": [ |
| 347 | + "{777: 'Mary', 1111: 'John'}\n", |
| 348 | + "2\n", |
| 349 | + "Mary\n" |
| 350 | + ] |
| 351 | + } |
| 352 | + ], |
| 353 | + "source": [ |
| 354 | + "# A Ptthon dictionary is a \"mapping\" type. We map a \"key\" to a \"value\".\n", |
| 355 | + "# For example, we can map a \"student_id\" to the \"name\" of a student.\n", |
| 356 | + "# The sytax is simple: We use the curly braces, and delimit each key:value pair by the \"colon\"\n", |
| 357 | + "students={1111: \"John\", 777: \"Mary\"}\n", |
| 358 | + "print \"Printing the 'students' dict: \", students\n", |
| 359 | + "print \"The length of the 'students' dict is: \", len(students)\n", |
| 360 | + "# This is how you access the value of the key 777\n", |
| 361 | + "print print \"The value of the key 777 in the 'students' dict is: \", students[777]" |
| 362 | + ] |
348 | 363 | }, |
349 | 364 | { |
350 | 365 | "cell_type": "code", |
351 | | - "execution_count": null, |
| 366 | + "execution_count": 5, |
352 | 367 | "metadata": { |
353 | | - "collapsed": true |
| 368 | + "collapsed": false |
354 | 369 | }, |
355 | | - "outputs": [], |
356 | | - "source": [] |
| 370 | + "outputs": [ |
| 371 | + { |
| 372 | + "name": "stdout", |
| 373 | + "output_type": "stream", |
| 374 | + "text": [ |
| 375 | + "The value of the key 'CS' in the 'students' dict is: John\n" |
| 376 | + ] |
| 377 | + } |
| 378 | + ], |
| 379 | + "source": [ |
| 380 | + "# The keys don't have to be integers; they can be strings too. \n", |
| 381 | + "# Lets have keys represent the school of a student:\n", |
| 382 | + "students={\"CS\": \"John\", \"Business\": \"Mary\"}\n", |
| 383 | + "print \"The value of the key 'CS' in the 'students' dict is: \", students['CS']" |
| 384 | + ] |
357 | 385 | }, |
358 | 386 | { |
359 | 387 | "cell_type": "code", |
360 | | - "execution_count": null, |
| 388 | + "execution_count": 6, |
361 | 389 | "metadata": { |
362 | | - "collapsed": true |
| 390 | + "collapsed": false |
363 | 391 | }, |
364 | | - "outputs": [], |
365 | | - "source": [] |
| 392 | + "outputs": [ |
| 393 | + { |
| 394 | + "name": "stdout", |
| 395 | + "output_type": "stream", |
| 396 | + "text": [ |
| 397 | + "The value of the key 'CS' in the 'students' dict is: ['John', 'Alex', 'Amanda']\n" |
| 398 | + ] |
| 399 | + } |
| 400 | + ], |
| 401 | + "source": [ |
| 402 | + "# A value in a Python dict can be a string, a list, another dict, etc.\n", |
| 403 | + "# So, if \"Alex\" and \"Amanda\" are also students in CS, then we can have the value for the key 'CS' as a list:\n", |
| 404 | + "students={\"CS\": [\"John\", \"Alex\", \"Amanda\"] , \"Business\": \"Mary\"}\n", |
| 405 | + "# And now when we print, we get all the students in CS as a full list:\n", |
| 406 | + "print \"The value of the key 'CS' in the 'students' dict is: \", students['CS']" |
| 407 | + ] |
366 | 408 | }, |
367 | 409 | { |
368 | 410 | "cell_type": "code", |
|
0 commit comments