putty
Blank Screen Problempython3
else
Clauselen
Functionfor
Loop with Sequencesfor
Loopin
Operatordel
StatementI have posted homework 2 here.
It is due this coming Sunday at 11:59 PM.
The first graded quiz will be given on Tuesday next week.
The questions from the graded quiz are taken from the ungraded quizzes of the previous week.
Unless I tell you otherwise, you should expect a graded quiz each week.
The quiz will be available on Gradescope on Tuesday.
The quiz will be available from the Gradescope web site but only for the last 15 minutes of the class.
Once you start the quiz, you will have 15 minutes to finish it.
If you do not take the quiz when it is offered you cannot take it at another time because Gradescope does not permit this.
If you fail to take the quiz but have a valid excuse, send me an email.
In order to take the quiz on Gradescope, you need to sign in to my Gradescope section.
Sometime during the next few days I will enter all your UMB email addresses into Gradescope.
You will receive an email from Gradescope.
What happens next depends on whether you have already registered with Gradescope.
If you are new to Gradescope
If you are already registered with Gradescope
You will find more information about Gradescope here.
You will find links to the recordings of class meetings here.
You will also find this link on the class web page.
You will links to the Zoom Office Hours meetings here .
You will also find this link on the class web page.
You may find it useful to look at my materials for IT 116.
I have a web site where I post most of the materials for my courses for the previous semester.
You will find these materials here http://96.126.105.215/umb_classes_html/it116_html/class_page_it116.html .
I have included this link on the class page for the course.
Go to the index for the Class Notes and search for a topic.
You will find the index here http://96.126.105.215/umb_classes_html/it116_html/class_notes_it116/class_notes_index_it116.html .
You must follow certain rules when you create your homework scripts.
You will find those rules here.
There are a number of on-line sites that where students can post homework problems and invite others to post solutions.
Using such sites on your homework assignments is cheating.
My graders and I search such sites when scoring your homework assignments.
When a student is caught using such sites, they will receive a score of 0 on the assignment.
And the incident will be reported to the Administration.
I will never write a recommendation for students caught cheating in any of my classes.
All my homework assignments are copyrighted.
Posting my homework assignments on such sites is a violation of copyright law.
Any student posting my work on such sites could face legal consequences.
putty
Blank Screen Problemputty
get a blank screen
./ex20.py /usr/bin/python3^M bad interpreter: No such file or directory
dos2unix
dos2unix FILENAME
python3
$ cat hello.py print("Hello world!")
chmod
command
$ chmod 755 hello_1.py $ ls -l hello_1.py -rwxr-xr-x 1 ghoffman faculty 50 Mar 13 21:58 hello_1.py
$ ./hello_1.py ./hello_1.py: line 3: syntax error near unexpected token `"Hello world!" ./hello_1.py: line 3: `print("Hello world!")'
$ cat hello2.py
#! /usr/bin/python3
# prints a friendly message
print("Hello world!")
python3
command
$ ./hello_2.py Hello world!
python3
2016-04-04 Red Sox @ Astros Win 7-5 2016-04-05 Red Sox @ Astros Win 2-1 2016-04-07 Red Sox vs Yankees Loss 7-9
>>> for = 5
File "<stdin>", line 1
for = 5
^
SyntaxError: invalid syntax
>>> name = "Glenn"
>>> print(nme)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'nme' is not defined
average = num_1 + num_2/2 print ("Average:", average)
$ ./file_open.py
Filename: xxxxxx
Traceback (most recent call last):
File "./file_open.py", line 6, in <module>
file = open(filename, "r")
FileNotFoundError: [Errno 2] No such file or directory: 'xxxxxx'
$ ./file_open.py
Filename: unreadable.txt
Traceback (most recent call last):
File "./file_open.py", line 6, in <module>
file = open(filename, "r")
PermissionError: [Errno 13] Permission denied: 'unreadable.txt'
try
/except
statement which has the following form
try:
STATEMENT
STATEMENT
...
except:
STATEMENT
STATEMENT
...
try
code block
causes a runtime error, the interpreter stops executing the code in the try
block
except
code block and executes those statementsopen
can cause runtime errors if you try to open a file that
does not exist
open
statements inside a try
block
$ cat open_file.py
#! /usr/bin/python3
filename = input("Filename: ")
try:
file = open(filename, "r")
for line in file:
print(line.rstrip())
except:
print("Could not open file", filename)
$ ./open_file.py
Filename: xxxx
Could not open file xxxx
for
loopelse
Clausetry
/except
statement can also have an
else
clause
try
block code
does not cause a runtime error
open
is followed by statements that will only
work if there is no runtime error
try
blockexcept
block
try:
file = open(filename, "r")
except:
print("Cannot open", filename)
else:
count = 0
total = 0
for line in file:
count += 1
date, temp = line.split()
temp = int(temp)
total += temp
average = round(total/count, 2)
print("Average:", average)
>>> list_1 = [1,2,3,4,5] >>> tuple_1 = (1,2,3,4,5) >>> string_1 = "12345" >>> list_1[0] 1 >>> tuple_1[1] 2 >>> string_1[2] '3'
>>> [1,2,3] + [4,5] [1, 2, 3, 4, 5] >>> (1,2,3) + (4,5) (1, 2, 3, 4, 5) >>> "123" + "45" '12345'
>>> [1,2,3] + [4,5] [1, 2, 3, 4, 5] >>> (1,2,3) + (4,5) (1, 2, 3, 4, 5) >>> "123" + "45" '12345' >>> [1,2,3] * 2 [1, 2, 3, 1, 2, 3] >>> (1,2,3) * 3 (1, 2, 3, 1, 2, 3, 1, 2, 3) >>> "123" * 4 '123123123123'
>>> empty_list = [] >>> empty_list [] >>> empty_tuple = () >>> empty_tuple () >>> empty_string = "" >>> empty_string "
>>> empty_list + [1] [1] >>> empty_tuple + (1,2) (1, 2) >>> empty_string + "123" '123'
len
Functionlen
that will return the
length of any sequence
>>> len([1,2,3]) 3 >>>len((1,2,3,4)) 4 >>> len("1234") 4
for
Loop with Sequencesfor
loops make it easy to work with the elements of a sequence
for LOOP_VARIABLE in SEQUENCE:
>>> list_1 = [1,2,3] >>> for element in list_1: ... print(element) ... 1 2 3 >>> tuple_1 = (1,2,3) >>> for element in tuple_1: ... print(element) ... 1 2 3 >>> string_1 = "123" >>> for element in string_1: ... print(element) ... 1 2 3
for
Loopfor
loop
for INDEX_VARIABLE in range(len(LIST)):
LIST[INDEX_VARIABLE] = NEW_VALUE
>>> numbs = [1,2,3,4,5] >>>for index in range(len(numbs)): ... numbs[index] += 1 ... >>> numbs [2, 3, 4, 5, 6]
SEQUENCE_VARIABLE[FIRST_INDEX:ONE_MORE_THAN_LAST_INDEX]
>>> list_1 = [1,2,3,4,5] >>> list_1[2:4] [3, 4] >>> tuple_1 = (1,2,3,4,5) >>> tuple_1[0,4] >>> tuple_1[0:4] (1, 2, 3, 4) >>> string_1 = "12345" >>> string_1[2:6] '345'
>>> list_1 [1, 2, 3, 4, 5] >>> list_1[:3] [1, 2, 3]
>>> tuple_1 (1, 2, 3, 4, 5) >>> tuple_1[2:] (3, 4, 5)
>>> string_1 '12345' >>> string_1[:] '12345'
>>> list_2 = list_1[:] >>> list_1 [1, 2, 3, 4, 5] >>> list_2 [1, 2, 3, 4, 5] >>> list_1[0] = 5 >>> list_1 [5, 2, 3, 4, 5] >>> list_2 [1, 2, 3, 4, 5]
in
Operatorin
is a boolean operator that can be used with sequencesin
returns True if the sequence contains the value
VALUE in SEQUENCE
>>> nums = [1,2,3,4,5] >>> 3 in nums True >>> 6 in nums False >>> numbers = (1,2,3,4,5) >>> 2 in numbers True >>> 7 in numbers False >>> digits = "0123456789" >>> "2" in digits True >>> 'a' in digits False
in
with strings you can search for more than 1 character
>>> digits = "0123456789" >>> "345" in digits True >>> "379" in digits False
Method | Description |
---|---|
append(item) | Adds item to the end of the list |
index(item) | Returns the index of the first element whose value is equal to item. A ValueError exception is raised if item is not found in the list. |
insert(index, item) | Inserts item into the list at the specified index . When an item is inserted into a list, the list is expanded in size to accommodate the new item. The item that was previously at the specified index, and all the items after it, are shifted by one position toward the end of the list. |
sort() | Sorts the items in the list so they appear in ascending order (from the lowest value to the highest value) |
remove(item) | Removes the first occurrence of item from the list. A ValueError exception is raised if item is not found in the list. |
reverse() | Reverses the order of the items in the list |
del
Statementdel
statement
del LIST_VARIABLE[INDEX]
>>> nums [1, 2, 3, 4, 5] >>> del nums[0] >>> nums [2, 3, 4, 5]
del
statement with tuple or strings because they
are immutable
max
will return the largest value in a sequence
>>> l1 = [1,2,3,4,5] >>> max(l1) 5
min
will return the smallest
>>> min(l1) 1
>>> l2 = ["abacus", "bear", "cat", "dog"] >>> max(l2) 'dog' >>> min(l2) 'abacus'
>>> l3 = [1,2,4,"five"]
>>> max(l3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() > int()
>>> nums_1 = [1,2,3,4,5] >>> nums_1[-1] 5 >>> nums_1[len(nums_1) -1] 5 >>> digits = "0123456789" >>> digits[-3] '7' >>> digits[len(digits) -3] '7'
>>> name ' Glenn ' >>> name.strip() 'Glenn' >>> team ' Red Sox' >>> team.strip() 'Red Sox'
>>> team.split() ["Red", "Sox"] >>> hamlet "To be\nor not to be" >>> hamlet.split() ["To", "be", "or", "not", "to", "be"]
>>> name "Glenn" >>> name.split() ["Glenn"]
>>> date1 "2016-05-03" >>> date1.split("-") ["2016", "05", "03"] >>> date2 "5/3/16" >>> date2.split("/")
>>> name.split("e") ["Gl", "nn"] >>> name.split("n") ["Gle", ", "]
>>> date3 "5--3--16" >>> date3.split("--") ["5", "3", "16"]