Docsity

Python Programming: Loop Structures and Summing Sequences in ECS 10 - Prof. Michael Gertz, Study notes of Computer Science

Toggle sidebar
bg1
1
1
ECS 10: Basic Concepts
of Computing
Prof. Michael Gertz
April 14, 2008
2
Announcements
Assignment 2 will be posted tomorrow (Tuesday)
For programming in Python, make also use of online
sources (e.g, tutorial at
http://docs.python.org/tut/tut.html,
see also http://www.python.org/doc/).
Information about 1st midterm (Monday, April 21) will be
posted starting Wednesday this week
Sample midterm
Solutions to sample midterm
Sections of the textbook to study
1st midterm has a multiple-choice and a program part
pf3
pf4
pf5

Partial preview of the text

Download Python Programming: Loop Structures and Summing Sequences in ECS 10 - Prof. Michael Gertz and more Study notes Computer Science in PDF only on Docsity!

1

ECS 10: Basic Concepts

of Computing

Prof. Michael Gertz

[email protected]

April 14, 2008

2

Announcements

„ Assignment 2 will be posted tomorrow (Tuesday)

„ For programming in Python, make also use of online sources (e.g, tutorial at http://docs.python.org/tut/tut.html, see also http://www.python.org/doc/).

„ Information about 1st midterm (Monday, April 21) will be posted starting Wednesday this week „ Sample midterm „ Solutions to sample midterm „ Sections of the textbook to study „ 1st midterm has a multiple-choice and a program part

3

In the previous lecture...

„ Strings are indexed 0 – length of the string -1 ( len (x))

„ Individual characters and substrings can be accessed and assigned to variables: [start:end] [position] var = “COMPUTER” len(var) is 8 sub_var = var[2:5] (=“MPU”) „ str (x) Æ string #return a string containing a nicely printable representation of an object. „ For all the built-in functions, see also http://docs.python.org/lib/built-in-funcs.html

4

Loop Structures

„ So far: Algorithm ~ Recipe ~ Program

„ Linear sequence of instructions (Python statements) typically input, sequence of computations, output

„ In practice, one often needs to repeat the same

sequence of instructions several times

„ depending on the user input „ depending on some computation „ ....

„ Sum of N numbers, N!, ....

Sections 2.6 & 8.

7

Most simple example

for i in range(10):

print i

Note that here i starts with 0 !!

This is always the case if you don’t specify a start explicitly.

8

A more complex example

9

...and one more „ Compute the sum of N integers; N is input by the user

10

Comments on the previous loop

„ When programming, it is sometimes useful to add some "debug" info: „ adding print statements can help to find problems! „ Concepts to understand: „ variable initialization ( sum = 0 ) „ iteration variable (the one bound via for in …) „ " accumulator variable" ( sum is updated in each round of the loop) „ the loop is definite or bounded (before entering, we know how many times we'll go through the loop, i.e., how often we iterate ) „ block structure : indent the loop body

13

More for-loop examples

14

"Unrolling" a for loop

for X in [1,2,3]:

y = 1 * 1 print "X =", 1 , print "y =", y

y = 2 * 2 print "X =", 2 , print "y =", y

y = 3 * 3 print "X =", 3 , print "y =", y

for X in [1,2,3]:

y = X * X print "X =", X, print "y =", y

X = 1

X = 2

X = 3