''' dataset is organized list of lists get text or get line closing_delimeter[ index_of_what_we want : where_to_start_searching_after_the_first_delimeter] import url #got to be in same folder as test programs use get_text( link ) to access web file ''' #import url reply = input( 'Enter link: ' ) link = reply.strip() #text = url.get_text( link ) #text now contains the appropriately formatted content of link ################## reply = input( 'Enter integers: ' ) #nbrs = int( reply ) #cannot work b/c int() can only take in one number argument and reply has multiple #'s strings = reply.split() #split the reply on the empty spaces between the input numbers --> this is a list!! #have to convert each of these string numbers one at a time #if get sum, initialize accumulator total_absolute_values = 0 for s in strings: #convert each number in strings list into an integer number = int( s ) #get absolute value of that number abs_number = abs( number ) #update total_absolute_values #accumulator (updated) = accumulator (old) + value_calculated total_absolute_values = total_absolute_values + abs_number #print out final result of loop print( total_absolute_values ) ################### #Why are strings immutable? # cannot change content of string # functions return a new string that has been modified from original #list is mutable = object can change #list[0] = 5 #has just changed the first element of list to 5 ################### #length of strings counts literal letters, spaces within single quotes, punctuations #strip() = gets rid of whitespaces in beginning and end #replace() = gets rid of all occurences of spaces in the string ################### x = [ 3, 1, '4', True ] x.append( '20' ) #adds '20' to end of list x print( x ) x = x.append( 11 ) print( x ) #when appending to a list, do not assign list = list.append(...)! ################### #when sorting strings, if string contains letters, sort by alphabetical order #when strings contain #'s, lexicographical order #look at characters one by one #so, 'A141' < 'A3' because 1 < 3 #so, max( v ) = 'A59' ################### #Python stores decimal in limited bits (64) --> most digits ~~15-16 digits ################### print() #print empty line in console reply = input( 'Enter four strings: ' ) strings = reply.split() #list of inputs sorted_strings = sorted( strings ) #sorted list of strings #no accumulator because you don't have to build anything here for s in sorted_strings: print( s ) #just printing vertically each element in the sorted_strings list #make sure your output matches up exactly with sample run!!! #################### #to "remove" a character from a string # str.replace( old_value, '' ) #removes value you want to replace and # replace with empty string aka nothing aka '' aka no space Questions concerning url.py: Exp: workingwithURL.py you should only need to use url.get_text() this returns a string Max and min from input list of numbers: exp: lines 3 - 19 reviewTest2018.py We have a list and we can look at every element in the list! Len of strings in a list: same logic as max and min but instead of casting to an int take the len within the for loop see 23 fall2017 (below) for exp Random A seed is just a value (such as a string or an integer) that will just determine the random output, so that so long as the seed is set to a certain seed, it will return the same output each time when using random. Take a look at the methods from the random module and know when to use when producing random values. You can look at whether or not you're working with numbers or lists and go from there when it comes to working with sequences as well. exp: lines 20 - 32 reviewTest2018.py indexing purpose: where an element is located in a sequence brackets acces the element at that index Lists: ['h', 1, True, 3] 'h' is at index 0 1 is at index 1 True is at index 2 3 is at index 3 Strings: 'Hello" H is at index 0 e is at index 1 l is at index 2 l is at index 3 o is at index 4 if you know the index of something you can acces it For loops aaaaaaahhh: For for loops, you want to go through different sequences. Some sequences include: - lists - strings - ranges How do you set up a for loop? Some examples are: for some_variable in sequence: for number in list_of_numbers: for every_element in list_of_elements: for every_character in string: for number in range(0,n): The iterator, the "variable" will go through your sequence and work with that specific "state" or value of that element/character/number at that given time while it's going through the for loop. You can name the iterator anything. for unicorn in range(0, 10): (but name it something useful!) for n in range(0,10): print(n) The compiler runs through all the code line by line before it hits the for loop and starts off with the first value of n in the for loop. It carries out everything in the for loop and continues onto the next value of n once it reaches the end of the indented statements in the for loop. n = 0 n = 1 n = ... all the way to 9 (10-1) for ch in "Hello": print(ch) This for loop prints each character in the string "Hello". Range is a short way of writing a list of numbers. The range tells you the first value (where you begin) and the last value (where you end) in a for loop. Ranges go from 0 to n-1 for a range (0,n) If a range is (0,n) it is going in mathematical notation [0,n) so includes the first value and everything up to and not including the last value (thus n-1). In a string, each character has an index. If we have string "hello" then we have indices, 0,1,2,3,4 corresponding to each character in the string. "Hello" you can think of a list of characters! (when running through a for loop as you loop through a string) Length and indices are a bit different. The length of "hello" is 5 but it goes from indices 0-4 because it starts at index 0. for x in ["Hi", "Ho"]: print(x) It will look at that first element "Hi" and then print that element and then look at second element "Ho" and print that element. The iterator is set to the first element and then the second and so on until it reaches the end. Nested Looping: !!!!!!! for x in ["H", 1, "oh", 3]: for y in [1, 2, 3]: print(x, y) when looking through the line of code, it will first print "H 1" then it will print out "H 3" and "H 4" and then since we are done with the loop with y it will go back up to the first for loop and x = 1 so y is again set equal to the first value so the next things printed to the console will be "1 1" , "1 3", and "1 4" and so on until going back up. It will go into the first for loop and then into the second for loop, run through that loop entirely, and then go back up into the first for loop and go on line by line until we are finished with the first for loop. Nested loops will be useful if you're looking at rows and columns or if you want to have one thing remain the same while doing more operations in another loop as the iterators in both loops changes as you run through code strings count and find .count(x) counts the number of occurences of whatever it passed into it. .find(x) finds the first occurence of whatever it passed into it. slicing Look at the module for str and look at the closed brackets splicing and ranges. Generally, s[i:j] will return a slice of the string from index i to j-1 and s[i] will return the character at index i. Boolean Logic - Booleans show up on past test - don't worry about it right now at all!! #23 Fall2017 reviewTest12018.py line 32 till end __author__ = 'Student' #Uncomment which bit of code you want to use # they are separated by comment block '''reply = input( "Enter a list of numbers: " ) list_numbers_as_strings = reply.split() new_list = [] # accumulators are useful when we want do or print something after modification / alteration of the original for number in list_numbers_as_strings: number = int( number ) new_list.append( number ) print( new_list ) maximum = max(new_list) print (maximum) # append is used when you want to add something to a list # concatenation using "+" is used for gluing strings together ''' ''' import random seed = input ("Give me a seed: ") random.seed( seed ) number = random.randrange(0, 5) # this will choose a value between 0 through 4 print(number) ''' ''' # WORDY.PY FALL 2017 words_string = input("Enter some words: ") # this prompts the user for some words and stores user input as a string words_list = words_string.split() # .split() will split the string into a list of words based on the string the user entered nbr_words = len( words_list ) # Accumulator(s) sum_lengths = 0 # set an accumulator for the total sum of the lengths of the words list_lengths = [] # set an empty list so that we can add the lengths of all the words as elements of this list for word in words_list: length_word = len( word ) # gets the length of word sum_lengths = sum_lengths + length_word # adds it to the sum accumulator list_lengths.append( length_word ) # adds it to our list of lengths of words average = sum_lengths // nbr_words # the average is the sum of the lengths divided by the number of words count_words_with_average_length = list_lengths.count( average ) # .count() will count the number of words with the same length as the average print( average, count_words_with_average_length ) # print the two entities side by side ''' import url #https://www.cs.virginia.edu/~cs1112/text/hidden.txt reply = input("Give me a url: ") reply = reply.strip() website_text = url.get_text( reply ) website_list = website_text.split( '\n' ) # .split() when called on a string normally splits on spaces but # when you give it an argument (in this case '\n') it will separate the string into list elements separated at # the newline character (at each new line) print( website_list ) first_occurence = website_text.find('a') print( first_occurence ) first_occurence_list = website_list.index('a') # this will return an error saying that 'a' is not in list since there is no #specific element 'a' in our list print ( first_occurence_list )