To Lecture Notes

IT 211 -- Feb 13, 2019

Lists

  1. A Python list is a collection of items separated with commas and delimited with square brakets, for example:
    list1 = [34, 59, 61, 23, 43]
    
  2. We can print all of the items in the list using a for statement:
    for n in list1:
        print(n)
        
    # Output:
    34
    59
    61
    23
    43
    
    or
    for n in list1:
        print(n, end=" -- ")
        
    # Output:
    34 -- 59 -- 61 -- 23 -- 43 --
    
    You can also print a list directly like this:
    print(list1)
    
    # Output:
    [34, 59, 61, 23, 43]
    
  3. We can extract individual items from the list using the index operator [ ]:
    print(list1[3])
    
    # Output:
    23
    
    This result is explained by this table:

    list1 Index   0   1   2   3   4
    Value 34 59 61 23 43

    Remember that computer scientists start counting at 0 instead of 1, thus the indices for a list start at 0.
  4. This suggests a different way to print the values in the array:
    for n in range(0, len(list1):
        print(n, end="; ")
        
    # Output:
    34; 59; 61; 23; 43;
    
  5. The items in a list often are, but are not necessarily all of the same datatype, for example:
    list2 = [154, "dog", 3.14159, True]
    for value in list2:
        print(value)
    # Output:
    154
    dog
    3.14159
    True
    
  6. We can check the datatypes of the values in the list like this:
    for value in list2:
        print(type(value))
    
    # Output:
    <class 'int'>
    <class 'str'>
    <class 'float'>
    <class 'bool'>
    

Project 6

More about User Defined Methods

Practice Problems

Write user and test defined methods. The method header is shown first with the description on subsequent lines.

  1. def print_greeting( ):
         Print a generic greeting, such as "Hello, how are you?"
    # Ans:
    def print_greeting( ):
        print("Hello, how are you?")
        
    print_greeting( )
    
    # Output:
    Hello, how are you?
    
  2. def print_greeting(the_person):
        Print a greeting to the person whose name is passed in as a parameter.
    def print_greeting(the_person):
       print(f"Hello, {the_person}, how are you?")
       
    print_greeting("Alice")
    
    # Output:
    Hello, Alice, how are you?
    
  3. def print_greeting(the_person):
        Return a greeting to the person whose name is passed in as a parameter
     
    # Ans:
    def get_greeting(the_name):
        return "Hello, " + the_name + ", how are you?"
        
    print(get_greeting("Alice"))
    
    # Ans:
    Hello, Alice, how are you?
    
  4. def meters(feet, inches):
        Return the length given in feet and inches converted to meters.
        Use the fact that one inch == 2.54 centimeters.
    # Ans: 
    def to_meter(feet, inches):
        m = (feet * 12.0 + inches) * 2.54 / 100.0
        return m
        
    print(to_meter(2, 3)
    
    # Output: 0.6912
    
  5. def to_feet_inches(meters):
         Return as a string the length given in meters converted to meters
         Use the fact that one inch == 2.54 centimeters.
    def to_feet_inches(meters):
    f = meters * 100.0 / (2.54 * 12.0)
    feet = int(f)
    inches = int(round((f - feet) * 12.0, 0))
    output = str(feet) + "' " + str(inches) + '"'
    return output
    
    print(to_feet_inches(0.6912))
    # Output:
    2' 3"
    

Project 5