|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Assignment 2\n", |
| 8 | + "\n", |
| 9 | + "From a given list of numbers, write a Python program to fetch the list of even numbers. Create a new list that stores only the even numbers from the given list. \n", |
| 10 | + "\n", |
| 11 | + "### Hint\n", |
| 12 | + "Attempt by using a for-loop along with an if statement to find out if the number is odd or even." |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "code", |
| 17 | + "execution_count": null, |
| 18 | + "metadata": {}, |
| 19 | + "outputs": [], |
| 20 | + "source": [ |
| 21 | + "numbers = [5,6,3,1,8,4,5,45,23,32,50,1,5,87,345,13,54]\n", |
| 22 | + "\n", |
| 23 | + "# Code here" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "Now create a function that accepts the `numbers` list as input, and returns a list of only even numbers. Using this function, extract the even numbers from both the lists provided below." |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": null, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "numbers1 = [5,6,3,1,8,4,5,45,23,32,50,1,5,87,345,13,54]\n", |
| 40 | + "numbers2 = [82,45,2,7,34,275,124,44,522,3,5,67,446,2,3]\n", |
| 41 | + "\n", |
| 42 | + "# Code here" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "markdown", |
| 47 | + "metadata": {}, |
| 48 | + "source": [ |
| 49 | + "Once you have successfully implemented the above, try to find the even numbers without using a for-loop.\n", |
| 50 | + "\n", |
| 51 | + "**Hint:** Use a function called `filter()` offered by Python to filter through elements of a list based on a custom filteration logic" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": null, |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [], |
| 59 | + "source": [ |
| 60 | + "numbers = [5,6,3,1,8,4,5,45,23,32,50,1,5,87,345,13,54]\n", |
| 61 | + "\n", |
| 62 | + "# Code here" |
| 63 | + ] |
| 64 | + } |
| 65 | + ], |
| 66 | + "metadata": { |
| 67 | + "kernelspec": { |
| 68 | + "display_name": "Python 3", |
| 69 | + "language": "python", |
| 70 | + "name": "python3" |
| 71 | + }, |
| 72 | + "language_info": { |
| 73 | + "codemirror_mode": { |
| 74 | + "name": "ipython", |
| 75 | + "version": 3 |
| 76 | + }, |
| 77 | + "file_extension": ".py", |
| 78 | + "mimetype": "text/x-python", |
| 79 | + "name": "python", |
| 80 | + "nbconvert_exporter": "python", |
| 81 | + "pygments_lexer": "ipython3", |
| 82 | + "version": "3.8.4" |
| 83 | + } |
| 84 | + }, |
| 85 | + "nbformat": 4, |
| 86 | + "nbformat_minor": 4 |
| 87 | +} |
0 commit comments