{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Review - 2,000xp\n", "\n", "\n", "\n", "## Preliminaries: 1000xp\n", "Here we create a list of our friends:\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "friends = ['Ann', 'Ben', 'Clara', 'David', 'Kelly', 'Rachel']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you write a loop that will print out each of our friends? Something like this:\n", "\n", " Ann\n", " Ben\n", " Clara\n", " David\n", " Kelly\n", " Rachel" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "# Your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose we have a list of numbers:\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "numbers = [11, 22, 37, 51, 17]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to print the sum of the numbers but we have an error in the following code. Can you fix it?" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "17\n" ] } ], "source": [ "total = 0\n", "for num in numbers:\n", " total = total + num\n", "print(num)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have a list of grades:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "grades = [89, 91, 79, 85, 97, 91]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you print out each grade, one per line like this:\n", "\n", " 89\n", " 91\n", " 79\n", " 85\n", " 97\n", " 91\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we want to print out only the grades over 90:\n", "\n", " 91\n", " 97\n", " 91\n", "\n", "Can you change your code to do so?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We represent a sentence as a list of words." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n", "sentence = ['the', 'man', 'saw', 'the', 'girl', 'on', 'the', 'hill', 'with', 'the', 'telescope']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you write code that will compute and print the number of occurrences of the word *the* in the sentence?" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "# Your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have another sentence" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "sentence2 = ['the', 'man', 'saw', 'a', 'girl', 'on', 'the', 'hill', 'with', 'the', 'telescope', 'on', 'a', 'sunny', 'day']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and we want to count the occurrences of *the* and *a* and print something like:\n", "\n", " the: 4\n", " a: 2\n", " \n", "Can you write the code?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have a list of our friends and their home states:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "friends = [['Ann', 'North Dakota'], ['Ben', 'Virginia'], ['Clara', 'Connecticut'], \n", " ['David', 'Minnesota'], ['Kelly', 'Kansas'], ['Rachel', 'New Mexico']]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plus we set a variable for the person we want to search for:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "searchPerson = 'Kelly'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we want to write code that finds the home state for the person `searchPerson` and prints it. For example, if the value of `searchPerson` is `Kelly` the code should print out `Kansas`. If `searchPerson` is `Rachel` it should print out `New Mexico`. Test your code by changing the value of searchPerson above." ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "# Your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we have a list of friends and their ages:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "friends = [['Ann', 21], ['Ben', 17], ['Clara', 24], \n", " ['David', 27], ['Kelly', 18], ['Rachel', 20],\n", " ['Jennifer', 15], ['Sam', 19], ['Vivic', 16]]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We would like to print out the friends who are between the ages of 18 and 21 inclusive (meaning if someone is 18 that person's name should be printed)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "# Your work here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dataframes\n", "\n", "\n", "\n", "Let's load in a small dataset of women athletes:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | Name | \n", "Sport | \n", "Height | \n", "Weight | \n", "
---|---|---|---|---|
0 | \n", "Asuka Teramoto | \n", "Gymnastics | \n", "54 | \n", "66 | \n", "
1 | \n", "Brittainey Raven | \n", "Basketball | \n", "72 | \n", "162 | \n", "
2 | \n", "Chen Nan | \n", "Basketball | \n", "78 | \n", "204 | \n", "
3 | \n", "Gabby Douglas | \n", "Gymnastics | \n", "49 | \n", "90 | \n", "
4 | \n", "Helalia Johannes | \n", "Track | \n", "65 | \n", "99 | \n", "