1: Booleans

Instructions

Assign the value True to the variable cat and the value False to the variable dog. Then use the print() function and the type() function to display the type for cat.

Answer


In [1]:
cat = True
dog = False

print(type(cat))


<class 'bool'>

2: Boolean operators

Instructions

Use the Boolean operators to determine if the following pairs of values are equivalent:

  • first element of cities and the string "Albuquerque". Assign the resulting Boolean value to first_alb
  • second element of cities and the string "Albuquerque". Assign the resulting Boolean value to second_alb
  • first element of cities and the last element in cities. Assign the resulting Boolean value to first_last

In [13]:
from cities import cities

In [14]:
print(cities)
first_alb = cities[0] == 'Albuquerque'
second_alb = cities[1] == 'Albuquerque'
first_last = cities[0] == cities[-1]


['Albuquerque', 'Anaheim', 'Anchorage', 'Arlington', 'Atlanta', 'Aurora', 'Austin', 'Bakersfield', 'Baltimore', 'Boston', 'Buffalo', 'Charlotte-Mecklenburg', 'Cincinnati', 'Cleveland', 'Colorado Springs', 'Corpus Christi', 'Dallas', 'Denver', 'Detroit', 'El Paso', 'Fort Wayne', 'Fort Worth', 'Fresno', 'Greensboro', 'Henderson', 'Houston', 'Indianapolis', 'Jacksonville', 'Jersey City', 'Kansas City', 'Las Vegas', 'Lexington', 'Lincoln', 'Long Beach', 'Los Angeles', 'Louisville Metro', 'Memphis', 'Mesa', 'Miami', 'Milwaukee', 'Minneapolis', 'Mobile', 'Nashville', 'New Orleans', 'New York', 'Newark', 'Oakland', 'Oklahoma City', 'Omaha', 'Philadelphia', 'Phoenix', 'Pittsburgh', 'Plano', 'Portland', 'Raleigh', 'Riverside', 'Sacramento', 'San Antonio', 'San Diego', 'San Francisco', 'San Jose', 'Santa Ana', 'Seattle', 'St. Louis', 'St. Paul', 'Stockton', 'Tampa', 'Toledo', 'Tucson', 'Tulsa', 'Virginia Beach', 'Washington', 'Wichita']

In [15]:
print(first_alb, second_alb, first_last)


True False False

3: Booleans with greater than

Instructions

The variable crime_rates is a list of integers containing the crime rates from the dataset. Perform the following comparisons:

  • evaluate if the first element in crime_rates is larger than the integer 500, assign the Boolean result to first_500
  • evaluate if the first element in crime_rates is larger than or equal to 749, assign the Boolean result to first_749
  • evaluate if the first element in crime_rates is greater than or equal to the last element in crime_rates, assign the Boolean result to first_last

Answer


In [16]:
crime_rates = [749, 371, 828, 503, 1379, 425, 408, 542, 1405, 835, 1288, 647, 974, 1383, 455, 658, 675, 615, 2122, 423, 362, 587, 543, 563, 168, 992, 1185, 617, 734, 1263, 784, 352, 397, 575, 481, 598, 1750, 399, 1172, 1294, 992, 522, 1216, 815, 639, 1154, 1993, 919, 594, 1160, 636, 752, 130, 517, 423, 443, 738, 503, 413, 704, 363, 401, 597, 1776, 722, 1548, 616, 1171, 724, 990, 169, 1177, 742]
print(crime_rates)

first = crime_rates[0]
first_500 = first > 500
first_749 = first >= 749
first_last = first >= crime_rates[-1]


[749, 371, 828, 503, 1379, 425, 408, 542, 1405, 835, 1288, 647, 974, 1383, 455, 658, 675, 615, 2122, 423, 362, 587, 543, 563, 168, 992, 1185, 617, 734, 1263, 784, 352, 397, 575, 481, 598, 1750, 399, 1172, 1294, 992, 522, 1216, 815, 639, 1154, 1993, 919, 594, 1160, 636, 752, 130, 517, 423, 443, 738, 503, 413, 704, 363, 401, 597, 1776, 722, 1548, 616, 1171, 724, 990, 169, 1177, 742]

In [17]:
print(first_500, first_749, first_last)


True True True

4: Booleans with less than

Instructions

The variable crime_rates is a list containing the crime rates from the dataset as integers. Perform the following comparisons:

  • determine if the second element in crime_rates is smaller than the integer 500, assign the Boolean result to second_500
  • determine if the second element in crime_rates is smaller than or equal to 371, assign the Boolean result to second_371
  • determine if the second element in crime_rates is smaller than or equal to the last element in crime_rates, assign the Boolean result to second_last

Answer


In [18]:
second = crime_rates[1]
second_500 = second < 500
second_371 = second <= 371
second_last = second <= crime_rates[-1]

In [19]:
print(second_500, second_371, second_last)


True True True

5: If statements

Instructions

Determine if the third element in cities is equivalent to the string "Anchorage". If it is equivalent, change the variable result to 1.

Answer


In [20]:
result = 0

if cities[2] == u"Anchorage":
    result = 1

In [21]:
assert result == 1

6: Nesting if statements

Instructions

Nest if statements in the following order:

  • first one checks if the first element in crime_rates is larger than 500
  • second one checks if the second element in crime_rates is larger than 300
  • if both statements evaluate to True, assign the value 3 to the variable results

Answer


In [23]:
reqults = 0

if crime_rates[0] > 500:
    if crime_rates[0] > 300:
        results = 3

7: If statements and for loops

Instructions

Create a new list, five_hundred_list, that contains only the elements from crime_rates that are greater than 500. To accomplish this, you'll need a for loop and an if statement:

  • the for loop specifies which list we want to iterate over and the name of the iterator variable (we use cr in our answer)
  • the if statement determines if the current element (cr) is larger than 500
    • if the current element (cr) is larger than 500, use the append() method to add it to five_hundred_list

Answer


In [27]:
five_hundred_list = []

for cr in crime_rates:
    if cr > 500:
        five_hundred_list.append(cr)

In [28]:
assert all([_>500 for _ in five_hundred_list])

8: Find the highest crime rate

Instructions

Now [...] we can find the highest crime rate. crime_rates is a list of integers where each integer is a crime rate.

One strategy is to:

  • assign the value at index 0 from crime_rates to a new integer variable called highest
  • use a for loop to compare each value in crime_rates to highest and assign that value to highest if it's larger

Find the largest integer in crime_rates using the strategy we just discussed and assign that value to the variable highest.

Answer


In [30]:
print(crime_rates)
highest = crime_rates[0]

for cr in crime_rates:
    if cr > highest:
        highest = cr


[749, 371, 828, 503, 1379, 425, 408, 542, 1405, 835, 1288, 647, 974, 1383, 455, 658, 675, 615, 2122, 423, 362, 587, 543, 563, 168, 992, 1185, 617, 734, 1263, 784, 352, 397, 575, 481, 598, 1750, 399, 1172, 1294, 992, 522, 1216, 815, 639, 1154, 1993, 919, 594, 1160, 636, 752, 130, 517, 423, 443, 738, 503, 413, 704, 363, 401, 597, 1776, 722, 1548, 616, 1171, 724, 990, 169, 1177, 742]

In [ ]: