Subscribe to DSC Newsletter

Learn Python in 3 days : Step by Step Guide

Guest blog by Deepanshu Bhalla. 

This tutorial helps you to get started with Python. It's a step by step practical guide to learn Python by examples. Python is an open source language and it is widely used as a high-level programming language for general-purpose programming. It has gained high popularity in data science world. As data science domain is rising these days, IBM recently predicted demand for data science professionals would rise by more than 25% by 2020. In the PyPL Popularity of Programming language index, Python scored second rank with a 14 percent share. In advanced analytics and predictive analytics market, it is ranked among top 3 programming languages for advanced analytics.
..

Table of Contents

.

1. Getting Started with Python
  • Python 2.7 vs. 3.6
  • Python for Data Science
  • How to install Python?
  • Spyder Shortcut keys
  • Basic programs in Python
  • Comparison, Logical and Assignment Operators
2. Data Structures and Conditional Statements
  • Python Data Structures
  • Python Conditional Statements
3. Python Libraries
  • List of popular packages (comparison with R)
  • Popular python commands
  • How to import a package
4. Data Manipulation using Pandas
  • Pandas Data Structures - Series and DataFrame
  • Important Pandas Functions (vs. R functions)
  • Examples - Data analysis with Pandas
5. Data Science with Python
  • Logistic Regression
  • Decision Tree
  • Random Forest
  • Grid Search - Hyper Parameter Tuning
  • Cross Validation
  • Preprocessing Steps
.
1. Getting Started with Python
.
Python 2.7 vs 3.6
Google yields thousands of articles on this topic. Some bloggers opposed and some in favor of 2.7. If you filter your search criteria and look for only recent articles (late 2016 onwards), you would see majority of bloggers are in favor of Python 3.6. See the following reasons to support Python 3.6.

1. The official end date for the Python 2.7 is year 2020. Afterward there would be no support from community. It does not make any sense to learn 2.7 if you learn it today.

2. Python 3.6 supports 95% of top 360 python packages and almost 100% of top packages for data science.

What's new in Python 3.6
It is cleaner and faster. It is a language for the future. It fixed major issues with versions of Python 2 series. Python 3 was first released in year 2008. It has been 9 years releasing robust versions of Python 3 series.
.
Python for Data Science
Python is widely used and very popular for a variety of software engineering tasks such as website development, cloud-architecture, back-end etc. It is equally popular in data science world. In advanced analytics world, there has been several debates on R vs. Python. There are some areas such as number of libraries for statistical analysis, where R wins over Python but Python is catching up very fast. With popularity of big data and data science, Python has become first programming language of data scientists.

There are several reasons to learn Python. Some of them are as follows -
  1. Python runs well in automating various steps of a predictive model. 
  2. Python has awesome robust libraries for machine learning, natural language processing, deep learning, big data and artificial Intelligence. 
  3. Python wins over R when it comes to deploying machine learning models in production.
  4. It can be easily integrated with big data frameworks such as Spark and Hadoop.
  5. Python has a great online community support.
Do you know these sites are developed in Python?
  1. YouTube
  2. Instagram
  3. Reddit
  4. Dropbox
  5. Disqus
How to Install Python
There are two ways to download and install Python
  1. Download AnacondaIt comes with Python software along with preinstalled popular libraries.
  2. Download Python from its official website. You have to manually install libraries.

Recommended : Go for first option and download anaconda. It saves a lot of time in learning and coding Python

Coding Environments
Anaconda comes with two popular IDE :
  1. Jupyter (Ipython) Notebook
  2. Spyder
Spyder. It is like RStudio for Python. It gives an environment wherein writing python code is user-friendly. If you are a SAS User, you can think of it as SAS Enterprise Guide / SAS Studio. It comes with a syntax editor where you can write programs. It has a console to check each and every line of code. Under the 'Variable explorer', you can access your created data files and function. I highly recommend Spyder!
.

Spyder - Python Coding Environment

