Mastering Python for Data Analysis: A Comprehensive Guide

Part 1

Introduction

In this article, we will explore the basic concepts in Python. Python is a high-level programming language that is applicable across diverse domains. Python is flexible and easy to learn. Its application spans across areas like building applications, data science, machine learning, etc. Python has a large community support which makes it quite easy to get support and solutions whenever you are stuck with a problem.

As a data analyst, the knowledge of Python is crucial for automating and accelerating the execution of tasks. Automating tasks, and running scripts that would take longer in SQL can be achieved faster using Python. Using Python makes tasks faster and more seamless.

Let's get straight to work.

Basic Python

print('I love python!')

The syntax above is a simple syntax of Python.

Let's break it down:

print is a built-in Python function that helps Python display or output information to your console or terminal.

The Parentheses and the quotes are part of the syntax structure.

Comments

Comments help to provide an explanation, temporary code removal, or any additional information within the code. They are not executed by a Python interpreter.

#Now this is a comment.
print('I love python')
"""This is a multiline comment.
For commenting multiple lines
"""

Documentation

Documentation helps to understand the purpose of a block of code or the intent of the programmer.

#This code will not be executed
#pirnt('data analyst use python now more than ever!')
#Documentation
#The above shows what comments do in python cod

You can study more about comments here

Data Types

Data types define the kind of values a variable can hold, and also the kinds of operations that can be performed on it.

You may be wondering what variables are...

Variables are like labeled containers. They also symbolize a space in the computer program's memory where data is stored and can also be called to reference that location. For example, suppose I want to store the number of pens in a variable, then I create a variable called number_of_pens and assign it a value.

number_of_pen = 20
#number_of_pen is a variable name. this case is also known as snake_case

Now that we understand what variables are, let's move on to DataType.

There are various types of data types in Python, let's explore some of them.

Number

Integer: Integer(negative, zero, and positive) numbers such as -1,-2,1,2...

Float: Decimal numbers such as -3.5, -2.25, -1.0, 0.0, 1.1, 2.2, 3.5 ...

Complex: such as 3 + j, 9 + 5j

You can read more about numberhere.

an_integer = 4
a_float = -5.90
a_complex = 3+j

Strings

This consists of one or more characters enclosed in single or double quotes. For more on strings, check here.

a_string = 'How are you today?'

Boolean

A boolean is a data type that is either true or false.

Read more on boolean from Python docs.

True #is this language python? if it is python then the value is true
false #is this language python? if it is not then the value is false

List

A list contains elements of different data types, enclosed in a square bracket, it can also hold an ordered collection of elements.

Lists in Python are mutable, meaning you can modify their contents by adding, removing, or changing elements.

For more on List, click here.

colors = ["Red", "Green", "Blue", "Yellow", "Purple"]
#the above contains an ordered collecttion of colors
diff_data_type = [1, "Hello", 3.14, True]
#the second shows that a list can data of different types.

Dictionary

A dictionary in Python is an unordered and mutable collection of data in a key-value pair format. They are defined using curly braces {}.

The key-value pairs are separated by colons, you can also check here for more on Dictionary.

employee_info = {
    "name": "Abishola",
    "age": 49,
    "dept": "Fiance",
    "Year_joined": 2020
}

Tuples

A tuple is an ordered collection of different data types just like a list but the difference between a list and a tuple is that tuples can not be modified once they are created, they are also defined using parentheses ().

Explore more on tuples here.

student_a = ("Abishola", 25, "Engineer")
scores= ( 30, 50, 60, 70, 70,65)

Set

Set in Python is an unordered collection of items, Set in Python stores unique items and is also defined by curly braces.

Primary Colors={Red, Blue, Yellow}
A={2,35,8,111,14}

In the example above, the order is not important.

Learn more about Set here.

Let's wrap it up

Now that we have been able to identify various data types, let's do a quick check, to validate data type.

To check the data type of a variable, we use the type function.

print(type(A))

This would output the type of data, also notice the statement, it's a built-in Python function*.*

Next Steps

In our next article, we will go into detail about functions, you can also download the Visual Studio code editor here to practice if you haven't done so.