''' Questions arising during class 16 review ''' # # # Test next classssss :DDDDDDDDDDDDDD # # ''' FOR LOOPS ''' # # STRUCTURE OF A FOR LOOP # # for lv in sequence: # # loop variable loops through each smaller # # part of the larger sequence # # and assumes a new value one by one # # each iteration (repetition of the loop) # # Sequences: # # Strings -> characters one by one # # Lists -> elements one by one # # Ranges -> numbers one by one # # range(i,j)-> i to j - 1 # s = 'giraffe' words = ['apple','banana','casaba','bosc','endive'] # # for c in s: # print('c=', c) # # for w in words: # print('w=',w) # # range_start = 0 # range_end = 5 # for i in range( range_start, range_end ): # 0-4 # print('i=',i) # # What if we had a range(3,5)? It will run twice. # # For values 3 and 4 # # # for i in range(3,5): # # print(i) # # for w in words: # print('w=',w) # for c in w: # print(' c=', c) # For each outer loop value, we do two things: # we print out w = the word # and we print out all of its characters # the inner loop has to finish before we # move onto the next value of the outer loop # We started out with 'apple' and we have to finish the # inner loop (printing out each character of apple) # before we move onto the next word 'banana' for i in range(0, 6): # 0-5 print('i=',i, end=' ' ) for j in range(0,i+1): # print('i=',i, 'j=',j) print(j, end=' ') # print() # Nested looping # digit_box.py (datasets/tables/nested for loops): n = 5 for r in range(1,n+1): for c in range(1, n+1): product = r*c % 10 print(product, end=' ') print() # end= '''Indexing (picking things out of strings and lists)''' s = 'data acquisition' # 0123456789012345 # the repeated 1-5 is 11,12,13,14,15 ns = len(s) # 16 print('ns=', ns ) print('s[0]=', s[0]) # first character (0) print('s[ns-1]=', s[ns-1]) # last character (15) '''Subscripting: Going into a sequence and getting the # character/element at that number position for a string/list ''' # s[ index ] # get character in a string - index is a number # words[ index ] # get element in list - index is a number print( words ) nw = len( words ) # 5 print('nw=', nw ) print('words[0]=', words[0]) # first character (0) print('words[nw-1]=', words[nw-1]) # last character (4) print( words[1] ) # 'banana' print( 'words[1][1]=', words[1][1] ) # character at index 1 in word at index 1 in words # words = ['apple','banana',...] # 012345 # 0 1 ''' SLICING ''' # Getting multiple characters in a string using number positions # Getting multiple elements in a list using number positions s = 'sunny with a chance of bananas' # 012345678901234567890123456789 # repeated ones are 11,12,... ns = len(s) # 30 print( ns ) middle = ns//2 # INDEXES CANT BE DECIMALS (15) middle_characters = s[ middle - 4 : middle + 4 ] # 15-4: 15+4 = 11:19 -> characters at indexes 11 to 18 print('middle characters =', middle_characters ) # 'a chance' i = s.find('b') # GET ME INDEX WHERE THIS SUBSTRING IS print( i ) # .find() returns - 1 if not in your string j = words.index('endive') print( words ) print(j) # k = words.index('end') # That's not in our list! # print(k) # .index() blows up if it's not in your list # DONT ask about get_dataset or everyone in this class # will hate you for the rest of the semester # get_contents( link ) -> returns text on webpage as # a huge string # url.py (data acquisition) # url.get_contents( link ) -> returns a big string # do manipulations of the string from there # Accumulation # I'll send out a review for this.. # Seeds (random module) # reply = input('enter seed') # random.seed( reply ) helps you set the seed # for random output # if you put in a certain seed and have the same # seed that we put in you will get the same random # output