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,