diff --git a/Doc/tutorial/introduction.rst b/Doc/tutorial/introduction.rst --- a/Doc/tutorial/introduction.rst +++ b/Doc/tutorial/introduction.rst @@ -22,9 +22,9 @@ Some examples:: # this is the first comment - SPAM = 1 # and this is the second comment - # ... and now a third! - STRING = "# This is not a comment." + spam = 1 # and this is the second comment + # ... and now a third! + string = "# This is not a comment because it's inside quotes." .. _tut-calculator: @@ -44,55 +44,46 @@ 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 ``+``, ``-``, ``*`` and ``/`` work just like in most other languages -(for example, Pascal or C); parentheses can be used for grouping. For example:: +(for example, Pascal or C); parentheses (``()``) can be used for grouping. +For example:: ->>> 2+2 +>>> 2 + 2 4 ->>> # This is a comment - ... 2+2 - 4 ->>> 2+2 # and a comment on the same line as code - 4 ->>> (50-5*6)/4 +>>> 50 - 5*6 + 20 +>>> (50 - 5*6) / 4 5.0 ->>> 8/5 # Fractions aren't lost when dividing integers +>>> 8 / 5 # division always returns a floating point number 1.6 -Note: You might not see exactly the same result; floating point results can -differ from one machine to another. We will say more later about controlling -the appearance of floating point output. See also :ref:`tut-fp-issues` for a -full discussion of some of the subtleties of floating point numbers and their -representations. +The integer numbers (e.g. ``2``, ``4``, ``20``) have type :class:`int`, +the ones with a fractional part (e.g. ``5.0``, ``1.6``) have type +:class:``float``. We will see more about numberic types later in the tutorial. -To do integer division and get an integer result, -discarding any fractional result, there is another operator, ``//``:: +Classic division (``/``) always return a float. To do integer division and get +an integer result, discarding any fractional result you can use the ``//`` +operator, and to calculate the remainder you can use ``%``:: ->>> # Integer division returns the floor: - ... 7//3 +>>> 17 / 3 # classic division returns a float + 5.666666666666667 +>>> +>>> 17 // 3 # integer division discards the fractional part + 5 +>>> 17 % 3 2 ->>> 7//-3 - -3 +>>> 5 * 3 + 2 # result * divisor + remainder + 17 The equal sign (``'='``) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt:: >>> width = 20 ->>> height = 5*9 +>>> height = 5 * 9 >>> width * height 900 -A value can be assigned to several variables simultaneously:: - ->>> x = y = z = 0 # Zero x, y and z ->>> x - 0 ->>> y - 0 ->>> z - 0 - -Variables must be "defined" (assigned a value) before they can be used, or an -error will occur:: +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): @@ -107,49 +98,6 @@ >>> 7.0 / 2 3.5 -Complex numbers are also supported; imaginary numbers are written with a suffix -of ``j`` or ``J``. Complex numbers with a nonzero real component are written as -``(real+imagj)``, or can be created with the ``complex(real, imag)`` function. -:: - ->>> 1j * 1J - (-1+0j) ->>> 1j * complex(0, 1) - (-1+0j) ->>> 3+1j*3 - (3+3j) ->>> (3+1j)*3 - (9+3j) ->>> (1+2j)/(1+1j) - (1.5+0.5j) - -Complex numbers are always represented as two floating point numbers, the real -and imaginary part. To extract these parts from a complex number *z*, use -``z.real`` and ``z.imag``. :: - ->>> a=1.5+0.5j ->>> a.real - 1.5 ->>> a.imag - 0.5 - -The conversion functions to floating point and integer (:func:`float`, -:func:`int`) don't work for complex numbers --- there is not one correct way to -convert a complex number to a real number. Use ``abs(z)`` to get its magnitude -(as a float) or ``z.real`` to get its real part:: - ->>> a=3.0+4.0j ->>> float(a) - Traceback (most recent call last): - File "", line 1, in ? - TypeError: can't convert complex to float; use abs(z) ->>> a.real - 3.0 ->>> a.imag - 4.0 ->>> abs(a) # sqrt(a.real**2 + a.imag**2) - 5.0 - In interactive mode, the last printed expression is assigned to the variable ``_``. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example:: @@ -167,6 +115,10 @@ 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 :class:`int` and :class:`float`, Python supports other type of +numbers, like :class:`complex`, :class:`~decimal.Decimal`, and +:class:`~fractions.Fraction`. + .. _tut-strings: @@ -174,13 +126,15 @@ ------- Besides numbers, Python can also manipulate strings, which can be expressed in -several ways. They can be enclosed in single quotes or double quotes:: +several ways. They can be enclosed in single quotes (``'...'``) or +double quotes (``"..."``) with the same result. ``\`` can be used to escape +quotes:: ->>> 'spam eggs' +>>> 'spam eggs' # single quotes 'spam eggs' ->>> 'doesn\'t' +>>> 'doesn\'t' # use \' to escape the single quote... "doesn't" ->>> "doesn't" +>>> "doesn't" # ...or use double quotes instead "doesn't" >>> '"Yes," he said.' '"Yes," he said.' @@ -189,38 +143,37 @@ >>> '"Isn\'t," she said.' '"Isn\'t," she said.' -The interpreter prints the result of string operations in the same way as they -are typed for input: inside quotes, and with quotes and other funny characters -escaped by backslashes, to show the precise value. The string is enclosed in +By default, the interpreter output the strings inside quotes and keeps other +funny characters escaped by backslashes. The string is enclosed in double quotes if the string contains a single quote and no double quotes, else it's enclosed in single quotes. The :func:`print` function produces a more -readable output for such input strings. +readable output, by removing the enclosing quotes and by printing escaped and +special characters:: -String literals can span multiple lines in several ways. Continuation lines can -be used, with a backslash as the last character on the line indicating that the -next line is a logical continuation of the line:: +>>> '"Isn\'t," she said.' + '"Isn\'t," she said.' +>>> print('"Isn\'t," she said.') + "Isn't," she 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(), the newline is printed + First line. + Second line. - hello = "This is a rather long string containing\n\ - several lines of text just as you would do in C.\n\ - Note that whitespace at the beginning of the line is\ - significant." +If you don't want to use special characters, you can use *raw strings* by adding +an ``r`` before the first quote:: - print(hello) +>>> print('C:\some\name') # here \n means newline! + C:\some + ame +>>> print(r'C:\some\name') # note the r before the quote + C:\some\name -Note that newlines still need to be embedded in the string using ``\n`` -- the -newline following the trailing backslash is discarded. This example would print -the following: - -.. code-block:: text - - This is a rather long string containing - several lines of text just as you would do in C. - Note that whitespace at the beginning of the line is significant. - -Or, strings can be surrounded in a pair of matching triple-quotes: ``"""`` or -``'''``. End of lines do not need to be escaped when using triple-quotes, but -they will be included in the string. So the following uses one escape to -avoid an unwanted initial blank line. :: +String literals can span multiple lines in several ways. One way is using +triple-quotes: ``"""..."""`` or ``'''...'''``. End of lines are automatically +included in the string, but it's possible to prevent this by adding a ``\`` at +the end of the line. The following example:: print("""\ Usage: thingy [OPTIONS] @@ -228,7 +181,7 @@ -H hostname Hostname to connect to """) -produces the following output: +produces the following output (note that the initial newline is not included):: .. code-block:: text @@ -236,43 +189,26 @@ -h Display this usage message -H hostname Hostname to connect to -If we make the string literal a "raw" string, ``\n`` sequences are not converted -to newlines, but the backslash at the end of the line, and the newline character -in the source, are both included in the string as data. Thus, the example:: - - hello = r"This is a rather long string containing\n\ - several lines of text much as you would do in C." - - print(hello) - -would print: - -.. code-block:: text - - This is a rather long string containing\n\ - several lines of text much as you would do in C. - Strings can be concatenated (glued together) with the ``+`` operator, and repeated with ``*``:: ->>> word = 'Help' + 'A' ->>> word - 'HelpA' ->>> '<' + word*5 + '>' - '' +>>> hero = 'bat' + 'man' + 'batman' +>>> 'na '*10 + hero + '!'! + 'na na na na na na na na na na batman!' -Two string literals next to each other are automatically concatenated; the first -line above could also have been written ``word = 'Help' 'A'``; this only works -with two literals, not with arbitrary string expressions:: +Two string literals (the ones enclosed between quotes) next to each other are +automatically concatenated; the first line above could also have been written +``hero = 'bat' 'man'``; this only works with two literals, not with arbitrary +string expressions:: ->>> 'str' 'ing' # <- This is ok - 'string' ->>> 'str'.strip() + 'ing' # <- This is ok - 'string' ->>> 'str'.strip() 'ing' # <- This is invalid - File "", line 1, in ? - 'str'.strip() 'ing' - ^ +>>> 'bat' 'man' # two string literals are concatenated automatically + 'batman' +>>> animal = 'bat' +>>> animal 'man' # can't concatenate a variable and a string literal + File "", line 1 + animal 'man' + ^ SyntaxError: invalid syntax Strings can be subscripted (indexed); like in C, the first character of a string

AltStyle によって変換されたページ (->オリジナル) /