Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
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:
http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 2/15
2018/10/30 session1
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.
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:
How to use:
conda list
http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 5/15
2018/10/30 session1
for example if I want to create a Python 3.0 environment named py3 with pandas installed I could run
in the cmd.
remove an environment
use
activate env_name
deactivate
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.
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
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.
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:
Docstrings
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
EXAMPLE
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
http://localhost:8888/nbconvert/html/Desktop/Pythontraining/session1.ipynb?download=false 11/15
2018/10/30 session1
In [65]: x = 1
y = 2
x==y
Out[65]: False
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.
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!
Hello, World!
HELLO, WORLD!
hello, world!
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