2017.4.21
argument
A value passed to a function (or method) when calling the function. There are two kinds of argument:
Arguments are assigned to the named local variables in a function body. See the Calls section for the rules governing this assignment. Syntactically, any expression can be used to represent an argument; the evaluated value is assigned to the local variable.
keyword argument: an argument preceded by an identifier (e.g.
name=
) in a function call or passed as a value in a dictionary preceded by**
. For example,3
and5
are both keyword arguments in the following calls tocomplex()
:complex(real=3, imag=5)complex(**{'real': 3, 'imag': 5})
positional argument: an argument that is not a keyword argument. Positional arguments can appear at the beginning of an argument list and/or be passed as elements of an iterable preceded by
*
. For example,3
and5
are both positional arguments in the following calls:complex(3, 5)complex(*(3, 5))
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. In that encoding,although the standard library only uses ASCII characters for identifiers.
To declare an encoding other than the default one, a special comment line should be added as the first line of the file. The syntax is as follows:
# -*- coding: encoding -*-
where encoding is one of the valid codecs
supported by Python.
For example, to declare that Windows-1252 encoding is to be used, the first line of your source code file should be:
# -*- coding: cp-1252 -*-
One exception to the first line rule is when the source code starts with a UNIX “shebang” line. In this case, the encoding declaration should be added as the second line of the file. For example:
#!/usr/bin/env python3# -*- coding: cp-1252 -*-
3.1. Using Python as a Calculator
3.1.1. Numbers
Division (/
) always returns a float. To do floor division and get an integer result (discarding any fractional result) you can use the //
operator; to calculate the remainder you can use %
:
>>> 17 / 3 # classic division returns a float5.666666666666667 >>> 17 // 3 # floor division discards the fractional part5 >>> 17 % 3 # the % operator returns the remainder of the division2 >>> 5 * 3 + 2 # result * divisor + remainder17
With Python, it is possible to use the **
operator to calculate powers [1]:
>>> 5 ** 2 # 5 squared25>>> 2 ** 7 # 2 to the power of 7128
[1] | Since ** has higher precedence than - , -3**2 will be interpreted as -(3**2) and thus result in -9 . To avoid this and get 9 , you can use (-3)**2 . |
浮点数和整型数值计算结果还是浮点数。
>>> 3 * 3.75 / 1.57.5>>> 7.0 / 23.5
交互式代码中,_代表上一个表达式。
>>> tax = 12.5 / 100>>> price = 100.50>>> price * tax12.5625>>> price + _113.0625>>> round(_, 2)113.06
In addition to int
and float
, Python supports other types of numbers, such as Decimal
and Fraction
. Python also has built-in support for complex numbers, and uses the j
or J
suffix to indicate the imaginary part (e.g. 3+5j
).
3.1.2. Strings
The print()
function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters:
If you don’t want characters prefaced by \
to be interpreted as special characters, you can use raw strings by adding an r
before the first quote:
>>> print('C:\some\name') # here \n means newline!C:\someame >>> print(r'C:\some\name') # note the r before the quoteC:\some\name
Strings can be concatenated (glued together) with the +
operator, and repeated with *
:include literals,variables,expressions.
>>> # 3 times 'un', followed by 'ium' >>> 3 * 'un' + 'ium' 'unununium'
This feature is particularly useful when you want to break long strings:
>>> text = ('Put several strings within parentheses ' ... 'to have them joined together.') >>> text'Put several strings within parentheses to have them joined together.'
Strings can be indexed (subscripted), with the first character having index 0;Note that since -0 is the same as 0, negative indices start from -1.
>>> word = 'Python' >>> word[0] # character in position 0 'P'
>>> word[-1] # last character 'n'
In addition to indexing, slicing is also supported.
>>> word[0:2] # characters from position 0 (included) to 2 (excluded) 'Py'
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.
>>> word[:2] # character from the beginning to position 2 (excluded) 'Py' >>> word[4:] # characters from position 4 (included) to the end 'on'
Attempting to use an index that is too large will result in an error:
However, out of range slice indexes are handled gracefully when used for slicing:
>>> word[42] # the word only has 6 characters Traceback (most recent call last): File "", line 1, in IndexError: string index out of range
>>> word[4:42] 'on' >>> word[42:] ''
Python strings cannot be changed — they are immutable.If you need a different string, you should create a new one:
>>> word[0] = 'J' ... TypeError: 'str' object does not support item assignment
>>> 'J' + word[1:] 'Jython'
The built-in function len()
returns the length of a string: