Python Core Concepts for Data Science Explained (with code) | by Frank Andrade | Towards Data Science

Python offers a good number of libraries used in data science such as Pandas, Numpy, and Scikit-learn. Learning those libraries right away and skipping the basics isn’t good though.
If you would like to learn Python for data science, you should master Python core concepts first. Having a solid foundation in Python will help you avoid common mistakes and bad practices. As a result, learning Python libraries used in data science will be much easier.
In this guide, we’ll see some must-know Python concepts every data scientist should know. At the end of this article, you will find a Python for Data Science Cheat Sheet in PDF version (section 9 in the table of contents below)
Table of Contents1. Python Attributes vs Methods2. Python Data Types3. Python Variables4. Python Lists5. Python Dictionary6. Python If Statement7. Python For Loop8. Python Function9. Python for Data Science Cheat Sheet (Free PDF)
I can’t tell how long I used the words “attribute” and “method” interchangeably when I was a beginner in Python.
When you learn libraries like Pandas, you’ll frequently call attributes and methods, so it’s good to know what’s the difference between them.
Attribute: An attribute is a variable stored in a class. That is, a value associated with an object. It’s referenced by name using dotted expressions. For an object Foo, you call an attribute bar as Foo.bar
Method: A method is a function that is defined inside a class body. For an object Foo, you call a method baz as Foo.baz()
When it comes to data science, we don’t work with classes so often, so don’t worry if you didn’t quite understand the definitions. For now, you can memorize the notations.
We’ll see a lot of practical examples of Python methods throughout this guide.
Don’t feel like reading? Watch my video instead:
Every value in Python is an object. An object has different data types. Here are the most common data types in Python.
String (str): A string represents a series of characters. In Python, anything inside quotes (single quotes or double quotes) is a string.Boolean (bool): True/False valuesInteger (int): Numbers that can be written without a fractional component.Float (float): Numbers that contain floating decimal points.
String (str): A string represents a series of characters. In Python, anything inside quotes (single quotes or double quotes) is a string.
Boolean (bool): True/False values
Integer (int): Numbers that can be written without a fractional component.
Float (float): Numbers that contain floating decimal points.
We’ll see other data types like lists and dictionaries in detail in the following sections.
To check the data type of an object, use the type function.
IN [0]: type(1)OUT[0]: intIN [1]: type(2.3)OUT[1]: floatIN [2]: type("Hello World")OUT[2]: str
We can apply different functions to strings as we would do in Microsoft Excel. However, in Python we use methods. A method is a function that “belongs to” an object. To call a method we use the . sign after the object.
Let’s see some string methods to change the case of a text.
IN [3]: "Hello World".upper()OUT[3]: HELLO WORLDIN [4]: "Hello World".lower()OUT[4]: hello worldIN [5]: "hello world".title()OUT[5]: Hello World
Variables help us store data values. In Python, we often work with data, so variables are useful to manage this data properly. To assign a value to a variable, we use the = sign.
Let’s create a message that says “I’m learning Python” and store it in a variable called message_1.
message_1 = "I'm learning Python"
We can create as many variables as we want. Just make sure to assign different names to new variables. Let’s create a new message that says “and it’s fun!” and store it in a variable called message_2.
message_2 = "and it's fun!"
We can put message_1 and message_2 together by using the + operator. This is called string concatenation.
IN [6]: message_1 + message_2OUT[6]: "I'm learning Pythonand it's fun!"
We need to add a blank space “ ”to make the text readable. This time let’s also store this new message in a new variable called message
message = message_1 + ' ' + message_2IN [7]: messageOUT[7]: "I'm learning Python and it's fun!"
Now you could change the message written in message_1 or message_2 and that will also change our new message variable.
In Python, lists are used to store multiple items in a single variable. Lists are ordered and mutable containers. In Python, we call ‘mutable’ to objects that can change their values. That is, elements within a list can change their value.
To create a list, we have to introduce the elements inside square brackets [], separated by commas.
Consider the following list named “countries” that contains some of the countries with the largest population around the world.
countries = ['United States', 'India', 'China', 'Brazil']
Keep in mind that lists can have elements of different types and duplicated elements.
By indexing, we can obtain a list item or element by its position. Each item in a list has an index (position in the list). Python uses zero-based indexing. That is, the first element (“United States”) has an index 0, the second (“India”) has index 1, and so on.
To access an element by its index we need to use square brackets [].
IN [8]: countries[0]OUT[8]: United StatesIN [9]: countries[1]OUT[9]: IndiaIN [10]: countries[3]OUT[10]: Brazil
There are also negative indexes that help us get elements starting on the last position of the list, so instead of using indexes from 0 and above, we use indexes from -1 and below.
Let’s get the last element of the list but now using a negative index.
IN [11]: countries[-1]OUT[11]: Brazil
Slicing means accessing parts of a list. A slice is a subset of list elements. The slice notation has the following form:
list_name[start:stop]
where “start” represents the index of the first element, and stop represents the element to stop at (without including it in the slice).
Let’s see some examples:
IN [12]: countries[0:3]OUT[12]: ['United States', 'India', 'China']IN [13]: countries[1:]OUT[13]: ['India', 'China', 'Brazil']IN [14]: countries[:2]OUT[14]: ['United States', 'India']
There are a lot of things you can do with lists. You can add new elements, remove any element, update values, and so on. In this. section, we will see the most common list operation and methods.
Adding elements to a listThere are different methods that help us add a new element to a list. Let’s test both of them using thecountries list (we’re going to define the countries list on each test to start with the same number of elements)
# append(): Adds a new element at the end of a listcountries = ['United States', 'India', 'China', 'Brazil']IN [15]: countries.append('Canada')IN [16]: countriesOUT[16]: ['United States', 'India', 'China']# insert(): Adds a new element at a specific positioncountries = ['United States', 'India', 'China', 'Brazil']IN [17]: countries.insert(0,'Canada')IN [18]: countriesOUT[18]: ['Canada', 'United States', 'India', 'China', 'Brazil']
We can even use the + to concatenate lists.
countries_2 = ['UK', 'Germany', 'Austria']IN [19]: countries + countries_2OUT[19]: ['Canada', 'United States', 'India', 'China', 'Brazil', 'UK', 'Germany', 'Austria']
And we can put the list countries and countries_2 inside another list, which is called a nested list.
nested_list = [countries, countries_2]IN [20]: nested_listOUT[20]: [['Canada', 'United States', 'India', 'China', 'Brazil'], ['UK', 'Germany', 'Austria']]
Removing elementsThere are different methods that help us remove an element from a list. Let’s test them with the firstcountries list we created.
# remove(): Removes the first matching value.IN [21]: countries.remove('United States')IN [22]: countriesOUT[22]: ['India', 'China', 'Brazil']# pop(): Removes an item at a specific index and then returns it.IN [23]: countries.pop(0)IN [24]: countriesOUT[24]: ['China', 'Brazil']# del: Removes an item at a specific indexIN [25]: del countries[0]IN [26]: countriesOUT[26]: ['Brazil']
Sorting a listWe can easily sort a list using the .sort() method. Let’s create a new list called numbers and then sort it from the smallest to the largest number.
numbers = [4, 3, 10, 7, 1, 2]IN [27]: numbers.sort()IN [28]: numbersOUT[28]: [1, 2, 3, 4, 7, 10]
We can add the reverse argument to the .sort() method to control the order. If we want it to be descendent, we set reverse=True.
numbers.sort(reverse=True)
Update value on a listTo update a value on a list we use indexing to locate the element we want to update and then set it to a new value using the = sign.
numbers[0] = 1000IN [29]: numbersOUT[29]: [1000, 2, 3, 4, 7, 10]
Copying a listWhenever we need to make a copy of an existing list, we can use the following options:
# option 1: [:]new_list = countries[:]# option 2: .copy()new_list_2 = countries.copy()
The [:] comes from the slicing technique we learned before. In this case, we set neither a start nor a stop value, so the entire list is selected. On the other hand, the .copy() method explicitly makes a copy of a list.
Be aware that making copies of an object using the code below won’t create an independent copy as we did with the previous options.
new_list_3 = countries
In Python, dictionaries help us store data in key:value pairs (one pair is called an item). Like lists, dictionaries are mutable, but unlike lists, dictionaries are accessed via keys and duplicate keys are not allowed.
A dictionary has the following form:
my_dict = {'key1':'value1', 'key2':'value2'}
Let’s create our first dictionary using some information about me.
my_data = {'name':'Frank', 'age':26}
Let’s see how to access a value in a dictionary and some common methods.
my_data['name'] # returns value that corresponds to "name" keymy_data.keys() # returns all keys of the dictionarymy_data.values() # returns all values of the dictionarymy_data.items() # returns the pairs key-value of a dictionary
Similar to lists, we can add and update values in a dictionary. Let’s start by adding a new pair key-value that represents my height.
my_data['height']=1.7
After running the code above, the dictionary gets a new item.
IN [30]: my_dataOUT[30]: {'name': 'Frank', 'age': 26, 'height': 1.7}
If the data I added wasn’t correct, we can update it with the .update() method and even add more data.
Let’s update my height and add a new key.
my_data.update({'height':1.8,'languages':['English', 'Spanish']})
Now my dictionary has the following items:
{'name': 'Frank', 'age': 26, 'height': 1.8, 'languages': ['English', 'Spanish']}
We can use the .copy method to make a copy of a dictionary.
new_dict = my_data.copy()
The new_dict dictionary will have the same items that the original my_data dictionary had.
We can use the same methods used in lists to remove dictionary items.
# pop(): Removes an item by specifying its keyIN [35]: my_data.pop('height')IN [36]: my_dataOUT[36]: {'name': 'Frank', 'age': 26, 'languages': ['English', 'Spanish']}# del: Removes an item with a specific keyIN [37]: del my_data['languages']IN [38]: my_dataOUT[38]: {'name': 'Frank', 'age': 26}# clear(): Removes all items in a dictionaryIN [39]: my_data.clear()IN [40]: my_dataOUT[40]: []
The if statement is one of the most common statements used in Python. It’s a conditional statement used to decide whether a statement (or block of statements) will be executed.
Here’s the syntax of the if statement:
if <condition>:    <code>elif <condition>:    <code>...else:    <code>
Let’s see the if statement in action with a simple example. The following code will output a message based on the value we introduce in a age variable.
age = 18if age>=18:    print("You're an adult!")elif age>=13:    print("You're a teenager!")else:    print("You're a kid")
The code below is saying “If the age is equal or greater than 18, you’re an adult; if the age is between 13 and 17, you’re a teenager; and if the age is less than 13, you’re a kid”
But that’s not all! We can use the if statement with thein keyword to verify if an element is in a list.
Let’s verify if the country China is in our list “countries.”
countries = ['United States', 'India',             'China', 'Brazil']if 'China' in countries:    print("Country is in list")else:    print("Not in list")
In this example, the output will be “Country is in list.”
The for loop is a frequently used loop in Python. It allows us to loop through an iterable object (e.g., list, dictionaries) and perform the same action to each element.
Here’s the syntax of the for loop:
for <variable> in <list>: <code>
Let’s loop through our countries list and print all the elements.
for country in countries: print(country)United StatesIndiaChinaBrazil
We can use the for loop and if statement together to perform an action to only certain elements. For example, let’s loop through the same list but now only print the element “United states”
for country in countries:    if country == "United States":        print(country)United States
Sometimes we need to enumerate elements in a loop. We can do this with the for loop.
for i, element in enumerate(<iterable>):    <code>
Let’s loop and enumerate the elements inside the countries list.
for i, country in enumerate(countries):    print(i)    print(country)0United States1India2China3Brazil
We can obtain the key and item separately while looping through a dictionary. We only need to use the .items() method.
for key, value in my_dict.items():    <code>
Let’s loop through the my_data dictionary and obtain the keys and values.
for key, value in my_data.items():    print(key)    print(value)
Python has lots of built-in functions that can help us perform a specific task. Let’s have a look at the most common of them.
len() ->  calculates the length of any iterable objectmax() -> returns the item with the highest value in an iterablemin() -> returns the item with the lowest value in an iterabletype() -> returns the type of an objectrange() -> returns a sequence of numbers
We can also create our own functions. Here’s the syntax used to create a function:
def function(<params>):    <code>    return <data>
Looks easy right? Let’s create a function that sums two values a and b.
def sum_values(a,b):    x = a+b    return x
Every time we call the function sum_values we will sum the a and b values.
IN []: sum_values(2,3)OUT[]: 5
That’s it! Those are all the Python core concepts you need to know before learning any data science library.
Join my email list with 3k+ people to get my Python for Data Science Cheat Sheet I use in all my tutorials (Free PDF)
If you enjoy reading stories like these and want to support me as a writer, consider signing up to become a Medium member. It’s $5 a month, giving you unlimited access to thousands of Python guides and Data science articles. If you sign up using my link, I’ll earn a small commission with no extra cost to you.