Introduction to Computer Programming

Lab 1: Strings and Definite Loops:

pre-lab exercises


  1. Python basics in the IDLE shell.

    If after you enter a line such as:

     for _ in range(7):

    IDLE displays

     ...

    and indents the cursor that means that you continue typing to complete the compound statement. Hit enter an extra time to execute the compound statement in the shell. Try these examples in the IDLE shell, in order, one at a time:

     >>> 'This is a string'
    
     >>> greeting = 'hello'
     >>> greeting
     >>> len(greeting)
    
     >>> word = 'wheel'
     >>> word
     >>> len(word)
     >>> word = word + 's'
     >>> word
     >>> len(word)
    
     >>> greeting + 'n' + word
    
     >>> 756 + 1
     >>> '756' + '1'
     >>> '756' + 1
     >>> 756 + '1'
    
     >>> for _ in range(5):
             print('laziness rules!')
    
     >>> a = 3
     >>> b = 1
     >>> for _ in range(2 * a):
             print(b)
             b = b + 1
     >>> b
    
     >>> for c in 'Computer Science rocks!':
             print(c)
    
     >>> name = 'Mike'
     >>> blah = ''
     >>> blah
     >>> len(blah)
     >>> for c in name:
             print(blah)
             blah = blah + c
    
     >>> poem = 'Computers are cool\nimpatience and laziness\nMike is no poet'
     >>> len(poem)
     >>> poem
     >>> print(poem)
  2. From IDLE, Open the file lab1_examples.py. Read over the file in detail. Bring questions to ask at the start of lab.

  3. Run the module in IDLE and then experiment with using each of the functions in the IDLE shell, as in:

     >>> pow2(20)
    
     >>> n_stars(3)
     >>> print(n_stars(7))
    
     >>> square_string('Hi!')
     >>> print(square_string('Goodbye!'))