Python 学习笔记 2. 基础语法
保留字符
and | exec | not |
assert | finally | or |
break | for | pass |
class | from | print |
continue | global | raise |
def | if | return |
del | import | try |
elif | in | while |
else | is | with |
except | lambda | yield |
行和缩进
Python 最具特色的就是用缩进来写模块。
缩进的空白数量是可变的,但是所有代码块语句必须包含相同的缩进空白数量,这个必须严格执行。
以下示例缩进为四个空格:
python
>>> if True:
... print ("True")
... else:
... print ("False")
...
True
>>>
1
2
3
4
5
6
7
2
3
4
5
6
7
建议你在每个缩进层次使用 单个制表符 或 两个空格 或 四个空格 , 切记不能混用。
注释
单行注释采用 #
开头。
python
# 第一个注释
print ("Hello, Python!") # 第二个注释
1
2
2
多行注释使用三个单引号 ( '''
) 或三个双引号 ( """
)。
python
'''
这是多行注释,使用单引号。
这是多行注释,使用单引号。
这是多行注释,使用单引号。
'''
"""
这是多行注释,使用双引号。
这是多行注释,使用双引号。
这是多行注释,使用双引号。
"""
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
多行语句
可以使用斜杠( \
)将一行的语句分为多行显示。
python
>>> item_one = 'one '
>>> item_two = 'two '
>>> item_three = 'three '
>>> total = item_one + \
... item_two + \
... item_three
>>> total
'one two three '
>>>
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
命令行参数
使用 -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; alsoPYTHONDONTWRITEBYTECODE=x
-c cmd
: program passed in as string (terminates option list)-d
: debug output from parser; alsoPYTHONDEBUG=x
-E
: ignorePYTHON*
environment variables (such asPYTHONPATH
)-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; alsoPYTHONINSPECT=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; alsoPYTHONOPTIMIZE=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; alsoPYTHONNOUSERSITE
-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; alsoPYTHONUNBUFFERED=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 alsoPYTHONWARNINGS=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 filesfile
: program read from script file-
: program read from stdin (default; interactive mode if a tty)arg ...
: arguments passed to program insys.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 issys.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. UsePYTHONMALLOC=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.