set path=%path%;C:/python36widows 退出解释器: ^Z or quit()The interpreter’s line-editing features include interactive editing, history substitution and code completion on systems that support readline.Perhaps the quickest check to see whether command line editing is supported is typing Control-P
to the first Python prompt you get. If it beeps, you have command line editing; see Appendix Interactive Input Editing and History Substitution for an introduction to the keys. If nothing appears to happen, or if ^P
is echoed, command line editing isn’t available; you’ll only be able to use backspace to remove characters from the current line.
Control-P会触发提示窗"Print to defaut printer", 似乎和其他应用有热键冲突。 行编辑功能不能使用。
The interpreter Operates somewhat like the Unix shell: when called with standard input connected to a tty device, it reads and executes commands interactively; when called with a file name argument or with a file as standard input, it reads and executes a script from that file.
A second way of starting the interpreter is python -c command [arg] ...
, which executes the statement(s) in command, analogous to the shell’s -c
option. Since Python statements often contain spaces or other characters that are special to the shell, it is usually advised to quote command in its entirety with single quotes.
Some Python modules are also useful as scripts. These can be invoked using python -m module [arg] ...
, which executes the source file for module as if you had spelled out its full name on the command line.
When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. This can be done by passing -i
before the script.
All command line options are described in Command line and environment.
2.1.1. Argument Passing
When known to the interpreter, the script name and additional arguments thereafter are turned into a list of strings and assigned to the argv
variable in the sys
module. You can access this list by executing import sys
. The length of the list is at least one; 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.2.1.2. Interactive Mode (是什么?)
the primary prompt usually three greater-than signs (>>>
); 比如在windows cmd下输入python,则显示:C:/Users/xxxxx>pythonPython 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>>continuation linesit prompts with the secondary prompt, by default three dots (...
). Continuation lines are needed when entering a multi-line construct. 一次输入多行代码,代码体内部行用secondary prompt打头。2.2. The Interpreter and Its Environment
2.2.1. Source Code Encoding
By default, Python source files are treated as encoded in UTF-8. 编辑器需要识别UTF-8编码,所用字体也用支持UTF-8不使用默认编码要明示,通常在源程序文件首行。
# -*- coding: encoding -*-3. An Informal Introduction to Python
Note that a secondary prompt on a line by itself in an example means you must type a blank line; this is used to end a multi-line command. 多行命令用空行终止
python的注释号 #, 字符串中的#不算注释开头
3.1. Using Python as a Calculator
>>> 8 / 5 # division always returns a floating point number1.6区别于C/C++ 整型相除得到整型int 整型 float 浮点型
floor division 用 //取余 %
乘方 **
赋值=
The equal sign (=
) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt:
未赋值的变量不能直接使用
In interactive mode, the last printed expression is assigned to the variable _
上一个表达式计算结果存在变量_中, _当作只读用,不要对其赋值,否则就相当于定义了新的同名变量,不再有存前结果的功能
取小数位 round( 3.14159, 2) 3.14
其他数据类型Decimal, Fraction, complex number
3.1.2. Strings
字符串“” 和''等效,交互模式下,解释器会用单引号,用反斜杠escape特殊符号print()会直接把根据escape符号意义打印,如果不希望这么做, 使用raw string,
>>> print('C:/some/name') # here /n means newline!C:/someame>>> print(r'C:/some/name') # note the r before the quoteC:/some/name字符串可以占多行 """ """ ''' '''
+连接字符串 * 重复
字符串常量挨着会自动合并, 用于代码中字符串拆多行,用括号括起来就可以
>>> text = ('Put several strings within parentheses '... 'to have them joined together.')>>> text'Put several strings within parentheses to have them joined together.'字符串索引,可正可负,从左数从0开始加,从右数从-1开始减如何取子串, str[a,b] str[:a] str[b:] 含左不含右
字符串不能改变,给字符串字符赋值会引起报错
拓展
Text Sequence Type — strStrings are examples of sequence types, and support the common operations supported by such types.String MethodsStrings support a large number of methods for basic transformations and searching.Formatted string literalsString literals that have embedded expressions.Format String SyntaxInformation about string formatting with str.format()
.printf-style String FormattingThe old formatting operations invoked when strings are the left operand of the %
operator are described in more detail here.3.1.3. Lists
和字符串一样可以去子列表。子列表操作会返回一个新的对象。列表可以合并
>>> squares + [36, 49, 64, 81, 100][1, 4, 9, 16, 25, 36, 49, 64, 81, 100]列表可以修改append()在列表尾部添加
直接修改子列表
用空列表修改,相当于删除
列表可以嵌套
>>> a = ['a', 'b', 'c']>>> n = [1, 2, 3]>>> x = [a, n]>>> x[['a', 'b', 'c'], [1, 2, 3]]>>> x[0]['a', 'b', 'c']>>> x[0][1]'b'3.2. First Steps Towards Programming
01 >>> # Fibonacci series:(动态规划版)02 ... # the sum of two elements defines the next03 ... a, b = 0, 104 >>> while b < 10:05 ... print(b)06 ... a, b = b, a+b07 ...03, a multiple assignment, the variables a
and b
simultaneously get the new values 0 and 1.
06, again, the expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right. 右边两部分表达式先估值再同时分别付给左边两个变量,=右侧两个表达式从左到右估值
In Python, like in C, any non-zero integer value is true; zero is false. 非零true 零false 非空串true 空串false
比较运算符同C
python靠缩进来组织代码块,同一块中的语句缩进量需要相等,用空行来结束代码块。
print()
可以严格控制输出格式。用end=' x',可以避免换行。
>>> a, b = 0, 1>>> while b < 1000:... print(b, end=',')... a, b = b, a+b...1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,