Week3 Lecture1
Week3 Lecture1
Greater than or equal to >= 3>=4 False Python uses == for equality,
because = is used for assignment
Not equal to != 3!=4 True
String Comparisons
§ Boolean comparisons can also be applied to strings, whether
single characters or sets of characters
§ Compare two strings by their dictionary order, comparing
letter by letter
Description Operator Example Result of example
equality == 'cat' == 'cat' True
0-9
48-57
A-Z a-z
65-90 97-122
Strings as Integers (ASCII Encoding)
§ When you compare two strings, what you
are really doing is comparing their numerical
representations
§ For example in ASCII the characters ‘a’ and
‘w’ are encoded as 97 and 119, respectively
§ The comparison ‘a’ > ‘w’ would translate to
97 > 119, evaluating to False
>>> chr(97)
‘a’
Strings and Logic Operators
§ We can use string comparisons in Boolean/logical expressions:
>>> 50 > 5 or ‘t’ > ‘a’ and ‘c’ != ‘d’
True
>>> ‘ABC’ > ‘abc’ #remember ord(‘A’) < ord(‘a’)
False
>>> ‘abcdefghijklmnopqrstuvwxyz’ > ‘z’ #compares one at a time
False
>>> ‘ab’ > ‘a’ #if first part is equal, longer is greater
True
Testing for Substrings
§ The in operator provides another way to check whether a string
appears inside another string
§ Results in a Boolean (True or False)
Click Link:
1. String
Comparisons
RECAP: Making Choices
statements
statements
Sequential structure
Conditional structure
if True
statements condition
statements statements
False
statements
RECAP: Adding the else statement
§ A more general form of the if conditional statement is:
if expression:
body1
else:
body2
§ ONLY 1 of body1 or body2 will be executed.
§ if statement is True, executes body1
§ if statement is False, executes body2
Adding the elif (else if) statement
§ The most general form of the if conditional statement is:
if condition1:
body1 § Note the colons (:) and the indents!
elif condition2: § ONLY 1 body will be executed.
body2 §
§
if statement is True, execute body1, exits if structure
if statement is False, continue to elif statement
elif conditionN: § elif statement is True, execute elif body, exits if structure
bodyN §
§
elif statement is False, continue to next elif statement
All if’s and elif’s are False, execute else statement
else:
other_body
Multiple if vs if-elif
True
True
if condition
if condition
statements
Conditional structure
statements False
False
True
True
elif condition
if condition
statements
statements
False
False
Making Choices statements
True
if condition
Conditional structure
False
statements
elif True
condition
statements
False
else statements
Let’s Code!
§ Let’s take a look at how this works in
Python!
§ if-elif-else statements
Open your
notebook
Click Link:
2. More on if
Statements
Nested if Statements
§ It is possible to place an if statement
within the body of another if statement
if precipitation:
if temperature > 0:
print(“Bring your umbrella!”)
else:
print(“Wear your boots and winter coat!”)
§ Equivalent code:
if precipitation and temperature > 0:
print(“Bring your umbrella!”)
elif precipitation:
print(“Wear your boots and winter coat!”)
Choose your own adventure!
§ Let’s take a look at how this works in
Python!
§ Nested if statements
Open your
notebook
Click Link:
2. Nested ifs
String Comparisons and More “if” Statements.
Week 3 | Lecture 1 (3.1)