Lecture slides for week 6 - Department of Computer Science and

Download Report

Transcript Lecture slides for week 6 - Department of Computer Science and

Introduction to Programming
Department of Computer Science and Information
Systems
Steve Maybank
[email protected]
Spring 2017
Week 6: Relational Operators and Boolean
Variables
17 February 2017
Birkbeck College, U. London
1
Revision: Strings
 String literals: "Hello", "World!"
 Length: len("Hello")
# value 5
 Convert a number to a string:
str(5)
str(34.2)
# value "5"
# value "34.2"
 String concatenation: "H"+"W" # value "HW"
17 February 2017
PFE Section 2.4.4
2
Revision: String Indexing
 The characters in a string are indexed left to right, beginning
with the index 0
 Individual characters are obtained using […], e.g.
"Hello" [1] # value "e"
"Hello" [4] # value "o"
 Negative indices can be used, counting from the right, -1, -2, ...
"Hello" [-1] # value "o"
"Hello" [-3] # value "l"
 Valid indices for "Hello"
-5, -4, -3, -2, -1, 0, 1, 2, 3, 4
17 February 2017
PFE Section 2.4.4
3
Revision: Escape Sequences
 \" : include the character double quote in a string, e.g. "a\"b" is
the string with three characters a, double quote (") and b
 \n : new line, e.g. print("*\n*")
 \\: include the character backslash in a string, e.g. "a\\b" is the
string with three characters a, backslash (\) and b.
 len("a\"b") = len("*\n*") = len("a\\b") = 3
17 February 2017
PFE Section 2.4
4
Revision: Format Specifiers
 %5d place an integer right justified in a field of 5 characters
 %7.2f place a floating point number with two digits after the
decimal point right justified in a field of 7 characters. The decimal
point and the – sign, if present, each count as characters
 %9s place a string right justified in a field of 9 characters
 print("%5d" % 56)
# three spaces then 56
 print("%8.2f" % -586.189) # one space then -586.19
 print("%8s" % "Hello")
# three spaces then "Hello"
17 February 2017
PFE Section 2.5.3
5
Relational Operators
Python
>
>=
<
<=
==
!=
17 February 2017
Math
Notation
>
≥
<
≤
=
≠
Description
Greater than
Greater than or equal
Less than
Less than or equal
Equal
Not equal
PFE Section 3.2
6
Examples of Relational Operators
Expression
Value
Comment
3 <= 4
3 =< 4
3>4
4<4
True
Error
False
False
4 <= 4
3 == 5-2
3 != 5-1
3 = 6/2
1.0/3.0 ==
0.333333333
"10" > 5
True
True
True
Error
False
3 is less than 4; <= test for “less than or equal”
Use <= for “less than or equal”, not =<
> is the opposite of <=
For True, the left-hand side must be strictly smaller
than the right hand side
The two sides are equal
== tests for equality
!= tests for inequality. It is true that 3 is not 5-1
Use == to test for equality
The values are close but they are not exactly equal
17 February 2017
Error
A string cannot be compared with a number
PFE Section 3.2
7
Relational Operators and Strings






name1
name2
name3
name1
name1
name1
17 February 2017
= "John"
= "John"
= "Smith"
== name2
== name3
!= name3
PFE Section 3.2
# True
# False
# True
8
Ordering of Single Characters
 All uppercase letters come before lower case letters
 The space character comes before all printable characters
 Numbers come before letters
 Example
" " < "0" < "1" < "9" < "A" < "B" < "Z" < "a" < "b" < "z"
17 February 2017
PFE Section 3.2
9
Examples of Lexicographic Ordering
Given strings s1, s2 find the longest string s such that
s1 = s+u1
s2 = s+u2
s1
"catch"
"coal"
"tone"
"pit"
"pitch"
s2
"cart"
"coat"
"ton"
"pith"
"pitch"
17 February 2017
u1
"tch"
"l"
"e"
""
""
u2
"rt"
"t"
""
"h"
""
PFE Section 3.2
u1[0]<u2[0]?
No
Yes
~
~
~
order
s1 > s2
s1 < s2
s1 > s2
s1 < s2
s1 == s2
10
Summary of Lexicographic Ordering
 Given strings s1, s2, find the longest string s
such that
s1 = s+u1




If
If
If
If
s2 = s+u2
u1 = u2 = "", then s1 = s2
u1 = "" and u2 ≠ "", then s1 < s2
u1 ≠ "" and u2 = "", then s1 > s2
u1 ≠ "" and u2 ≠ "", then
if u1[0] < u2[0] then s1 < s2
if u1[0] > u2[0] then s1 > s2
17 February 2017
PFE Section 3.2
11
Boolean Variables
 Variables of type bool have the value True or the value
False, e.g.
failed = False
passed = True
 True and False are special values, not numbers or
strings.
 True and False are reserved words
17 February 2017
PFE Section 3.7
12
Boolean Operators
 A Boolean operator takes one or more Boolean values
as input and produces a Boolean value as output.
 Example: and
input: two Boolean values True, True
output: True
 flag = True and True
# The Boolean variable flag has the value True
17 February 2017
PFE Section 3.7
13
Truth Tables
A
True
True
False
False
B
A and B
True True
False False
True False
False False
A
True
False
17 February 2017
A
True
True
False
False
B
True
False
True
False
A or B
True
True
True
False
not A
False
True
PFE Section 3.7
14
Boolean Operator Examples
Expression
0 < 200 and 200 < 100
0 < 200 or 200 < 100
0 < 200 or 100 < 200
0<x and x<100 or x == -1
Value
Comment
False
Only the first condition is
true
The first condition is true
The or is not exclusive or
The and operator has a
higher precedence than the
or operator
0 < 200 is true, therefore its
negation is false
There is no need to compare
a Boolean variable with True
It is clearer to use not than
to compare with False
True
True
(0<x and x<100) or x== -1
not (0 < 200)
False
frozen == True
frozen
frozen == False
not frozen
17 February 2017
PFE Section 3.7
15
The Operators and, or
 Avoid confusing the operators and, or
 E.g. x in the range 0 to 100 inclusive
0 <= x and x <= 100
 E.g. x outside the range 0 to 100 inclusive
x < 0 or x > 100
17 February 2017
16
Chaining Relational Operators
 The expression
0 <= value <= 100
is equivalent in Python to
value >= 0 and value <= 100
 The expression
a<x>b
is equivalent to
a < x and x > b
17 February 2017
PFE Section 3.7
17
Short Circuit Evaluation
 Logical expressions are evaluated left to right.
Evaluation stops when the value of the expression is
known.
 Examples:
True or Anything
# True
False and Anything
# False
quantity > 0 and price/quantity < 10
# False if quantity == 0
17 February 2017
PFE Section 3.7
18
De Morgan’s Law
 Version 1
not(A and B) is the same as (not A) or (not B)
 Version 2
not (A or B) is the same as (not A) and (not B)
 To obtain version 2 from version 1, write
not (A or B) = not(not not A or not not B)
= not not (not A and not B)
= not A and not B
17 February 2017
PFE Section 3.7
19
Example of De Morgan’s Law
 if not (country == "USA" and state != "AK" and state != "HI") :
shippingCharge = 20.00
 if(country != "USA" or state == "AK" or state == "HI") :
shippingCharge = 20.00
17 February 2017
PFE Section 3.7
20
PFE Review Question R3.20
 Of the following pairs of strings, which comes first in lexicographic
order?
"Tom", "Jerry"
"Tom", "Tomato"
"church", "Churchill"
"car manufacturer", "carburettor"
"36", "A1"
"36", "a1"
17 February 2017
PFE R3.20
21
Examples
 Let x, y, z be variables with integer values. Construct a Boolean
expression that is True if and only if exactly one of x, y, z is equal
to zero.
 Construct a Boolean expression with the following truth table,
using one or more of the operators and, or, not.
A
True
True
False
False
17 February 2017
B
Expression
True False
False True
True
True
False False
PFE Section 3.7
22