Python學習筆記(四)

元組

  • 元組是不可變序列.
  • 元組提供了一種完整性的約束.
>>> test=(1,2,3,4)
>>> len(test)
4
>>> test+(6,5)
(1, 2, 3, 4, 6, 5)
>>> test[0]
1
>>> test[3]
4
>>> test[0]=999
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

文件

  • 調用內置的open函數創建一個文件對象,以字符串的形式傳遞給它的一個外部的文件名以及一個處理模式的字符串.
>>> test=open('data.txt','w')   #寫入權限
>>> test.write('Hello\n')
>>> test.write('world\n')
>>> test.close()    #與open對應
>>> test=open('data.txt')   #'r'是默認模式
>>> bytes=test.read()
>>> bytes
'Hello\nworld\n'
>>> print bytes
Hello
world
>>> bytes.split()
['Hello', 'world']
  • 文件對象提供了多種讀和寫的方法(read,readline),預覽對象方法可以使用dir(file),並可對任何輸出的變量名十一歐諾個help.
>>> dir(file)
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']
>>> help(file.name)
  • open函數能偶實現在python中編寫絕大多數文件處理.對於更高級的任務,python的而外的文件類工具:pipes,fifos,sockets,keyed-access,files,對象持久,基於描述符的文件,關係型數據庫和麪向對象數據庫接口

其他核心類型

  • 集合:通過調用內置set函數而創建的對象的容器.支持數學集合操作:
>>> s=set('spam')
>>> j=set(['h','a','m'])
>>> s,j
(set(['a', 'p', 's', 'm']), set(['a', 'h', 'm']))
>>> s&j
set(['a', 'm'])
>>> s|j
set(['a', 'p', 's', 'h', 'm'])
>>> s-j
set(['p', 's'])
  • 十進制數(固定精度浮點數)和布爾值,以及佔位符對象None:
>>> reload(decimal)
<module 'decimal' from '/usr/lib/python2.7/decimal.pyc'>
>>> d=decimal.Decimal('3.141')
>>> d+1
Decimal('4.141')
>>> 1>3,33>1
(False, True)
>>> bool("Helloworld")
True
>>> bool('spam')
True
>>> bool()
False
>>> x=None
>>> print x
None
>>> test=[None]* 4
>>> test
[None, None, None, None]
>>> type(ds)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'ds' is not defined
>>> type(test)
<type 'list'>
>>> type(type(test))
<type 'type'>
  • 檢驗代碼的類型
>>> if type(test)==type([]):print 'yes'
... 
yes
>>> test
[None, None, None, None]
>>> if type(test)==list:print 'yes'
... 
yes
>>> if isinstance(test,list):print 'yes'
... 
yes
  • 用戶定義的類:python是面向對象編程(可選但很強大的特性.可以通過支持程序定製而節省開發時間)
>>> class Worker:
...     def __init__(self,name,pay):
...             self.name=name
...             self.pay=pay
...     def lastName(self):
...             return self.name.split()[-1]    #以空格分隔
...     def giveRaise(self,percent):
...             self.pay *= (1.0 + percent)
... #定義一個類,self是一個新對象
>>> bob = Worker('Bob Smith',5000)
>>> sue=Worker('Sue Jones',6000)
>>> bob.lastName()
'Smith'
>>> sue.lastName()
'Jones'
>>> sue.giveRaise(.10)
>>> sue.pay
6600.000000000001
>>> sue.giveRaise(.10)
>>> sue.pay
6600.000000000001   #這個類定義了一個新的對象的種類,有name和pay兩個屬性(狀態),有兩個行爲編寫爲函數的形式.就像函數那樣去調用類,會生成新類型的一個實例.並且類的方法調用時,類的方法自動截獲被處理的實例(self參數)
  • 採用”self”對象,一個類中的函數總有一個隱含的對象.類是Python科學的一個特性.
發佈了73 篇原創文章 · 獲贊 43 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章