""" Purpose: introduce lists and looping """ # looping is a very powerful skill that will expand our problem solving skills # it's also a little confusing, so follow along and ask questions # it also will never go away # two types of loops: for loops and while loops # loop = repeat = iterate # Get string of choice reply = input( "Enter text: " ) # Convert text into a list of words # a list is a new thing that we're seeing #w1,w2,w3 = reply.split() # if we know they're entering exactly 3 words, this will work... but what if we don't words = reply.split() # this will hand back a list of the words that were entered, split on whitespace # lists are shown in brackets [] with items separated by commas when we print it # lists are indexed the same way as strings # lists can hold strings, or numbers, or both, or other lists # Display list of words print() print( "words: ", words ) print() # Print words one by one # for each word in the list words, print that word # let's translate that into code now # for is a keyword, don't name a variable for, it indicates looping ( seen in bold face ) # print is not a keyword, but still don't name your variables that because then you can't print() # for ___ in ____ : is how every python for loop begins # for loop_variable in sequence ( what we want to loop through ) : # loop variables are a special kind of variable, they change with each iteration through the loop # looks through the list words and runs once for each word in words # every time you go through the loop, the loop variable will have a different value from the list # we have indentation now, for loops are multi-line statements, indicated by indentation # when you're done with the loop, backspace back to column 1 # current_word is a special loop variable, and will change value each time automatically, we don't have to # assign the loop variable the values # the computer will end the for loop when there's no more indentation # the computer knows to stop looping when it has gone through all of the list words # EVERY for loop will look like for ____ in _____ : # we can have for loops in for loops, that's called nesting for current_word in words : print( "current_word:", current_word ) # this will change every time we run through the loop # print( "let's continue on looping" ) current_word_length = len( current_word ) print( "current_word_length:", current_word_length ) print() # everything indented under the for loop declaration is the body of the for loop. there must be at least # one action in the for loop or python will get mad at us :( # for loops come in three flavors: looping through a list (super common and handy!), looping through a string, # looping through a range of numbers print( "All done!" ) # no longer indented - indicating we are done with the for loop, this will run once # we called our loop variable current_word to emphasize that current_word is updating and changing with each # iteration of the for loop # we can loop through more than just lists, such as strings, tuples, and more! # we do a lot of list processing in this class # if we want to repeat an action n times, we'll look at the range() function, where we can specify how many # times we want our for loop to run