Python For Data Science - The Basics - Data Science Parichay
Python For Data Science - The Basics - Data Science Parichay
Hi, welcome to Python for Data Science, a short series of articles to help
beginners in Data Science learn the fundamentals of the python programming
language with a focus on Data Science use cases. There are no prerequisites, the
series aims to introduce learners in Data Science to python in as simple and as
no-nonsense way as possible.
This is the second article in the series. In our first article, we introduced how data
science is changing the world and why Python is preferred by a majority of data
science practitioners. By the end of THIS article, you’ll have an idea about the
basics of the python programming language. Also, you’ll write your very first
Python program!
Table of Contents
Introduction to the Jupyter Notebook Environment
Python Basics
Expressions
Variables
Data Types
Operators
Comments
Recommended Reading
Introduction to the Jupyter Notebook Environment
This website uses cookies to improve your experience. We'll assume you're okay with this, but you can
opt-out if you wish.
Jupyter Notebook is a web-based application to execute code, document, display
Cookie settings ACCEPT
visualizations, etc. all inside of a single notebook. And, it is this versatility that has
made Jupyter Notebooks one of the most popular tools among data science
practitioners.
A Jupyter notebook, simply, is a series of cells. Inside each of these cells, you can
either execute code or show some text. And, based on the input in a cell, we get
an appropriate output on execution. The following image shows a sample Jupyter
Notebook.
The introductory article in the series has the steps documented to install
Anaconda. Anaconda comes with tools like Jupyter, Spider, etc pre-installed
avoiding you the trouble of installing them individually. If you have Anaconda
installed, you can launch Jupyter Notebook from the Anaconda Navigator.
Purdue Global
Clouduses
This website Computing
cookies to&improve your experience. We'll assume you're okay with this, but you can
opt-out if you wish.
Solutions LEARN MORE
CookieBachelor's
settings ACCEPT
Information
Technology LEARN MORE
Bachelor's
Biomedical
Engineering LEARN MORE
Bachelor's
Liberty University
Information
Systems LEARN MORE
Associate's
STEM
Mathematics LEARN MORE
Associate's
If you’d like a quick refresher on Jupyter Notebooks, check out our article
Introduction to Jupyter Notebook. For the purpose of this tutorial series, we
recommend using Jupyter Notebooks as your go-to environment for executing
python code.
Python Basics
Python, as a programming language, is designed to have a very high level of code
readability. It’s also an interpreted language, meaning it runs your code line by
line without having to compile your entire code as is the case in languages like C
or Java.
This
This combination
website makes
uses cookies Python
to improve youran ideal candidate
experience. foryou're
We'll assume beginners who
okay with want
this, to get
but you can
opt-out if youwet
their feet wish.with programming without having to worry about complicated
syntax.
Cookie For instance,
settings ACCEPT this is how a program to print a “Hello World!” looks like in
C++, Java, and Python.
C++
#include <iostream>
int main() {
std::cout << "Hello, World!";
return 0;
}
Java
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Python
print("Hello, World!")
As you can see how simple and intuitive python is compared to C++ and Java.
While python in itself is a powerful object-oriented language with its own syntax
and standard library, for this article we’ll focus on its basic building blocks.
Expressions
A Python Expression is an instruction to the python interpreter to evaluate a
resulting value. Expressions consist of atoms and operators. Operators act
(operate) on the atoms, which are the individual units in the expression to
produce a resulting value. Variables, literals (constants), and function calls are
common types of atoms frequently used in expressions.
Sample Expression
Forwebsite
This example, 1+2 is to
uses cookies animprove
expression with + , the
your experience. We'lladdition operator,
assume you're acting
okay with on you
this, but thecan
opt-out if youvalues
constant wish. (also called literals) 1 and 2 to give the result 3 .
Find Schools
Age = 23
In the above example 23, (the item or the value) is stored in the variable Age (the
box). In python, the = operator is used for variable assignment. The syntax for
doing it is variable = value, for example, Age = 23 .
When a variable is assigned for the first time, it’s said to be initialized. This
variable can now be used in different expressions. If you assign a new value to
the variable, its older value is forgotten. For instance, if you run the command
Age = 45 , all further references to the variable Age would result in 45.
The below table shows examples of some valid and invalid variable names in
Python.
rick 8rick
R82 82
_rick ric$
Data Types
Python is a dynamically typed programming language. Meaning, you don’t need
to define the type of a variable beforehand. It determines the type during run-
time. But, what does it mean for a variable to have a data type?
Simply put, the data type of a variable characterizes the type of data that
particular variable stores. The data type is important because it tells the python
interpreter what sorts of operations can be associated with it. For example, it’s
logical to evaluate the expression 2+2 but it’s not logical to evaluate 2+'cat' .
Python has multiple built-in data types. But, for the purpose of our introduction,
we’ll be focusing on the following data types:
Find Schools
Numeric Types: Numeric data types are associated with numbers. Python uses
int as the data type for integers and float for real numbers (numbers with a
decimal
This websitepoint). Example,
uses cookies 13 would
to improve have intWe'll
your experience. as assume
its datayou're
typeokay
butwith
13.2this,
or even 13.0
but you can
opt-out
wouldifbe
you float
wish. .
NOTE: The behavior of an operator can change based on the types of the values
it’s operating on. For instance, the addition operator + when used with two
strings concatenates them together.
Operators
Operators are special symbols in python used to perform operations on variables
and values in expressions. These operations could be arithmetic, comparison,
logical, assignment, etc. Python has the following different types of operators:
Comparison Operators: These operators are used for comparing values. They
return a boolean value, either True or False .
This website uses cookies to improve your experience. We'll assume you're okay with this, but you can
opt-out if you wish.
Logical Operators: These operators are used to perform logical and , or , and
not operations. They operate on boolean values and return a boolean value.
There are other operators as well, like Bitwise, Identity, and Membership
operators in python. For more details on different operators in Python check out
Operators in Python.
Comments
Comments are parts in the code that are meant to be ignored by the Python
interpreter during execution. The comments are meant for humans. Writing clear
and informative comments is one of the best coding practices. It not only helps
you remember why you did what you did but also helps other developers
understand your code better.
1. # This is a comment
2. a = 2 + 2 # + 3 Anything coming after the # symbol on the same line
is ignored.
3. # In the above example, a evaluates to 4
Output: The print() function is used to display an output to the standard output
device, example, your screen. We can also print the output to a file.
Here’s an example of a program that asks the user for her name and displays a
dynamic greeting.
Chapter-1 of the book Automate the Boring Stuff with Python. This book is
specially designed to have an implementation first approach. The online
version of the book is freely available.
In the next article in this Python for Data Science series, we’d be covering
important flow of control constructs like conditionals and loops in python. Stay
curious and keep learning!
Here’s the complete list of articles of our five-part tutorial series on Python for
Data Science:
email address
SUBSCRIBE
Author
This website uses cookies to improve your experience. We'll assume you're okay with this, but you can
opt-out if you wish.
Piyush Raj
Piyush is a data professional passionate about using data to understand
things better and make informed decisions. He has experience working as
a Data Scientist in the consulting domain and holds an engineering degree
from IIT Roorkee. His hobbies include watching cricket, reading, and
working on side projects.
Tags