Python Tutorial : Data Types for Data Science in Python

Published: 22 March 2020
on channel: DataCamp
149
4

Want to learn more? Take the full course at https://learn.datacamp.com/courses/da... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.

---

Hello, I'm Jason Myers and welcome to the Python data types for data science course. I'm an author, open-source maintainer, and an avid Python user.

In most programming languages, the data type system set the stage for the capabilities of the language. Understanding how to use the fundamental data types of a language greatly empowers you as a data scientist. You've already encountered integers and strings, so let's start with container sequences.

A container sequence gets its name because it holds a sequence of elements of other data types. In the data science world, we'll use these containers to store our data for aggregation, order, sorting, and more. Python provides several container sequences such as lists, sets, and tuples to name a few. They can be mutable meaning that they can have elements added and removed from them. Immutability, not able to be altered, allows us to protect our reference data, and replace individual data points with sums, avgs, derivations, etc. We can iterate, also known as looping, over the data contained within these containers. Being able to iterate over these sequences allows us to group data, aggregate it, and process it over time. Let's start with learning about container types by looking at lists.

Often we need to hold an ordered collection of items, and lists allow us to do just that. Lists are mutable so we can add or remove data from them. Lists also allow us to access an individual element within them using an index. Let's see this in action. If I wanted to store a list of cookies I've eaten this week. I would begin by creating that list of cookies. When I eat another cookie, I'll want to add that to my list as well, which I can do with the append method. Then I can print the whole list of cookies. If I wanted to print the cookie I ate third; then I could use an index, which in this case would be 2 since indexes starts at zero, and print that element from the list. In addition to appending each cookie one at a time, we might also want to combine multiple lists.

Python empowers us to combine lists in a few ways. First, we can use operators like the plus sign to add two lists together. When we do this, we will get a new list object returned to us. For example, we can add a list of cookies and cakes to create a list of deserts. Additionally, we can use the extend method on the list to combine two lists effectively appending all the values from a second list. You'll have the chance to use this in the exercises.

Earlier, we took advantage of the indexability of lists to return the item at a certain index. However, if we only know the value of an element, we can use it with the index method to get its index. Here I'm trying to remember when I had a sugar cookie. Another reason I might want to know the index of value is to remove it from a list with the pop method. Here I want to remove that sugar cookie from the list of cookies. So I pass the index I found earlier to the pop method, and I store the result of the pop method, which is the value we removed. Finally, I print the list so I can ensure it was removed.

Often when working with lists, we want to work on that list one element at a time. We can do that by iterating over the list using a for loop. Here I want to print each cookie I've eaten this week. So I loop over each cookie in the cookies list and print it. Python provides the sorted function that accepts an iterable such as a list and returns a new list with the elements in the proper order. Here I sort the cookies list and then print it so I can see the cookie names in alphabetical order. Time for you to try this on your own.

#DataCamp #PythonTutorial #DataTypesforDataScienceinPython


On this page of the site you can watch the video online Python Tutorial : Data Types for Data Science in Python with a duration of hours minute second in good quality, which was uploaded by the user DataCamp 22 March 2020, share the link with friends and acquaintances, this video has already been watched 149 times on youtube and it was liked by 4 viewers. Enjoy your viewing!