Data Science Python

[Pages:24]Data Science Python

Anaconda

Python 3.x Includes ALL major Python data science packages

Sci-kit learn Pandas PlotPy Jupyter Notebooks



Python - simple commands

Python is an interactive interpreter started from the shell:

lutz$ python Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:53:06) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 3 + 10.5 13.5 >>> 7/2 3.5 >>> print("hello world!") hello world! >>>

Python - loading and running a file

Python is also an environment to run program files:

Assume that we have the following Python program file `helloworld.py':

""" helloworld.py

This is the classic program every programmer writes when he or she learns a new programming language. """

def hello(): "Just print 'hello world!' and that's it" print("hello world!") # print inserts a newline char

Red - docstring Green - comment

Python - loading and running a file

Docstring vs Comment

A docstring should document what your code does

Important for the user of your code Docstrings are exported by Python into the help system

A comment should comment on how your code does it

Important for your peer programmers modifying/understanding your code Comments stay internal to the code

Python - loading and running a file

Load the file and run the function in Python:

lutz$ python Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:53:06) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import helloworld >>> helloworld.hello() hello world! >>>

Functions belong to modules - if you want to execute a function in a module you have to provide the module name as a qualifier!

Python - loading and running a file

The `help' command

Docstrings shine!!! - automatically generated documentation of your module

>>> import helloworld >>> helloworld.hello() hello world! >>> help(helloworld)

Help on module helloworld:

NAME helloworld - helloworld.py

DESCRIPTION This is the classic program every programmer writes when he or she learns a new programming language.

FUNCTIONS hello() Just print 'hello world!' and that's it

FILE /home/lutz/Documents/CSC310/Python/slideset-1/helloworld.py

Python - import * considered dangerous

`from import *'

Any function or variable in is imported into your local scope WITHOUT a module qualifier:

lutz@ip-172-31-26-47:~/Documents/CSC310/Python/slideset-1$ python

Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:53:06)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> from helloworld import *

>>> hello()

hello world!

>>>

Very Dangerous! - it can lead to silent name clashes with

strange effects on your code!

Python - import * considered dangerous

Assume we have another file that defines the function `hello()' then:

Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:53:06)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux

"""

Type "help", "copyright", "credits" or "license" for more infhoermlloaatigoani.n.py

>>> from helloworld import *

>>> from helloagain import * >>> hello() hello again!

Here we demonstrate that Python silently clobbers names clashes if you are not careful. """

>>>

Silently overwrote the original hello() - the original is no longer available!!

def hello(): "Print out 'hello again!' and that's it" print("hello again!")

Never use `from import *' - you have no control over your name space!

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download