Você está na página 1de 15

2018/10/30 session1

Python Traning Part 1: The introduction of


Python for Data Science

Session 1: Python Basics

The introduction of python

What is Python?
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.
Python was created by Guido van Rossum and was first released in 1991.
Python was named 2017’s IEEE Spectrum number one programming language. Stack Overflow data
from 2011 to 2017 demonstrates a steady upward trajectory in question views for Python, and their
forecasted growth also puts Python out above other programming languages.

What can Python do?


Python can be used on a server to create web applications.
Python can be used alongside software to create workflows.
Python can connect to database systems. It can also read and modify files.
Python can be used to handle big data and perform complex mathematics.
Python can be used for rapid prototyping, or for production-ready software development.

Why Python?
Python Has a Healthy, Active and Supportive Community.
Python Has Amazing Libraries.
Python Has Big Data. It is one of the most popular languages used in data science, second only to R.
It’s also being used for machine learning and AI systems and various modern technologies.
Python Is Reliable and Efficient.
Python Is Accessible. Python has a simple syntax similar to the English language. Python has syntax
that allows developers to write programs with fewer lines than some other programming languages. it’s
a great language for beginners.

Before Started:
The most recent major version of Python is Python 3. However, Python 2 will be used in the session.
An example of the difference between Python 2.X and Python 3.X:
"print": In 2.X: print "The answer is", 2; In 3.X: print("The answer is", 2)

http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 1/15
2018/10/30 session1

How to install:

1. Installing Python 2.7.15 (for windows)


