List Exercises 1

Harold Nelson

9/24/2020

Exercise

Create and print a list called west_coast containing the names of the states on the west coast of the US. Note there are four.

Answer

west_coast = ['Alaska','Washington','Oregon','California']
print(west_coast)
## ['Alaska', 'Washington', 'Oregon', 'California']

Exercise

Use a for loop to print the names in your list.

Answer

for x in west_coast:
    print(x)
## Alaska
## Washington
## Oregon
## California

Exercise

Print the second element in your list using its index.

Answer

print(west_coast[1])
## Washington

Exercise

Compute and print the length of the string west_coast.

Answer

print(len(west_coast))
## 4

Exercise

Print the contents, length and type of range(4).

Answer

print(range(4))
## range(0, 4)
print(len(range(4)))
## 4
print(type(range(4)))
## <class 'range'>

Exercise

Create and print the list opposite_corner with the names Florida, Georgia, and South Carolina. Do not include Alabama. Then create the list corners by concatenating the two existing lists. Print corners.

Answer

opposite_corner = ['Florida','Georgia', 'South Carolina']
print(opposite_corner)
## ['Florida', 'Georgia', 'South Carolina']
corners = west_coast + opposite_corner
print(corners)
## ['Alaska', 'Washington', 'Oregon', 'California', 'Florida', 'Georgia', 'South Carolina']

Exercise

Append North Carolina to your list using the append method.

Answer

corners.append("North Carolina")
print(corners)
## ['Alaska', 'Washington', 'Oregon', 'California', 'Florida', 'Georgia', 'South Carolina', 'North Carolina']

Exercise

Insert Alabama between California and Florida.

Answer

corners.insert(4,"Alabama")
print(corners)
## ['Alaska', 'Washington', 'Oregon', 'California', 'Alabama', 'Florida', 'Georgia', 'South Carolina', 'North Carolina']

Exercise

p, q and r are three boolean variables. On separate lines, print out every possible combination of the three.

Answer

truth_values = [True,False]
for p in truth_values:
    for q in truth_values:
        for r in truth_values:
            print(p,q,r)
    
    
## True True True
## True True False
## True False True
## True False False
## False True True
## False True False
## False False True
## False False False