Skip to content

Latest commit

 

History

History
25 lines (15 loc) · 1.02 KB

problem_set_2_comprehensions.md

File metadata and controls

25 lines (15 loc) · 1.02 KB

Comprehensions

List Comprehensions

Use a single-line list comprehension to create the following list:

  1. A list of all the odd numbers from 17 to 34.
  2. A list of all the odd numbers in: years = [1492, 1776, 1812, 1945, 1969, 1984, 1985, 2001, 2015]
  3. The square of all numbers from 4 to 14.
  4. The number of times each digit occurs in the sequence: pi = "141592653589793238462643383279502881". (Hint: str.count('0'). Hint: your answer should be a list 10 items long, one for each digit 0 through 9.)

Dictionary Comprehensions

Use a single-line dictionary comprehension to create a dictionary of the following terms.

  1. Using zip, where the keys are keys = ["a", "b", "c"] and the values are values = [1, 2, 3].
  2. A dictionary where the keys are 1 through 10 and the values are the cube of the keys.
  3. A dictionary where the keys are 1 through 10 and the values are True if the key is odd and False if even.

Solutions

Back to Lecture