diff --git a/pip2/source/Assignments/ps6.rst b/pip2/source/Assignments/ps6.rst index f79986bdf..e69de29bb 100644 --- a/pip2/source/Assignments/ps6.rst +++ b/pip2/source/Assignments/ps6.rst @@ -1,352 +0,0 @@ -:orphan: - -.. Copyright (C) Paul Resnick. Permission is granted to copy, distribute - and/or modify this document under the terms of the GNU Free Documentation - License, Version 1.3 or any later version published by the Free Software - Foundation; with Invariant Sections being Forward, Prefaces, and - Contributor List, no Front-Cover Texts, and no Back-Cover Texts. A copy of - the license is included in the section entitled "GNU Free Documentation - License". - -.. highlight:: python - :linenothreshold: 500 - - -Week 6: ends October 12 -======================= - -1. Do the multiple choice questions and exercises in the textbook chapters, including the ones at the bottom of the chapters, below the glossary. Don't forget to click Save for each of the exercises, and always access the textbook by clicking on the link from cTools, so that you'll be logged in. - - * Before Tuesday's class: - * Read :ref:`Optional and Keyword Parameters`, and do the exercises in that chapter - * Read `Blog post and demo of sliding window compression `_ - - * Before Thursday's class: - * Read :ref:`Tuples`, and do the exercises in that chapter - * Read :ref:`Nested Data Structures and Nested Iteration`, and do the exercises in that chapter - * Read :ref:`Installing a Native Python Interpreter and Text Editor ` and follow the instructions to set up for running python on your computer - - -#. Reading responses - - * By Monday midnight: - * Read `Tutorial on unix diff `_ (This will help you understand the section of "The Most Human Human" below). - * Read *The Most Human Human*, Chapter 10, p.237-259. - * Answer :ref:`Reading Response 7 `. - -#. Problem set **Due:** **Sunday, October 12 at 5 pm** - - * Do the :ref:`Native Python Interpreter and Text Editor part of Problem Set 6. ` - - * Save answers to the exercises in Problem Set 6: :ref:`Problem Set 6 ` - - - -Reading Response ----------------- - -.. _reading_response_7: - -Suppose you write and edit a long text file over the course of several days, saving a new version every 15 minutes or so (``myfile1.txt``, ``myfile2.txt``, ``myfile3.txt``,...). Eventually, you have 100 different versions of the file. Now consider the whole directory containing all 100 versions of the file. Would it have a lot of redundancy? As a compression technique, how might you take advantage of the unix diff command in order to reduce the total amount of space required to store all 100 versions of the file? - -.. activecode:: rr_7_2 - - # Fill in your response in between the triple quotes - s = """ - - """ - print s - - - -If you were to compute information entropy scores for all the students you've met since enrolling at the University of Michigan, which of them has the highest entropy and why? - -.. activecode:: rr_7_2 - - # Fill in your response in between the triple quotes - s = """ - - """ - print s - - -Command Line Problems ---------------------- - -.. _unix_pset6: - -Turn these in as screenshots via CTools in the Assignments tab! - -#. Make a new file in your text editor, and save it as ``new_program.py``. (This is a Python program!) - -#. In your ``new_program.py`` file, write the following code (copy it from here). - -..activecode:: example_code_ps6 - - def cool_machine(x): - y = x**2 +7 - print y - - z = 65.3 - print z + cool_machine(8) - -Then, run the Python program in your native Python interpreter. You should get an error. Take a screenshot of this and upload it to CTools. - -Make edits to this code so it will work, without an error, and then save it with a different name (``fixed_program.py``). Now, run unix ``diff`` on these two files. Take a screenshot of the output, and upload it to CTools. - - -Problem Set ------------ - -.. _problem_set_6: - -1. Write three function calls to the function ``give_greeting``: -one that will return the string ``Hello, SI106!!!``, -one that will return the string ``Hello, world!!!``, -and one that will return the string ``Hey, everybody!``. - -You may print the return values of those function calls, but you do not have to. - -You can see the function definition in the code below, but that's only so you can understand exactly what the code is doing so you can choose how to call this function. Feel free to make comments to help yourself understand, but otherwise DO NOT change the function definition code! HINT: calling the function in different ways and printing the results, to see what happens, may be helpful! - -.. activecode:: ps_6_1 - - def give_greeting(greet_word="Hello",name="SI106",num_exclam=3): - final_string = greet_word + ", " + name + " " + "!"*num_exclam - return final_string - - #### DO NOT change the function definition above this line (only comments are OK) - - # Write your three function calls below - - -#. Define a function called mult_both whose input is two integers, whose default parameter values are the integers 3 and 4, and whose return value is the two input integers multiplied together. - -.. activecode:: ps_6_2: - - # Write your code here - - ==== - - import test - print "\n---\n\n" - print "Testing whether your function works as expected (calling the function mult_both)" - test.testEqual(mult_both(), 12) - test.testEqual(mult_both(5,10), 50) - - - -#. Print the second element of each tuple in the list ``new_tuple_list``. - -.. activecode:: ps_6_3 - - new_tuple_list = [(1,2),(4, "umbrella"),("chair","hello"),("soda",56.2)] - - -#. You can get data from Facebook that has nested structures which represent posts, or users, or various other types of things on Facebook. We won't put any of our actual Facebook group data on this textbook, because it's publicly available on the internet, but here's a structure that is almost exactly the same as the real thing, with fake data. - -Notice that the stuff in the variable ``fb_data`` is basically a big nested dictionary, with dictionaries and lists, strings and integers, inside it as keys and values. (Later in the course we'll learn how to get this kind of thing directly FROM facebook, and then it will be a bit more complicated and have real information from our Facebook group.) - -Follow the directions in the comments! - -.. activecode:: ps_6_4 - - # first, look through the data structure saved in the variable fb_stuff to get a sense for it. - - fb_stuff = { - "data": [ - { - "id": "2253324325325123432madeup", - "from": { - "id": "23243152523425madeup", - "name": "Jane Smith" - }, - "to": { - "data": [ - { - "name": "Your Facebook Group", - "id": "432542543635453245madeup" - } - ] - }, - "message": "This problem might use the accumulation pattern, like many problems do", - "type": "status", - "created_time": "2014-10-03T02:07:19+0000", - "updated_time": "2014-10-03T02:07:19+0000" - }, - - { - "id": "2359739457974250975madeup", - "from": { - "id": "4363684063madeup", - "name": "John Smythe" - }, - "to": { - "data": [ - { - "name": "Your Facebook Group", - "id": "432542543635453245madeup" - } - ] - }, - "message": "Here is a fun link about programming", - "type": "status", - "created_time": "2014-10-02T20:12:28+0000", - "updated_time": "2014-10-02T20:12:28+0000" - }] - } - - # Here are some questions to help you. You don't need to comment answers to these, but we suggest doing so! They may help you think through this big nested data structure. - # What type is the structure saved in the variable fb_data? - # What type does the expression fb_data["data"] evaluate to? - # What about fb_data["data"][1]? - # What about fb_data["data"][0]["from"]? - # What about fb_data["data"][0]["id"]? - - # Now write a line of code to assign the value of the first message in the big fb_data data structure to a variable called first_message. Do not hard code your answer! (Write it in terms of fb_data.) - - - ==== - - import test - test.testEqual(first_message,fb_data["data"][0]["message"]) - - -#. Here's a warm up exercise on defining and calling a function: - -.. activecode:: ps_6_5 - - # Define a function is_prefix that takes two strings and returns True if the - # first one is a prefix of the second one, False otherwise. - - - - # Here's a couple example function calls, printing the return value to show you what it is. - print is_prefix("He","Hello") # should print True - print is_prefix("Hi","Hello") # should print False - - -#. Now, in the next few questions, you’ll build components and then a complete program that lets people play Hangman. Below is an image from the middle of a game... - -.. image:: Figures/HangmanSample.JPG - -See the flow chart[LINK] for a better understanding of what's happening in the code for the Hangman game overall. - -The first task you have to build part of the Hangman game follows: - -.. activecode:: ps_6_6 - - # define the function blanked(). - # It takes a word and a string of letters that have been revealed. - # It should return a string with the same number of characters as - # the original word, but with the unrevealed characters replaced by _ - - # a sample call to this function: - print(blanked("Hello", "el")) - #should output _ell_ - - -#. The second task to build part of the Hangman game: - -.. activecode:: ps_6_7 - - # define the function health_prompt(). The first parameter is the current - # health and the second is the the maximum health you can have. It should return a string - # with + signs for the current health, and - signs for the health that has been lost. - - - - - print(health_prompt(3, 7)) - #this should produce the output - #health: +++---- - - print(health_prompt(0, 4)) - #this should produce the output - #health: ---- - - -#. Here's almost all of the Hangman game code. Paste your definition of the functions ``blanked`` and ``health_prompt`` at the start of this code. Then you can play when you run the program..! - -.. activecode:: ps_6_8 - - # Here's where you should paste your function definitions. - - - - def game_state_prompt(txt, h, m_h, word, guesses): - res = txt + "\n" - res = res + health_prompt(h, m_h) + "\n" - if guesses != "": - res = res + "Guesses so far: " + guesses.upper() + "\n" - else: - res = res + "No guesses so far" + "\n" - res = res + "Word: " + blanked(word, guesses) + "\n" - - return(res) - - def main(): - max_health = 3 - health = max_health - secret_word = raw_input("What's the word to guess? (Don't let the player see it!)") - secret_word = secret_word.upper() # everything in all capitals to avoid confusion - guesses_so_far = "" - game_over = False - - feedback = "let's get started" - - # Now interactively ask the user to guess - while not game_over: - # replace this comment with code that invokes game_state_prompt and assign the return value to the variable prompt - prompt = game_state_prompt(feedback, health, max_health, secret_word, guesses_so_far) - next_guess = raw_input(prompt) - next_guess = next_guess.upper() - feedback = "" - if len(next_guess) != 1: - feedback = "I only understand single letter guesses. Please try again." - elif next_guess in guesses_so_far: - feedback = "You already guessed that" - else: - guesses_so_far = guesses_so_far + next_guess - if next_guess in secret_word: - if blanked(secret_word, guesses_so_far) == secret_word: - feedback = "Congratulations" - game_over = True - else: - feedback = "Yes, that letter is in the word" - else: # next_guess is not in the word secret_word - feedback = "Sorry, " + next_guess + " is not in the word." - health = health - 1 - if health <= 0: - feedback = " Waah, waah, waah. Game over." - game_over= True - - print(feedback) - print("The word was..." + secret_word) - - import sys #don't worry about this line; you'll understand it next week - sys.setExecutionLimit(60000) # let the game take up to a minute, 60 * 1000 milliseconds - main() - - -#. Look at the code for the Hangman game, below. (This code does NOT have your functions in it, so all the line numbers will be the same.) Then look at the flow chart. Write which lines of code go with which lines of the flow chart box, by answering the questions in comments below. - -.. activecode:: ps_6_9 - - # What line(s) of code do what's mentioned in box 1? - - # What line(s) of code do what's mentioned in box 2? - - # What line(s) of code do what's mentioned in box 3? - - # What line(s) of code do what's mentioned in box 4? - - # What line(s) of code do what's mentioned in box 5? - - # What line(s) of code do what's mentioned in box 6? - - # What line(s) of code do what's mentioned in box 7? - - # What line(s) of code do what's mentioned in box 8? - - # What line(s) of code do what's mentioned in box 9? diff --git a/pip2/source/Debugging/KnowyourerrorMessages.rst b/pip2/source/Debugging/KnowyourerrorMessages.rst index e52a2df03..f17b886c7 100644 --- a/pip2/source/Debugging/KnowyourerrorMessages.rst +++ b/pip2/source/Debugging/KnowyourerrorMessages.rst @@ -216,7 +216,7 @@ Here's an example of a type error created by a Polish learner. See if you can f So, the solution to this problem is to change lines 3 and 4 so they are assignment statements. -**Finding Clues** One thing that can help you in this situation is to print out the values and the types of the variables involved in the statement that is causing the error. You might try adding a print statement after line 4 ``print(x, type(x))`` You will see that at least we have confirmed that x is of type string. Now you need to start to work backward through the program. You need to ask yourself, where is x used in the program? x is used on lines 2, 3, and of course 5 and 6 (where we are getting an error). So maybe you move the print statement to be after line 2 and again after 3. Line Three is where you expect the value of x to be changed to an integer. Could line 4 be mysteriously changine x back to a string? Not very likely. So the value and type of x is just what you would expect it to be after line 2, but not after line 3. This helps you isolate the problem to line 3. In fact if you employ one of our earler techniques of commenting out line 3 you will see that this has no impact on the error, and is a big clue that line 3 as it is currently written is useless. +**Finding Clues** One thing that can help you in this situation is to print out the values and the types of the variables involved in the statement that is causing the error. You might try adding a print statement after line 4 ``print x, type(x)`` You will see that at least we have confirmed that x is of type string. Now you need to start to work backward through the program. You need to ask yourself, where is x used in the program? x is used on lines 2, 3, and of course 5 and 6 (where we are getting an error). So maybe you move the print statement to be after line 2 and again after 3. Line Three is where you expect the value of x to be changed to an integer. Could line 4 be mysteriously changine x back to a string? Not very likely. So the value and type of x is just what you would expect it to be after line 2, but not after line 3. This helps you isolate the problem to line 3. In fact if you employ one of our earler techniques of commenting out line 3 you will see that this has no impact on the error, and is a big clue that line 3 as it is currently written is useless. NameError diff --git a/pip2/source/Installation/Figures/bitbucketafterpush.JPG b/pip2/source/Installation/Figures/bitbucketafterpush.JPG new file mode 100755 index 000000000..dbd8d270b Binary files /dev/null and b/pip2/source/Installation/Figures/bitbucketafterpush.JPG differ diff --git a/pip2/source/Installation/Figures/clone.JPG b/pip2/source/Installation/Figures/clone.JPG new file mode 100755 index 000000000..52a70de02 Binary files /dev/null and b/pip2/source/Installation/Figures/clone.JPG differ diff --git a/pip2/source/Installation/Figures/clone2.JPG b/pip2/source/Installation/Figures/clone2.JPG new file mode 100755 index 000000000..67b0902f2 Binary files /dev/null and b/pip2/source/Installation/Figures/clone2.JPG differ diff --git a/pip2/source/Installation/Figures/directory.JPG b/pip2/source/Installation/Figures/directory.JPG new file mode 100755 index 000000000..864405b92 Binary files /dev/null and b/pip2/source/Installation/Figures/directory.JPG differ diff --git a/pip2/source/Installation/Figures/downloads.png b/pip2/source/Installation/Figures/downloads.png new file mode 100644 index 000000000..813267480 Binary files /dev/null and b/pip2/source/Installation/Figures/downloads.png differ diff --git a/pip2/source/Installation/Figures/emptyterminal.png b/pip2/source/Installation/Figures/emptyterminal.png new file mode 100644 index 000000000..efd49dd47 Binary files /dev/null and b/pip2/source/Installation/Figures/emptyterminal.png differ diff --git a/pip2/source/Installation/Figures/environment.JPG b/pip2/source/Installation/Figures/environment.JPG new file mode 100755 index 000000000..bc31847d7 Binary files /dev/null and b/pip2/source/Installation/Figures/environment.JPG differ diff --git a/pip2/source/Installation/Figures/firstprog.JPG b/pip2/source/Installation/Figures/firstprog.JPG new file mode 100755 index 000000000..a671af589 Binary files /dev/null and b/pip2/source/Installation/Figures/firstprog.JPG differ diff --git a/pip2/source/Installation/Figures/firstprogram_tw.png b/pip2/source/Installation/Figures/firstprogram_tw.png new file mode 100644 index 000000000..3b2871dcb Binary files /dev/null and b/pip2/source/Installation/Figures/firstprogram_tw.png differ diff --git a/pip2/source/Installation/Figures/fork.JPG b/pip2/source/Installation/Figures/fork.JPG new file mode 100755 index 000000000..ad9f37f71 Binary files /dev/null and b/pip2/source/Installation/Figures/fork.JPG differ diff --git a/pip2/source/Installation/Figures/forkconfig.JPG b/pip2/source/Installation/Figures/forkconfig.JPG new file mode 100755 index 000000000..509e4e071 Binary files /dev/null and b/pip2/source/Installation/Figures/forkconfig.JPG differ diff --git a/pip2/source/Installation/Figures/gitconfig.png b/pip2/source/Installation/Figures/gitconfig.png new file mode 100644 index 000000000..5684f1f90 Binary files /dev/null and b/pip2/source/Installation/Figures/gitconfig.png differ diff --git a/pip2/source/Installation/Figures/gitstatus1.JPG b/pip2/source/Installation/Figures/gitstatus1.JPG new file mode 100755 index 000000000..18e7db50a Binary files /dev/null and b/pip2/source/Installation/Figures/gitstatus1.JPG differ diff --git a/pip2/source/Installation/Figures/gitstatus2.JPG b/pip2/source/Installation/Figures/gitstatus2.JPG new file mode 100755 index 000000000..83404e1e8 Binary files /dev/null and b/pip2/source/Installation/Figures/gitstatus2.JPG differ diff --git a/pip2/source/Installation/Figures/gitstatus3.JPG b/pip2/source/Installation/Figures/gitstatus3.JPG new file mode 100755 index 000000000..be4ffdde2 Binary files /dev/null and b/pip2/source/Installation/Figures/gitstatus3.JPG differ diff --git a/pip2/source/Installation/Figures/gitstatus4.JPG b/pip2/source/Installation/Figures/gitstatus4.JPG new file mode 100755 index 000000000..d93baefee Binary files /dev/null and b/pip2/source/Installation/Figures/gitstatus4.JPG differ diff --git a/pip2/source/Installation/Figures/helloworld.JPG b/pip2/source/Installation/Figures/helloworld.JPG new file mode 100755 index 000000000..126d0b484 Binary files /dev/null and b/pip2/source/Installation/Figures/helloworld.JPG differ diff --git a/pip2/source/Installation/Figures/helloworldmac.png b/pip2/source/Installation/Figures/helloworldmac.png new file mode 100644 index 000000000..b7fb93155 Binary files /dev/null and b/pip2/source/Installation/Figures/helloworldmac.png differ diff --git a/pip2/source/Installation/Figures/pullupstream.JPG b/pip2/source/Installation/Figures/pullupstream.JPG new file mode 100755 index 000000000..a9d340542 Binary files /dev/null and b/pip2/source/Installation/Figures/pullupstream.JPG differ diff --git a/pip2/source/Installation/Figures/pythoninterpreter.JPG b/pip2/source/Installation/Figures/pythoninterpreter.JPG new file mode 100755 index 000000000..b91e66a0d Binary files /dev/null and b/pip2/source/Installation/Figures/pythoninterpreter.JPG differ diff --git a/pip2/source/Installation/Figures/returntypedpython.png b/pip2/source/Installation/Figures/returntypedpython.png new file mode 100644 index 000000000..076749b09 Binary files /dev/null and b/pip2/source/Installation/Figures/returntypedpython.png differ diff --git a/pip2/source/Installation/Figures/secondprog1.JPG b/pip2/source/Installation/Figures/secondprog1.JPG new file mode 100755 index 000000000..25aa88d25 Binary files /dev/null and b/pip2/source/Installation/Figures/secondprog1.JPG differ diff --git a/pip2/source/Installation/Figures/secondprog2.JPG b/pip2/source/Installation/Figures/secondprog2.JPG new file mode 100755 index 000000000..8e3bd2718 Binary files /dev/null and b/pip2/source/Installation/Figures/secondprog2.JPG differ diff --git a/pip2/source/Installation/Figures/tabs.JPG b/pip2/source/Installation/Figures/tabs.JPG new file mode 100755 index 000000000..aa73a2ee7 Binary files /dev/null and b/pip2/source/Installation/Figures/tabs.JPG differ diff --git a/pip2/source/Installation/Figures/terminalicon.png b/pip2/source/Installation/Figures/terminalicon.png new file mode 100644 index 000000000..251a5002d Binary files /dev/null and b/pip2/source/Installation/Figures/terminalicon.png differ diff --git a/pip2/source/Installation/Figures/typedpython.png b/pip2/source/Installation/Figures/typedpython.png new file mode 100644 index 000000000..ef585f56e Binary files /dev/null and b/pip2/source/Installation/Figures/typedpython.png differ diff --git a/pip2/source/Installation/Figures/typepythonversion.png b/pip2/source/Installation/Figures/typepythonversion.png new file mode 100644 index 000000000..163d87c2d Binary files /dev/null and b/pip2/source/Installation/Figures/typepythonversion.png differ diff --git a/pip2/source/Installation/Figures/upstream.JPG b/pip2/source/Installation/Figures/upstream.JPG new file mode 100755 index 000000000..6db30ba02 Binary files /dev/null and b/pip2/source/Installation/Figures/upstream.JPG differ diff --git a/pip2/source/Installation/Figures/upstream2.JPG b/pip2/source/Installation/Figures/upstream2.JPG new file mode 100755 index 000000000..625a25552 Binary files /dev/null and b/pip2/source/Installation/Figures/upstream2.JPG differ diff --git a/pip2/source/Installation/Figures/whichpythonfinal.png b/pip2/source/Installation/Figures/whichpythonfinal.png new file mode 100644 index 000000000..754cad16e Binary files /dev/null and b/pip2/source/Installation/Figures/whichpythonfinal.png differ diff --git a/pip2/source/Installation/NextSteps.rst b/pip2/source/Installation/NextSteps.rst new file mode 100644 index 000000000..21608df41 --- /dev/null +++ b/pip2/source/Installation/NextSteps.rst @@ -0,0 +1,60 @@ +.. Copyright (C) Brad Miller, David Ranum, Jeffrey Elkner, Peter Wentworth, Allen B. Downey, Chris + Meyers, Dario Mitchell, Paul Resnick. Permission is granted to copy, distribute + and/or modify this document under the terms of the GNU Free Documentation + License, Version 1.3 or any later version published by the Free Software + Foundation; with Invariant Sections being Forward, Prefaces, and + Contributor List, no Front-Cover Texts, and no Back-Cover Texts. A copy of + the license is included in the section entitled "GNU Free Documentation + License". + +.. _next_steps: + +Preparing for the Rest of the Course +==================================== + +It has been convenient to be able to execute python code right in the browser. To write and +execute bigger programs, to use modules beyond the few (like the ``random`` module) that have been implemented for this environment, and to perform file and network operations, you will need to run Python *natively* on your computer. + +First, we'll provide an overview of how writing and running programs on your own computer works. Then we'll provide instructions for installing what you need and getting the new setup going. + +There are different instructions for Windows users and Mac users below. (If you use Linux, there are no instructions for you -- we assume in that case you know what you are doing here, but if you are confused, let us know.) + +None of the steps that follow is particularly difficult, but there are a lot of them, and if you inadvertently skip one, you may not have a good time. We recommend that you print out this chapter and use a pen to check off the steps as you do them, or do the equivalent on screen with a PDF file. + +Important Concepts +================== + +Up till now, we have written code in the active code windows and clicked "Run" to run it. You saw results in the console, below the active code windows-- remember, ``print`` is for people. + +When you write code and run it *on your own computer*, what happens is basically this: + +You'll write a program and save it as a file. You need a text editor to do this (more about this below! Note that MS Word is *not* a text editor). + +.. note:: + + All programs you will write from here on will be stored in files (or groups of files, but we'll get to that) that are saved in a certain way, with the extension **.py**, so the computer knows these are *Python programs*. + +You will have an interpreter for Python programs installed on your computer. Read on to find out how to do that. Naturally enough, that interpreter will be called ``python``. + +You'll use the command prompt, which you've already been learning about, to essentially say "hey, python, find this file that has my Python program, and interpret and then run it." The analog of clicking on the "Run" button in our online environment will be to type ``python `` at a command prompt. For example: + +.. image:: Figures/secondprog1.JPG + +You'll always have to know exactly where you saved your code file on your computer. Similar to how every single character matters when you write Python code, you have to tell the computer exactly where the program file you want to run is. + +In summary, the biggest differences from the online environment you've been using will be that you'll switch back and forth between two windows (one for editing the code, another for running it), you will name all the code files you save, and it's very important how and where you save your files. This is where your file system and Unix knowledge from the past few weeks will help! + +Preparing Your Computer for the Rest of the Course +================================================== + +We will walk you through the process of setting up your computer to run Python natively. It will involve the following steps: + +1. Install and configure a text editor + +#. Install and configure Python + +Then, you'll save your files in the folder/file structure we've been working with for the Unix Problems, so you have one folder where all of your code lives. **Make sure** to save all your code in this same place, where you can find it and know exactly where it all is -- not doing so will create a headache for you! + +The instructions diverge a bit, depending on whether you are on Windows or a Mac. (If you're on +Linux, we presume that you know what you're doing already and can make appropriate +improvisations from the instructions for Windows or Mac, but feel free to ask.) \ No newline at end of file diff --git a/pip2/source/Installation/TextEditor.rst b/pip2/source/Installation/TextEditor.rst new file mode 100644 index 000000000..4e37df535 --- /dev/null +++ b/pip2/source/Installation/TextEditor.rst @@ -0,0 +1,89 @@ +.. Copyright (C) Brad Miller, David Ranum, Jeffrey Elkner, Peter Wentworth, Allen B. Downey, Chris + Meyers, Dario Mitchell, Paul Resnick. Permission is granted to copy, distribute + and/or modify this document under the terms of the GNU Free Documentation + License, Version 1.3 or any later version published by the Free Software + Foundation; with Invariant Sections being Forward, Prefaces, and + Contributor List, no Front-Cover Texts, and no Back-Cover Texts. A copy of + the license is included in the section entitled "GNU Free Documentation + License". + + +.. _text_editor_installation: + + +Installing a Text Editor +======================== + +You will need a text editor. There are many options for this. For example, serious +programmers often use Eclipse or XCode, which are environments that include some useful tools, or Vim or Emacs, which are text editors that you can use inside the command prompt, which require learning a lot of keyboard commands. We do not recommend those for beginners programming -- better to focus on the problem-solving itself, and there's a lot of stuff to configure in those that you don't need right now. (Many serious programmers don't use those either!) + +Definitely **do not** use MS Word. Word will not save files in the right format, so you will not be able to run programs, and it doesn't do any syntax highlighting or other useful things. Default programs that come with your operating system like TextEdit for Mac or plain Notepad for Windows are also not a good idea -- this can lead to file formatting issues, and you won't have syntax highlighting and other useful features. + + +.. _windows_install: + +Windows Instructions +==================== + +The editor that we will help you to use is called **NotePad++**. Please download it from +`this site `_. Download it and then run the installer to install NotePad++, like you would most programs you download. + +.. note:: + + Important! Before you create your first program, you need to make one small change in the Preferences for NotePad++. This will save you lots of "Python indent errors" anguish later. + Under *Settings -> Preferences -> Language Menu/Tab Settings*, tick the check box for "Expand Tabs", leaving the value at "4", and + press the "Close" button. + + .. image:: Figures/tabs.JPG + + +Follow the instructions below. It should be +quite intuitive. The one thing to keep in mind is that NotePad++ is an environment +for *creating* python programs. It doesn't run them! You'll have to install a little +more stuff to run your programs, as described in later sections. +(If you'd like to see a demonstration of NotePad++, Dr. Chuck has a screen cast for the use of NotePad++. +You can either view this `on YouTube `_ or you can download the high-quality `QuickTime version `_ +of the screen cast. You will need Apple QuickTime installed to view this video. ) + +Start NotePad++ from either a Desktop icon or from the Start Programs menu and enter your first Python program into NotePad++: + + .. image:: Figures/helloworld.JPG + :width: 300px + +Save your program as ``firstprog.py``. You can save it anywhere. In a little while we'll +create a code folder in a convenient place on your machine and you can resave the file then. +You will notice that after you save the file, NotePad++ will color your code based on the Python syntax rules. +Syntax coloring is a very helpful feature as it gives you visual feedback about your program and can help you track down syntax errors more easily. +NotePad++ only knows that your file is a Python file after you save it with a ``.py`` suffix, also known as file extension. It's like the ``.txt`` file extension we've seen that means a file is a plain text file, except this ``.py`` extension means that this file is a Python program. + + .. image:: Figures/firstprog.JPG + :width: 300px + + +.. _mac_install: + +Mac Instructions +================ + +The editor that we will help you to use is called **TextWrangler**. (TextWrangler and Notepad++ are very similar, but one runs on Macs and one runs on Windows.) Please download it from +`the TextWrangler site `_. Download it and then run the installer to install TextWrangler, like you would most programs you download. + +TextWrangler may ask you to register for something, or to install other programs. You can hit Cancel -- you do not need to register for anything to use TextWrangler, you do not need any other programs, and it will not expire. + +Follow the instructions that follow. It should be +quite intuitive. Keep in mind the concepts from earlier -- TextWrangler is an environment (a piece of software) +for _creating_ python programs. It's not intended (in this course) for running them! + +Start TextWrangler from a Dock shortcut icon, finding it in your Applications folder, or startinit from Spotlight. Enter your first Python program into TextWrangler: + + .. image:: Figures/helloworldmac.png + :width: 300px + +Save your program as ``firstprog.py``, in your 106 folder. You will notice that after you save the file, TextWrangler will color your code based on the Python syntax rules. That's because you saved it with the ``.py`` file extension, which tells the computer this file is a Python program. + +Syntax coloring is a very helpful feature, as it gives you visual feedback about your program and can help you track down syntax errors more easily. +TextWrangler only knows that your file is a Python file after you save it with a ``.py`` suffix, also known as file extension. It's like the ``.txt`` file extension we've seen that means a file is a plain text file, except this ``.py`` extension means that this file is a Python program. + + .. image:: Figures/firstprogram_tw.png + :width: 300px + diff --git a/pip2/source/Installation/pythonInstall.rst b/pip2/source/Installation/pythonInstall.rst new file mode 100644 index 000000000..4d7f426ce --- /dev/null +++ b/pip2/source/Installation/pythonInstall.rst @@ -0,0 +1,83 @@ +.. Copyright (C) Brad Miller, David Ranum, Jeffrey Elkner, Peter Wentworth, Allen B. Downey, Chris + Meyers, Dario Mitchell, Paul Resnick. Permission is granted to copy, distribute + and/or modify this document under the terms of the GNU Free Documentation + License, Version 1.3 or any later version published by the Free Software + Foundation; with Invariant Sections being Forward, Prefaces, and + Contributor List, no Front-Cover Texts, and no Back-Cover Texts. A copy of + the license is included in the section entitled "GNU Free Documentation + License". + + + +Install and configure Python +============================ + +Now, you'll need to install the Python interpreter on your computer. This process will again be a little bit different depending on whether you have a computer that runs a Mac or the Windows operating system. Follow the appropriate set of instructions: + +* :ref:`Windows ` +* :ref:`Mac ` + +.. _windows_python_install: + +Install and configure python for Windows +---------------------------------------- + +Please download and install Python 2.7 from: + +https://www.python.org/downloads/release/python-278/ + +Download and install the file Windows x86 MSI Installer (2.7.8) - when the install process asks you which directory to use - make sure to keep the default directory of C:\Python27\. If you are not sure if your Windows is 64-bit - install the 32-bit version of Python, the +one that just says, "Windows x86 MSI Installer (2.7.8)". If you know that you have 64-bit Windows, you can download the X86-64 MSI Installer. + +.. note:: + + Make sure that you install the latest version of Python 2.x - do not install Python 3.x. + There are signficant differences between Python 2 and Python 3 and this book/site is based on Python 2. + +With just this installation, you can get an interactive python interpreter where +you can type code one line at a time and have it executed. You may find some options +on the Windows menu for this, such as Idle. We *do not* recommend using these. + +With just this installation it is also possible to run python from the Windows command prompt. +But the Windows command prompt is tricky to deal with. To establish +greater consistency with the environment in which Mac users will be working and +because it's just a better command prompt, we will invoke Python using Git Bash. + +You have one configuration to do, to tell Git Bash where in the file system to find the Python interpreter. Follow the steps below to do that. + +#. Launch the program Git Bash in the usual way that you launch Windows programs. A shortcut for Git Bash was created during installation. + +#. At the command prompt, paste this command ``export PATH="$PATH:/c/Python27"``. That will tell Windows where to find Python. (This assumes that you installed it in C:\\Python27, as we told you to above.) + +#. Check to make sure that this worked correctly by entering the command ``python --version``. It should say Python 2.7.8 (or 2.7.something), as shown in the figure below. + +#. Assuming that worked correctly, you will want to set up git bash so that it always knows where to find python. To do that, enter the following command: ``echo 'export PATH="$PATH:/c/Python27"' > .bashrc``. That will save the command into a file called .bashrc. .bashrc is executed every time git bash launches, so you won't have to manually tell the shell where to find python again. + +#. Check to make sure that worked by typing exit, relaunching git bash, and then typing ``python --version`` again. + +.. image:: Figures/environment.JPG + +.. _mac_python_install: + +Install and configure python for Mac +------------------------------------ + +Because you have a mac, you're lucky in this case -- you already have Python. It comes pre-installed. However, we need to make sure you have the correct version of Python. We will be using version **2.7.** + +If you have Mac OS 10.7 (Lion) or later, you definitely have Python 2.7. If you have Mac OS 10.6 (Snow Leopard) or earlier, you may have a different version of Python. If so, let's get this straightened out early -- come see one of the instructors. (If this applies to many people, we will provide additional instructions for that installation!) + +To find out what version of Python you have, you'll first need to open your Terminal program. + +Now you're going to use a command that will tell us what version of python you have installed on your mac. + +Type: ``python -V``, and press return. That process should look something like this: + + .. image:: Figures/typedpython.png + :width: 300px + + .. image:: Figures/returntypedpython.png + :width: 300px + +If you see a 2.7 (and the third number can be anything) on the screen, like in that image above, you're fine. If you get an error, please see one of the instructors! + +You're now all ready to run Python. \ No newline at end of file diff --git a/pip2/source/Installation/runpythonprograms.rst b/pip2/source/Installation/runpythonprograms.rst new file mode 100644 index 000000000..c234c7cdb --- /dev/null +++ b/pip2/source/Installation/runpythonprograms.rst @@ -0,0 +1,24 @@ +.. Copyright (C) Brad Miller, David Ranum, Jeffrey Elkner, Peter Wentworth, Allen B. Downey, Chris + Meyers, Dario Mitchell, Paul Resnick. Permission is granted to copy, distribute + and/or modify this document under the terms of the GNU Free Documentation + License, Version 1.3 or any later version published by the Free Software + Foundation; with Invariant Sections being Forward, Prefaces, and + Contributor List, no Front-Cover Texts, and no Back-Cover Texts. A copy of + the license is included in the section entitled "GNU Free Documentation + License". + + +.. _run_code_file: + +Run a Python Code File +====================== + +Finally, you are ready to run a python program! At the terminal window, type ``python secondprog.py``. This will invoke the python interpreter, executing the code in file secondprog.py. That file just contains the line ``print "hello world"``, so *hello world* is output to the console. Notice that the console is just the area in the terminal window underneath where you entered the command that invoked python. + +.. image:: Figures/secondprog1.JPG + +.. note:: + + If in a terminal window you type ``python`` without specifying a filename, it launches the **python interpreter** and gives a little different command prompt. It's then waiting for you type python commands one at a time, which it immediately evaluates. In my experience, using the python interpreter is very confusing for beginning students, because it mixes up the idea of printed representations being generated only by explicit print statements (print is for people!). If you accidentally launch the python interpreter, I encourage you to just kill it, by typing ``exit()``. + + .. image:: Figures/pythoninterpreter.JPG \ No newline at end of file diff --git a/pip2/source/NestedData/ListswithComplexItems.rst b/pip2/source/NestedData/ListswithComplexItems.rst index 5dacca248..ad86c0cf6 100644 --- a/pip2/source/NestedData/ListswithComplexItems.rst +++ b/pip2/source/NestedData/ListswithComplexItems.rst @@ -123,7 +123,7 @@ You can even have a list of functions (!). def square(x): return x*x - L = [square, abs, lambda x: x + 1] + L = [square, abs] print "****names****" for f in L: @@ -143,7 +143,7 @@ function square that is defined on lines 1 and 2. The second is the built-in pyt function abs. The third is an anonymous function that returns one more than its input. In the first for loop, we do not call the functions, we just output their printed representations. The output -confirms that square truly is a function object. For some reason, in codelens, it's not able to produce a nice +confirms that square truly is a function object. For some reason, in our online environment, it's not able to produce a nice printed representation of the built-in function abs, so it just outputs In the second for loop, we call each of the functions, passing in the value -2 each time and printing whatever value the function returns. @@ -157,7 +157,7 @@ Step through it in Codelens if that's not all clear to you yet. def square(x): return x*x - L = [square, abs, lambda x: x + 1] + L = [square, abs] print "****names****" for f in L: diff --git a/pip2/source/NestedData/NestedIteration.rst b/pip2/source/NestedData/NestedIteration.rst index fc7af505b..84e51841f 100644 --- a/pip2/source/NestedData/NestedIteration.rst +++ b/pip2/source/NestedData/NestedIteration.rst @@ -19,7 +19,7 @@ to traverse them. for x in nested1: print "level1: " for y in x: - print "\tlevel2: " + y + print " level2: " + y Line 3 executes once for each top-level list, three times in all. With each sub-list, line 5 executes once for each item in the sub-list. Try stepping through it in Codelens to make sure you understand what the nested iteration does. @@ -30,7 +30,7 @@ line 5 executes once for each item in the sub-list. Try stepping through it in C for x in nested1: print "level1: " for y in x: - print "\tlevel2: " + y + print " level2: " + y .. parsonsprob:: nested_data_12 diff --git a/pip2/source/NestedData/intro-NestedDataandNestedIteration.rst b/pip2/source/NestedData/intro-NestedDataandNestedIteration.rst deleted file mode 100644 index 755165422..000000000 --- a/pip2/source/NestedData/intro-NestedDataandNestedIteration.rst +++ /dev/null @@ -1,12 +0,0 @@ -.. Copyright (C) Brad Miller, David Ranum, Jeffrey Elkner, Peter Wentworth, Allen B. Downey, Chris - Meyers, and Dario Mitchell. Permission is granted to copy, distribute - and/or modify this document under the terms of the GNU Free Documentation - License, Version 1.3 or any later version published by the Free Software - Foundation; with Invariant Sections being Forward, Prefaces, and - Contributor List, no Front-Cover Texts, and no Back-Cover Texts. A copy of - the license is included in the section entitled "GNU Free Documentation - License". - -Introduction: Nested Data and Nested Iteration -================================ - diff --git a/pip2/source/OptionalAndKeywordParameters/KeywordParameters.rst b/pip2/source/OptionalAndKeywordParameters/KeywordParameters.rst index dd907cad0..8e4138ba2 100644 --- a/pip2/source/OptionalAndKeywordParameters/KeywordParameters.rst +++ b/pip2/source/OptionalAndKeywordParameters/KeywordParameters.rst @@ -19,7 +19,7 @@ Keyword Parameters ================== -In the previous section, on :ref:`Optional Parameters ` you learned how to define default values for formal parameters, which made it optional to provide values for those parameters when invoking the functions. +In the previous section, on :ref:`Optional Parameters ` you learned how to define default values for formal parameters, which made it optional to provide values for those parameters when invoking the functions. In this chapter, you'll see one more way to invoke functions with optional parameters, with keyword-based parameter passing. This is particularly convenient when there are several optional parameters and you want to provide a value for one of the later parameters while not providing a value for the earlier ones. diff --git a/pip2/source/OptionalAndKeywordParameters/OptionalParameters.rst b/pip2/source/OptionalAndKeywordParameters/OptionalParameters.rst index 451c0ffec..b0d2eefc8 100644 --- a/pip2/source/OptionalAndKeywordParameters/OptionalParameters.rst +++ b/pip2/source/OptionalAndKeywordParameters/OptionalParameters.rst @@ -41,7 +41,7 @@ supplying a different value. .. activecode:: opt_params_1 print int("100") - print int("100", 10)) # same thing, 10 is the default value for the base + print int("100", 10) # same thing, 10 is the default value for the base print int("100", 8) # now the base is 8, so the result is 1*64 = 64 .. note:: Tom Lehrer's New Math diff --git a/pip2/source/Tuples/UnpackingDictionaryItems.rst b/pip2/source/Tuples/UnpackingDictionaryItems.rst index 89355b139..9037c14f6 100644 --- a/pip2/source/Tuples/UnpackingDictionaryItems.rst +++ b/pip2/source/Tuples/UnpackingDictionaryItems.rst @@ -20,7 +20,6 @@ You can iterate over the key-value pairs. d = {"k1": 3, "k2": 7, "k3": "some other value"} for p in d.items(): - print p print p[0] print p[1] print '*** LOOP ***' diff --git a/pip2/source/toc.rst b/pip2/source/toc.rst index bd8592b90..0040a8b92 100644 --- a/pip2/source/toc.rst +++ b/pip2/source/toc.rst @@ -9,6 +9,7 @@ Assignments .. toctree:: :maxdepth: 1 + Assignments/ps6.rst Assignments/ps5.rst Assignments/ps4.rst Assignments/ps3.rst @@ -274,9 +275,12 @@ Installation :maxdepth: 2 Installation/FirstSteps.rst + Installation/NextSteps.rst + Installation/TextEditor.rst + Installation/pythonInstall.rst + Installation/runpythonprograms.rst - Unix ::::