Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "metadata": {
- "name": "",
- "signature": "sha256:250e0bc23963ee1fb2cb756e0f51fa9ba254dbe7fc5b16d5a318d2c9a1e2e59b"
- },
- "nbformat": 3,
- "nbformat_minor": 0,
- "worksheets": [
- {
- "cells": [
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "cd '/Users/jenniferli/Desktop/Python_Class/'"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "run anthem.py"
- ],
- "language": "python",
- "metadata": {},
- "outputs": [],
- "prompt_number": 3
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Python as a calculator"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "1 + 1"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "4 % 3"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "1.0 + 5"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 6,
- "metadata": {},
- "source": [
- "Is 6840827 divisible by 7?"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Variables"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "x = 3\n",
- "y = 4.2\n",
- "x + y"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 6,
- "metadata": {},
- "source": [
- "What happens if you enter an undefined variable, such as z?"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Strings"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "mystring = \"I love Python!\"\n",
- "print mystring"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Comments"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# This is a comment. \n",
- "# Comments allow you to annotate your code.\n",
- "# You can quickly comment blocks of code using Cmd + /\n",
- "# There is no spell-checking."
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Logical Operators"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "1 == 1"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "1 != 3"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "x = 3\n",
- "(1 == 1) and x > 3\n",
- "(1 == 1) or x > 3"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "If/Else Statements"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# note the identation!\n",
- "cookies = 5\n",
- "kale = 4\n",
- "if cookies == kale:\n",
- " print \"cookies are equal to kale.\"\n",
- "elif cookies > kale:\n",
- " print \"cookies are better than kale.\"\n",
- "else:\n",
- " print \"kale is better than cookies.\""
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 6,
- "metadata": {},
- "source": [
- "Write code to check whether a number is even or odd. "
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Lists and Indexing"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "mylist = [1,2,3,7,4]\n",
- "mylist[0], mylist[-1]\n",
- "len(mylist)\n",
- "mylist.find(7)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 6,
- "metadata": {},
- "source": [
- "How do you get the first two elements of mylist?"
- ]
- },
- {
- "cell_type": "heading",
- "level": 6,
- "metadata": {},
- "source": [
- "Can you print mylist in reverse order? Bonus points if you can find two ways to do it. (Hint: try typing mylist.<Tab>?)"
- ]
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "For-loops"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "for i in range(10):\n",
- " print i\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 6,
- "metadata": {},
- "source": [
- "Write a for-loop to print out each letter in your name. "
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "myname = \"jennifer\"\n",
- "for i in range(len(myname)):\n",
- " print myname[i],\n",
- " "
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "While-loops"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "i = 0\n",
- "while i < 10:\n",
- " print i\n",
- " i+=1"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 6,
- "metadata": {},
- "source": [
- "Write a while-loop to print out each letter in your name."
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Solution 1\n",
- "index = 0\n",
- "while index <len(myname):\n",
- " print myname[index],\n",
- " index +=1\n",
- "\n",
- "# Solution 2\n",
- "while myname:\n",
- " print myname[0],\n",
- " myname = myname[1:]\n",
- " "
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# What happens if you run the following code? \n",
- "while True:\n",
- " print 1"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 6,
- "metadata": {},
- "source": [
- "Print out all powers of 2 that are less than 1000."
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "start = 1\n",
- "while start < 1000:\n",
- " print start,\n",
- " start *=2"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# How many times does the number 'e' appear in the following excerpt?\n",
- "\n",
- "mr_darcy = \"I have faults enough, but they are not, \\\n",
- "I hope, of understanding. My temper I dare not vouch for. \\\n",
- "It is, I believe, too little yielding\u2014 certainly too little for the convenience of the world. \\\n",
- "I cannot forget the follies and vices of other so soon as I ought, nor their offenses against myself. \\\n",
- "My feelings are not puffed about with every attempt to move them. \\\n",
- "My temper would perhaps be called resentful. \\\n",
- "My good opinion once lost, is lost forever.\" "
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "## Solution 1\n",
- "count = 0\n",
- "for i in mr_darcy:\n",
- " if i =='e':\n",
- " count+=1\n",
- " else:\n",
- " pass\n",
- " \n",
- "## Solution 2 (Advanced)\n",
- "sum([1 for i in mr_darcy if i =='e'])\n",
- " \n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# Challenge! Your friend just sent you a secret text! Translate it using Python. \n",
- "\n",
- "english = \"abcdefghijklmnopqrstuvwxyz\"\n",
- "secret_language = \"hvicgnwzxqektmuspjlbyodrfa\"\n",
- "\n",
- "text = 'sfbzum xl bzg mgd vkhie'"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "translated = ''\n",
- "for i in text:\n",
- " if i == ' ':\n",
- " translated = translated + ' '\n",
- " else:\n",
- " translated = translated + english[secret_language.find(i)]"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 2,
- "metadata": {},
- "source": [
- "Functions "
- ]
- },
- {
- "cell_type": "heading",
- "level": 6,
- "metadata": {},
- "source": [
- "Why use functions? "
- ]
- },
- {
- "cell_type": "heading",
- "level": 6,
- "metadata": {},
- "source": [
- "Reason #1: Reusability"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "# a simple function \n",
- "def myfunction():\n",
- " print(\"I love Python.\")\n",
- "\n",
- "myfunction()"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "def lovefunction(mystring):\n",
- " print(\"I love \" + mystring)\n",
- "\n",
- "lovefunction(\"cookies\")\n",
- "lovefunction(\"summer\")\n",
- "lovefunction(\"Python\")"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "favorite_foods = [\"cookies\",\"ice cream\"]\n",
- "healthy_foods = [\"kale\",\"quinoa\"]\n",
- "\n",
- "def myfoodpicker(food_item):\n",
- " \n",
- " if food_item in favorite_foods:\n",
- " print(\"Yum!\")\n",
- " elif food_item in healthy_foods:\n",
- " print(\"Yuck!\")\n",
- " else:\n",
- " print(\"Can I have ice cream with that?\")\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 6,
- "metadata": {},
- "source": [
- "Reason #2: Abstraction"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": true,
- "input": [
- "## Find the largest prime less than or equal to a certain number \n",
- "def find_largest_prime(mynumber):\n",
- " ## use simple sieve method\n",
- " ## highly inefficient for large values\n",
- " possible_results = range(1, mynumber + 1)\n",
- " possible_factors = range(2, int(floor(mynumber/2))+1)\n",
- " \n",
- " def get_multiples(factor):\n",
- " # get all multiples of factor \n",
- " return [factor*i for i in range(1, int(floor(mynumber/factor)) + 1)]\n",
- " \n",
- " for i in possible_factors:\n",
- " # remove all multiples of said factor\n",
- " possible_results = [x for x in possible_results if x not in get_multiples(i)]\n",
- " \n",
- " return possible_results[-1] # return largest value\n",
- "\n",
- "## using the function \n",
- "\n",
- "find_largest_prime(100)\n",
- "find_largest_prime(50)\n",
- " "
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 6,
- "metadata": {},
- "source": [
- "Write a function to check if a number is divisible by 7."
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "def divisible_by_7(mynumber):\n",
- " if mynumber%7 ==0:\n",
- " print \"This number is divisible by 7\"\n",
- " else:\n",
- " print \"This number is not divisible by 7\"\n"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- },
- {
- "cell_type": "heading",
- "level": 6,
- "metadata": {},
- "source": [
- "Project: Your goal is to build a chatterbot that produces typical teenager responses.\n",
- "For example, if you ask the chatterbot a question, then it respond with \"It was boring.\"\n",
- "Feel free to be creative!"
- ]
- },
- {
- "cell_type": "code",
- "collapsed": false,
- "input": [
- "import string\n",
- "import re\n",
- "import numpy as np\n",
- "import sys\n",
- "\n",
- "def mood_generator(mood):\n",
- " if mood == 'good':\n",
- " score = np.random.random(1) + 0.1\n",
- " elif mood == 'bad':\n",
- " score = np.random.random(1) - 0.1\n",
- " else:\n",
- " score = np.random.random(1)\n",
- " return score\n",
- "\n",
- "\n",
- "def make_bot(mood):\n",
- " mood_score = mood_generator(mood)\n",
- " \n",
- " done = False\n",
- " while not done:\n",
- " mom_says = raw_input(\"Say something: (Type 'quit' to exit the program)\")\n",
- " ## remove quotes\n",
- " if mom_says == 'quit' or len(mom_says) == 0:\n",
- " done = True\n",
- " else:\n",
- " if mom_says[-1] == '?':\n",
- " if mood_score > 0.5:\n",
- " print \"It was alright.\"\n",
- " else:\n",
- " print \"Answer: Leave me alone.\"\n",
- " \n",
- " elif mom_says[-1] == '!':\n",
- " if mood_score > 0.5:\n",
- " print \"Ok! Ok!\"\n",
- " else:\n",
- " print \"Answer: Stop yelling!\"\n",
- " elif mom_says[-1] == '.':\n",
- " if mood_score > 0.5:\n",
- " print \"Answer: Whatever.\"\n",
- " else:\n",
- " print \"Answer: Whatever\"\n",
- " elif re.search('homework', string.lower(mom_says)):\n",
- " print \"I'll do that later\"\n",
- " else:\n",
- " pass\n",
- " \n",
- "if __name__ == '__main__':\n",
- " mood = sys.argv[1]\n",
- " make_bot(mood)"
- ],
- "language": "python",
- "metadata": {},
- "outputs": []
- }
- ],
- "metadata": {}
- }
- ]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement