for Loop with StringsThe final exam will be given on Thursday, May 21st. It will be on Blackboard. I will use the same format as the Midterm. The exam will be a two parts exam (Questions and writing short segments of Python code).
The last day of our class will be on Tuesday, May 12th, We will have an optional Zoom meeting for any question you may have. I will send you the Zoom information.
Class 28, on Thursday, May 7th, will be a review session.
You will only be responsible for the material in Class Notes 28 and the review for the Mid-term, which you will find here.
The final will be an online exam.
Program Status Code,Term Code,Academic Career Code,Person ID,Person Name,First Name,Last Name,Program Code,Plan Code,Student Email Address,Personal Email Address AC,2610,UGRD,01459837,"Smith,John",John,Smith,CSM-U,IT-BS,John.Smith001@umb.edu,jsmith@gmail.com AC,2610,UGRD,01636476,"Jacobs,Jane",Jane,Jacobs,CSM-U,IT-BS,Jane.Jacobs002@umb.edu,jjacobs@gmail.com AC,2610,UGRD,01523746,"Miller,Alan",Alan,Miller,CSM-U,IT-BS,Alan.Miller001@umb.edu,amiller@gmail.com ...
#! /usr/bin/python3
# takes a CSV file from the Registrar and turns it into
# tuples of the form
# (STUDENT_ID, FIRST_NAME, LAST_NAME, UMB_EMAIL)
# takes a stripped string, quotes it and adds a comma
def quote_comma(string):
return '"' + string.strip() + '", '
in_filename = input("Input filename: ")
in_file = open(in_filename, 'r')
tuple = ''
for line in in_file:
line = line.strip()
values = line.split(',')
if 'UGRD' in values[2]: # skip first line with labels
tuple += '('
id = values[3]
first_name = values[6]
last_name = values[7]
umb_email = values[10]
tuple += quote_comma(id) + quote_comma(first_name) + quote_comma(last_name) + umb_email
tuple += '),\n'
print(tuple)
("01459837", "John", "Smith", John.Smith001@umb.edu),
("01636476", "Jane", "Jacobs", Jane.Jacobs002@umb.edu),
("01523746", "Alan", "Miller", Alan.Miller001@umb.edu),
...
>>> list_1 = [1 , 2.5, True, "foo"] >>> for element in list_1: ... print(element, type(element)))) ... 1 <class 'int'> 2.5 <class 'float'> True <class 'bool'> foo <class 'str'>
>>> list_2 = [ 1, 2, 3, 4, [5, 6, 7,]] >>> for element in list_2: ... print(element, type(element)) ... 1 <class 'int'> 2 <class 'int'> 3 <class 'int'> 4 <class 'int'> [5, 6, 7] <class 'list'>
>>> two_d_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> two_d_list [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> for element in two_d_list: ... print(element) ... [1, 2, 3] [4, 5, 6] [7, 8, 9]
>>> two_d_list[0] [1, 2, 3] >>> two_d_list[1] [4, 5, 6] >>> two_d_list[2] [7, 8, 9]
>>> two_d_list[0][0] 1 >>> two_d_list[0][1] 2 >>> two_d_list[0][2] 3
>>> tuple_1 = (1, 2, 3, 4, 5) >>> tuple_1 = (1, 2, 3, 4, 5) (1, 2, 3, 4, 5)
>>> tuple_2 = (1, 2.5, False, "Sam") >>> tuple_2 (1, 2.5, False, 'Sam')
>>> tuple_1[0] 1
for loop to print all the elements
>>> for number in tuple_1: ... print(number) ... 1 2 3 4 5
tuple Conversion Functiontuple
>>> name = 'Glenn'
>>> tuple(name)
('G', 'l', 'e', 'n', 'n')
>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>> tuple(digits) (1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
>>> team = "Red Sox"
>>> team[0] 'R'
>>> num = 4 >>> team[num] 'S'
>>> team[num +1] 'o'
>>> team[len(team) - 1] 'x'
>>> team[len(team)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> 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 'abcde' >>> letters[-1] 'e'
>>> letters[-1] 'e' >>> letters[-2] 'd' >>> letters[-3] 'c' >>> letters[-4] 'b' >>> letters[-5] 'a' >>> letters[-len(letters)] 'a'
letters[-1]
letters[len(team) - 1]
>>> numbers = [1, 2, 3, 4, 5] >>> numbers[-1] 5
>>> values = (1, 2, 3, 4, 5) >>> values[-1] 5
for Loop with Stringsfor loop
>>> 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 are happening
>>> string_1 'Red Sox' >>> string_1 = "Go " + string_1 >>> string_1 'Go Red Sox'
>>> team = "Boston Red Sox"
>>> team[0:6] 'Boston'
>>> team.index(' ')
6
team[0:team.index(' ')]
'Boston'
>>> team[:team.index(' ')]
'Boston'
>>> def first_name(full_name):
... return full_name[:full_name.index(' ')]
...
>>> first_name('Glenn Hoffman')
'Glenn'
>>> def last_name(full_name):
... return full_name[full_name.index(' ') + 1:]
...
>>> last_name('Glenn Hoffman')
'Hoffman'
digits = ["one", "two", "three", ...
>>> digits = ["zero", "one", "two", "three", "four"] >>> digits += ["five", "six", "seven", "eight", "nine"]
for loop
>>> for i in range(len(digits)): ... print(str(i) + ":\t" + digits[i]) ... 0: zero 1: one 2: two 3: three 4: four 5: five 6: six 7: seven 8: eight 9: nine
>>> def digits_to_words(number): ... new_string = "" ... number_string = str(number) ... for digit in number_string: ... digit = int(digit) ... new_string += digits[digit] + " " ... return new_string ... >>> digits_to_words(512) 'five one two ' >>> digits_to_words(10428) 'one zero four two eight '
for loopstr conversion function on the number
for loop with give us each digit in turnint
if two words do not have the same length:
return False
for each character in word 1:
if that character is not in word 2:
return False
else
remove the character from word 2
return True
list conversion function
def is_anagram(word_1, word_2):
if len(word_1) != len(word_2):
return False
word_2 = list(word_2) # turn into list so we can use pop
for ch1 in word_1:
if ch1 not in word_2:
return False
ch2_index = word_2.index(ch1)
word_2.pop(ch2_index)
return True
set a string to "0"
for seven times:
add a randomly selected digit to the end of the string
print ('0' + str(random.randrange(1000000,9999999)))
range$ ./phony_id_create.py 05494043 $ ./phony_id_create.py 04385371
PWD_LENGTH = 20
CHARACTERS = (
('!','@', '#', '$', '%', '^', '&', '*'),
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'),
('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'),
('1', '2', '3', '4', '5', '6', '7', '8', '9', '0')
)
def random_char():
tuple_index = random.randrange(4)
tuple = CHARACTERS[tuple_index]
return tuple[random.randrange(len(tuple))]
passwd = ''
for i in range(PWD_LENGTH):
passwd += random_char()
print(passwd)
$ ./password_create.py d*Y&tn%Zk$75G29k*J!N