for
Loop with Stringsin
and not in
in
Operator on a FileThe final exam will be held on Tuesday, December 17th from 3:00 - 6:00 PM.
The exam will be given in this room.
If for some reason you are not able to take the Final at the time it will be offered, you MUST send an email to me before the exam so we can make alternative arrangements.
The final will consist of questions taken from the Weekly Graded Quizzes, along with questions asking you to write short segments of Python code.
There is a link to the answers to the graded quizze on the class web page.
60% of the points on this exam will consist of questions from the Graded Quizzes.
You do not need to study a Class Quiz question if the topic is not mentioned in either the Midterm or Final review.
The remaining 40% will come from 4 questions that ask you to write some code.
To study for the code questions you should be able to
A good way to study for the code questions is to review the Class Exercises, homework solutions and Class Notes.
The last class on Tuesday, December 10th, will be a review session.
You will only be responsible for the material in that review session, which you will find here, and the review for the Midterm, which you will find here.
Although the time alloted for the exam is 3 hours, I would expect that most of you would not need that much time.
The final is a closed book exam.
To prevent cheating, certain rules will be enforced during the exam.
Remember, the Midterm and Final determine 50% of your grade.
At the end of each semester we offer you the opportunity to say what you think about this course.
What have I done right?
What have I done wrong?
What can I do better?
You are not asked for you name.
So the submissions are anonymous.
I will not see your responses until after I have submitted grades for this course.
We collect this feedback through Course Evaluations.
I will use what you say to make this course better.
To complete the course evaluation, use the following link .
You have until Friday, May 24th, to submit the evaluation.
The new IT Club will hold it's first event today, at 3 PM in the Integrated Sciences Complex, Room 3300.
Jason Agress wil speak about his experiences in the IT industry.
This is a great chance to meet other IT students and expand your network.
Holiday food will be served.
You will find a poster for the event here.
Are there any questions before I begin?
>>> team = "Red Sox"
>>> team[0] 'R'
>>> letters = "abcde"
>>> len(letters)
5
>>> letters[4]
'e'
>>> letters[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> letters[-1] 'e' >>> letters[-2] 'd' >>> letters[-3] 'c' >>> letters[-4] 'b' >>> letters[-5] 'a' >>> letters[-len(letters)] 'a'
>>> numbers = [1, 2, 3, 4, 5] >>> numbers[-1] 5
>>> values = (1, 2, 3, 4, 5) >>> values[-1] 5
for
Loop with Stringsfor
loop can iterate over a string
>>> for ch in team: ... print(ch) ... R e d S o x
>>> def character_count(string, char): ... count = 0 ... for ch in string: ... if ch == char: ... count += 1 ... return count ... >>> character_count("Mississippi", "i") 4 >>> character_count("Mississippi", "s") 4 >>> character_count("Mississippi", "p") 2
for
loop to reverse a string
>>> def string_reverse(string): ... new_string = "" ... for ch in string: ... new_string = ch + new_string ... return new_string ... >>> string_reverse("Mississippi") 'ippississiM' >>> string_reverse("radar") 'radar'
>>> string_1 = "Red" >>> string_1 = string_1 + " Sox" >>> string_1 'Red Sox'
string_1 += " Sox"two things happened
>>> string_1 'Red Sox' >>> string_1 = "Go " + string_1 >>> string_1 'Go Red Sox'
>>> team = "Boston Red Sox"
>>> team[0:6] 'Boston'
IT 116 Scores Class Exercise 24 ====================================================== __ Elvis Barbosa __ Eddie Beazer __ George Boamah __ Vincent Chen ...
in
and not in
in
operatorin
operator when studying lists
>>> numbers = [1, 2, 3, 4, 5] >>> 2 in numbers True >>> "foo" in numbers False
in
operator on a string
>>> team = "Red Sox" >>> "R" in team True >>> "z" in team False
in
works slightly differently with strings>>> "Red" in team True >>> "Reds" in team False
not in
also works with strings
>>> "Red" not in team False >>> "Reds" not in team True
not in
not
is a
logical operator
it is not used that way here
not
as a logical operator with the in
operator
in
>>> not "Red" in team False >>> not "Reds" in team True
not in
is a single operator
that returns the opposite boolean value that in
does
in
Operator on a Filein
operatorfor line in file: if "__" in line: add to no script list elif "EE" in line: add to not executable list elif "SS" in line: add to syntax error list elif "XX" in line: add to error list
Method | Description |
---|---|
isalnum | Returns True if the string contains
only alphabetic letters or digits and has a length of
at least 1. Returns False otherwise. |
isalpha | Returns True if the string contains
only alphabetic letters and has a length of at least 1. Returns False otherwise. |
isdigit | Returns True if the string contains
only digits and has a length of at least 1. Returns False otherwise. |
islower | Returns True if all of the
alphabetic letters in the string are lowercase and has
a length of at least 1. Returns False otherwise. |
isspace | Returns True if the string contains
only whitespace characters and has a length of at least
1. Returns False otherwise. |
isupper | Returns True if all of the
alphabetic
letters in the string are uppercase and has a length of
at least 1. Returns False otherwise. |
True
when
all the characters are letters or digits
>>> first_name 'Glenn' >>> first_name.isalnum() True >>> digits '0123456789' >>> digits.isalnum() True >>> user 'alpha26' >>> user.isalnum() True
False
if the string contains a space
>>> team = "Red Sox" >>> team 'Red Sox' >>> team.isalnum() False
>>> jumble 'adbcd1234!@#$%' >>> jumble.isalnum() False
>>> empty '' >>> empty.isalnum() False
True
when all
the characters in the string are letters
>>> first_name 'Glenn' >>> first_name.isalpha() True >>> digits '0123456789' >>> digits.isalpha() False
>>> user 'alpha26' >>> user.isalpha() False >>> team 'Red Sox' >>> team.isalpha() False
False
when the string is empty
>>> empty '' >>> empty.isalpha() False
True
when all
the characters are digits
>>> digits '0123456789' >>> digits.isdigit() True >>> user 'alpha26' >>> user.isdigit() False
False
when the string is empty
>>> empty '' >>> empty.isdigit() False
True
...>>> noun 'watch' >>> noun.islower() True >>> first_name 'Glenn' >>> first_name.islower() False
>>> user 'alpha26' >>> user.islower() True >>> jumble 'adbcd1234!@#$%' >>> jumble.islower() True
False
if there are no letters in the string
>>> digits '0123456789' >>> digits.islower() False >>> empty '' >>> empty.islower() False
True
...>>> league 'MLB' >>> league.isupper() True >>> standard 'HTML 5' >>> standard.isupper() True >>> team 'Red Sox' >>> team.isupper() False
False
when no letters are present in the string
>>> digits '0123456789' >>> digits.isupper() False >>> empty '' >>> empty.isupper() False
True
when all
the characters are
whitespace
Space | " " |
Tab | "\t" |
Newline | "\n" |
>>> whitespace ' \t\n' >>> whitespace.isspace() True
>>> team 'Red Sox' >>> team.isspace() False
>>> empty '' >>> empty.isspace() False
Method | Description |
---|---|
lower | Returns a copy of the string with all alphabetic letters
converted to lowercase. Any character that is already lowercase, or is not an alphabetic letter, is unchanged. |
upper | Returns a copy of the string with all alphabetic letters
converted to uppercase. Any character that is already uppercase, or is not an alphabetic letter, is unchanged. |
lstrip | Returns a copy of the string with leading characters
removed. When run without an argument, it removes leading whitespace characters. When run with an argument, removes those characters. |
rstrip | Returns a copy of the string with all trailing
characters removed. When run without an argument, it removes tailing whitespace characters. When run with an argument, removes those characters. |
strip | Returns a copy of the string with leading and trailing
characters removed. When run without an argument, it removes leading and tailing whitespace characters. When run with an argument, removes those characters. |
>>> team 'Red Sox' >>> team.lower() 'red sox' >>> name 'Glenn' >>> name.lower() 'glenn' >>> league 'NFL' >>> league.lower() 'nfl'
>>> jumble 'adbcd1234!@#$%' >>> jumble.lower() 'adbcd1234!@#$%' >>> user 'alpha26' >>> user.lower() 'alpha26'
>>> name 'Glenn' >>> name.upper() 'GLENN' >>> noun 'leaf' >>> noun.upper() 'LEAF'
>>> jumble 'adbcd1234!@#$%' >>> jumble.upper() 'ADBCD1234!@#$%'
>>> str1 ' foo ' >>> str1.lstrip() 'foo ' >>> str2 '\t\tfoo\t\t' >>> str2.lstrip() 'foo\t\t' >>> str3 '\n\nfoo\n\n' >>> str3.lstrip() 'foo\n\n'
>>> str4 '---foo---' >>> str4.lstrip("-") 'foo---'
>>> str5 = "- - - foo- - - " >>> str5.lstrip("- ") 'foo- - - '
>>> str1 ' foo ' >>> str1.rstrip() ' foo' >>> str2 '\t\tfoo\t\t' >>> str2.rstrip() '\t\tfoo' >>> str3 '\n\nfoo\n\n' >>> str3.rstrip() '\n\nfoo'
>>> str4 '---foo---' >>> str4.rstrip("-") '---foo'
>>> str5 '- - - foo- - - ' >>> str5.rstrip("- ") '- - - foo'
>>> str1 ' foo ' >>> str1.strip() 'foo' >>> str2 '\t\tfoo\t\t' >>> str2.strip() 'foo' >>> str3 '\n\nfoo\n\n' >>> str3.strip() 'foo'
>>> str4 '---foo---' >>> str4.strip("-") 'foo'
>>> str5 '- - - foo- - - ' >>> str5.strip("- ") 'foo'
Method | Description |
---|---|
endswith(substring) | The method returns True if the string ends with the substring. |
startswith(substring) | The method returns True if the string starts with the substring. |
find(substring) | The method returns the lowest index in the string where substring is found. If string is not found, the method returns −1. |
replace(old_substring, new_substring) | The method returns a copy of the string with all instances of old_substring replaced by new_substring. |
True
>>> team = "Red Sox" >>> team.endswith("Red") False >>> team.endswith("Sox") True >>> team.endswith(" Sox") True
True
>>> team 'Red Sox' >>> team.startswith("Sox") False >>> team.startswith("Red") True
>>> team 'Red Sox' >>> team.find("Red") 0 >>> team.find(" ") 3 >>> team.find("Sox") 4 >>> nonsense 'foo bar bletch foo' >>> nonsense.find("foo") 0
>>> team.find("Yankees") -1
>>> nonsense 'foo bar bletch foo' >>> nonsense.replace("foo", "bam") 'bam bar bletch bam'
>>> team = "Red Sox" >>> team.replace("Socks", "Stockings") 'Red Sox'