Python standard libraries

  • Built in types

  • Built in functions

  • Built in modules

# range(start,stop)
nums = range(0,11) # returns a range object

nums_list = list(nums)
print((nums))
print(type(nums))
print(nums_list)
range(0, 11)
<class 'range'>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
nums_list = list(range(0,11))
print(nums_list)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
nums_list = list(range(0,12,2))
print(nums_list)
[0, 2, 4, 6, 8, 10]

Using built-in function: enumerate()

letters = ['a', 'b', 'c','d']
indexed_letters = enumerate(letters)

indexed_letters_list = list(indexed_letters)
print(type(indexed_letters))
print(indexed_letters_list)
<class 'enumerate'>
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd')]
value = [i for i in indexed_letters_list if indexed_letters_list[i] == 0]
print(value)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-733846be1089> in <module>
----> 1 value = [i for i in indexed_letters_list if indexed_letters_list[i] == 0]
      2 print(value)

<ipython-input-20-733846be1089> in <listcomp>(.0)
----> 1 value = [i for i in indexed_letters_list if indexed_letters_list[i] == 0]
      2 print(value)

TypeError: list indices must be integers or slices, not tuple

Example of an exercise

# Create a range object that goes from 0 to 5
nums = ____(____)
print(type(nums))

# Convert nums to a list
nums_list = ____(____)
print(nums_list)

# Create a new list of odd numbers from 1 to 11 by unpacking a range object
nums_list2 = [*____(____,____,____)]
print(nums_list2)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-21-16778cc6f499> in <module>
      1 # Create a range object that goes from 0 to 5
----> 2 nums = ____(____)
      3 print(type(nums))
      4 
      5 # Convert nums to a list

NameError: name '____' is not defined