###### TEST REMINDERS ###### # Must be done at 2 - 3:30 pm on Wednesday (for non-SDAC students) # Highly recommend you attend the Zoom session # you may ask clarification questions during Zoom # typos may be announced during the Zoom session # You may not use any notes, homework, classwork, internet except the ones that are allowed on the website # Download all of the files at the start of class and not before # Submit as you go # Triple check that you have submitted all the files # There will be a separate Zoom session just for the TAs where you can ask us questions outloud and not just # through chats # GOOD LUCK! 🍀🍀🍀 import url # if error is something like link is bad --> make sure there's no typo --> must be exact # if 404 error - the file is not found - that url doesn't exist --> also check spelling of the filename or the # repository repository = "http://www.cs.virginia.edu/~cs1112/datasets/csv/" filename = "elevations.csv" link = repository + filename # get complete link to a specific file # uncomment these lines for filename = "romans.csv" # text = url.get_contents( link ) # get us the content of page as a big string # print( text ) # # number_of_lines = text.count( "\n" ) # count the number of new line characters --> tells us how many lines # # there are in text # # this is the same as trying to find how many rows there are # # we can't use len( text ) because text is a string and not a dataset # # print( number_of_lines ) # print() table = url.get_dataset( link ) # get the formatted table of the content for row in table: print( row ) header = table[0] # get the 0th row of the table data = table[ 1 : ] # get row 1 and the rest of the rows from table print() ######## SPLICING ############ # LIST SPLICING first_two_data_rows = data[0 : 2] # the first two rows of the table starts at 0 and includes # rows up to but NOT including 2 # this is a new table that has two rows: the 0th row from + 1st row from # same as: # first_two_data_rows = table[1 : 3] # we used data because we don't want the header row and # the rest of rows in is in also print( first_two_data_rows ) print() # STRING SPLICING # very similar to list s = "zyxwvutsrqponmlk" # 0123456789... i = 4 j = 7 substring = s[i : j] # get a smaller string from s where it contains characters from index to characters # up to but not including at index # substring = "vut" print( substring ) ###### REMAINDER OPERATOR ##### # % x = 125 ones = x % 10 # get the last digit of x == get the remainder of 125 / 10 --> 5 last_two_digits = x % 100 # get the last two digits of x == get the remainder of 125 / 100 --> 25 # 125 goes into 100 once, with 25 as remainder # the remainder will never be bigger than the second number base_4_numbers = [0, 1, 2, 3] # when number is 4, 4 % 4 = 0 import random random.seed( x ) # only set seed once and before using any random functions digit = random.randrange(0, 4) # or digit = random.choice( base_4_numbers ) # lines 68 and 69 are essentially the same thing