python-An Informal Introduction to Python

1.Numbers

The interpreter acts as a simple calculator: you can type an expression at it an it will write the value.Expression syntax is straightforward: the operators +,-,*and/ work just like in most othe languages(for example,Pascal or C);parenthesses(())can be used for grouping.Fox 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.2, 4, 20)have tppe int, the ones with a fractional part(e.g.5.0, 1.6)have type float.We will see more about numeric types later in the tutorial.

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 return a float
5.666666666666667
>>> 17 // 3  # float division discards the fractional part
5
>>> 17 % 3  # the % operator return the remainder of the division
2 >>> 5 * 3 + 2 # result * divisor + remainder 17

 

With Python, it is possible to use the ** operator to calulate powers:

>>> 5 ** 2   # 5 squared
25
>>> 2 ** 7  # 2 to the power of 7
128

the equal sign(=) is used to assign a value to a varible. 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
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined

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 _. 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 int and float,Python support other types of numbers, such as Decimal and Fraction.Python also has built-in support for comples numbers, and uses j or J suffix to indicate the imaginary part(e.g. 3+5j).

2.Strings

Besides numbers,Python can also manipulate strings, which can be expressed in several ways.They can be enclosed in single quotes('...') or doule quotes(''...") with the same result can used to escape quotes:

>>> 'spam eggs'
'spam eggs'
>>> 'doesn\'t'
"doesn't"
>>> "doesn't"
"doesn't"
>>> '"Yes", they said.'
'"Yes", they said.'
>>> "\"Yes,\" they said."
'"Yes," they said.'
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'

 

>>> print('C:\some\name') #here \n means newline!
C:\some
ame
>>> print(r'C:\some\name')
C:\some\name

 

>>> print("""\
... Usage:thingy[OPTIONS]
... -H
... -h hostname
... """)
Usage:thingy[OPTIONS]
-H
-h hostname
>>> 3 * 'un' + 'ium'
'unununium'

 

>>> 3 * 'un' + 'ium'
'unununium'

Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.

>>> 'Py' 'thon'
'Python'
>>> text = ('Put serval strings within parentheses '
... 'to have them joined together.')
>>> text
'Put serval strings within parentheses to have them joined together.

 

>>> prefix = 'Py'
>>> prefix 'thon'
  File "<stdin>", line 1
    prefix 'thon'
                ^
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
  File "<stdin>", line 1
    ('un' * 3) 'ium'
                   ^
SyntaxError: invalid syntax
>>> prefix + 'thon'
'Python'
>>> word = 'Python'
>>> word[0]
'P'
>>> word[5]
'n'
>>> word[-1]
'n'
>>> word[-2]
'o'
>>> word[-6]
'P'

 

>>> word[0:2]
'Py'
>>> word[2:5]
'tho'
>>> word[:2] + word[2:]
'Python'
>>> word[:2]
'Py'
>>> word[4:]
'on'
>>> word[-2:]
'on'
>>> word[42]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> word[4:42]
'on'
>>> word[42:]

>>> 'J' + word[1:]
'Jython'
>>> word[:2] + 'py'
'Pypy'

 

>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34

 

posted @ 2018-10-18 19:41  zero_zero_zero  阅读(195)  评论(0编辑  收藏  举报