Homework 1: Welcome to Python

Due: Part I due at 11pm on Wednesday, January 8, 2014 (nothing to turn in).
Due: Part II due at 11pm on Friday, January 10, 2014. Submit via this turnin page.

Learning Objectives:

You will make use of all of these skills throughout the quarter.

Python is an easy-to-use but powerful programming language that is particularly well-suited to quickly creating small programs.

IDLE is more than just a code editor; it is a simple but powerful IDE, or "integrated development environment", which lets you edit, run, navigate, and debug Python code.

Part I: Get started with Python

Follow the CSE 140 instructions for installing Python. Do this even if you already have a version of Python installed on your computer — for CSE 140, you will want a specific Python installation, namely Enthought Python Distribution.

Read (and follow along with!) a short tutorial for IDLE. (You could also try another short tutorial for IDLE, but that is optional.)

Optionally, follow the first parts of a Python tutorial.

There is nothing to turn in for Part I of the assignment.

Part II: Programming exercises

Download the file hw1.py.

For problems in this homework, you will edit your local copy of the hw1.py file. You may do your work by editing and running hw1.py (we recommend this approach), or by typing code at the Python interpreter and copying it into the appropriate part of hw1.py when you are satisfied with it. When you have completed this homework, running the hw1.py file should compute the answers to all the problems, and print the following output (with the ____ replaced by the values you calculated). Please make your output match the following exactly.

Problem 1 solution follows:
Root 1: ____
Root 2: ____

Problem 2 solution follows:
1/2: ____
1/3: ____
1/4: ____
1/5: ____
1/6: ____
1/7: ____
1/8: ____
1/9: ____
1/10: ____

Problem 3 solution follows:
Triangular number 10 via loop: ____
Triangular number 10 via formula: ____

Problem 4 solution follows:
10!: ____

Problem 5 solution follows:
10!: ____
9!: ____
8!: ____
7!: ____
6!: ____
5!: ____
4!: ____
3!: ____
2!: ____
1!: ____

Problem 6 solution follows:
e: ____

Don't forget to include documentation (source code comments, on a line starting with #) as appropriate in file hw1.py.

Problem 1: Roots

Compute and print both roots of the quadratic equation x2-5.86 x+ 8.5408.

Hint: recall that the roots of a quadratic equation ax2+bx+c are x=

−b ± √ b2 − 4ac 
          2a          

Hint: use the math.sqrt function to compute the square root. (If you are using the Python interpreter, you need to first do import math. This already appears in the hw1.py file that you will be editing, so everything is already set up for you if you are working within that file.)

Problem 2: Reciprocals

Use a for loop to print the decimal representations of 1/2, 1/3, ..., 1/10, one on each line.

Problem 3: Triangular numbers

Use a for loop to compute the 10th triangular number. The nth triangular number is defined as 1+2+3+...+n. (You can also compute the nth triangular number as n*(n+1)/2. Use this formula to double-check that your loop is correct.)

Hint: This outline is an almost-complete solution. You only have to replace each ellipsis by an expression.

n = 10
triangular = 0
for i in ...:
    triangular = ...
print "Triangular number", n, "via loop:", triangular
print "Triangular number", n, "via formula:", n*(n+1)/2

Your code should correctly calculate the 11th, 12th, or any other triangular number just by changing the first line to set n to 11, 12, or any other number. Using the range function should help you accomplish this.

Problem 4: Factorial

Use a for loop to compute 10!, the factorial of 10. Recall that the factorial of n is 1*2*3*...*n.

Hint: Your answer will be similar to your answer to "Problem 3: Triangular numbers".

As in Problem 3, your code should calculate 11!, 12!, or any other number's factorial just by changing the first line to set n to 11, 12, or any other number.

Problem 5: Multiple factorials

Write code to print the first 10 factorials, in reverse order. In other words, write code that prints 10!, then prints 9!, then prints 8!, ..., then prints 1!. Its literal output will be:

10!: 3628800
9!: 362880
8!: 40320
7!: 5040
6!: 720
5!: 120
4!: 24
3!: 6
2!: 2
1!: 1

The first line of your solution should assign a variable numlines to 10. Then, as in Problems 3 and 4, the rest of the code should print the correct number of lines and correct factorial on each line just by setting numlines to a different value.

Hint: Use two nested for loops.
The outer loop sets the value of n to the values numlines, numlines-1, numlines-2, ..., 1, in succession.
Then, the body of that loop is itself a loop — exactly your solution to "Problem 4: Factorial", without the first line n = 10 that hard-codes the value of n.

Problem 6: Sums of reciprocals of factorials

Compute the following value:
  1 + 1/1! + 1/2! + 1/3! + 1/4! + ... + 1/10!
The value should be close to e (≈ 2.71828), the base of the natural logarithms.

Hint: The easiest way to solve this is with two nested for loops. It is possible, but tricky, to compute this using only one for loop. That is not necessary for this assignment.

Hint: Copy your solution to "Problem 5: Multiple factorials", then modify it. Rather than printing the factorials, you will add their reciprocals to a running total, then print that total at the end.

Hint: don't try to work the very first "1 +" into your loop; do it outside the loops (either at the very beginning or the very end of the outer loop).

Submit your work

You are almost done!

Now you're done!