Top Banner
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010
61

1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

1

PythonChapter 4

Branching statements and loops

© Samuel Marateck 2010

Page 2: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

2

PythonChapter 4

Branching statements and loops

© Samuel Marateck 2010

Page 3: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

3

The type of a variable is determined by whatis defined by the type of the literal assigned to it. Example: value = 12So value is an integer identifier. If you then assign ‘asd’ to value,value = ‘asd’What type a variable is value now?

Page 4: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

4

value becomes a string variable.

Page 5: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

5

We now introduce a new type of literal, the

boolean literal. It can have two values,

True and False. Note that these two literals

must be capitalized.

Page 6: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

6

In computer science, an expression consist

of a variable, literal or both. So examples of

expressions are: x, 3.3, x+2, and ‘asd’.

Page 7: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

7

A boolean expression must have a true or

false value. Can you give an example of

a boolean expression ?

Page 8: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

8

Can you give an example of a boolean

expression ?

True and False are the simplest boolean

expressions.

Page 9: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

9

Normally, boolean expressions have a

comparison operator. The following slide is a

table of some of these operators.

Page 10: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

10

Operator Explanation

== Equal to

!= Not equal to

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

Page 11: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

11

Note that the operators consisting of two

characters cannot have an embedded blank.

So > = would cause an error. It should be

written >=.

Page 12: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

12

Next we show how to use a boolean

expression in an if statement.

Page 13: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

13

if x == 3:

print(x)

print(‘nyu’)

print(‘end’)

Page 14: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

14

The boolean expression in if x == 3: is

x==3. If it’s true, then the block of

statements following the if is executed. Here

the block is:

print(x)

print(‘nyu’)

Note that the block must be indented.

Page 15: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

15

The Python environment automatically

indents statements in the block. To end the

block you must manually unindent.

Page 16: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

16

if x == 3: print(x) print(‘nyu’)print(‘end’)After the block is executed, the statements after the block, here print(‘end’) executed. If the boolean expression is false, the block is skipped and the statements after the block, here print(‘end’), are executed.

Page 17: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

17

Now we explore the if-else construct:

Page 18: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

18

if x == 3:

print(x)

print(‘nyu’)

else:

print(x**2)

print(‘end’)

Page 19: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

19

If x==3 is true the block

print(x)

print(‘nyu’)

is executed, then print(‘end’) is executed.

If x==3 is false, the block after the else:,

print(x**2) is executed. Then print(‘end’) is

executed.

Page 20: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

20

In

if True :

print(‘doggy’)

the literal True is the boolean expression

and it is true, so print(‘doggy’) is executed.

Page 21: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

21

The for loop

In for j in range(10): print(j)the range of j is 0 ≤ j ≤ 9 so print(j) is executed ten times, once for each value of j in this range. Note that j is an integer and is called the loop index.

Page 22: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

22

To add the integers from 1 to 10, set a variable sum to zero and add to it in the loopsum = 0for j in range(11): sum = sum + j print(sum)Since the print is indented, it is executed with sum = sum + jFor each value of j where now 0 ≤ j ≤ 10. The last two statements is the block comprising the scope of the loop.

Page 23: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

23

A table showing how sum is incremented.

j sum sum + j

0 0 0

1 0 1

Page 24: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

24

A table showing how sum is incremented.

j sum sum + j

0 0 0

1 0 1

2 1 3

Page 25: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

25

A table showing how sum is incremented.

j sum sum + j

0 0 0

1 0 1

2 1 3

3 3 6

Page 26: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

26

A table showing how sum is incremented.

j sum sum + j

0 0 0

1 0 1

2 1 3

3 3 6

4 6 10

Page 27: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

27

A table showing how sum is incremented.

j sum sum + j

0 0 0

1 0 1

2 1 3

3 3 6

4 6 10

5 10 15

etc

Page 28: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

28

A table showing how sum is incremented.

j sum sum + j

0 0 0

1 0 1

2 1 3

3 3 6

4 6 10

5 10 15

6 15 21

etc

Page 29: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

29

Since when j = 0, sum is not incremented,

we can rewrite the loop as:

sum = 0

for j in range(1, 11):

sum = sum + j

print(sum)

Now the range of j is 1 ≤ j ≤ 10 and the increment

by default is 1.

Page 30: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

30

If you want the increment to be 2, write:

for j in range(1, 11, 2):

Page 31: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

31

If you want j to decrease, starting at 10,

and going to 1, write:

for j in range(10, 0, -1):

The -1 indicates a negative increment of 1.

the range of j is 10>= j >= 1.

Page 32: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

32

In general for a positive for loop increment,

the second number in range must be greater

than the first for the loop to be executed at

least once. So in for j in range(2,2) the loop

will be executed zero times. The second 2

