## 2011年12月30日 星期五

### [ The python tutorial ] 3. An Informal Introduction to Python

3.1. Using Python as a Calculator
Let’s try some simple Python commands. Start the interpreter and wait for the primary prompt, >>>. (It shouldn’t take long.

- Numbers

>>> 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
5.0
>>> 8/5 # Fractions aren't lost when dividing integers
1.6

>>> # Integer division returns the floor:
... 7//3
2
>>> 7//-3
-3

>>> x = y = z = 0 # Zero x, y and z
>>> x
0
>>> y
0
>>> z
0

>>> # try to access an undefined variable
... n
Traceback (most recent call last):
File "", line 1, in
NameError: name 'n' is not defined

>>> 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)

>>> a=1.5+0.5j
>>> a.real
1.5
>>> a.imag
0.5

>>> 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

>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06

- Strings

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

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."

print(hello)

>>> word
'HelpA'
>>> word[4]
'A'
>>> word[0:2]
'He'
>>> word[2:4]
'lp'

>>> word[:2] # The first two characters
'He'
>>> word[2:] # Everything except the first two characters
'lpA'

Python 的字串是 immutable 的, 也就是你不能使用 [] 改變它的值 :
>>> word[0] = 'x'
Traceback (most recent call last):
File "", line 1, in ?
TypeError: 'str' object does not support item assignment
>>> word[:1] = 'Splat'
Traceback (most recent call last):
File "", line 1, in ?
TypeError: 'str' object does not support slice assignment

>>> word[:2] + word[2:] # s[:i] + s[i:] equals s.
'HelpA'
>>> word[:3] + word[3:]
'HelpA'

>>> word[1:100] # 範圍超過, 頂多印到字串尾
'elpA'
>>> word[10:]
''
>>> word[2:1]
''

>>> word[-1] # The last character
'A'
>>> word[-2] # The last-but-one character
'p'
>>> word[-2:] # The last two characters
'pA'
>>> word[:-2] # Everything except the last two characters
'Hel'

>>> word[-100:]
'HelpA'
>>> word[-10] # error
Traceback (most recent call last):
File "", line 1, in ?
IndexError: string index out of range

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

Sequence Types — str, bytes, bytearray, list, tuple, range
Strings are examples of sequence types, and support the common operations supported by such types.

String Methods
Strings support a large number of methods for basic transformations and searching.

String Formatting
Information about string formatting with str.format() is described here.

Old String Formatting Operations
The old formatting operations invoked when strings and Unicode strings are the left operand of the % operator are described in more detail here.

Unicode 從 Python 3.0 以後開始支援. 在某些時候你需要 Unicode 內的字符時, 可以使用 Unicode-Escape encoding :
>>> 'Hello\u0020World !' # \u0020=0x0020 是空白
'Hello World !'

>>> "李".encode('utf-8')
b'\xe6\x9d\x8e'

- Lists

>>> a
['spam', 'eggs', 100, 1234]
>>> a[2] = a[2] + 23
>>> a
['spam', 'eggs', 123, 1234]

>>> # Replace some items:
... a[0:2] = [1, 12]
>>> a
[1, 12, 123, 1234]
>>> # Remove some:
... a[0:2] = []
>>> a
[123, 1234]
>>> # Insert some:
... a[1:1] = ['bletch', 'xyzzy']
>>> a
[123, 'bletch', 'xyzzy', 1234]
>>> # Insert (a copy of) itself at the beginning
>>> a[:0] = a
>>> a
[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
>>> # Clear the list: replace all items with an empty list
>>> a[:] = []
>>> a
[]

>>> a = ['a', 'b', 'c', 'd']
>>> len(a)
4

>>> q = [2, 3]
>>> p = [1, q, 4]
>>> len(p)
3
>>> p[1]
[2, 3]
>>> p[1][0]
2

>>> p[1].append('xtra')
>>> p
[1, [2, 3, 'xtra'], 4]
>>> q # 因為是 shallow copy, 所以原有串列的值也會改變
[2, 3, 'xtra']

3.2 First Steps Towards Programming

1. a, b = 01

1. int t = b
2. b = a + b
3. a = t

### [ Python 常見問題 ] python generator “send” function purpose?

Source From  Here   Question   Can someone give me an example of why the "send" function associated with Python generator function...