Skip to content

Python 学习笔记 2. 基础语法

保留字符

andexecnot
assertfinallyor
breakforpass
classfromprint
continueglobalraise
defifreturn
delimporttry
elifinwhile
elseiswith
exceptlambdayield

行和缩进

Python 最具特色的就是用缩进来写模块

缩进的空白数量是可变的,但是所有代码块语句必须包含相同的缩进空白数量,这个必须严格执行。

以下示例缩进为四个空格:

python
>>> if True:
...     print ("True")
... else:
...     print ("False")
...
True
>>>

建议你在每个缩进层次使用 单个制表符两个空格四个空格 , 切记不能混用。

注释

单行注释采用 # 开头。

python
# 第一个注释
print ("Hello, Python!") # 第二个注释

多行注释使用三个单引号 ( ''' ) 或三个双引号 ( """ )。

python
'''
这是多行注释,使用单引号。
这是多行注释,使用单引号。
这是多行注释,使用单引号。
'''

"""
这是多行注释,使用双引号。
这是多行注释,使用双引号。
这是多行注释,使用双引号。
"""

多行语句

可以使用斜杠( \ )将一行的语句分为多行显示。

python
>>> item_one = 'one '
>>> item_two = 'two '
>>> item_three = 'three '
>>> total = item_one + \
...         item_two + \
...         item_three
>>> total
'one two three '
>>>

命令行参数

使用 -h 参数查看各参数帮助信息。

下面是 python 3.7.3 执行 python -h 的结果:

usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Options and arguments (and corresponding environment variables):

  • -b : issue warnings about str(bytes_instance), str(bytearray_instance) and comparing bytes/bytearray with str. (-bb: issue errors)
  • -B : don't write .pyc files on import; also PYTHONDONTWRITEBYTECODE=x
  • -c cmd : program passed in as string (terminates option list)
  • -d : debug output from parser; also PYTHONDEBUG=x
  • -E : ignore PYTHON* environment variables (such as PYTHONPATH)
  • -h : print this help message and exit (also --help)
  • -i : inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal; also PYTHONINSPECT=x
  • -I : isolate Python from the user's environment (implies -E and -s)
  • -m mod : run library module as a script (terminates option list)
  • -O : remove assert and __debug__-dependent statements; add .opt-1 before .pyc extension; also PYTHONOPTIMIZE=x
  • -OO : do -O changes and also discard docstrings; add .opt-2 before .pyc extension
  • -q : don't print version and copyright messages on interactive startup
  • -s : don't add user site directory to sys.path; also PYTHONNOUSERSITE
  • -S : don't imply 'import site' on initialization
  • -u : force the stdout and stderr streams to be unbuffered; this option has no effect on stdin; also PYTHONUNBUFFERED=x
  • -v : verbose (trace import statements); also PYTHONVERBOSE=x can be supplied multiple times to increase verbosity
  • -V : print the Python version number and exit (also --version) when given twice, print more information about the build
  • -W arg : warning control; arg is action:message:category:module:lineno also PYTHONWARNINGS=arg
  • -x : skip first line of source, allowing use of non-Unix forms of #!cmd
  • -X opt : set implementation-specific option
  • --check-hash-based-pycs always|default|never: control how Python invalidates hash-based .pyc files
  • file : program read from script file
  • - : program read from stdin (default; interactive mode if a tty)
  • arg ...: arguments passed to program in sys.argv[1:]

Other environment variables:

  • PYTHONSTARTUP: file executed on interactive startup (no default)
  • PYTHONPATH : ';'-separated list of directories prefixed to the default module search path. The result is sys.path.
  • PYTHONHOME : alternate <prefix> directory (or <prefix>;<exec_prefix>). The default module search path uses <prefix>\python{major}{minor}.
  • PYTHONCASEOK : ignore case in 'import' statements (Windows).
  • PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.
  • PYTHONFAULTHANDLER: dump the Python traceback on fatal errors.
  • PYTHONHASHSEED: if this variable is set to 'random', a random value is used to seed the hashes of str, bytes and datetime objects. It can also be set to an integer in the range [0,4294967295] to get hash values with a predictable seed.
  • PYTHONMALLOC: set the Python memory allocators and/or install debug hooks on Python memory allocators. Use PYTHONMALLOC=debug to install debug hooks.
  • PYTHONCOERCECLOCALE: if this variable is set to 0, it disables the locale coercion behavior. Use PYTHONCOERCECLOCALE=warn to request display of locale coercion and locale compatibility warnings on stderr.
  • PYTHONBREAKPOINT: if this variable is set to 0, it disables the default debugger. It can be set to the callable of your debugger of choice.
  • PYTHONDEVMODE: enable the development mode.

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.