downloading the Installer from:https://www.python.org/downloads/release/python-2715/
(https://www.python.org/downloads/release/python-2715/)

follow the instruction

http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 2/15
2018/10/30 session1

select all options including add python.exe to Path

verifying the installation: type 'python' in cmd, the output should be as below.

http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 3/15
2018/10/30 session1

2. Installing pip
pip is s recommended tool for installing Python packages. With PIP, you could install other package
using the command 'pip install (packagename) '
check if you already installed pip: type 'pip -V' or 'pip --version' in cmd. if pip is ready, the output should
be as below.

if not, please go to https://pypi.org/project/pip/ (https://pypi.org/project/pip/) and follow the


instruction.

3. Anaconda (Recommend)
Anaconda is a free and open source
distribution of the Python and R
programming languages for data
science and machine learning
related applications (large-scale
data processing, predictive
analytics, scientific computing),
that aims to simplify package
management and deployment. Package versions are managed by the package management system conda.

Easily install 1,400+ data science packages for Python/R and manage your packages,
dependencies, and environments—all with the single click of a button. Free and open
source.----Anaconda official site

http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 4/15
2018/10/30 session1

How to install:

Please go to https://www.anaconda.com/download/ (https://www.anaconda.com/download/) to


download the suitable installer.
Follow the instruction. You could refer http://docs.anaconda.com/anaconda/install/windows/
(http://docs.anaconda.com/anaconda/install/windows/) if any problems occur.
optional: type

conda upgrade --all


in the cmd after installation to upgrade all packages.

How to use:

install,upgrade and remove a package:

conda install package_name

conda update package_name

conda remove package_name

list all installed package:

conda list

search for a package:

http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 5/15
2018/10/30 session1

conda search search_term

create an environment with a list of needed packages:

conda create -n env_name list of packages

for example if I want to create a Python 3.0 environment named py3 with pandas installed I could run

conda create -n py3 python=3.0 pandas

in the cmd.
remove an environment

conda env remove -n env_name

list all environment

conda env list

use

activate env_name

deactivate

to enter and exit an environment in windows


Share your environment:

conda env export > environment.yaml

conda env create -f environment.yaml

4. Python IDEs
While writing python code, integrating modules and libraries to build large systems, a simple text editor is not
enough—we need a good integrated development environment for that.

Here are some recommedations:

Pycharm
Spyder (Aanaconda will install this in your computer)
Sublime Text
Eclipse with PyDev

http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 6/15
2018/10/30 session1

Let's get started

1. Encoding Chinese
ASCII is default in Python 2.X. If no specific decoding declared, you may have problems dealing with Chinese
charecters. Please add
# -/*- coding: UTF-8 -/*-
on the top of your script to declare the encoding.

In [56]: print "Hello, World!"


Hello, World!

2. Python Syntax
Keywords, Identifiers
Keywords are special words which are reserved and have a specific meaning. Python has a
set of keywords that cannot be used as variables in programs.
All keywords in Python are case sensitive.
The Python keywords are listed below

Indentations

Where in other programming languages the indentation in code is for readability only, in Python the indentation is
very important. Python uses indentation to indicate a block of code.
Example:

In [57]: if 5 > 2:
print "Five is greater than two!"
Five is greater than two!

http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 7/15
2018/10/30 session1

In [58]: if 5 > 2:
print "Five is greater than two!"
File "<ipython-input-58-1e0bf9659808>", line 2
print "Five is greater than two!"
^
IndentationError: expected an indented block
In [59]: if 5 > 2:
print "Five is greater than two!"
print "Five is greater than two!"
File "<ipython-input-59-0bb7b28e5645>", line 3
print "Five is greater than two!"
^
IndentationError: unindent does not match any outer indentation level
Comments

Python has commenting capability for the purpose of in-code documentation. Comments start with a #, and
Python will render the rest of the line as a comment:

In [60]: #This is a comment.


print "Hello, World!"
Hello, World!

Docstrings

Python also has extended documentation capability, called docstrings.


Docstrings can be one line, or multiline.
Python uses triple quotes at the beginning and end of the docstring:

In [61]: """This is a
multiline docstring."""
print "Hello, World!"
Hello, World!

http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 8/15
2018/10/30 session1

3. Numeric Data Type


integer
float
long
complex

EXAMPLE

Calculation and function

http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 9/15
2018/10/30 session1

In [62]: x = 4
print 'the type of x is', type(x)
y = -9.77
print 'the type of y is', type(y)
z= 3+5j
print 'the type of z is', type(z)
the type of x is <type 'int'>
the type of y is <type 'float'>
the type of z is <type 'complex'>

In [63]: x = 4
y = -9.77
print
abs(y)
print
pow(x,y)
print
cmp(x,y)
import math
print math.fabs(y)
9.77
1.3118189031e-06
1
9.77

In [64]: z = 4.66
print
int(z)

4. Boolean
Boolean is another important type in Python. A Boolean can take on two values.The first value is true, just
remember we use an uppercase T.Boolean values can also be false, with an uppercase F. Using the type
command on a Boolean value, we obtain the term bool, this is short for Boolean. If we cast a Boolean true to an
integer or float, we will get a 1. If we cast a Boolean false to an integer or float, we get a zero. If you cast a 1 to a
Boolean, you get a true. Similarly, if you cast a 0 to a Boolean, you get a false.

http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 10/15
2018/10/30 session1

5.Python Operators
Python Arithmetic Operators

Python Assignment Operators

Python Comparison Operators

http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 11/15
2018/10/30 session1

Python Logical Operators

Python Identity Operators

Python Membership Operators

Python Bitwise Operators


http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 12/15
2018/10/30 session1

In [65]: x = 1
y = 2
x==y

Out[65]: False

In [66]: x < 2 and x>1


Out[66]: False

In [67]: print 9//2


print -9//2
print 9%2
4
-5
1

http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 13/15
2018/10/30 session1

6. String
Strings are amongst the most popular types in Python. We can create them simply by enclosing
characters in quotes.
Python treats single quotes the same as double quotes. But need to keep consistent.
If you want to use single quotes in a string generated with double quotes, just do it. But if you want to
use double quotes in double quotes, you need to use escape character. Vice versa.

In [68]: pythonstr = 'hello world!'


pythonstr1 = "hello world!"
pythonstr2 = "'hello,world!'"
print
pythonstr,pythonstr1
print
pythonstr2

hello world! hello world!


'hello,world!'

Slice and index

S = “PYTHON!”

s[start]
s[start : end]
s[start : end : step]

http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 14/15
2018/10/30 session1

In [69]: S="PYTHON!"
print
len(S)
print
S[0]
print
S[0:3]
print
S[-1]
print
S[-3:-1]
print
S[0:5:2]
print
S[::2]

7
P
PYT
!
ON
PTO
PTO!

In [70]: a = " Hello, World! "


print
a.strip()
print
a.upper()
print
a.lower()

Hello, World!
HELLO, WORLD!
hello, world!

In [71]: A="Python"+ ' is the best'


B=A.replace('Python', 'C')
B

Out[71]: 'C is the best'

In [72]: S="PYTHON!"
S.find('YT')

Out[72]: 1

In [73]: 3*S

Out[73]: 'PYTHON!PYTHON!PYTHON!'

In [75]: a='1'
b='2'
print a+b
12

In [74]: a=1
b=2
print a+b
3

http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 15/15

Você também pode gostar