{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Introduction to Python \n", "\n", "-- _for Economists and Statisticians_\n", "\n", "Feng Li\n", "\n", "School of Statistics and Mathematics\n", "\n", "Central University of Finance and Economics\n", "\n", "[feng.li@cufe.edu.cn](mailto:feng.li@cufe.edu.cn)\n", "\n", "[https://feng.li/python](https://feng.li/python)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Ecomists' _old_ toolbox\n", "\n", "\n", "### Data\n", "\n", "- GDP, GNP, usually with fewer observations\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Software\n", "\n", "\n", "- Economists have relied on **Stata** for over 30 years because of its breadth, accuracy, extensibility, and reproducibility. \n", "\n", "- Whether you are researching school selection, minimum wage, GDP, or stock trends, Stata provides all the statistics, graphics, and data-management tools needed to pursue a broad range of economic questions." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Why Python?\n", "\n", "- Python is a widely used general-purpose, high-level programming language. \n", "\n", "- Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages such as C++ or Java. \n", "\n", "- The language provides constructs intended to enable clear programs on both a small and large scale." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Python Features\n", "\n", "The core philosophy of the language is summarized by the document \"PEP 20 (The Zen of Python)\" as\n", "- Beautiful is better than ugly\n", "- Explicit is better than implicit\n", "- Simple is better than complex\n", "- Complex is better than complicated\n", "- Readability counts\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "\n", "**Python is compact and readable**. Programs written in Python are typically much shorter than equivalent C, C++, or Java programs.\n", "\n", "**Python is extensible**: if you know how to program in C, it is easy to add a new built-in function or module to the interpreter, either to perform critical operations at maximum speed." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Why we should use Python?\n", "\n", "- Open source!\n", "\n", "- Easy to learn and use with no programing experiences.\n", "\n", "- Rich ecosystem for modeling and data analysis" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Fun fact!\n", "\n", "The language is named after the BBC show `Monty Python’s Flying Circus` (1969-1974) and has nothing to do with reptiles." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Python modules\n", "\n", "- Python has a large standard library, commonly cited as one of Python's greatest strengths, providing tools suited to many tasks. \n", "\n", "- The standard library is **not essential** to run Python or embed Python within an application. \n", "\n", "- As of March 2018, the Python package Index (PyPI), the official repository for third-party Python software, contains over 130,000 packages with a wide range of functionality.\n", "\n", "- Notable applications: web scraping, scientific computing, text processing, image processing" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Installing Python\n", "\n", "\n", "- Python is free software.\n", "\n", "- On Windows and Mac, Anaconda (https://www.anaconda.com/) is the easiest way to perform Python data science and machine learning on a single machine.\n", "\n", "- To install Python on other systems, check out the [Python Setup and Usage section in Python help documentation](https://docs.python.org/3.9/using/index.html)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Using Python 3\n", "\n", "\n", "- To start a Python interpreter, type the command in your terminal: `python3`.\n", "\n", "- To terminate the Python interpreter, type an end-of-file character (`Control-D` on Unix, `Control-Z` on Windows) at the primary prompt. If that doesn’t work, you can exit the interpreter by typing the following command: `quit()`.\n", "\n", "```sh\n", "fli@carbon:~$ python3\n", "Python 3.9.7 (default, Sep 24 2021, 09:43:00) \n", "[GCC 10.3.0] on linux\n", "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", ">>>\n", "```" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "## Executing Python scripts\n", "\n", "- Write down your Python script and name it as `hello.py` with `.py` extension\n", " ```sh\n", " #!/usr/bin/env python3\n", " print('Hello World')\n", " ```\n", "- Go to your terminal, and run it with Python interpreter \n", " ```sh\n", " python3 hello.py\n", " ``` \n", "- You could also make your script _executable_ (like .exe file on Windows)\n", " ```sh\n", " chmod +x hello.py\n", " ```\n", "- Run the script in your terminal\n", " ```sh\n", " ./hello.py\n", " ```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Using interactive Python shell\n", "\n", "IPython is a command shell for interactive computing in multiple programming languages\n", "\n", "- Interactive shells (terminal and Qt-based).\n", "- Jupyter notebook with support for code, text, mathematical expressions, inline plots and other media.\n", "- Support for interactive data visualization and use of GUI toolkits.\n", "- Flexible, embeddable interpreters to load into one's own projects.\n", "- Tools for parallel computing.\n", "\n", "To start IPython interactive environment, type `ipython` in your terminal." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Getting Python help\n", "\n", "- Help for Python module `help('module')`\n", "- Help for Python function `help(function)`. \n", "- Use `q` to quit the help session.\n", "\n", "- With IPython, use the command `?function` or `function?`. `??function` or `function??` can also be used to type the entire function including both the `docstring` and the code.\n", "\n", "- This is the official documentation is available at https://docs.python.org/3.9/" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Text Editors/IDEs\n", "\n", "To edit Python code, you just need a handy text editor. There are many available, check out the following pages \n", "\n", "- Spider: Anaconda's build-in editor\n", "- Visual Studio Code (https://code.visualstudio.com/Download)\n", "- Sublime Text (https://www.sublimetext.com/download)\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Coding Style\n", "\n", "\n", "\n", "- Use 4-space indentation, and no tabs.\n", "\n", "- Wrap lines so that they don't exceed 79 characters.\n", "\n", "- Use blank lines to separate functions and classes, and larger blocks of code inside functions.\n", "\n", "- When possible, put comments on a line of their own." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- Use `docstrings`.\n", "\n", "- Use spaces around operators and after commas, but not directly inside bracketing constructs: `a = f(1, 2) + g(x=3, y=4)`.\n", "\n", "- use **CamelCase** for classes and **lower_case_with_underscores** for functions and methods. \n", "\n", "- Always use `self` as the name for the first method argument.\n", "\n", "- Do not use fancy encodings even if Python 3 has full UTF support. Plain ASCII works best in any case." ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.8" } }, "nbformat": 4, "nbformat_minor": 1 }