|
389 | 389 | "cell_type": "markdown", |
390 | 390 | "metadata": {}, |
391 | 391 | "source": [ |
392 | | - "### Python 2 vs Python 3" |
| 392 | + "### Task (Introducing the range function)" |
| 393 | + ] |
| 394 | + }, |
| 395 | + { |
| 396 | + "cell_type": "markdown", |
| 397 | + "metadata": {}, |
| 398 | + "source": [ |
| 399 | + "1. Write a Python program which iterates through integers from 1 to 50 (using a for loop). For an integer that is even, append it to the list even_numbers. For an integer that is off, append it the list odd_numbers" |
393 | 400 | ] |
394 | 401 | }, |
395 | 402 | { |
396 | 403 | "cell_type": "code", |
397 | | - "execution_count": 11, |
| 404 | + "execution_count": 18, |
398 | 405 | "metadata": { |
399 | 406 | "collapsed": true |
400 | 407 | }, |
401 | 408 | "outputs": [], |
402 | 409 | "source": [ |
403 | | - "# range\n", |
404 | | - "# python 2 xrange and range are same\n", |
405 | | - "# python 2 is list \n", |
406 | | - "# in python 2 long ass list are slow\n", |
407 | | - "# xrange long as number is faster because it is what is called a generator" |
| 410 | + "# Making empty lists to append even and odd numbers to. \n", |
| 411 | + "even_numbers = []\n", |
| 412 | + "odd_numbers = []\n", |
| 413 | + "\n", |
| 414 | + "for number in range(1,51):\n", |
| 415 | + " if number % 2 == 0:\n", |
| 416 | + " even_numbers.append(number)\n", |
| 417 | + " else: \n", |
| 418 | + " odd_numbers.append(number) " |
408 | 419 | ] |
409 | 420 | }, |
410 | 421 | { |
411 | | - "cell_type": "markdown", |
| 422 | + "cell_type": "code", |
| 423 | + "execution_count": 20, |
412 | 424 | "metadata": {}, |
413 | | - "source": [] |
| 425 | + "outputs": [ |
| 426 | + { |
| 427 | + "name": "stdout", |
| 428 | + "output_type": "stream", |
| 429 | + "text": [ |
| 430 | + "Even Numbers: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]\n" |
| 431 | + ] |
| 432 | + } |
| 433 | + ], |
| 434 | + "source": [ |
| 435 | + "print(\"Even Numbers: \", even_numbers)" |
| 436 | + ] |
414 | 437 | }, |
415 | 438 | { |
416 | | - "cell_type": "markdown", |
| 439 | + "cell_type": "code", |
| 440 | + "execution_count": 21, |
417 | 441 | "metadata": {}, |
| 442 | + "outputs": [ |
| 443 | + { |
| 444 | + "name": "stdout", |
| 445 | + "output_type": "stream", |
| 446 | + "text": [ |
| 447 | + "Odd Numbers: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]\n" |
| 448 | + ] |
| 449 | + } |
| 450 | + ], |
418 | 451 | "source": [ |
419 | | - "**if this tutorial doesn't cover what you are looking for, please leave a comment on the youtube video and I will try to cover what you are interested in. (Please subscribe if you can!)**" |
| 452 | + "print(\"Odd Numbers: \", odd_numbers)" |
420 | 453 | ] |
421 | 454 | }, |
422 | 455 | { |
423 | 456 | "cell_type": "markdown", |
424 | 457 | "metadata": {}, |
425 | 458 | "source": [ |
426 | | - "https://www.youtube.com/watch?v=JqGjkNzzU4s" |
| 459 | + "### Python 2 vs Python 3 (range function differences)" |
427 | 460 | ] |
428 | 461 | }, |
429 | 462 | { |
430 | | - "cell_type": "markdown", |
431 | | - "metadata": {}, |
| 463 | + "cell_type": "code", |
| 464 | + "execution_count": 23, |
| 465 | + "metadata": { |
| 466 | + "collapsed": true |
| 467 | + }, |
| 468 | + "outputs": [], |
432 | 469 | "source": [ |
433 | | - "Relevent questions to answer" |
| 470 | + "# python 2 xrange and Python 3 range are same (resembles a generator)\n", |
| 471 | + "# python 2 range generates a list\n", |
| 472 | + "\n", |
| 473 | + "# Importance: Long lists are slow" |
434 | 474 | ] |
435 | 475 | }, |
436 | 476 | { |
437 | 477 | "cell_type": "markdown", |
438 | 478 | "metadata": {}, |
439 | 479 | "source": [ |
440 | | - "### Task" |
| 480 | + "**if this tutorial doesn't cover what you are looking for, please leave a comment on the youtube video and I will try to cover what you are interested in. (Please subscribe if you can!)**" |
441 | 481 | ] |
442 | 482 | }, |
443 | 483 | { |
444 | 484 | "cell_type": "markdown", |
445 | 485 | "metadata": {}, |
446 | 486 | "source": [ |
447 | | - "1. Write a Python program which iterates through integers from 1 to 50 (using a for loop). For an integer that is even, append it to the list even_numbers. For an integer that is off, append it the list odd_numbers" |
| 487 | + "https://www.youtube.com/watch?v=JqGjkNzzU4s" |
448 | 488 | ] |
449 | 489 | }, |
450 | | - { |
451 | | - "cell_type": "code", |
452 | | - "execution_count": null, |
453 | | - "metadata": { |
454 | | - "collapsed": true |
455 | | - }, |
456 | | - "outputs": [], |
457 | | - "source": [] |
458 | | - }, |
459 | 490 | { |
460 | 491 | "cell_type": "markdown", |
461 | 492 | "metadata": {}, |
|
483 | 514 | "source": [ |
484 | 515 | "https://www.quora.com/How-can-I-make-for-loops-work-in-Python" |
485 | 516 | ] |
486 | | - }, |
487 | | - { |
488 | | - "cell_type": "markdown", |
489 | | - "metadata": { |
490 | | - "collapsed": true |
491 | | - }, |
492 | | - "source": [ |
493 | | - "Write a list comprehension that creates a list containing all of the numbers that are both exact squares and odd in the range 1 to 100." |
494 | | - ] |
495 | | - }, |
496 | | - { |
497 | | - "cell_type": "code", |
498 | | - "execution_count": null, |
499 | | - "metadata": { |
500 | | - "collapsed": true |
501 | | - }, |
502 | | - "outputs": [], |
503 | | - "source": [] |
504 | 517 | } |
505 | 518 | ], |
506 | 519 | "metadata": { |
|
0 commit comments