安裝pydev
+ eclips
1.安裝java-jdk
2.設定環境變數,用cmd-java看是否安裝成功
3.下載Eclipse
IDE for C/C++ Developers
|
class
DerivedClassName(BaseClassName):
<statement-1>
.
.
.
<statement-N>
|
|
class
Father:
def
__init__(self,f_name,f_age):
self.f_name
= f_name
self.f_age
= f_age
def
f_info(self):
print("papa
name:",self.f_name,"papa age:",self.f_age)
class
Child(Father):
def
__init__(self,c_name,c_age,f_name,f_age):
Father.__init__(self,f_name,f_age)
self.c_name
= c_name
self.c_age
= c_age
self.f_info()
def
fam_info(self):
print("age
sum:",self.f_age+self.c_age)
|
|
class
Father(object):
def
__init__(self,f_name,f_age):
self.f_name
= f_name
self.f_age
= f_age
def
f_info(self):
print("papa
name:",self.f_name,"papa age:",self.f_age)
class
Child(Father):
def
__init__(self,c_name,c_age,f_name,f_age):
super(Child,self).__init__(f_name,f_age)
self.c_name
= c_name
self.c_age
= c_age
self.f_info()
def
fam_info(self):
print("age
sum:",self.f_age+self.c_age)
|
|
class
Father:
def __init__(self,f_name,f_age):
self.f_name = f_name
self.f_age = f_age
def f_info(self):
print("papa name:",self.f_name,"papa
age:",self.f_age)
class
Mother:
def __init__(self,m_name,m_age):
self.m_name = m_name
self.m_age = m_age
def m_info(self):
print("mama name:",self.m_name,"mama
age:",self.m_age)
class
Child(Father,Mother):
def
__init__(self,c_name,c_age,f_name,f_age,m_name,m_age):
Father.__init__(self,f_name,f_age)
Mother.__init__(self,m_name,m_age)
self.c_name = c_name
self.c_age = c_age
self.f_info()
self.m_info()
def fam_info(self):
print("age
sum:",self.f_age+self.m_age+self.c_age)
|
|
class
Mother(object):
def
__init__(self,m_name,m_age):
self.m_name
= m_name
self.m_age
= m_age
def
m_info(self):
print("mama
name:",self.m_name,"mama age:",self.m_age)
class
Father(Mother):
def
__init__(self,f_name,f_age,m_name,m_age):
super(Father,self).__init__(m_name,m_age)
self.f_name
= f_name
self.f_age
= f_age
def
f_info(self):
print("papa
name:",self.f_name,"papa age:",self.f_age)
class
Child(Father,Mother):
def
__init__(self,c_name,c_age,f_name,f_age,m_name,m_age):
super(Child,self).__init__(f_name,f_age,m_name,m_age)
self.c_name
= c_name
self.c_age
= c_age
self.f_info()
self.m_info()
def
fam_info(self):
print("age
sum:",self.f_age+self.m_age+self.c_age)
|
|
class
A(object):
def
__init__(self):
super(A,self).__init__()
print
"A"
class B(A):
def
__init__(self):
super(B,self).__init__()
print
"B"
class C(A):
def
__init__(self):
super(C,self).__init__()
print
"C"
class
D(object):
def
__init__(self):
super(D,self).__init__()
print
"D"
class
E(B,C,D):
def
__init__(self):
super(E,self).__init__()
print
"E"
|
|
class
A(object):
def
__init__(self):
print
"A"
class B(A):
def
__init__(self):
print
"B"
class C(A):
def
__init__(self):
print
"C"
class
D(object):
def
__init__(self):
print
"D"
class
E(B,C,A,D):
def
__init__(self):
D.__init__(self)
A.__init__(self)
C.__init__(self)
B.__init__(self)
print
"E"
|
def
max(a, b): return
a
if
a
> b else
b |
lambda
a, b: a if
a
< b else
b
|
min
= lambda a, b: a if
a
< b else
bminimum
= minmin(10,
20) # 傳回10 |
modu,原始碼主檔名就是模組名稱。import
modu 陳述句會在相同目錄下尋找
modu.py,如果沒找到,則會試著尋找在 sys.path中遞迴地尋找
modu.py,如果還是沒有,則會引發 ImportError 例外。import、import
as 與 from
import 是陳述句
Note
可以利用imp.reload()來自動載入更新過的檔案
For
efficiency reasons, each module is only imported once per
interpreter session. Therefore, if you change your modules, you
must restart the interpreter – or, if it’s just one module
you
want to test interactively, use imp.reload(),
e.g.import imp; imp.reload(modulename).
|
if __name__ == "__main__"
__name__ variable
to have a value "__main__".
If this file is being imported from another module, __name__ will
be set to the module's name.
#!/usr/bin/python
# Filename:
using_name.py
if __name__
== '__main__':
print
'This program is being run by itself'
else:
print 'I
am being imported from another module'
|
>>>
========================= RESTART ================================
>>>
This
program is being run by itself
>>>
import using_name
I am being
imported from another module
>>>
using_name.__name__
'using_name'
>>>
|
class Account:
def __init__(self, number, name):
self.number = number
self.name = name
self.balance = 0
def deposit(self, amount):
if amount <= 0:
raise ValueError('must be positive')
self.balance += amount
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
else:
raise RuntimeError('balance not enough')
|
>>>
acct = Account('123456', 'mary')
>>>
acct.number
'123456'
>>>
acct.name
'mary'
>>>
acct.deposit(100)
>>>
acct.balance
100
>>>
acct.withdraw(50)
>>>
acct.balance
50
|
import
pack.modu 陳述時,Python
會尋找
pack
目錄,看看裏頭是否有
__init__.py
檔案,然後看看目錄中是否有個
modu.py
檔案。__init__.py
檔案空白也無所謂,實際上當中也可以寫些程式碼,用來執行這個套件中都會需要的初始工作,不過慣例上,除非你有真正不得已的理由,請保持
__init__.py
檔案空白。在找到模組後,實際上會執行其中頂層範疇中的程式碼,之後,模組中的變數、函式、類別等名稱,可以透過 pack.modu 來取得。sound/ Top-level package __init__.py Initialize the sound package formats/ Subpackage for file format conversions __init__.py wavread.py wavwrite.py aiffread.py aiffwrite.py auread.py auwrite.py ... effects/ Subpackage for sound effects __init__.py echo.py surround.py reverse.py ... filters/ Subpackage for filters __init__.py equalizer.py vocoder.py karaoke.py ... |
import sound.effects.echo sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)
from sound.effects import echo echo.echofilter(input, output, delay=0.7, atten=4)
from sound.effects.echo import echofilter echofilter(input, output, delay=0.7, atten=4)
>>> 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
** 2 # 5 squared,雙星號平方
25
|
>>>
tax = 12.5 / 100
>>>
price = 100.50
>>>
price * tax
12.5625
>>>
price + _ #
變數_,可以用來表示最後的運算結果
113.0625
|
>>> a
= 5
>>> a
+= 1 #
連加不使用++,
而是+=
>>> a
6
|
>>>
'spam eggs' # single quotes
'spam eggs'
>>>
"doesn't" # ...or use double quotes instead
"doesn't"
>>>
"\"Yes,\" he said."
'"Yes," he said.'
|
>>>
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
|
print("""\
Usage: thingy
[OPTIONS]
-h
Display this usage message
-H
hostname Hostname to connect to
""")
|
>>> 3
* 'un' + 'ium'
'unununium'
|
+---+---+---+---+---+---+
| P | y | t |
h | o | n |
+---+---+---+---+---+---+
0 1 2 3
4 5 6
-6 -5 -4 -3
-2 -1
>>>
word[:2] # character from the beginning to position 2 (excluded)
'Py'
>>>
word[-2:] # characters from the second-last (included) to the end
'on'
|
>>>
word[0] = 'J'
...
TypeError: 'str' object does not
support item assignment
|
>>> s
= 'supercalifragilisticexpialidocious'
>>>
len(s)
34
|
>>> cubes = [1, 8, 27, 65,
125] # something's wrong here
>>> 4 ** 3 # the cube of
4 is 64, not 65!
64
>>> cubes[3] = 64 #
replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
|
>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
|
>>> a = [1,4,2,6]
>>> a.append(3) # 增加數值
>>> a
[1, 4, 2, 6, 3]
>>> a.remove(4) # 移除數值
>>> a
[1, 2, 6, 3]
>>> a.pop() # 移除最後一位的數值
3
>>> a
[1, 2, 6]
>>> a.sort() # 排序
>>> a
[1, 2, 6]
>>> a.reverse() # 順序顛倒
>>> a
[6, 2, 1]
>>>
>>> a.index(1) #
列出數值6的index
2
>>> del a[0] # 移除引數0
>>> a
[2, 1]
>>>
|
>>> from collections
import deque
>>> queue = deque(["Eric",
"John", "Michael"])
>>> queue.append("Terry")
# Terry arrives
>>> queue.append("Graham")
# Graham arrives
>>> queue.popleft()
# The first to arrive now leaves
'Eric'
>>> queue.popleft()
# The second to arrive now leaves
'John'
>>> queue
# Remaining queue in order of arrival
deque(['Michael', 'Terry',
'Graham'])
|
>>> for i in range(5):
... print(i)
...
0
1
2
3
4
|
range(5, 10)
5 through 9
range(0, 10, 3)
0, 3, 6, 9
range(-10, -100, -30)
-10, -40, -70
|
>>> t = 123,456,'hihi'
>>> t[1]
456
>>> t[1] = 123
Traceback (most recent call last):
File "<pyshell#2>",
line 1, in <module>
t[1] = 123
TypeError: 'tuple' object does not
support item assignment
>>> t
(123, 456, 'hihi')
>>>
|
>>> t = 2,5,4,8,6
>>> t
(2, 5, 4, 8, 6)
>>> t.sort()
Traceback (most recent call last):
File "<pyshell#14>",
line 1, in <module>
t.sort()
AttributeError: 'tuple' object has
no attribute 'sort'
>>> t1 = list(t)
>>> t1.sort()
>>> t1
[2, 4, 5, 6, 8]
>>>
|
>>> basket = {'apple',
'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket)
# show that duplicates have been removed
{'orange', 'banana', 'pear',
'apple'}
>>> 'orange' in basket
# fast membership testing
True
>>> 'crabgrass' in basket
False
>>> # Demonstrate set
operations on unique letters from two words
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a
# unique letters in a
{'a', 'r', 'b', 'c', 'd'}
>>> a - b
# letters in a but not in b
{'r', 'd', 'b'}
>>> a | b
# letters in either a or b
{'a', 'c', 'r', 'd', 'b', 'm', 'z',
'l'}
>>> a & b
# letters in both a and b
{'a', 'c'}
>>> a ^ b
# letters in a or b but not both
{'r', 'd', 'b', 'm', 'z', 'l'}
|
>>> tel = {'jack': 4098,
'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127,
'jack': 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127,
'jack': 4098}
>>> list(tel.keys())
['irv', 'guido', 'jack']
>>> sorted(tel.keys())
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False
|