<jupyter_start><jupyter_text>Introduction to PythonThis notebook is primarily focused on introducing the specifics of using Python in an interactive environment such as Datalab. It is not intended to provide a complete tutorial to Python as a language. If you're completely new to Python, no problem! Python is quite straightforward, and there are lots of resources. The interactive step-by-step material at [Codecademy](https://www.codecademy.com/tracks/python) might be of interest.To get started, below is a code cell that contains a Python statement. You can run it by pressing `Shift+Enter` or clicking the the `Run` toolbar button with the cell selected.<jupyter_code>print("Hello World")<jupyter_output>Hello World<jupyter_text>You can edit the cell above and re-execute it to iterate over it. You can also add additional code cells to enter new blocks of code.<jupyter_code>import sys

number = 10

def square(n):
    return n * n<jupyter_output><empty_output><jupyter_text>The cell above created a variable named `number` and a function named `square`, and placed them into the _global namespace_. It also imported the `sys` module into the same namespace. This global namespace is shared across all the cells in the notebook.As a result, the following cell should be able to access (as well as modify) them.<jupyter_code>print('The number is currently %d' % number)
number = 11
sys.stderr.write('And now it is %d' % number)

square(number)<jupyter_output>The number is currently 10<jupyter_text>By now, you've probably noticed a few interesting things about code cells:* Upon execution, their results are shown inline in the notebook, after the code that produced the results. These results are included into the saved notebook. Results include outputs of print statements (text that might have been written out to stdout as well as stderr) and the final result of the cell.* Some code cells do not have any visible output.* Code cells have a distinguishing border on the left. This border is a washed out gray color when the notebook is first loaded, indicating that a cell has not been run yet; the border changes to a filled blue border after the cell runs.  Getting Help Python APIs are usually accompanied by documentation. You can use `?` to invoke help on a class or a method. For example, execute the cells below:<jupyter_code>str?
g = globals()
g.get?<jupyter_output><empty_output><jupyter_text>When run, these cells produce docstring content that is displayed in the help pane within the sidebar. The code cells also provide auto-suggest. For example, press `Tab` after the '.' to see a list of members callable on the `g` variable that was just declared.<jupyter_code># Intentionally incomplete for purposes of auto-suggest demo, rather than running unmodified.
g.<jupyter_output><empty_output><jupyter_text>Function signature help is also available. For example, press `Tab` in the empty parentheses below.<jupyter_code>str()<jupyter_output><empty_output><jupyter_text>Note that help in Python relies on the interpreter being able to resolve the type of the expression that you are invoking help on.If you have not yet executed code, you may be able to invoke help directly on the class or method you're interested in, rather than the variable itself. Try this.<jupyter_code>import datetime

datetime.datetime?<jupyter_output><empty_output><jupyter_text>Python Libraries Datalab includes the standard Python library and a set of libraries that you can easily import. Most of the libraries were installed using `pip`, the Python package manager, or `pip3` for Python 3.<jupyter_code>%%bash
pip list --format=columns<jupyter_output>Package                                         Version       
----------------------------------------------- --------------
appdirs                                         1.4.2         
attrs                                           16.3.0        
Automat                                         0.5.0         
avro                                            1.8.1         
backports-abc                                   0.5           
backports.shutil-get-terminal-size              1.0.0         
beautifulsoup4                                  4.5.3         
bleach                                          1.5.0         
brewer2mpl                                      1.4.1         
bs4                                             0.0.1         
certifi                                         2017.1.23     
cffi                                            0.8.6         
chardet                                         2.3.0         
cloudml                                         0.1.9.1[...]<jupyter_text>If you have suggestions for additional packages to include, please submit feedback proposing the inclusion of the packages in a future version.  Installing a Python Library You can use `pip` to install your own Python 2 libraries, or `pip3` to install Python 3 libraries.Keep in mind that this will install the library within the virtual machine instance being used for Datalab, and the library will become available to all notebooks and all users sharing the same instance.The library installation is temporary. If the virtual machine instance is recreated, you will need to reinstall the library.The example, below, installs [scrapy](http://scrapy.org/), a library that helps in scraping web content.<jupyter_code>%%bash
apt-get update -y
apt-get install -y -q python-dev python-pip libxml2-dev libxslt1-dev zlib1g-dev libffi-dev libssl-dev
pip install -q scrapy<jupyter_output>Hit http://security.debian.org jessie/updates InRelease
Hit http://ftp.us.debian.org testing InRelease
Ign http://deb.debian.org jessie InRelease
Hit http://deb.debian.org jessie-updates InRelease
Get:1 http://security.debian.org jessie/updates/main amd64 Packages [444 kB]
Hit http://deb.debian.org jessie Release.gpg
Hit http://deb.debian.org jessie Release
Get:2 http://ftp.us.debian.org testing/main Sources [8936 kB]
Get:3 http://deb.debian.org jessie-updates/main amd64 Packages [17.6 kB]
Get:4 http://deb.debian.org jessie/main amd64 Packages [9049 kB]
Fetched 18.4 MB in 14s (1279 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
libffi-dev is already the newest version.
libxml2-dev is already the newest version.
libxslt1-dev is already the newest version.
python-dev is already the newest version.
python-pip is already the newest version.
zlib1g-dev is already the newest version.
libssl-dev is already the newest version.
0[...]<jupyter_text>Inspecting the Python evironment by running `pip list`, we should now see that Scrapy is installed and ready to use.<jupyter_code>%%bash
pip list --format=columns<jupyter_output>Package                                         Version       
----------------------------------------------- --------------
appdirs                                         1.4.2         
attrs                                           16.3.0        
Automat                                         0.5.0         
avro                                            1.8.1         
backports-abc                                   0.5           
backports.shutil-get-terminal-size              1.0.0         
beautifulsoup4                                  4.5.3         
bleach                                          1.5.0         
brewer2mpl                                      1.4.1         
bs4                                             0.0.1         
certifi                                         2017.1.23     
cffi                                            0.8.6         
chardet                                         2.3.0         
cloudml                                         0.1.9.1[...]
StarcoderdataJupyterStructuredCleanDedup