Jupyter (Ipython) Notebook
Jupyter is equivalent to markdown in R. It is useful when you need to present your work to others or when you need to create step by step project report as it can combine code, output, words, and graphics.
.
Spyder Shortcut Keys
The following is a list of some useful spyder shortcut keys which makes you more productive.
  1. Press F5 to run the entire script
  2. Press F9 to run selection or line 
  3. Press Ctrl + 1 to comment / uncomment
  4. Go to front of function and then press Ctrl + I to see documentation of the function
  5. Run %reset -f to clean workspace
  6. Ctrl + Left click on object to see source code 
  7. Ctrl+Enter executes the current cell.
  8. Shift+Enter executes the current cell and advances the cursor to the next cell

List of arithmetic operators with examples

Arithmetic Operators Operation Example
+ Addition 10 + 2 = 12
Subtraction 10 – 2 = 8
* Multiplication 10 * 2 = 20
/ Division 10 / 2 = 5.0
% Modulus (Remainder) 10 % 3 = 1
** Power 10 ** 2 = 100
// Floor 17 // 3 = 5
(x + (d-1)) // d Ceiling (17 +(3-1)) // 3 = 6

Basic Programs
.
Example 1
#Basics
x = 10
y = 3
print("10 divided by 3 is", x/y)
print("remainder after 10 divided by 3 is", x%y)
Result :
10 divided by 3 is 3.33
remainder after 10 divided by 3 is 1
Example 2
x = 100
x > 80 and x <=95
x > 35 or x < 60
x > 80 and x <=95 Out[45]: False
x > 35 or x < 60 Out[46]: True 
Comparison & Logical Operators Description Example
> Greater than 5 > 3 returns True
/td> Less than 5 < 3 returns False
>= Greater than or equal to 5 >= 3 returns True
<= Less than or equal to 5 <= 3 return False
== Equal to 5 == 3 returns False
!= Not equal to 5 != 3 returns True
and Check both the conditions x > 18 and x <=35
or If atleast one condition hold True x > 35 or x < 60
not Opposite of Condition not(x>7)

Assignment Operators
It is used to assign a value to the declared variable. For e.g. x += 25 means x = x +25.
x = 100
y = 10
x += y
print(x)
print(x) 110
In this case, x+=y implies x=x+y which is x = 100 + 10.
Similarly, you can use x-=y, x*=y and x /=y
.
2. Data Structures and Conditional Statements
.
Python Data Structure
In every programming language, it is important to understand the data structures. Following are some data structures used in Python.

1. List
It is a sequence of multiple values. It allows us to store different types of data such as integer, float, string etc. See the examples of list below. First one is an integer list containing only integer. Second one is string list containing only string values. Third one is mixed list containing integer, string and float values.
  1. x = [1, 2, 3, 4, 5]
  2. y = [‘A’, ‘O’, ‘G’, ‘M’]
  3. z = [‘A’, 4, 5.1, ‘M’]
Get List Item
We can extract list item using Indexes. Index starts from 0 and end with (number of elements-1).
x = [1, 2, 3, 4, 5]
x[0]
x[1]
x[4]
x[-1]
x[-2]
x[0] Out[68]: 1  x[1] Out[69]: 2  x[4] Out[70]: 5  x[-1] Out[71]: 5  x[-2] Out[72]: 4 

x[0] picks first element from list. Negative sign tells Python to search list item from right to left. x[-1] selects the last element from list.

You can select multiple elements from a list using the following method
x[:3] returns [1, 2, 3]

2. Tuple
A tuple is similar to a list in the sense that it is a sequence of elements. The difference between list and tuple are as follows -
  1. A tuple cannot be changed once created whereas list can be modified.
  2. A tuple is created by placing comma-separated values inside parentheses ( ). Whereas, list is created inside square brackets [ ]
Examples
K = (1,2,3)
City = ('Delhi','Mumbai','Bangalore')
Perform for loop on Tuple
for i in City:
    print(i)
Read more (sections 3-5) here..

Views: 65814

Comment

You need to be a member of Data Science Central to add comments!

Join Data Science Central

Comment by Shixiang Wang yesterday

Nice

Follow Us

Videos

  • Add Videos
  • View All

Resources

© 2017   Data Science Central   Powered by

Badges  |  Report an Issue  |  Privacy Policy  |  Terms of Service