''' ''' # For loops words = ['x','y','z'] # This computer is being super difficult sorry for w in words: print( w ) s = "today" for ch in s: print( ch ) for i in range(0,10): print( i ) w = 25 h = 8 for x in range (0, w): for y in range( 0, h ): spot = ( x , y ) print( spot ) # SPOT IS WHATS KNOWN AS A TUPLE # Think about math/physics # Tuple is defined as (value1, value2. ...) # Lists are SQUARE BRACKETS! [] # Notice how there are h values for each x value # (0,0) # (0,1) # (0,2) # (0,3) # (0,4) # (0,5) # (0,6) # (0,7) # x value is 0 and each y value changes for one x value # If you print spot, it will be the last spot value. print( "Last spot was", spot) number_of_rows = 3 number_of_columns = 4 # We want to build a new table! table = [] # We intend to fill this in later on! for r in range(0, number_of_rows): row = [] # We are filling up our larger list (table) with smaller lists (row) for c in range(0, number_of_columns): # Filling in the rows with c values value = r * c row.append( value ) table.append( row ) # Once we're done building the row (outside the C loop), we are going to add that filled up row # into our larger table print( table ) # NEVER DO LIST = LIST.append(x) DONT SET IT EQUAL!!! # IF YOU PRINT OUT A LIST IT WILL HAVE BRACKETS IN THE CONSOLE! [1,2,3] for row in table: # Print out each row in the larger table one by one on a separate line print( row ) # This is how to print out your rows without commas and brackets! # Notice how we need a double nested for loop. for row in table: # Go through each row in the table .. so we start off with the row [0,0,0,0] string = "" # Initialize an empty string for value in row: # Go through each value in each row (starting wtih the first 0 if we're on the first row) string = string + str(value) + " " # Assemble the string for each row by converting the number to a string and adding it with a space print( string ) # print string of each row values s = "wahoowa" # 0123456 # INDEXES # FIRST INDEX IS ALWAYS 0 # LAST INDEX IS ALWAYS AT LEN(SEQUENCE) - 1 n = len( s ) # 7 Characters for Wahoowa! for i in range(0, n): ch = s[i] # subscripting! Getting characters at index positions in the original string print( ch ) # Subscripting a single character may be on the exam! # Getting a single character at an index position in a sequence. for i in range(0, n): substring = s[ i: n ] # SLICING! Getting characters at index positions in the original string - NOT ON TEST!!!!!!!!!!!!!!! DONT ASK!!!!! print( substring ) # Prints the index all the way to the end of the string s3 = " love is all you need " s3.strip() # WE DIDNT DO ANYTHING TO s3 print( s3 ) s4 = s3.strip() # Assign it to a variable to store it print( s4 ) # .strip() only gets rid of LEADING and TRAILING whitespace meaning the outer whitespace NOT the inner # We can still get the words out of this string words1 = s4.split() # .split() SPLITS ON WHITESPACE print( words1 ) words2 = s4.split('e') # .split() can take in an argument and split on this instead! print( words2 ) # .split() SEPARATES AND GETS RID OF WHATEVER IS PASSED INTO THE FUNCTION # so when we .split('e') we are separating on the e's and getting rid of them! # IMPORTANT: How might you split multiple lines?? like on a text block?? # Use stringvariable.split('\n') # split on the newline character to get a list of lines! s5 = s3.replace(" ", "-") # Takes in two arguments: what's getting replaced and what to replace it with print( s5 ) # WE can cover exam questions at the TA review session and at OH!!! s6 = "eureka" number_e = s6.count( "e" ) # stringname.count( what we want to count ) print( number_e ) list1 = [] # Accumulator for character in s6: # We go through each character in the string list1.append( character ) # We add each character to our list print( list1 ) # Print out the list once we've built it once at the end total = 0 # Number Accumulator for i in range(0,10): # Go through each number value in our range value = i ** 2 total = total + value print( total ) import random random.seed( "CS 1112" ) # SEED WILL GENERATE THE SAME OUTPUT EACH TIME FOR RANDOM # GENERATION strings = ["r", "an","dom"] n = len( strings ) answer = "" # String accumulator answer2 = [] for i in range(0,n): # Do this n times ch = random.choice( strings ) # Get a random choice from our list of strings called strings answer = answer + ch # Concatenate the random choice to our final string answer answer2.append( ch ) print( answer ) print( answer2 ) # GENERAL PROCESS OF GETTING WEB DATA # THIS MIGHT MIGHT BE ON YOUR EXAM # WEB ACQUISITION OF TEXT FILE # Process: Start with big folder (repository), prompt for/ have file name as string, # assemble whole link through concatenation, call get_contents( link ) to get # the whole text (the text is a BIG string) # We may ask you to do string manipulation from here. # REPOSITORY = "http:/cs.virginia.edu/~cs1112/datasets/words/" # FILE_NAME = 'most-misspelled' # link = REPOSITORY + FILE_NAME # text = url.get_contents( link ) # print( text ) ''' import url link = "https:/cs.virginia.edu/~cs1112/words/most-misspelled" print() print( link ) print() text = url.get_contents( link ) print( text ) ''' # You only use .append() with lists # Create a list or use an existing list list3 = [] list4 = [1,2,3] list3.append("CS") list4.append("CS") # NOTICE how I didn't do list3 = list3.append("CS") # list3 is now ["CS"] # list4 is now [1,2,3,"CS"] # When do we use "\n"? # If we make you get a text file with a bunch of stuff on newlines, # those lines have a newline character ("\n") at the end of each line # and we can call .split("\n") on the string of the text # text.split("\n") to get a list of lines from the huge chunk of text of lines # str() # str() function converts whatever is in the parentheses to a string