means that the upper limit of the loop index

is 1.

Page 33: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

33

In general for a negative for loop increment,

the second number in range must be less

than the first for the loop to be executed at

least once. So in for j in range(2,2, -1) the

loop will be executed zero times. The

second 2 means that the limit of the loop

index is 3.

Page 34: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

34

If you want the sum to be printed only at the

end of the loop’s execution, unindent the

print,

for j in range(1, 11):

sum = sum + j

print(sum)

Page 35: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

35

In general, in for j in range(n), the range of j

is 0 ≤ j ≤ n-1.

Page 36: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

36

If you want to build a string consisting of increasing numbers, e.g.,012345678910111213141516171819, trys = ‘’for j in range(0,20): s = s + jprint(s)Because s is a string, the ‘+’ here means concatenation not addition.

Page 37: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

37

While wont the following program compile?

How do you correct it?

s = ‘’

for j in range(0,20):

s = s + j

print(s)

Page 38: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

38

Since s is a string, j must be converted to a

string by writing str(j)

s = ‘’

for j in range(0,20):

s = s + str(j)

print(s)

Page 39: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

39

A table showing how the string s is incremented.

j s s + str(j)

0 ‘’ ‘0’

Page 40: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

40

A table showing how the string s is incremented.

j s s + str(j)

0 ‘’ ‘0’

1 ‘0’ ’01’

Page 41: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

41

A table showing how the string s is incremented.

j s s + str(j)

0 ‘’ ‘0’

1 ‘0’ ’01’

2 ’01’ ’012’

Page 42: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

42

A table showing how the string s is incremented.

j s s + str(j)

0 ‘’ ‘0’

1 ‘0’ ’01’

2 ’01’ ’012’

3 ’012’ ’0123’

Page 43: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

43

A table showing how the string s is incremented.

j s s + str(j)

0 ‘’ ‘0’

1 ‘0’ ’01’

2 ’01’ ’012’

3 ’012’ ’0123’

4 ’0123’ ’01234’

Page 44: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

44

A table showing how the string s is incremented.

j s s + str(j)

0 ‘’ ‘0’

1 ‘0’ ’01’

2 ’01’ ’012’

3 ’012’ ’0123’

4 ’0123’ ’01234’

5 ’01234’ ’012345’

Page 45: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

45

A table showing how the string s is incremented.

j s s + str(j)

0 ‘’ ‘0’

1 ‘0’ ’01’

2 ’01’ ’012’

3 ’012’ ’0123’

4 ’0123’ ’01234’

5 ’01234’ ’012345’

6 ’012345’ ’0123456’

etc

Page 46: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

46

How do you get the computer to print only

digits, e.g.,

01234567890123456789

Page 47: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

47

Let’s pause here and ask, what is 4//10?

Page 48: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

48

Let’s pause here and ask, what is 4//10?

The answer is 0. So the remainder, 4%10, is

4.

What is 14//10?

Page 49: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

49

What is 14//10?

The answer is 1. So the remainder, 14%10,

is 4.

How should we rewrite the program to get

01234567890123456789?

Page 50: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

50

s = ‘’

for j in range(0,20):

n = j%10

s = s + str(n)

print(s)

Page 51: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

51

Using characters in a for

Given a string s = ‘asd1’, in

for c in s:

print(c)

during execution, the Python virtual machine

iterates though the string s and prints each

character in the string.

Page 52: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

52

You don’t have to use c and s. So given the string t = ‘asd1’, you could writefor b in t: print(b)and get the same results.

How do you write a program that counts the characters in a string?

inu

Page 53: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

53

s = ‘asd1’,

n = 0

for c in s:

n = n + 1

print(‘number of characters is ‘, n)

Page 54: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

54

What happens if instead you write:

s = ‘asd1’,

for c in s:

n = 0

n = n + 1

print(‘number of characters is ‘, n)

Page 55: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

55

Since n= 0 is in the loop, the counter n is set

to zero for each character.

Page 56: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

56

Nested loops

How do you produce the following pattern in

which the number of columns and rows is

dictated by the loop indices.

XXXXXX

XXXXXX

XXXXXX

Page 57: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

57

How do you write a program that produces:

XXXXXX

and then skip to a new line?

Page 58: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

58

for j in range(6):

print(x, end = ‘’)

print() # skips to a new line

Page 59: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

59

How do we repeat this pattern 3 times?

Page 60: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

60

We nest

for j in range(6):

print(x, end = ‘’)

print() # skips to a new line

in another loop

Page 61: 1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.

61

for k in range(3):

for j in range(6):

print(x, end = ‘’)

print() # skips to a new line

Note that the print() is executed after the

for j loop, so it’s executed three times not six

times. The loop index of the outer loop k must be

different than the loop index of the inner loop, j.