Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
36 views

Python For Data Science - The Basics - Data Science Parichay

The document introduces Python basics for data science, including Jupyter notebooks, expressions, variables, data types, and operators. It provides examples of writing a simple Python program and recommends further reading.

Uploaded by

pratham agrasen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Python For Data Science - The Basics - Data Science Parichay

The document introduces Python basics for data science, including Jupyter notebooks, expressions, variables, data types, and operators. It provides examples of writing a simple Python program and recommends further reading.

Uploaded by

pratham agrasen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

This website uses cookies to improve your experience.

We'll assume you're okay with this, but you can


opt-out if you wish.

Cookie settings ACCEPT

Python for Data Science – The Basics

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

Common Input/Output Functions

Write your first program

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.

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.

Highlighted programs for you


SPONSORED ℹ

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

Arizona State University Online


Information
Security 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 .

Cookie settings ACCEPT


Variables
Variables are named memory locations used to store a value or a reference. For
simplicity, imagine variables as boxes where you can store items. Whenever you
need to use these “items” you can simply do that by accessing them through
their “boxes”.

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.

Rules for variable names: Variable names should be descriptive. Something,


that conveys what sort of value it contains. It’s up to you on what you want to
name your variables. But, it has to follow the below python naming rules:
1.website
This It should
usesbe a single
cookies word your
to improve without any spaces.
experience. We'll assume you're okay with this, but you can
opt-out if you wish.
2. It can contain only alphabets, numbers, and the underscore _ special
Cookie settings
character. ACCEPT

3. It can not begin with a number.

The below table shows examples of some valid and invalid variable names in
Python.

Valid Names Invalid Names

rick 8rick

R82 82

rick_morty rick morty

_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. .

Cookie settings ACCEPT


String Type: A string is a sequence of unicode characters. String values are
enclosed in single '' or double "" quotes. Example, name = "Rick" .

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:

Arithmetic Operators: These operators are used for mathematical


computations like addition, subtraction, multiplication, etc. They generally
operate on numerical values and return a numerical value.

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.

Cookie settings ACCEPT

Logical Operators: These operators are used to perform logical and , or , and
not operations. They operate on boolean values and return a boolean value.

The Assignment Operator: In python, = is used as the assignment operator. It’s


used to assign values to variables. For example, x = 2 will assign the value 2 to
the variable x.
The assignment operator can be compounded with different arithmetic
operators, for example, x += 2 is the same as x = x + 2

NOTE: It’s important to keep in mind that == is a comparison operator and is


used as an equality check while = is the assignment operator used to assign
values
This to uses
website variables.
cookies to improve your experience. We'll assume you're okay with this, but you can
opt-out if you wish.
Operator Precedence: The order in which operators are executed in expressions
Cookie settings ACCEPT
is called the Operator Precedence. Similar to the mathematics, in Python, the
exponent operator, **, is evaluated first, the *, /, //, and % are evaluated next,
from left to right followed by the + and – operators, which are also evaluated left
to right. The evaluation order can be changed by the help of parenthesis ()
which have the highest precedence.

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.

In python, we use the # symbol to denote the beginning of a comment. Anything


coming after the # symbol on the same line is considered as a comment by the
interpreter.

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

Common Input/Output functions


Python comes with some standard functions that help us take input from the
user and display some output on the screen. One of these has been used in a
number of examples in this tutorial. Can you guess the function we’re referring
to?

Yes, it is the print function.


These
This built-in
website functions,
uses cookies namely,
to improve input() and
your experience. print()
We'll assume areokay
you're widely
withused foryou can
this, but
opt-out if youinput
standard wish. and standard output operations respectively.

Cookie settings ACCEPT


Input: The input() function is used to obtain input from the user. Whenever a call
is made to the input() function, the program execution is paused to allow the user
to type in an input. After the user presses the enter key, all the characters typed
are returned as a string.

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.

Write your first Program!


Now that we’ve covered the basics, it’s time to get your hands dirty. From the
concepts covered until now, try writing a program yourself. It can be something
as simple as writing a “Hello, World!” or something totally different. With the
input/output functions covered, you can make your program dynamic as well.

Here’s an example of a program that asks the user for her name and displays a
dynamic greeting.

1. # Wish the user


2. # Input the name of the user using the input function
3. name = input()
4. # Display a custom greetings message.
5. print("Hello, ", name)

The following is the result of running the above code in Jupyter.


Recommended Reading
This website uses cookies to improve your experience. We'll assume you're okay with this, but you can
opt-out if you wish.
With the topics covered in this tutorial we hope that you got an understanding of
Cookie settings ACCEPT
some of the fundamental building blocks of programming in python –
Expressions, Variables, Data Types, Operators, Comments, and Basic
Input/Output. If you’d like to dive deeper, we recommend the following (opening
available) resource to supplement the topics covered –

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:

Python for Data Science-Introduction

Python for Data Science – The Basics

Python for Data Science: Control flow Statements

Python for Data Science – Functions

Python for Data Science – Data Structures

Subscribe to our newsletter for more informative guides and tutorials.


We do not spam and you can opt out any time.

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.

Cookie settings ACCEPT

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.

View all posts  

Tags

comments data types expressions operators python variables

© Data Science Parichay


Contact • Disclaimer • Privacy Policy

You might also like