{ "cells": [ { "cell_type": "markdown", "metadata": { "nbpresent": { "id": "87bda419-0e10-456f-97a7-c9bb59832de4" }, "slideshow": { "slide_type": "slide" } }, "source": [ "# An Introduction to Python\n", "## Lists\n", "\n", "### This notebook contains the programming exercises for [An Introduction to Python: Lists.](https://www.youtube.com/watch?v=bUULmSg9lMc)\n", "\n", "References:\n", "- Fundamentals of Python Programming. Halterman, Richard. Southern Adventist Univesity. 2018.\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Exercises\n", "## Do the following problems." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Jupyter Notebook Shortcuts:\n", "\n", "There are two modes when a cell is highlighted. \n", "\n", "#### Command Mode: Press ESC to activate. The cell has a blue border if this mode is active. In this mode, you can add, delete, create, copy and paste cells.\n", "\n", "- create a new cell above the current cell: a\n", "- create a new cell below the current cell: b\n", "- delete the current cell: dd\n", "- change the current cell’s type to “Code”: y\n", "- change the current cell’s type to “Markdown”: m\n", "\n", "#### Edit Mode: Press ENTER to activate. The cell has a green border if this mode is active. In this mode, you can edit and type text into the cell.\n", "\n", "- execute the current cell and create a new cell: SHIFT + ENTER" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create a list of 5 string objects representing different types of foods. Use a for loop to iterate over the list and print the objects separated by spaces. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use the list created in the previous problem and demonstrate the use of append, remove and pop by adding to, removing from and popping elements from the list." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Given the list `lst = [20, 1, -34, 40, -8, 60, 1, 3]` evaluate the following expressions. Write your answers in markdown first then use code to verify your answer.\n", "\n", "- lst[0:3] \n", "- lst[4:8] \n", "- lst[-5:-3]\n", "- lst[4:]\n", "- lst[:]\n", "- lst[:4]\n", "- lst[1:5] \n", "- -34 in lst\n", "- -34 not in lst \n", "- len(lst)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create the following list: [1,2,3,4,5,6,7,8]. Use slicing to obtain the following sublists:\n", "- [2,3,4,5]\n", "- [1,3,5,7]\n", "- [1,2,3,4,5,6] (use negative indices for this problem)\n", "- [1,2,3,4,5,6,7,8]\n", "- [8,7,6,5,4,3,2,1]\n", "- [6,5,4,3]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create an empty list. \n", "\n", "1) Then use a for loop to append to the list the multiples of 3 from 12 to 2340. Print out the length of this list. \n", "\n", "2) Redo part 1) of this problem but use list comprehensions instead of a for loop.\n", "\n", "3) Use list comprehension as in part 2) but add in the additional condition that the numbers need to be a multiple of 7." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use the built-in function `list()` and the `range()` function to create a list of even numbers from 2 to 50. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Use `sum` to find the sum of a list created in the previous problem." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Write the list represented by each of the following list comprehension expressions. Write your answers in markdown then check with code. \n", "\n", "- [x + 1 for x in [2, 4, 6, 8]]\n", "- [10*x for x in range(5, 10)]\n", "- [x for x in range(10, 21) if x % 3 == 0]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Suppose lst = [-2,4,6,-4,-5,12,-7,5,3]. Use list comprehension to extract the positive numbers from the list." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Write a segment of code that asks the user to input a positive integer `n`. Use list comprehension to create a list `factors` which is a list of factors of `n`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Without running the following code, can you tell what it does for a given integer `n`? (Pretty cool!)\n", "```python\n", "[x for x in range(2, n) if n % x == 0] == []\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Create a list where each element of the list is another list of a student name(string) and a gpa(float). Your list should have 4 student-gpa pairs. Then use a nested loop to print out each student name and gpa, each on a different line.\n", "\n", "One sample output:\n", "\n", "```python\n", "Mike 2.3 \n", "John 3.5 \n", "Michele 3.7 \n", "Carol 3.2 \n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [default]", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 1 }