Introduction and lists

Data Types in Python

Jason Myers

Instructor

Data types

  • Data type system sets the stage for the capabilities of the language
  • Understanding data types empowers you as a data scientist
Data Types in Python

Container sequences

  • Hold other types of data
  • Used for aggregation, sorting, and more
  • Can be mutable (list, set) or immutable (tuple)
  • Iterable
Data Types in Python

Lists

  • Hold data in order it was added
  • Mutable
  • Index
Data Types in Python

Accessing single items in list

cookies = ['chocolate chip', 'peanut butter', 'sugar']
cookies.append('Tirggel')
print(cookies)
['chocolate chip', 'peanut butter', 'sugar', 'Tirggel']
print(cookies[2])
sugar
Data Types in Python

Combining lists

  • Using operators, you can combine two lists into a new one
cakes = ['strawberry', 'vanilla']

desserts = cookies + cakes

print(desserts)
['chocolate chip', 'peanut butter', 'sugar', 'Tirggel', 'strawberry', 'vanilla']
  • .extend() method merges a list into another list at the end
cookies.extend(cakes)
Data Types in Python

Finding elements in a list

  • .index() method locates the position of a data element in a list
position = cookies.index('sugar')

print(position)
3
Data Types in Python

Removing elements in a List

  • .pop() method removes an item from a list and allows you to save it
name = cookies.pop(position)

print(name)
sugar
print(cookies)
['chocolate chip', 'peanut butter', 'Tirggel']
Data Types in Python

Iterating over lists

  • List comprehensions are a common way of iterating over a list to perform some action on them
titlecase_cookies = [cookie.title() for cookie in cookies]
print(titlecase_cookies)
Chocolate Chip
Peanut Butter
Tirggel
Data Types in Python

Sorting lists

  • sorted() function sorts data in numerical or alphabetical order and returns a new list
print(cookies)
['chocolate chip', 'peanut butter', 'Tirggel']
sorted_cookies = sorted(cookies)

print(sorted_cookies)
['Tirggel', 'chocolate chip', 'peanut butter']
Data Types in Python

Let's practice!

Data Types in Python

Preparing Video For Download...