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

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

  • View
    221

  • Download
    0

Embed Size (px)

Citation preview

1

PythonChapter 4

Branching statements and loops

© Samuel Marateck 2010

2

PythonChapter 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?

4

value becomes a string variable.

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.

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’.

7

A boolean expression must have a true or

false value. Can you give an example of

a boolean expression ?

8

Can you give an example of a boolean

expression ?

True and False are the simplest boolean

expressions.

9

Normally, boolean expressions have a

comparison operator. The following slide is a

table of some of these operators.

10

Operator Explanation

== Equal to

!= Not equal to

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

11

Note that the operators consisting of two

characters cannot have an embedded blank.

So > = would cause an error. It should be

written >=.

12

Next we show how to use a boolean

expression in an if statement.

13

if x == 3:

print(x)

print(‘nyu’)

print(‘end’)

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.

15

The Python environment automatically

indents statements in the block. To end the

block you must manually unindent.

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.

17

Now we explore the if-else construct:

18

if x == 3:

print(x)

print(‘nyu’)

else:

print(x**2)

print(‘end’)

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.

20

In

if True :

print(‘doggy’)

the literal True is the boolean expression

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

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.

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.

23

A table showing how sum is incremented.

j sum sum + j

0 0 0

1 0 1

24

A table showing how sum is incremented.

j sum sum + j

0 0 0

1 0 1

2 1 3

25

A table showing how sum is incremented.

j sum sum + j

0 0 0

1 0 1

2 1 3

3 3 6

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

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

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

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.

30

If you want the increment to be 2, write:

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

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.

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.

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.

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)

35

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

is 0 ≤ j ≤ n-1.

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.

37

While wont the following program compile?

How do you correct it?

s = ‘’

for j in range(0,20):

s = s + j

print(s)

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)

39

A table showing how the string s is incremented.

j s s + str(j)

0 ‘’ ‘0’

40

A table showing how the string s is incremented.

j s s + str(j)

0 ‘’ ‘0’

1 ‘0’ ’01’

41

A table showing how the string s is incremented.

j s s + str(j)

0 ‘’ ‘0’

1 ‘0’ ’01’

2 ’01’ ’012’

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’

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’

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’

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

46

How do you get the computer to print only

digits, e.g.,

01234567890123456789

47

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

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?

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?

50

s = ‘’

for j in range(0,20):

n = j%10

s = s + str(n)

print(s)

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.

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

53

s = ‘asd1’,

n = 0

for c in s:

n = n + 1

print(‘number of characters is ‘, n)

54

What happens if instead you write:

s = ‘asd1’,

for c in s:

n = 0

n = n + 1

print(‘number of characters is ‘, n)

55

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

to zero for each character.

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

57

How do you write a program that produces:

XXXXXX

and then skip to a new line?

58

for j in range(6):

print(x, end = ‘’)

print() # skips to a new line

59

How do we repeat this pattern 3 times?

60

We nest

for j in range(6):

print(x, end = ‘’)

print() # skips to a new line

in another loop

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.