{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lab 0: Getting Started with Python Notebooks 5000xp\n", "\n", "## feel free to work with a partner\n", "\n", "\n", "## Who are you?\n", "The first thing to do is print out your name and while we are at it, print another person's name:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python as a calculator\n", "As mentioned in the Datacamp course, Python can function as a simple interactive calculator. For example, suppose I want to add these numbers together:\n", "\n", " 142, 125, 78, 14\n", " \n", " \n", "In Python we can write:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "359" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "142 + 125 + 78 + 14" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To execute that code we place the cursor in that code box and press the shift key plus return. And here is probably a middle school math problem. My recent trip to the vet included the following:\n", "\n", "Description | Price\n", " :---: | :---:\n", " Heartworm (Ehrlichia Test) | 46.50\n", " Bordetella Vaccine | 21.56\n", " Distemper Combo | 39.20\n", " \n", " I receive a 5% multiple pet discount which is not included in the prices above. \n", " \n", " What is my total cost factoring in the discount (don't worry about taxes and fractions of pennies)?\n", " \n", " Please compute this using one line of Python.\n", "\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variables\n", "\n", "Here are some variables we defined: (be sure to execute the code block)\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "vWalk = 'walk'\n", "vTalk ='talk'\n", "vSleep = 'sleep'\n", "past = 'ed'\n", "progressive = 'ing'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using these variable names can you create expressions that **print**:\n", "\n", " walked\n", " talking\n", " sleeping\n", " \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is some code that asks a user for two numbers (go ahead and run this cell):" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter the first number: 2\n", "Enter the second: 5\n", "You entered 2 and 5\n" ] } ], "source": [ "number1 = input(\"Enter the first number: \")\n", "number2 = input(\"Enter the second: \")\n", "print(\"You entered \" + str(number1) + ' and ' + str(number2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you write code that displays the average of the two numbers the person typed in?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists\n", "\n", "Can you create a list named `team` containing the names of the people on your team?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you print out the length of the list `team` as well as the first member and the last member?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A new member, Sally, has been added to your team. Can you add her to the list (and then print the length of the list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lists of lists\n", "\n", "Here we created a simple Battleship game containing one sub:\n", " " ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.']]\n" ] } ], "source": [ "grid = [['.', '.', '.', '.', '.'],\n", " ['.', 'S', '.', '.', '.'],\n", " ['.', 'S', '.', '.', '.'],\n", " ['.', '.', '.', '.', '.'],\n", " ['.', '.', '.', '.', '.']]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So `grid` is a variable whose type is a list. Can you print out the two occurrences of `S` using the variable `grid`. So something like:\n", "\n", " print(grid______)\n", " print(grid______)\n", " \n", "where you are replacing the underlines with some code." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions and methods\n", "\n", "Suppose I have a variable `word`:\n" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "word = 'abstemious'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "can you write one line of code that counts how many vowels are in that varaible? You may have to count each vowel( *a*, *e*, *i*, *o*, *u*) separately and then add them up." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you do the same with " ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": true }, "outputs": [], "source": [ "word = 'ALUMINOSILICATES'\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There's a function `round` that takes two arguments:\n", "\n", " round(number, places) \n", " \n", "It rounds the number `number` to how ever many places specified. Here is an example:\n", "\n", " " ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.36" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "round(1.35721, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you modify the vet bill example above so my total bill is rounded to the nearest penny?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The accountant for the vet office isn't happy with the rounding. For example, if the bill was 100.254 it gets rounded to 100.25 and the vet loses four tenths of a penny. That doesn't sound like much but it adds up. And they are tired of dealing with all that change. So now they would like to always round up to the nearest whole dollar So even 100.01 would get rounded to 101. Fortunately the `math` package has a function `ceil` for that. Here is the documentation:\n", "\n", " math.ceil(x)\n", " Return the ceiling of x as a float, the smallest integer value greater than or equal to x.\n", "\n", "Can you do the required import and modify your code so my bill gets rounded up? \n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## formula conversion\n", "\n", "The formula for computing someone's BMI (body mass index) is \n", "\n", "## $$bmi=\\frac{weightK}{heightM^2}$$\n", "\n", "`weightK` is weight in kilograms and `heightM` is height in meters. In the code cell below I set several variables indicating someone's height in inches and their weight in pounds. Using these variables in a formula can you print their BMI?\n", "\n", "note:\n", "\n", "* an inch is equal to .0254 meters\n", "* a kilogram is equal to 2.205 pounds" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# My trip\n", "Just last week I drove from my home in New Mexico to Fredericksburg:\n", "\n", "\n", "The trip was 1959 miles. My mileage averaged 20.5 in my Ford F150. And the average cost of gas was 2.07 a gallon.\n", "Can you create three variables:\n", "\n", " distance\n", " mpg\n", " price\n", " \n", "and use them in a formula to print out the total cost of gas for the trip?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Berkeley Graduate School\n", "\n", "Here are some admission data for a particular department at Berkeley:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "menAdmitted = 512\n", "menRejected = 313\n", "womenAdmitted = 89\n", "womenRejected = 18" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you write code to print out the percent admitted for both men and women\n", "\n", " xx% of men were admitted\n", " xx% of women were admitted\n", " \n", "I will help you get started:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# change the 0s to your code\n", "percentMen = 0\n", "percentWomen = 0\n", "\n", "# now print the results" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Congratulations! \n", "You've accomplished a giant step in learning Data Science. First, you installed Anaconda and learned how to open Notebooks. Plus you practiced your new Python programming skills!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.1" } }, "nbformat": 4, "nbformat_minor": 2 }