Python 初体验#
叩开 Python 的大门!
Python 是什么?#
Python 是一种清晰而强大的面向对象的编程语言,可与 Perl、Ruby、Scheme 或 Java 媲美。
Python 一些值得注意的特点:
使用优雅的语法,使你写的程序更容易阅读。
它是一种易于使用的语言,使你的程序能够简单地运行。这使得 Python 成为原型开发和其他临时性编程任务的理想选择,同时不影响可维护性。
配有大型标准库,支持许多常见的编程任务,如连接到网络服务器,用正则表达式搜索文本,读取和修改文件。
Python 的交互式模式使得测试简短的代码片段变得很容易。还有一个捆绑的开发环境叫 IDLE。
通过添加用 C 或 C++ 等编译语言实现的新模块,可以轻松地进行扩展。
也可以嵌入到一个应用程序中,提供一个可编程的接口。
可在任何地方运行,包括 Mac OS X、 Windows、 Linux 和 Unix,非官方的构建也可用于 Android 和 iOS。
从两种意义上说,Python 是自由软件。下载或使用 Python,或在你的应用程序中包含它,都不需要任何费用。Python 还可以自由地修改和重新发布,因为虽然语言有版权,但它在 开源许可 下可以使用。
Python 的一些编程语言特点是:
有多种基本数据类型可用:数字(浮点、复数和无限长度的长整数)、字符串(ASCII 和 Unicode)、列表和字典。
Python 支持面向对象的编程,具有类和多继承性。
代码可以被分组为模块和包。
该语言支持触发和捕捉异常,从而使异常处理更加简洁。
数据类型是强类型和动态类型。混合不兼容的类型(例如,试图添加一个字符串和一个数字)会导致引发一个异常,因此异常会被更快地捕获。
Python 包含高级编程功能,如生成器和列表理解。
Python 的自动内存管理使你不必在代码中手动分配和释放内存。
请阅读 SimplePrograms 的简短程序集,其长度逐渐增加,展示了 Python 的语法和可读性。
编写 Pythonic 代码并不难 —— 但你必须习惯于(PEP)的代码风格规则。你可以在 Pythonchecker.com 这样的在线资源中测试、检查和改进你的代码风格。
Python的设计哲学是“优雅”、“明确”、“简单”。它的重要准则被称为“Python之禅”。在 Python 解释器内运行 import this
可以获得完整的列表(翻译为中文):
优美优于丑陋。明了优于隐晦。
简单优于复杂。复杂优于凌乱。
扁平优于嵌套。稀疏优于稠密。
可读性很重要。
Python 开发者的哲学是“用一种方法,最好是只有一种方法来做一件事”。
在设计 Python 语言时,如果面临多种选择,Python 开发者一般会拒绝花俏的语法,而选择明确没有或者很少有歧义的语法。
为什么学习 Python?#
易学
可读和可维护的代码
与主要平台和系统兼容
强大的标准库
许多开源框架和工具
简化复杂的软件开发
学习 Python 可以做什么?#
翻译自:what-can-i-do-with-python。
列出一些常用 Python 库。
做一般的软件开发#
Web 开发#
CLI 开发#
CLI:命令行界面
GUI 开发#
库 |
描述 |
---|---|
Kivy is a library for rapid development of applications with innovative user interfaces, such as multi-touch applications. It runs on Linux, Windows, macOS, Android, iOS, and Raspberry Pi. |
|
PyQt is a set of Python bindings for the Qt application framework. It includes classes for building GUI applications. It also provides classes for networking, threads, SQL databases, and more. It supports the Windows, Linux, and macOS platforms. |
|
PySimpleGUI is a library that aims to transform the tkinter, Qt, wxPython, and Remi GUI frameworks into a simpler interface. It uses Python core data types to define windows and simplify event handling. |
|
Qt for Python is a project that provides the official set of Python bindings ( |
|
tkinter is a standard Python interface to the Tk GUI toolkit. It allows you to build GUI applications without the need for third-party dependencies. It’s available on most Unix platforms as well as on Windows systems. |
|
wxPython is a Python binding for the wxWidgets C++ library. It allows you to create applications for Windows, macOS, and Linux with a single code base. It gives applications a native look and feel because it uses the platform’s native API. |
游戏开发#
Library |
Description |
---|---|
Arcade is a Python library for creating 2D video games. It’s ideal for people learning to program because they don’t need to learn a complex game framework to start creating their own games. |
|
PyGame is a set of Python modules designed for writing video games. It adds functionality on top of the SDL library. It allows you to create full-featured games and multimedia programs. The library is highly portable and runs on several platforms and operating systems. |
|
pyglet is a powerful Python library for creating games and other visually rich applications on Windows, macOS, and Linux. It supports windowing, user interface event handling, OpenGL graphics, loading images, and playing videos and music. |
潜心研究数据科学和数学#
机器学习#
Library |
Description |
---|---|
Keras is an industrial-strength deep learning framework with an API designed for human beings. It allows you to run new experiments and try more ideas quickly. It follows best practices for reducing cognitive load. |
|
NLTK is a platform for building Python programs to work with human language data. It provides libraries for classification, tokenization, stemming, tagging, parsing, and semantic reasoning. |
|
PyTorch is an open source machine learning framework that accelerates the path from research prototyping to production deployment. |
|
scikit-learn is an open source machine learning library that supports supervised and unsupervised learning. It’s an efficient tool for predictive data analysis that’s accessible to everybody and reusable in various contexts. |
|
TensorFlow is an end-to-end open source platform for machine learning. It has a comprehensive, flexible ecosystem of tools, libraries, and community resources that will help you build and deploy ML-powered applications. |
科学计算#
Library |
Description |
---|---|
NumPy is a fundamental package for scientific computing with Python. It offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It provides a high-level syntax that makes it accessible and productive. |
|
SciPy is a Python-based collection of open source software for mathematics, science, and engineering. |
|
SimPy is a process-based discrete-event simulation framework based on Python. It can help you simulate real-world systems, such as airports, customer services, highways, and more. |
数据分析和可视化#
Library |
Description |
---|---|
Bokeh is an interactive data visualization library for web browsers. It provides tools for constructing elegant and versatile graphics. It can help you quickly make interactive plots, dashboards, and data applications. |
|
Dash is a Python framework for building web analytic applications quickly. It’s ideal for building data visualization applications with custom user interfaces that render in the browser. |
|
Matplotlib is a library for creating static, animated, and interactive data visualizations in Python. |
|
pandas is a powerful and flexible open source tool for analyzing and manipulating data. It provides fast, flexible, and expressive data structures to work with relational or labeled data. |
|
Seaborn is a Python data visualization library based on Matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics that allow you to explore and understand your data. It integrates closely with pandas data structures. |
网络爬虫#
Library |
Description |
---|---|
Beautiful Soup is a Python library for pulling data out of HTML and XML files into parse trees. The library provides methods and Pythonic idioms to navigate, search, modify, and extract information from parse trees. |
|
|
|
Scrapy is a fast, high-level web crawling and web scraping framework. It allows you to crawl websites and extract structured data from their pages. |
|
|
加快工作流程并使之自动化#
DevOps#
DevOps 包括软件开发和一般 IT运营。DevOps 允许你处理你的应用程序和软件产品的整个生命周期。它包括开发、测试、打包和部署,以及其他相关操作。
Library |
Description |
---|---|
Ansible is a tool for software provisioning, configuration management, and application deployment. It enables infrastructure as code. |
|
Docker Compose is a tool for defining and running multicontainer Docker applications. You can configure your application’s services with a YAML file. Then, with a single command, you can create and start all the services from your configuration file. It works on production, staging, development, testing, and more. |
To get started with DevOps, check out:
开发环境#
为你和你的队友构建一个高效的环境是软件开发的一个基本部分。为此,Python 有一套很好的工具,允许你在每个项目的虚拟环境中隔离你的包、库和 Python 版本。
工具 |
描述 |
---|---|
|
|
|
|
|
|
|
|
|
要建立一个有效的开发环境,请查看。
软件打包与部署#
软件开发周期的另一个关键部分是打包、分发和部署你的产品给你的终端用户或客户。在 Python 中,部署应用程序和库的一个快速而流行的方法是将它们发布到 PyPI。
Tool |
Description |
---|---|
Flit is a tool that provides a quick way to put your Python packages and modules on PyPI. It helps you set up the information about your package, so you can publish it to PyPI with minimal effort. |
|
Poetry is a tool for creating, building, installing, and packaging Python projects. It also allows you to publish your projects to PyPI. It tracks and resolves your project’s dependencies. It uses your current virtual environments or creates new ones to isolate your packages from your system-wide Python installation. |
|
PyInstaller is a tool that freezes Python applications into stand-alone executables that work under Windows, GNU/Linux, macOS, and others. |
|
setuptools is a collection of enhancements to the Python distutils that allows you to build and distribute Python distributions, especially those that depend on other packages. |
|
Twine is a utility for publishing Python packages on PyPI. It allows you to upload source and binary distributions of your projects. |
To get started, check out:
With these resources, you can get started with packaging and deploying your Python applications, libraries, and packages to your end users, clients, and colleges. Also, the Python Packaging Authority provides a lot of useful information and tutorials to help you distribute Python packages with modern tools.
数据库系统#
Library |
Database |
Description |
---|---|---|
MongoEngine is a document-object mapper for working with MongoDB using object-oriented programming in Python. |
||
MySQL Connector is a self-contained Python driver for communicating with MySQL servers. |
||
Psycopg is a PostgreSQL database adapter for the Python programming language. |
||
PyMongo is a Python distribution containing tools for working with MongoDB databases. It provides a native Python driver for this type of database system. |
||
SQLAlchemy is a Python SQL toolkit and object-relational mapper for SQL databases. |
||
|
To get started with databases, check out:
Creating and working with databases is a powerful way to manage data in your Python applications. Databases add significant functionality and versatility to your programs and allow you to provide exciting features to your users and client. Managing databases is a fundamental skill in your developer education.
软件测试#
Tool |
Description |
---|---|
doctest is a standard module that searches your docstrings for pieces of text that look like interactive Python sessions and executes them to verify that they work correctly. |
|
pytest is a robust and mature testing framework that allows you to write and automate tests. It can scale from small unit tests to complex functional tests for your applications and libraries. |
|
tox is a generic virtualenv management and test command-line tool. It allows you to check if your packages install correctly within different Python versions and interpreters. It can run your tests in each of the configured environments. |
|
|
To get started with testing, check out:
As a developer, you need to produce reliable code that works correctly. This means that you need to test your code every time you change it or add new features. Automated tests are the way to go in these situations.
构建嵌入式系统和机器人#
随着科学技术的发展,物联网、家庭自动化、自动驾驶汽车和机器人等领域已经变得越来越流行。
Library |
Description |
---|---|
BBC micro:bit is a pocket-sized computer that introduces you to how software and hardware work together. It is programmable with Python. |
|
CircuitPython is a programming language designed to simplify experimenting and learning to code on low-cost microcontroller boards. |
|
MicroPython is a lean and efficient implementation of Python. It includes a small subset of the Python standard library. It’s optimized to run on microcontrollers and in constrained environments. |
|
PythonRobotics is a compilation of various robotics algorithms with visualizations. It’s focused on autonomous navigation. Its goal is to allow you to understand the basic ideas behind each robotic algorithm it provides. |
|
Raspberry Pi is a general-purpose, Linux-based computer. It has a complete operating system with a GUI interface that is capable of running many different programs at the same time. Python comes built in on the Raspberry Pi. |
|
rospy is a client library for ROS (Robot Operating System). Its API enables Python programmers to quickly interface with ROS to create complex and reliable robot behaviors. |
To get started with embedded Python, check out:
If you want to start creating a hardware-related project with Python, then look at how to build physical projects with Python on the Raspberry Pi. In this project, you’ll learn how to set up a Raspberry Pi, run Python code on it, read input from its sensors, send signals to its electronic components, and more.