''' Purpose: capture review coding ''' import url '''TOPICS''' # random.seed() function # The random library allows us to use functions from the random library to # generate random values / choose random values from sequences import random random.seed() # No argument -> Reset Python number generator to current time in producing the next value # random.seed( s ) takes in the argument seed s which will influence the output of our program. # random.seed( 1112 ) # reset the random number generator to use 1112 as the influencer to produce random output random.seed() # resets seed each time to the current time # IMPORTANT: As long as this seed is set, the random output will be the SAME each time. x = [ 1,2,'abc',3.14,[5,6] ] # There are 5 things in this list (2 numbers, 1 string, 1 float, 1 list) # random.choice( sequence ) chooses a random value from the sequence! r1 = random.choice( x ) # choose a random value from the list x! print( r1 ) # The random output is always going to be the same for a certain seed (1112) # IMPORTANT: Since the seed was 1112, we produced the same random output ('abc') each time # for that given seed. # random.seed( s ) influences our output. We ask you to set the seed to what we # set the seed so you get the same output as us (look at chatter.py and how your output # should've matched ours for a given seed!) No coding questions involving random - possible # short answer question involving random! # SEED -> influences output -> same random output so long as that seed is set! ''' URL.PY ''' # The url module provides functions for web acquisition. # So url.get_contents( link ) gets the contents of the webpage as a large string # url.get_dataset( link ) gets the csv from the webpage and turns it into a dataset (list of lists) ''' TEST PREP ''' # Start with homework (make sure you can code it up on your own!) # BUTTTTTTTTTTTTTTTTTTTTT also look at previous tests under Testing!!! (14/10 would recommend) # Practicing the coding questions on previous exams as well as the short answer # will help you get faster at coding problems and will give you so much exposure to the # structure of the exam / common questions / anything we could throw at you basically so # you'd be super prepared. # From what I've seen.... the more exams you practice and practice you get with coding, # the better you'll perform :) ''' INDEXING ''' # Indexing is a way to look into the sequence and get the part of the sequence at the numeric index. # strings -> get characters at indices # lists -> get elements at indices # Strings s = 'a string' # 01234567 # These are indices (multiple index values!) # The length os s is 8 but our indices go from 0 to 7! # The first index is 0 # The last index is at length(s) - 1 (in this case 8-1 = 7) ch = input("Enter character: ") i1 = s.find( ch ) # Returns the number index where ch occurs in s / returns -1 if ch not found # i2 = s.index( ch ) # Returns the number index where ch occurs in s / throws an error if ch not found print( i1 ) # print( i2 ) # Getting a character in the string using an index (subscripting) # s = 'computer' # 01234567 # s[0] -> 'c' # s[4] -> 'u' # Lists x = [ 1,2,'abc',3.14,[5,6] ] # 0 1 2 3 4 # Indexes reply = input( 'Enter number: ' ) j = int( reply ) i2 = x.index( j ) # Returns the number index where j occurs / blows up (throws an error) if j is not found print(i2) # Getting an element in the list using an index (Subscripting a List) # x[0] -> 1 # x[4] -> [5,6] dataset = [[1,7,3],[14,5,6],[7,8,9]] # get the dataset list_max_values = [] # set up dataset processing (if needed) # We might want to build up a list of values by looping through the rows in our dataset for row in dataset: # consider rows one by one # process current row of the dataset max_value_of_row = max( row ) # process the row list_max_values.append( max_value_of_row ) # finish off dataset (if needed) print( list_max_values ) # [7,14,9] (list of the max values in each smaller list in our dataset) ''' CALLING INT ''' s = '1 2 3' # t = s.split() # ['1','2','3'] # You cannot call int(t) because then you would be calling int(['1','2','3']) # and the int() function cannot convert an entire list to a list of integers # x1 = int( s ) # Cannot call int('1 2 3') # print( x1 ) # int('1') or int(1.2) # int(x) can only convert ONE thing. # GIVE IT A SINGLE STRING OR A SINGLE FLOAT TO CONVERT TO AN INT # Cannot do int('1 2 3') # Cannot do int(['1','2','3']) t = ['1','2','3'] numbers = [] for ns in t: int_ns = int( ns ) numbers.append( int_ns ) # numbers [1,2,3] print( numbers ) ''' REVEAL.PY IDEA FOR LAST 3 LINES (SUBSCRIPTING FROM A LIST OF WORDS) ''' list_strings = ['the','fox','jumped','over','the','lazy','run'] # 0 1 2 3 4 5 6 indices = [1,5,3] for i in indices: # Go through the list of numbers (the indexes we'll use to get words from our list) word = list_strings[ i ] # words[i] in reveal.py (get the word at each index i) print( i, word ) # print(word) in reveal.py (print the word at index i)