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