THE PYTHON PROGRAMMING LANGUAGE
WHY PYTHON ?
WHY "PYTHON" ?
-
Monty Python Reference
"When he began implementing Python, Guido van Rossum was also reading the published scripts from “Monty Python's Flying Circus”, a BBC comedy series from the 1970s. Van Rossum thought he needed a name that was short, unique, and slightly mysterious, so he decided to call the language Python." -- General Python FAQ
- So nothing to do with reptiles... except for its logo:
WHY LEARNING PYTHON?
-
In general :
- Easy to learn
- Elegant yet simple and compact syntax
- Dynamic typing + interpreted language
- High-level data structures
- Object-Oriented Programming
- Many libraries
- Great for both scripting and software/app development
-
Free and open-source software, with vibrant community
-
Extensible: add new built-in modules to Python (written in C)
-
For Data Scientists :
- Very rich scientific computing libraries
-
All DS tasks can be performed with Python:
- accessing, collecting, cleaning, analysing, visualising data
- modelling, evaluating models, integrating in prod, scaling
PYTHON for ds
Components
-
Python
- Python language
- Modules of the standard library
- A large number of specialised modules and apps written in Python
- Development tools
-
IPython
- an interactive Python shell
- Jupyter Notebook: web application to create and share documents that contain live code, equations, visualizations and explanatory text
- NumPy: provides powerful numerical array objects, and routines to manipulate them
- SciPy: high-level data processing routines. Optimisation, regression, interpolation, etc.
- Matplotlib: 2- and 3-D visualisations and plots
- Pandas: powerful data structures especially to deal with time series
- Scikit-learn: Machine Learning toolbox
- statsmodels: statistical modelling toolbox (not covered in this course)
PYTHON 2 vs. PYthon 3
WHAT IS IT ABOUT?
-
2 major versions of Python in widespread use:
Python 2.x and Python 3.x - Some features in Python 3 are not backward compatible with Python 2
- Some Python 2 libraries have not been updated to work with Python 3
- I still mostly use Python 2 (although I also have Python 3 installed on my laptop) due to such libraries, but as more of them are migrating to Python 3, I'll soon use it full time
- Marek uses mostly Python 3 as all his libraries are ported
- Bottom-line: there is no wrong choice, as long as all the libraries you need are supported by the version you choose
- In this masterclass: we'll write Python3(-compatible) code
Examples
Text
# Python 2 only:
print 'Hello'
# Python 2 and 3:
print('Hello')
# Python 2 only:
print 'Hello', 'World'
# Python 2 and 3:
from __future__ import print_function
print('Hello', 'World')
- From print statements (Py2) to print function calls (Py3):
- From integer division to true division:
# Python 2 only:
assert 2 / 3 == 0
# Python 2 and 3:
assert 2 // 3 == 0
# Python 3 only:
assert 3 / 2 == 1.5
# Python 2 and 3:
from __future__ import division # (at top of module)
assert 3 / 2 == 1.5
- To explore and test more differences:
IPython Notebook by Sebastian Raschka
- To keep your code Python 2 and 3 compatible:
a comprehensive Cheat Sheet
INSTALLING PYthon
and all useful packages
several options
-
[RECOMMENDED] Anaconda (Windows, Linux, OSX) :
- For new Python users who want to install a full Python environment for Data Science
- Download it here
- Follow the simple installation instructions
- Manual installation with Python package manager :
- (Only) if you already have been using Python on your computer for some time
- Make sure to install: Python, pip and virtualenv
- Follow installation instructions for: OS X, Linux or Windows
- Once in your dedicated virtualenv for this masterclass, use pip to install of the necessary packages:
Warning: this might require some external dependencies to be installed. When in doubt: Google your error messages!https://www.anaconda.com/distribution/
pip install -U numpy, scipy, ipython, matplotlib, pandas, scikit-learn
RUNNING
THE IPYTHON interpreter
and a python file
your first python file
- Open your favorite editor
- Create a file hello_world.py
- Inside write the following:
- Now from your shell/terminal/console run:
- Then simply run the IPython interpreter:
- And from there run again your hello_world.py file:
- Explore further:
s = 'Hello World!'
print(s)
$ ipython hello_world.py
$ ipython
In [1]: %run hello_world.py
In [2]: s
In [3]: i = 2
In [4]: s2 = 'Hi'
In [5]: s+s2
JUPYTER notebook
in action
Start IPython Notebook:
$ jupyter notebook
Tutorial
More about Jupyter Notebooks... in this Jupyter Notebook,
and even more in this blog post.
Tip: Try cloning or downloading it and then opening it.
Run and modify its code cells.
PYthon basics
TUTORIAL
Read and test for yourself the examples provided in:
The SciPy Lectures -- The Python Language
Tip: Practice those examples using alternatively python files, the IPython interpreter and an IPython Notebook.
TO PRACTICE
Intro to Python -- The Python Programming Language
By utstikkar
Intro to Python -- The Python Programming Language
Second set of slides for the Introduction to Python (and useful libraries) masterclass at the Data Science Retreat.
- 1,650