10 Must-Know Python Topics for Data Science | by Soner Yıldırım | Towards Data Science

Python is dominating the data science ecosystem. What I think the top two reasons for such dominance are being relatively easy to learn and the rich selection of data science libraries.
Python is a general purpose language so it is not just for data science. Web development, mobile application and game development are some use cases for Python.
If you are using Python only for data science related tasks, you do not have to be a Python expert. However, there are some core concepts and features that I think you must have in your possession.
What we cover in this article is not library-specific. They can be considered as base Python for data science. Even if you are only using Pandas, Matplotlib, and Scikit-learn, you need to have a comprehensive understanding of Python basics. Such libraries assume you are familiar with Python basics.
I will briefly explain each topic with a few examples and also provide a link to a detailed article for most of the topics.
Functions are building blocks in Python. They take zero or more arguments and return a value. We create a function using the def keyword.
Here is a simple function that multiplies two numbers.
def multiply(a, b):  return a * bmultiply(5, 4)20
Here is another example that evaluates a word based on its length.
def is_long(word):  if len(word) > 8:     return f"{word} is a long word."is_long("artificial")'artificial is a long word.'
Functions should accomplish a single task. Creating a function that performs a series of tasks defy the purpose of using functions.
We should also assign descriptive names to functions so we have an idea of what it does without seeing the code.
When we define a function, we specify its parameters. When a function is called, it must be provided with the values for the required parameters. The values for parameters are also known as arguments.
Consider the multiply function created in the previous step. It has two parameters so we provide values for these parameters when the function is called.
Positional arguments are declared by a name only.
Keyword arguments are declared by a name and a default value.
When a function is called, values for positional arguments must be given. Otherwise, we will get an error. If we do not specify the value for a keyword argument, it takes the default value.
Let’s redefine the multiply function with keyword arguments so we can see the difference.
def multiply(a=1, b=1):  return a * bprint(multiply(5, 4))20print(multiply())1
Functions are building blocks in Python. They take zero or more arguments and return a value. Python is pretty flexible in terms of how arguments are passed to a function. The *args and **kwargs make it easier and cleaner to handle arguments.
*args allow a function to take any number of positional arguments.
Here is a simple example:
def addition(*args):   result = 0   for i in args:      result += i   return resultprint(addition(1,4))5print(addition(1,7,3))11
**kwargs allow a function to take any number of keyword arguments.
By default, **kwargs is an empty dictionary. Each undefined keyword argument is stored as a key-value pair in the **kwargs dictionary.
Here is a simple example:
def arg_printer(a, b, option=True, **kwargs):   print(a, b)   print(option)   print(kwargs)arg_printer(3, 4, param1=5, param2=6)3 4True{'param1': 5, 'param2': 6}
towardsdatascience.com
Object oriented programming (OOP) paradigm is built around the idea of having objects that belong to a particular type. In a sense, the type is what explains us the object.
Everything in Python is an object of a type such as integers, lists, dictionaries, functions and so on. We define a type of object using classes.
Classes possess the following information:
Data attributes: What is needed to create an instance of a class
Methods (i.e. procedural attributes): How we interact with instances of a class.
towardsdatascience.com
List is a built-in data structure in Python. It is represented as a collection of data points in square brackets. Lists can be used to store any data type or a mixture of different data types.
Lists are mutable which is one of the reasons why they are so commonly used. Thus, we can remove and add items. It is also possible to update the items of a list.
Here are a few examples on how to create and modify a list.
words = ['data','science'] #create a listprint(words[0]) #access an item'data'words.append('machine') #add an itemprint(len(words)) #length of list3print(words)['data', 'science', 'machine']
towardsdatascience.com
List comprehension is basically creating lists based on other iterables such as lists, tuples, sets, and so on. It can also be described as representing for and if loops with a simpler and more appealing syntax. List comprehensions are relatively faster than for loops.
Here is a simple list comprehension that creates a list from another list based on a given condition.
a = [4,6,7,3,2]b = [x for x in a if x > 5]b[6, 7]
The following list comprehension applies a function to the items in another list.
words = ['data','science','machine','learning']b = [len(word) for word in words]b[4, 7, 7, 8]
towardsdatascience.com
Dictionary is an unordered collection of key-value pairs. Each entry has a key and value. A dictionary can be considered as a list with special index.
The keys must be unique and immutable. So we can use strings, numbers (int or float), or tuples as keys. Values can be of any type.
Consider a case where we need to store grades of students. We can either store them in a dictionary or a list.
One way to create a dictionary is writing key-value pairs in curly braces.
grades = {'John':'A', 'Emily':'A+', 'Betty':'B', 'Mike':'C', 'Ashley':'A'}
We can access a value in a dictionary using its key.
grades['John']'A'grades.get('Betty')'B'
towardsdatascience.com
A set is an unordered collection of distinct hashable objects. This is the definition of a set in the official Python documentation. Let’s open it up.
Unordered collection: It contains zero or more elements. There is no order associated with the elements of a set. Thus, it does not support indexing or slicing like we do with lists.
Distinct hashable objects: A set contains unique elements. The hashable means immutable. Although sets are mutable, the elements of sets must be immutable.
We can create a set by putting objects separated by a comma in curly braces.
a = {1, 4, 'foo'}print(type(a))<class 'set'>
Sets do not contain repeated elements so even if we try to add same elements more than once, the resulting set will contain unique elements.
a = {1, 4, 'foo', 4, 'foo'}print(a){1, 4, 'foo'}
towardsdatascience.com
Tuple is a collection of values separated by comma and enclosed in parenthesis. Unlike lists, tuples are immutable. The immutability can be considered as the identifying feature of tuples.
Tuples consist of values in parenthesis and separated by comma.
a = (3, 4)print(type(a))<class 'tuple'>
We can also create tuples without using the parenthesis. A sequence of values separated by comma will create a tuple.
a = 3, 4, 5, 6print(type(a))<class 'tuple'>
One of the most common use cases of tuples is with functions that return multiple values.
import numpy as npdef count_sum(arr):   count = len(arr)   sum = arr.sum()   return count, sumarr = np.random.randint(10, size=8)a = count_sum(arr)print(a)(8, 39)print(type(a))<class 'tuple'>
towardsdatascience.com
Lambda expressions are special forms of functions. In general, lambda expressions are used without a name.
Consider the following function that returns the square of a given number.
def square(x):   return x**2
The equivalent lambda expression is:
lambda x: x ** 2
Consider an operation that needs to be done once or very few times. Furthermore, we have many variations of this operation which are slightly different than the original one. In such case, it is not ideal to define a separate function for each operation. Instead, lambda expressions provide a much more efficient way of accomplishing the tasks.
towardsdatascience.com
We have covered some key concepts and topics of Python. Most data science related tasks are done through third-party libraries and frameworks such as Pandas, Matplotlib, Scikit-learn, TensorFlow, and so on.
However, we should have a comprehensive understanding of basic operations and concepts of Python in order to efficiently use such libraries. They assume you are familiar with the basics of Python.
Thank you for reading. Please let me know if you have any feedback.