{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# About Python (关于Python)\n", "\n", "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.\n", "\n", "The language Python is named after the BBC show _Monty Python’s Flying Circus_ and has nothing to do with reptiles.\n", "\n", "Python supports multiple programming paradigms, including _object-oriented_, _imperative_ and _functional programming_ or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.\n", "\n", "\n", "Python was conceived in the late 1980s, and its implementation was started in December 1989 by Guido van Rossum at CWI in the Netherlands as a successor to the ABC language (itself inspired by SETL) capable of exception handling and interfacing with the Amoeba operating system. Van Rossum is Python's principal author, and his continuing central role in deciding the direction of Python is reflected in the title given to him by the Python community, benevolent dictator for life.\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Features (语言特征)\n", "\n", "The core philosophy of the language is summarized by the document \"PEP 20 (The Zen of Python)\", which includes aphorisms such as:\n", "\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", "Python enables programs to be written compactly and readably. Programs written in Python are typically much shorter than equivalent C, C++, or Java programs, for several reasons:\n", "\n", "- the high-level data types allow you to express complex operations in a single statement;\n", "- statement grouping is done by **indentation** instead of beginning and ending brackets;\n", "- no variable or argument declarations are necessary.\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, or to link Python programs to libraries that may only be available in binary form (such as a vendor-specific graphics library). Once you are really hooked, you can link the Python interpreter into an application written in C and use it as an extension or command language for that application.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Installing Python (安装Python)\n", "\n", "On Linux machine, Python is usually installed by default. To install Python on other systems, check out the [Python Setup and Usage section in Python help documentation](https://docs.python.org/2.7/using/index.html).\n", "\n", "If you are using Windows, [Anaconda](https://www.anaconda.com/download/) is a good choice." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Using Python (使用Python)\n", "\n", "The Python interpreter is usually installed as `/usr/bin/python` on those machines where it is available.\n", "\n", "- To start a Python interpreter, type the command in your terminal: `python`.\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", "```\n", "fli@carbon:~$ python\n", "Python 2.7.9 (default, Mar 1 2015, 12:57:24) \n", "[GCC 4.9.2] on linux2\n", "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n", ">>> exit()\n", "```" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Executing Python scripts (如何执行Python脚本)\n", "\n", "- Write down your Python script and name it as `hello.py` with `.py` extension\n", "\n", "- Your script contents look like this \n", " \n", " ```\n", " #!/usr/bin/python\n", " print('Hello World')\n", " ```\n", " \n", "- Go to your terminal, make your script _executable_\n", " \n", " ```\n", " chmod +x hello.py\n", " ```\n", "- Run the script in your terminal\n", "\n", " ```\n", " ./hello.py\n", " ```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Note** The line `#!/usr/bin/python` should appear at the very beginning of your file. Alternatively, you can let your environment variable to specify which Python to use as \n", "```\n", "#!/usr/bin/env python\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is possible to use encodings different than ASCII in Python source files. The best way to do it is to put one more special comment line right after the `#!` line to define the source file encoding:\n", "\n", "```\n", "# -*- coding: utf-8 -*-\n", "```\n", "\n", "Your `hello.py` will be able handle more complicated texts. By using UTF-8, characters of most languages in the world can be used simultaneously in string literals and comments. Using non-ASCII characters in identifiers is not supported. To display all these characters properly, your editor must recognize that the file is UTF-8, and it must use a font that supports all the characters in the file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using IPython shell (Python交互式编程环境)\n", "\n", "IPython is a command shell for interactive computing in multiple programming languages, originally developed for the Python programming language, that offers introspection, rich media, shell syntax, tab completion, and history. IPython provides the following features:\n", "\n", "- Interactive shells (terminal and Qt-based).\n", "- A browser-based 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", "\n", "To start iPython interactive environment, type `ipython` in your terminal." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Getting help (获取帮助)\n", "\n", "- Help is available in Python sessions using `help(function)` . \n", "\n", "- Some functions (and modules) have very long help files. When using IPython, these can be paged using the command `?function` or `function?` so that the text can be scrolled using page up and down and `q` to `quit`. `??function` or `function??` can be used to type the entire function including both the `docstring` and the code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Libraries (Python库)\n", "\n", "Python has a large standard library, commonly cited as one of Python's greatest strengths, providing tools suited to many tasks. This is deliberate and has been described as a \"batteries included\" Python philosophy. For Internet-facing applications, a large number of standard formats and protocols are supported. Modules for creating graphical user interfaces, connecting to relational databases, pseudo random number generators, arithmetic with arbitrary precision decimals, manipulating regular expressions, and doing unit testing are also included.\n", "\n", "Some parts of the standard library are covered by specifications, but the majority of the modules are not. They are specified by their code, internal documentation, and test suite (if supplied). However, because most of the standard library is cross-platform Python code, there are only a few modules that must be altered or completely rewritten by alternative implementations.\n", "\n", "The standard library is not essential to run Python or embed Python within an application. Blender 2.49, for instance, omits most of the standard library.\n", "\n", "As of August 2015, the Python Package Index, the official repository of third-party software for Python, contains more than 65,000 packages offering a wide range of functionality, including:\n", "\n", "- graphical user interfaces, web frameworks, multimedia, databases, networking and communications\n", "- test frameworks, automation and web scraping, documentation tools, system administration\n", "- scientific computing, text processing, image processing\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Text Editors/IDEs (Python编辑器)\n", "\n", "To edit Python code, you just need a handy text editor. There are many available, check out the following pages \n", "\n", "- [Text editors in Python Wiki](https://wiki.python.org/moin/PythonEditors)\n", "- [Comparison_of_text_editors in Wikipedia](https://en.wikipedia.org/wiki/Comparison_of_text_editors)\n", "- [Integrated Development Environments](https://wiki.python.org/moin/IntegratedDevelopmentEnvironments)\n", "\n", "