Hướng dẫn dùng print list python
You can toggle the display of prompts and output by clicking on >>> width = 20 >>> height = 5 * 9 >>> width * height 9001 in the upper-right corner of an example box. If you hide the prompts and output for an example, then you can easily copy and paste the input lines into your interpreter. Show Many of the examples in this manual, even those entered at the interactive prompt, include comments. Comments in Python start with the hash character, >>> width = 20 >>> height = 5 * 9 >>> width * height 9002, and extend to the end of the physical line. A comment may appear at the start of a line or following whitespace or code, but not within a string literal. A hash character within a string literal is just a hash character. Since comments are to clarify code and are not interpreted by Python, they may be omitted when typing in examples. Some examples: # this is the first comment spam = 1 # and this is the second comment # ... and now a third! text = "# This is not a comment because it's inside quotes." 3.1. Using Python as a Calculator¶Let’s try some simple Python commands. Start the interpreter and wait for the primary prompt, >>> width = 20 >>> height = 5 * 9 >>> width * height 9001. (It shouldn’t take long.) 3.1.1. Numbers¶The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward: the operators >>> width = 20 >>> height = 5 * 9 >>> width * height 9004, >>> width = 20 >>> height = 5 * 9 >>> width * height 9005, >>> width = 20 >>> height = 5 * 9 >>> width * height 9006 and >>> width = 20 >>> height = 5 * 9 >>> width * height 9007 work just like in most other languages (for example, Pascal or C); parentheses ( >>> width = 20 >>> height = 5 * 9 >>> width * height 9008) can be used for grouping. For example: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.6 The integer numbers (e.g. >>> width = 20 >>> height = 5 * 9 >>> width * height 9009, >>> n # try to access an undefined variable Traceback (most recent call last): File "0, >>> n # try to access an undefined variable Traceback (most recent call last): File "1) have type >>> n # try to access an undefined variable Traceback (most recent call last): File "2, the ones with a fractional part (e.g. >>> n # try to access an undefined variable Traceback (most recent call last): File "3, >>> n # try to access an undefined variable Traceback (most recent call last): File "4) have type >>> n # try to access an undefined variable Traceback (most recent call last): File "5. We will see more about numeric types later in the tutorial. Division ( >>> width = 20 >>> height = 5 * 9 >>> width * height 9007) always returns a float. To do floor division and get an integer result you can use the >>> n # try to access an undefined variable Traceback (most recent call last): File "7 operator; to calculate the remainder you can use >>> n # try to access an undefined variable Traceback (most recent call last): File "8: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 17 With Python, it is possible to use the >>> n # try to access an undefined variable Traceback (most recent call last): File "9 operator to calculate powers 1: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 128 The equal sign ( >>> 4 * 3.75 - 1 14.00) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt: >>> width = 20 >>> height = 5 * 9 >>> width * height 900 If a variable is not “defined” (assigned a value), trying to use it will give you an error: >>> n # try to access an undefined variable Traceback (most recent call last): File " There is full support for floating point; operators with mixed type operands convert the integer operand to floating point: >>> 4 * 3.75 - 1 14.0 In interactive mode, the last printed expression is assigned to the variable >>> 4 * 3.75 - 1 14.01. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example: >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.06 This variable should be treated as read-only by the user. Don’t explicitly assign a value to it — you would create an independent local variable with the same name masking the built-in variable with its magic behavior. In addition to >>> n # try to access an undefined variable Traceback (most recent call last): File "2 and >>> n # try to access an undefined variable Traceback (most recent call last): File "5, Python supports other types of numbers, such as >>> 4 * 3.75 - 1 14.04 and >>> 4 * 3.75 - 1 14.05. Python also has built-in support for complex numbers, and uses the >>> 4 * 3.75 - 1 14.06 or >>> 4 * 3.75 - 1 14.07 suffix to indicate the imaginary part (e.g. >>> 4 * 3.75 - 1 14.08). 3.1.2. Strings¶Besides numbers, Python can also manipulate strings, which can be expressed in several ways. They can be enclosed in single quotes ( >>> 4 * 3.75 - 1 14.09) or double quotes ( >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.060) with the same result 2. >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.061 can be used to escape quotes: >>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," they said.' '"Yes," they said.' >>> "\"Yes,\" they said." '"Yes," they said.' >>> '"Isn\'t," they said.' '"Isn\'t," they said.' In the interactive interpreter, the output string is enclosed in quotes and special characters are escaped with backslashes. While this might sometimes look different from the input (the enclosing quotes could change), the two strings are equivalent. The string is enclosed in double quotes if the string contains a single quote and no double quotes, otherwise it is enclosed in single quotes. The >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.062 function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters: >>> '"Isn\'t," they said.' '"Isn\'t," they said.' >>> print('"Isn\'t," they said.') "Isn't," they said. >>> s = 'First line.\nSecond line.' # \n means newline >>> s # without print(), \n is included in the output 'First line.\nSecond line.' >>> print(s) # with print(), \n produces a new line First line. Second line. If you don’t want characters prefaced by >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.061 to be interpreted as special characters, you can use raw strings by adding an >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.064 before the first quote: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.60 String literals can span multiple lines. One way is using triple-quotes: >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.065 or >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.066. End of lines are automatically included in the string, but it’s possible to prevent this by adding a >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.061 at the end of the line. The following example: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.61 produces the following output (note that the initial newline is not included): >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.62 Strings can be concatenated (glued together) with the >>> width = 20 >>> height = 5 * 9 >>> width * height 9004 operator, and repeated with >>> width = 20 >>> height = 5 * 9 >>> width * height 9006: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.63 Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated. >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.64 This feature is particularly useful when you want to break long strings: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.65 This only works with two literals though, not with variables or expressions: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.66 If you want to concatenate variables or a variable and a literal, use >>> width = 20 >>> height = 5 * 9 >>> width * height 9004: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.67 Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.68 Indices may also be negative numbers, to start counting from the right: >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.69 Note that since -0 is the same as 0, negative indices start from -1. In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain substring: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 170 Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced. >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 171 Note how the start is always included, and the end always excluded. This makes sure that >>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," they said.' '"Yes," they said.' >>> "\"Yes,\" they said." '"Yes," they said.' >>> '"Isn\'t," they said.' '"Isn\'t," they said.'1 is always equal to >>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," they said.' '"Yes," they said.' >>> "\"Yes,\" they said." '"Yes," they said.' >>> '"Isn\'t," they said.' '"Isn\'t," they said.'2: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 172 One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 173 The first row of numbers gives the position of the indices 0…6 in the string; the second row gives the corresponding negative indices. The slice from i to j consists of all characters between the edges labeled i and j, respectively. For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds. For example, the length of >>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," they said.' '"Yes," they said.' >>> "\"Yes,\" they said." '"Yes," they said.' >>> '"Isn\'t," they said.' '"Isn\'t," they said.'3 is 2. Attempting to use an index that is too large will result in an error: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 174 However, out of range slice indexes are handled gracefully when used for slicing: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 175 Python strings cannot be changed — they are immutable. Therefore, assigning to an indexed position in the string results in an error: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 176 If you need a different string, you should create a new one: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 177 The built-in function >>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," they said.' '"Yes," they said.' >>> "\"Yes,\" they said." '"Yes," they said.' >>> '"Isn\'t," they said.' '"Isn\'t," they said.'4 returns the length of a string: >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 178 See also 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 >>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," they said.' '"Yes," they said.' >>> "\"Yes,\" they said." '"Yes," they said.' >>> '"Isn\'t," they said.' '"Isn\'t," they said.'5.printf-style String Formatting The old formatting operations invoked when strings are the left operand of the >>> n # try to access an undefined variable Traceback (most recent call last): File "8 operator are described in more detail here. 3.1.3. Lists¶Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type. >>> 17 / 3 # classic division returns a float 5.666666666666667 >>> >>> 17 // 3 # floor division discards the fractional part 5 >>> 17 % 3 # the % operator returns the remainder of the division 2 >>> 5 * 3 + 2 # floored quotient * divisor + remainder 179 Like strings (and all other built-in sequence types), lists can be indexed and sliced: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1280 All slice operations return a new list containing the requested elements. This means that the following slice returns a shallow copy of the list: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1281 Lists also support operations like concatenation: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1282 Unlike strings, which are immutable, lists are a mutable type, i.e. it is possible to change their content: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1283 You can also add new items at the end of the list, by using the >>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," they said.' '"Yes," they said.' >>> "\"Yes,\" they said." '"Yes," they said.' >>> '"Isn\'t," they said.' '"Isn\'t," they said.'7 method (we will see more about methods later): >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1284 Assignment to slices is also possible, and this can even change the size of the list or clear it entirely: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1285 The built-in function >>> 'spam eggs' # single quotes 'spam eggs' >>> 'doesn\'t' # use \' to escape the single quote... "doesn't" >>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," they said.' '"Yes," they said.' >>> "\"Yes,\" they said." '"Yes," they said.' >>> '"Isn\'t," they said.' '"Isn\'t," they said.'4 also applies to lists: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1286 It is possible to nest lists (create lists containing other lists), for example: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1287 3.2. First Steps Towards Programming¶Of course, we can use Python for more complicated tasks than adding two and two together. For instance, we can write an initial sub-sequence of the Fibonacci series as follows: >>> 5 ** 2 # 5 squared 25 >>> 2 ** 7 # 2 to the power of 7 1288 This example introduces several new features.
Footnotes 1Since >>> n # try to access an undefined variable Traceback (most recent call last): File "9 has higher precedence than >>> width = 20 >>> height = 5 * 9 >>> width * height 9005, >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.602 will be interpreted as >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.603 and thus result in >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.604. To avoid this and get >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.605, you can use >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.606.2 Unlike other languages, special characters such as >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.607 have the same meaning with both single ( >>> 4 * 3.75 - 1 14.09) and double ( >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.060) quotes. The only difference between the two is that within single quotes you don’t need to escape >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.610 (but you have to escape >>> 2 + 2 4 >>> 50 - 5*6 20 >>> (50 - 5*6) / 4 5.0 >>> 8 / 5 # division always returns a floating point number 1.611) and vice versa. |