DEV Community

pexpeter
pexpeter

Posted on • Updated on

Python 101: Introduction to Python for Data Science

Python logo

Python Definition

Python is an interpreted, high-level, general-purpose programming language. It was first released in 1991 by Guido van Rossum and has since become one of the most popular programming languages in the world. Its syntax has made it popular as its easy to learn and use.

Advantages of Python for Data Analysis

There are several data analysis preferred programming language including R, Stata and SAS.

Python is better compared to others due to:

  1. It's ease of use. It's syntax makes it easy to learn, write, and maintain code, even for beginners.
  2. Range of libraries: Python has a large number of libraries that provide a range of functionalities for data analysis, such as NumPy, Pandas, and Matplotlib.
  3. Open-source: Python is open-source, which means that it is freely available and can be used and modified by anyone.

Installing Python

Python application file can be accessed and downloaded from its main website for different operating systems. I will mainly use Windows for this article.

Welcome to Python.org

The official home of the Python Programming Language

favicon python.org

After installing python you have to choose an IDE (Integrated Development Environment) which is a software application that provides a comprehensive environment for software development.

The common IDE for data science are:

First Code

A successful setup of your code writing environment means you are ready to code. Python for data science requires several basic libraries to simplify your coding processes. They can be installed using a package manager used in python in your command prompt (cmd)by running the following:

  • NumPy
pip install numpy

Enter fullscreen mode Exit fullscreen mode
  • Pandas
pip install pandas
Enter fullscreen mode Exit fullscreen mode
  • Matplotlib
pip install matplotlib
Enter fullscreen mode Exit fullscreen mode
  • Scikit-Learn
pip install scikit-learn
Enter fullscreen mode Exit fullscreen mode

The libraries are installed for ease of use when dealing with tabular data (Pandas), arrays(NumPy), visualizations(Matplotlib) and Machine Learning (Scikit Learn). As you advance you come to know more libraries that come handy in your data science projects.

Using Python Libraries for Data Science

The installed libraries are not usable until they are called or modules in them are called. This is done easily through using the import and from library import module. Example:

#we use `as` as an alias so as to simplify our code
#pandas library
import pandas as pd
#numpy library
import numpy as np
#matplotlib library
import matplotlib.pyplot as plt
#scikit learn library
from sklearn.pipeline import make_pipeline
Enter fullscreen mode Exit fullscreen mode

As you may have noted, from is used to import a certain method or module from a library depending on the project you are working on.

Python Syntax

Operators in Python for Data Science

  1. Arithmetic operators: used for performing arithmetic operations such as addition(+), subtraction(-), multiplication(*), division(/), and modulus(%).
  2. Comparison operators: used for comparing two values and returning a Boolean value (True or False). They include equal to(==), not equal to(!=), greater than(>), less than(<), greater than or equal to(>=) and less than or equal to(<=).
  3. Logical operators: used for combining Boolean values and returning a Boolean result. These include logical AND, logical OR and logical NOT.
  4. Assignment operators: used for assigning a value to a variable and performing an operation on the variable at the same time. These include:
a = 5
a += 3    # equivalent to a = a + 3
a -= 2    # equivalent to a = a - 2
a *= 4    # equivalent to a = a * 4
a /= 2    # equivalent to a = a / 2
Enter fullscreen mode Exit fullscreen mode

Python Data Structures

Python comes with inbuilt data structures that enables data scientist store and manipulate data sets. They are the foundations that makes easy to integrate with the data science libraries.
The most common data structures are:

  • Lists:

A list is a collection of ordered elements, which can be of any data type. Example:

mylist = [1,2,3,4]

Enter fullscreen mode Exit fullscreen mode
  • Tuples:

A tuple is a collection of ordered elements, similar to a list. However, tuples are immutable, which means that once a tuple is created, its elements cannot be modified. Example:

mytuple = (1, 2, 3, 4, 5)
Enter fullscreen mode Exit fullscreen mode

-Dictionaries:

A dictionary is a collection of key-value pairs, where each key is associated with a value. Dictionaries are unordered and mutable, which means that you can add, remove, or modify key-value pairs in a dictionary. Example:

mydict= {"a": 2, "b": 3, "c": 4}
Enter fullscreen mode Exit fullscreen mode

These are the most commonly used data structures but others include sets and arrays.

Conclusion

Data science involves lot of projects from data collection to machine learning. The kind of project will dictate the kind of library and code to write. The most common data sources are apis, excel(flat databases), structured databases(SQL), unstructured databases(mongo) and mixed sometimes.

Python offers easy integration of data sources e.g pymongo library for mongodb databases, sqlite3 for sql databases and pandas for flat databases(excel, csv etc).

Examples:
-Pymongo

from pymongo import MongoClient
client = MongoClient(host="local host", port=27017)
Enter fullscreen mode Exit fullscreen mode

-sqlite3

import sqlite3
%load_ext.sql
%sql sqlite://path
Enter fullscreen mode Exit fullscreen mode

-pandas

df=pd.read_csv(filepath)
Enter fullscreen mode Exit fullscreen mode

The kind of data also will determine the type of code and libraries to install and use.

Its always advisable to come up with a clear plan of how to handle your project to avoid wrong method or libraries.

Data science with python is fun and easy to learn with dedication.

Thank you and for any clarification feel free to reach out.

Top comments (0)