''' Purpose: demonstrate the printing of a string of characters ''' # get a string of three letters reply = input( "Enter a three letter-string: " ) # reply is the string of the user input # reply[0] is how we get the character at index 0 ### print the characters ch0 = reply[0] ch1 = reply[1] ch2 = reply[2] # 'abc' # 012 # ch0 would be 'a' ch1 would be 'b' ch2 would be 'c' print( ch0 ) print( ch1 ) print( ch2 ) # get a string reply = input( "Enter a string: " ) # Now we want to print out the characters for a string of any number of characters! # Not just three characters! ### print the characters # for loopvariable in sequence: # statements # loopvariable takes on different values each time we go through the loop # You can name the loop variable WHATEVER you want! # Each new value is a smaller part of the sequence # Sequences can be lists, strings, or ranges #Broken into: elements characters numbers for the different values of the loop variable! # Keywords are special words in Python with special meanings (like for and in) - usually bolded blue in the IDE PyCharm for ch in reply: # ch is our loop variable and the sequence is reply (a string) - ch is each character in reply one by one print( ch ) # print out the value of ch for each new time we go through the loop # All statements inside the loop to be repeated for each value of the loop variable # are indented into the for loop reply = input( 'Enter text: ' ) # split() splits on whitespace so split() won't break a string up into characters unless they're separated by spaces # Another Python type: list # INTRODUCTION TO ACCUMULATION (more on this later) - BIG TOPIC IN THIS CLASS!! # Lists look like [...,...,...] where ... are elements # Accumulation using a list # new_list = [] # ACCUMULATOR INITIALIZED BEFORE THE FOR LOOP!!!!! # for ... in ...: # new_list.append( what we wanna add to the list ) # Now the list has elements in it :) new_list = [] # [] denotes empty list - we will initialize new_list as an empty list and add to it using the for loop for ch in reply: # new_list = [] # For a single loop accumulation, this resets the new_list to the empty list each time we go through the for loop - don't do this # print( new_list ) # Each time through the loop add ch to our new list new_list.append( ch ) # .append() is the function to add elements into a list! More on lists in the next few classes! # print( new_list ) print( new_list ) # NOT IN THE FOR LOOP - outside the for loop after we've added all the characters into the list # When the for loop finishes (executes the code for the last value of the loop variable / end of the sequence) it # automatically ends the for loop # But only indented statements are executed in the for loop itself - anything outside the for loop is not # part of the for loop.