「Python」- Quick Start and Tutorial

问题描述

该部分笔记将整理:与 Python 语言学习笔记及常用文档。

语法基础

if __name__ = '__main__':
	print("Hello World.")

定义变量

WIP

定义函数

WIP

笔记及相关文章

标准对象和模块的描述:「The Python Standard Library

正式的语言定义:「The Python Language Reference

使用C/C++编写扩展:「Extending and Embedding the Python Interpreter」「Python/C API Reference Manual

属于词汇表:「Glossary

交互式输入编辑和历史替换:「Interactive Input Editing and History Substitution

交互执行,选项-c执行命令,选项-m执行模块;

命令行选项及环境变量:「Command line and environment

命令行中的参数传递:import sys

  • when no script and no arguments are given, sys.argv[0] is an empty string
  • When the script name is given as ‘‘ (meaning standard input), sys.argv[0] is set to ‘-‘
  • When -c command is used, sys.argv[0] is set to ‘-c’
  • When -m module is used, sys.argv[0] is set to the full name of the located module
  • Options found after -c command or -m module are not consumed by the Python interpreter’s option processing but left in sys.argv for the command or module to handle.

交互模式:「Interactive Mode

文件编码:在第二行或者第一行使用:# -*- coding: encoding -*-

可用的文件编码:「codecs

支持的数据类型:int, float, Decimal, Fraction, complex numbers, String, List, Dictionary, …

编码风格指南:「PEP 8 — Style Guide for Python Code

Lambda:「关于Python中的lambda,这可能是你见过的最完整的讲解

指定文件编码

Working with utf-8 encoding in Python source
Correct way to define Python source code encoding
PEP 263 — Defining Python Source Code Encodings
2.1.4. Encoding declarations

根据官方文档所述,只要第一行或者第二行匹配正则表达式coding[=:]\s*([-\w.]+)则会被视为编码声明。

所以对于# -*- coding: utf-8 -*-coding: utf-8才是关键部分。

代码注释

What is the proper way to comment functions in Python? – Stack Overflow

in Function:

def test_function(p1, p2, p3):
    """
    test_function does blah blah blah.

    :param p1: describe about parameter p1
    :param p2: describe about parameter p2
    :param p3: describe about parameter p3
    :return: describe what it returns
    """ 
    pass

参考文献

Python 3.8.1 documentation
The Python Tutorial