1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
-
Upload
haden-london -
Category
Documents
-
view
220 -
download
0
Transcript of 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
![Page 1: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/1.jpg)
1
Chapter 4The while loop and boolean
operatorsSamuel Marateck ©2010
![Page 2: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/2.jpg)
2
Boolean operators are used in boolean expressions and operate on other boolean expressions. They are the and, or and not.For instance:
X>3 and y<=4X>3 or y<=4not(X>3 and y<=4)
![Page 3: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/3.jpg)
3
If an and is used, both expressions that it
connects must be true in order for the entire
expression to be true.
![Page 4: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/4.jpg)
4
If an or is used, if either expression that it
connects is true, then the entire
expression is true. The following tables
show how the and and or work.
![Page 5: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/5.jpg)
5
and True False
True True False
False False False
![Page 6: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/6.jpg)
6
or True False
True True True
False True False
![Page 7: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/7.jpg)
7
So for instance in:
x>3 and y<=4
If x>3 is True and y<=4 is False, the entire
expression is False.
![Page 8: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/8.jpg)
8
However, in:
x>3 or y<=4
If x>3 is True and y<=4 is False, the entire
expression is True.
![Page 9: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/9.jpg)
9
The not changes the boolean expression it
precedes so that not True is False. The not
must precede a boolean expression and
operates on the expression it immediately
precedes.
![Page 10: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/10.jpg)
10
In
not x > 3
what is the expression the not immediately
precedes?
![Page 11: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/11.jpg)
11
In
not x > 3
the not immediately precedes the variable x
which is not a boolean expression. What
happens and how do you correct this?
![Page 12: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/12.jpg)
12
It causes a compilation error. You write it as:
not (x > 3)
If the value of x is 3, what is the value of the
entire expression?
![Page 13: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/13.jpg)
13
not (x > 3)If the value of x is 3, what is the value of the entire expression?
Since x > 3 is false, not (x > 3) is true.
![Page 14: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/14.jpg)
14
The while loop
The while loop differs from the for loop in
that the variables in the while statement
must be defined before the loop.
![Page 15: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/15.jpg)
15
So in:
while x < 3:
the value of x must be defined before the
loop –this is called priming the while --and
execution of the loop will continue
while the value of x < 3 is true. Here is an
example:
![Page 16: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/16.jpg)
16
x = 0while x < 3: print(x)Since the value of x < 3 is true initially, the loop begins execution. The indented statements that follow the while are said to be in the scope of the while.How many times will the loop be executed?
![Page 17: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/17.jpg)
17
x = 0
while x < 3:
print(x)
How many times will the loop be executed?
An infinite number of times.
![Page 18: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/18.jpg)
18
So unless a statement in the scope of the while statement changes the boolean expression in the while, execution of the while continues.x = 0while x < 3: print(x).
![Page 19: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/19.jpg)
19
So we can write, for instance,
x = 0
while x < 3:
print(x)
x = x + 1
.
![Page 20: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/20.jpg)
20
Now the scope of the while,x = 0while x < 3: print(x) x = x + 1is . print(x) x = x + 1
![Page 21: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/21.jpg)
21
Since x changes, the loop will only be
executed a finite number of times.
x = 0
while x < 3:
print(x)
x = x + 1
How many times will it be executed?
![Page 22: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/22.jpg)
22
The loop,
x = 0
while x < 3:
print(x)
x = x + 1
will be executed three times. What is its
output?
![Page 23: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/23.jpg)
23
The output of
x = 0
while x < 3:
print(x)
x = x + 1
Is 0, 1 and 2.
![Page 24: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/24.jpg)
24
One of the purposes of the while is to insure
integrity of the input. In the following, the
required input is between 0 and 9
Inclusively.
So the while continues until the input
is between 0 and 9.
![Page 25: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/25.jpg)
25
invalid = True #primes the while while invalid : #continues if invalid is True n = input('type your number please \n') if 0 <= int(n) <= 9: invalid = False else: #continues the while print(n + ' is invalid') print('done')
![Page 26: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/26.jpg)
26
In while invalid, the boolean expression is
simply the expression invalid which is
originally True. When the input is in the
required range, invalid changes to False and
execution quits the loop.
![Page 27: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/27.jpg)
27
The ASCII Table
All characters used by the computer are given a code. For instance, the digits ‘0’ through ‘9’ have the codes 48 through 57 in the ascii table. The letters ‘A’ through ‘Z’ have the codes65 through 92. The function ord() gives the ascii code for a given character and chr() gives the character corresponding to the ascii code. Soord(‘A’) returns 65 and chr(65) returns ‘A’.
![Page 28: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/28.jpg)
28
What’s the meaning of ‘0’<= c<=‘9’ where c
is a character?
How does the computer evaluate it?
![Page 29: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/29.jpg)
29
What’s the meaning of ‘0’<= c<=‘9’ where c is a character?How does the computer evaluate it?
It’s the equivalent of 48<=ord(c)<=57. So thecomputer tests if the ascii value of the character c is between the two limits 48 and 57.
![Page 30: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/30.jpg)
30
The following counts the number of characters after the period.
#counts # of characters after the period #Tests for no period also
s = input('Type your sentence please \n') n = -1 #compensates for counting the periodflag = False for c in s: if c == '.' : flag = True
![Page 31: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/31.jpg)
31
A flag is initialized and set to False
#counts # of characters after the period #Tests for no period also
s = input('Type your sentence please \n')
n = -1 #compensates for counting the period
flag = False
![Page 32: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/32.jpg)
32
Once the flag is True, the counting begins in n=n+1.
s = input('Type your sentence please \n') n = -1 #compensates for counting the periodflag = False for c in s: if c == '.' : flag = True if flag : n = n + 1 #counts the period also
![Page 33: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/33.jpg)
33
If no period is encountered, the flag remains
False. So an error message is printed after
the loop is executed:
if flag == False: print('No period found ')
else:
print('# of characters after the period is ', n)
![Page 34: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/34.jpg)
34
Else the answer will be printed.
The entire program is now:
![Page 35: 1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.](https://reader033.fdocuments.us/reader033/viewer/2022051614/551b03c55503462e578b5370/html5/thumbnails/35.jpg)
35
#counts # of characters after the period #Tests for no period also s = input('Type your sentence please \n') n = -1 #compensates for counting the periodflag = False for c in s: if c == '.' : flag = True if flag : n = n + 1 #counts the period alsoif flag == False: print('No period found ') else: print('# of characters after the period is ', n)