Вы находитесь на странице: 1из 20

Do Now: 10 minutes

1. Download the file integerSum.py in Dropbox > Python Resources > Unit 2 Code. 2. Open Idle. Click File > Open and navigate to the folder integerSum.py is downloaded to and open it. 3. Run the code by doing Run > Run Module. The result of the code is 15. Show what numbers are added to get 15. 4. Modify the variable N and re-run the code in order fill-in the following table:

N Sum

10

100

1123

Homework
Write a program that calculates the factorial of a number N Submit via Dropbox by 11:59PM Sunday The first line of your code should be:
N = <some number>

Homework
Write a program that calculates the factorial of a number N Submit via Dropbox by 11:59PM Sunday The first line of your code should be:
N = <some number>

Test your code by checking the answers for various values of N on your computers calculator

For Loops -Used to do a task multiple timesparticularly useful for processing lists of data for <index> in <list>: <body>

For Loops -Used to do a task multiple timesparticularly useful for processing lists of data for <index> in <list>: <body> -<index> always refers to a particular entry in <list> -the first time through <index> refers to the first entry in the listthe second time through <index> refers to the second entry in the listand so on

For Loops print(hello) for i in [5, 6, 10, 100, 1123]: sum = (i)*(i+1)/2 print(sum) print(-----end of iteration-----) print(goodbye)

For Loops print(hello) Executed before the loop for i in [5, 6, 10, 100, 1123]: sum = (i)*(i+1)/2 print(sum) print(-----end of iteration-----) print(goodbye)
After the loop: only executed after the body of the loop has been executed ?? times.

For Loops print(hello) Executed before the loop for i in [5, 6, 10, 100, 1123]: sum = (i)*(i+1)/2 print(sum) print(-----end of iteration-----) print(goodbye)
After the loop: only executed after the body of the loop has been executed 5 times.

For Loops print(hello) for i in [5, 6, 10, 100, 1123]: sum = (i)*(i+1)/2 print(sum) print(-----end of iteration-----) print(goodbye)

i is the index [5, 6, 10, 100, 1123] is the list red is the body

For Loops print(hello) for i in [5, 6, 10, 100, 1123]: sum = (i)*(i+1)/2 print(sum) print(-----end of iteration-----) print(goodbye)
First time through the loop: i = ?? output of loop:
??

For Loops print(hello) for i in [5, 6, 10, 100, 1123]: sum = (i)*(i+1)/2 print(sum) print(-----end of iteration-----) print(goodbye)
First time through the loop: i=5 output of loop:
15 -----end of iteration-----

For Loops print(hello) for i in [5, 6, 10, 100, 1123]: sum = (i)*(i+1)/2 print(sum) print(-----end of iteration-----) print(goodbye)
Second time through the loop: i = ?? output of loop:
??

For Loops print(hello) for i in [5, 6, 10, 100, 1123]: sum = (i)*(i+1)/2 print(sum) print(-----end of iteration-----) print(goodbye)
Second time through the loop: i=6 output of loop:

21
-----end of iteration-----

For Loops print(hello) for i in [5, 6, 10, 100, 1123]: sum = (i)*(i+1)/2 print(sum) print(-----end of iteration-----) print(goodbye)
Third time through the loop: i = ?? output of loop:
??

For Loops print(hello) for i in [5, 6, 10, 100, 1123]: sum = (i)*(i+1)/2 print(sum) print(-----end of iteration-----) print(goodbye)
Third time through the loop: i = 10 output of loop:

55
-----end of iteration-----

Range()
-Really easy way to create lists

Range()
-Really easy way to create lists

range(0,10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] range(5,7) [5, 6] range(1, N+1) *1, 2, 3, , N+

Range()
for i in range(2,6): print(i)

Output of program:

Range()
for i in range(2,6): print(i)

Output of program: 2 3 4 5

Вам также может понравиться