0% found this document useful (0 votes)
4 views

Week3 Lecture1

Uploaded by

frankhao4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week3 Lecture1

Uploaded by

frankhao4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

String Comparisons and More “if” Statements.

Week 3 | Lecture 1 (3.1)


RECAP: Relational Operators
§ Relational (or comparison) operators take two values (examples: int,
float, str) and produce a bool value (True or False)

Description Operator Example Result Boolean


Less than < 3<4 True Expressions

Greater than > 3>4 False Boolean


Values
Equal to == 3==4 False

Less than or equal to <= 3<=4 True

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

inequality != 'cat' != 'Cat' True

less than < 'A' < 'a' True

greater than > 'a' > 'A' True

less than or equal <= 'a' <= 'a' True

greater than or equal >= 'a' >= 'A' True


Strings as Integers (ASCII Encoding)
§ Each character in a string is actually represented by integers following the
ASCII encoding
§ American Standard Code for Information Interchange
§ All uppercase letters come before all lowercase letters
§ Uppercase “Z” is less than lowercase “a”
Strings as Integers (ASCII Encoding)

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

0-9 A-Z a-z


48-57 65-90 97-122
ASCII Encoding
§ To obtain the ASCII (integer) representation, we use the built-in
function ord
>>> ord(‘a’)
97
§ To convert from ASCII integer representation back to a string, we use
the built-in function chr
§ Think ”character” (i.e. what is this integer’s character)

>>> 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)

>>> ‘c’ in ‘aeiou’


False
>>> ‘cad’ in ‘abracadabra’
True
>>> ‘zoo’ in ‘ooze’
False
Let’s Code!
§ Let’s take a look at how this works in
Python!
§ String comparisons
Open your
notebook

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)

You might also like