In this example, go to the prompt >>> ,..., Starts with input. Otherwise, # For the python comment Table of Contents1, use Python as the calculator 1.1 number 1.2 string 1.3 Unicode encoded string 1.4 List2 preliminary Program Design 1 use Python as the calculator 1.1 number first to enter the interaction mode, use Python as a calculator $ pythonPython 2.7.3 (default, Aug 1 2012, 05:16:07) [GCC 4.6.3] on linux2Type "help", "copyright ", "credits" or "license" for more information. >>> 1 + 23> 1-2-1> 2*36> 2/40> 2/0 Traceback (most recent call last): File "<stdin> ", line 1, I N <module> ZeroDivisionError: integer division or modulo by zero >>> (1 + 2) * 39 can also assign values to variables, then participate in the operation >>> x = 3 >>> y = 4 >>> (x + y) * 535 can also be consecutively assigned values, however, you cannot use variables that have not been defined >>> x = y = z = 3.4 >>>> x3.4 >>>>> cTraceback (most recent call last ): file "<stdin>", line 1, in <module> NameError: name 'C' is not defined> z3.4 at the same time, Python also supports plural numbers, which can be a + bj, or complex (a, B) means> 1 + 2j (1 + 2j)> 2j * 3j (-6 + 0j)> complex (1 + 2j) * compl Ex (1-2j) (5 + 0j) can also assign the plural value to the variable, and then extract the real part, virtual part >>> x = (3 + 4j) >>> x. real3.0> x. imag4.0float (), int (), which can be converted to floating point and integer respectively, but not applicable to the plural. the singular number can be abs () to obtain its absolute value, or its modulus. >>> Int (3.4) 3 >>> int (3.5) 3 >>> float (3) 3.0 >>>> a = 1 + 2j >>> a (1 + 2j) >>> float (a) Traceback (most recent call last): File "<stdin> ", line 1, in <module> TypeError: can't convert complex to float >>> abs (a) 2.23606797749979 >>> abs (3 + 4j) 5.0 in interactive mode, you can use _ to represent the result of the previous calculation >>> 1 + 2 3 >>> 4 + _ 7 >>> 5 * _ 351.2 strings in addition to mathematics, python can also operate on strings. Note the escape characters, single quotation marks, and double quotation marks. Try several examples and see >>> 'hello, python' hello, python' >>> "hello, python "'hello, python' >>> 'Don't 'file "<stdin>", line 1 'Don't '^ SyntaxError: invalid syntax >>> 'Don \ '"Don't" >>> "Dont' t" "Dont" >>>> "Don \'t" "Don the't "string can be divided into multiple rows, print with the print function >>> hello = "This is a string which is \ n \... used to test multi \... lines. ">>> print helloThis is a string which is used to test multilines. note the use of line breaks \ n and Multiline edits. You can also use three quotation marks to avoid writing these two escape characters. >>> Hello = """... usage: thingy [OPTIONS]... -h Display this usage message... -H hostname Display host name... ">>> print hello Usage: thingy [OPTIONS]-h Display this usage message-H hostname Display host name if no escape character is required, \ n indicates \ n, does not indicate escape, you can start with the r string >>> hello = r "this is \ n a test line" >>> print hellothis is \ n a test line >>> hello = "this is \ n a test line ">>> print hellothis is a test line string can be connected, even Can be multiplied (in fact, it is also a connection ~) >>> Words = 'A' + 'hello' + 'B'> print wordsA hello B> '<' + words * 3 + '> ''<A hello BA hello BA hello B> 'strings can also be extracted, subscript starts from 0, subscript can be negative, representing the reciprocal str [I: j] indicates from str subscript I to subscript (J-1), a total of j-I characters. Str [_: 3] indicates str [0: 3] str [2: _] indicates the subscript 2 from the beginning to the end> words = "hello"> words [0] 'H'> words [0: 3] 'hel'> words [] 'E' >>> words [: 3] 'hel' >>> words [2:] 'llo' >>>> words = "hello" >>> words [-1] 'O' >>> letters in the words [-2] 'l' string cannot be modified, only strings can be recreated> words 'hello'> words [0] = 'A' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment >>>> words = "hi" >>> words 'Hi' The string length can be obtained by using the len function >>>> words = "hello" >>> len (words) 51.3 Unicode-encoded strings use the u prefix to define Unicode-encoded strings use unicode functions to convert other forms of encoding to unicode-encoded strings use encode to convert unicode to other forms> u 'Hello python! 'U' Hello python! '>>> Unicode (' \ xc3 \ xa4 \ xc3 \ xb6 \ xc3 \ xbc', 'utf-8 ') u' \ xe4 \ xf6 \ xfc '> u "äöü ". encode ('utf-8') '\ xc3 \ xa4 \ xc3 \ xb6 \ xc3 \ xbc' 1. 4 ListPython has a series of data structures that can be used to combine multiple values. Among them, the most versatile is List> a = ['spam', 3, 4.5, "hi"] >>> a ['spam', 3, 4.5, 'Hi'] >>> a [0] 'spam' >>> a [1] 3 >>> a [-1] 'Hi' >>> a [1: -1] [3, 4.5] >>> 2 * a [: 3] + ['xxx'] ['spam', 3, 4.5, 'spam', 3, 4.5, 'xxx'] different from the string, each element of the List can be changed >>> a ['spam', 3, 4.5, 'Hi'] >>> a [1] = 6 >>> A ['spam', 6, 4.5, 'Hi'] At the same time, the List can also be easily inserted, replaced, and other operations >>> a ['spam', 6, 4.5, 'Hi'] >>> a [] = ['xxx', 'yyy'] >>> a ['spam', 'xxx', 'yyy', 6, 4.5, 'Hi'] >>> a = ['x', 'y', 'z'] >>> a ['x', 'y ', 'Z'] >>> a [0: 2] = ['A', 'B'] >>> a ['A', 'B ', 'Z'] len can also get the number of List elements >>> a ['A', 'B', 'z'] >>> len () 32 preliminary program design from the previous section, we can see that Python is quite flexible and easy to use. Next we will start Program Design ~ In the first example, the series from the famous Fibonacci: >>> # Fibonacci series... # the sum of two elements defines the next... a, B = 0, 1 >>> while B <10 :... print B... a, B = B, a + B... 112358 the above program is very simple. If you do not elaborate on it, you can also understand that the program written in python is simple and easy to understand. It is short and concise. Note that the while block is not like c, and those in java are surrounded by parentheses, instead, use indentation. If you do not want to wrap the line during output, you only need to add a comma after print B. >>> A, B = 0, 1 >>> while B <10 :... print B ,... a, B = B, a + B... 1 1 2 3 5 8