Introduction to Python¶

-- for Economists and Statisticians

Feng Li

School of Statistics and Mathematics

Central University of Finance and Economics

feng.li@cufe.edu.cn

https://feng.li/python

Ecomists' old toolbox¶

Data¶

  • GDP, GNP, usually with fewer observations

Software¶

  • Economists have relied on Stata for over 30 years because of its breadth, accuracy, extensibility, and reproducibility.

  • 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.

Why Python?¶

  • Python is a widely used general-purpose, high-level programming language.

  • 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.

  • The language provides constructs intended to enable clear programs on both a small and large scale.

Python Features¶

The core philosophy of the language is summarized by the document "PEP 20 (The Zen of Python)" as

  • Beautiful is better than ugly
  • Explicit is better than implicit
  • Simple is better than complex
  • Complex is better than complicated
  • Readability counts

Python is compact and readable. Programs written in Python are typically much shorter than equivalent C, C++, or Java programs.

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.

Why we should use Python?¶

  • Open source!

  • Easy to learn and use with no programing experiences.

  • Rich ecosystem for modeling and data analysis

Fun fact!¶

The language is named after the BBC show Monty Python’s Flying Circus (1969-1974) and has nothing to do with reptiles.

Python modules¶

  • Python has a large standard library, commonly cited as one of Python's greatest strengths, providing tools suited to many tasks.

  • The standard library is not essential to run Python or embed Python within an application.

  • 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.

  • Notable applications: web scraping, scientific computing, text processing, image processing

Installing Python¶

  • Python is free software.

  • 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.

  • To install Python on other systems, check out the Python Setup and Usage section in Python help documentation.

Using Python 3¶

  • To start a Python interpreter, type the command in your terminal: python3.

  • 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().

fli@carbon:~$ python3
Python 3.9.7 (default, Sep 24 2021, 09:43:00) 
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Executing Python scripts¶

  • Write down your Python script and name it as hello.py with .py extension
    #!/usr/bin/env python3
    print('Hello World')
    
  • Go to your terminal, and run it with Python interpreter
    python3 hello.py
    
  • You could also make your script executable (like .exe file on Windows)
    chmod +x hello.py
    
  • Run the script in your terminal
    ./hello.py
    

Using interactive Python shell¶

IPython is a command shell for interactive computing in multiple programming languages

  • Interactive shells (terminal and Qt-based).
  • Jupyter notebook with support for code, text, mathematical expressions, inline plots and other media.
  • Support for interactive data visualization and use of GUI toolkits.
  • Flexible, embeddable interpreters to load into one's own projects.
  • Tools for parallel computing.

To start IPython interactive environment, type ipython in your terminal.

Getting Python help¶

  • Help for Python module help('module')

  • Help for Python function help(function).

  • Use q to quit the help session.

  • 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.

  • This is the official documentation is available at https://docs.python.org/3.9/

Text Editors/IDEs¶

To edit Python code, you just need a handy text editor. There are many available, check out the following pages

  • Spider: Anaconda's build-in editor
  • Visual Studio Code (https://code.visualstudio.com/Download)
  • Sublime Text (https://www.sublimetext.com/download)

Coding Style¶

  • Use 4-space indentation, and no tabs.

  • Wrap lines so that they don't exceed 79 characters.

  • Use blank lines to separate functions and classes, and larger blocks of code inside functions.

  • When possible, put comments on a line of their own.

  • Use docstrings.

  • Use spaces around operators and after commas, but not directly inside bracketing constructs: a = f(1, 2) + g(x=3, y=4).

  • use CamelCase for classes and lower_case_with_underscores for functions and methods.

  • Always use self as the name for the first method argument.

  • Do not use fancy encodings even if Python 3 has full UTF support. Plain ASCII works best in any case.