Python初始學習必要掌握知識點(5分鐘速學)

來源:互聯網
上載者:User

標籤:學習   基礎   程式員   編程菜鳥   

1、字串
python中單引號和雙引號使用完全相同。使用三引號(‘‘‘或""")可以指定一個多行字串。轉義符 ‘\‘反斜線可以用來轉義,使用r可以讓反斜線不發生轉義。。 如 r"this is a line with \n" 則\n會顯示,並不是換行。按字面意義級聯字串,如"this " "is " "string"會被自動轉換為this is string。字串可以用 + 運算子串連在一起,用 * 運算子重複。Python 中的字串有兩種索引方式,從左往右以 0 開始,從右往左以 -1 開始。Python中的字串不能改變。Python 沒有單獨的字元類型,一個字元就是長度為 1 的字串。字串的截取的文法格式如下:變數[頭下標:尾下標]
#!/usr/bin/python3str=‘Runoob‘print(str)                 # 輸出字串print(str[0:-1])           # 輸出第一個到倒數第二個的所有字元print(str[0])              # 輸出字串第一個字元print(str[2:5])            # 輸出從第三個開始到第五個的字元print(str[2:])             # 輸出從第三個開始的後的所有字元print(str * 2)             # 輸出字串兩次print(str + ‘你好‘)        # 連接字串print(‘------------------------------‘)print(‘hello\nrunoob‘)      # 使用反斜線(\)+n轉義特殊字元print(r‘hello\nrunoob‘)     # 在字串前面添加一個 r,表示原始字串,不會發生轉義#列印結果如下RunoobRunooRnoonoobRunoobRunoobRunoob你好------------------------------hellorunoobhello\nrunoob

我分享一個我的群:725479218,群裡面有提供學習的地址,有想要學習的可以一起學習,還可提供技術交流

2、換行/不換行
print 預設輸出是換行的,如果要實現不換行需要在變數末尾加上 end="":
s = "lamxqx"print(s[1:3],end="")print(s*2)結果:amlamxqxlamxqx------------------------------------------s = "lamxqx"print(s[1:3])print(s*2)結果:amlamxqxlamxqx
3、判斷類型
lass A:    passclass B(A):    passisinstance(A(), A)  # returns Truetype(A()) == A      # returns Trueisinstance(B(), A)    # returns Truetype(B()) == A        # returns False--------------------------------------------區別:type()不會認為子類是一種父類類型。isinstance()會認為子類是一種父類類型
物件導向
例子--------------------------------------------------------------#!/usr/bin/python# -*- coding: UTF-8 -*-class Employee:   ‘所有員工的基類‘   empCount = 0   def __init__(self, name, salary):      self.name = name      self.salary = salary      Employee.empCount += 1   def displayCount(self):     print "Total Employee %d" % Employee.empCount   def displayEmployee(self):      print "Name : ", self.name,  ", Salary: ", self.salary"建立 Employee 類的第一個對象"emp1 = Employee("Zara", 2000)"建立 Employee 類的第二個對象"emp2 = Employee("Manni", 5000)emp1.displayEmployee()emp2.displayEmployee()print "Total Employee %d" % Employee.empCount-------------------------------------------------------知識點:1、empCount 變數是一個類變數,它的值將在這個類的所有執行個體之間共用。你可以在內部類或外部類使用 Employee.empCount 訪問。2、第一種方法__init__()方法是一種特殊的方法,被稱為類的建構函式或初始化方法,當建立了這個類的執行個體時就會調用該方法3、self 代表類的執行個體,self 在定義類的方法時是必須有的,雖然在調用時不必傳入相應的參數。執行以上代碼輸出結果如下:Name :  Zara ,Salary:  2000Name :  Manni ,Salary:  5000Total Employee 2你可以添加,刪除,修改類的屬性,如下所示:emp1.age = 7  # 添加一個 ‘age‘ 屬性emp1.age = 8  # 修改 ‘age‘ 屬性del emp1.age  # 刪除 ‘age‘ 屬性你也可以使用以下函數的方式來訪問屬性:getattr(obj, name[, default]) : 訪問對象的屬性。hasattr(obj,name) : 檢查是否存在一個屬性。setattr(obj,name,value) : 設定一個屬性。如果屬性不存在,會建立一個新屬性。delattr(obj, name) : 刪除屬性。hasattr(emp1, ‘age‘)    # 如果存在 ‘age‘ 屬性返回 True。getattr(emp1, ‘age‘)    # 返回 ‘age‘ 屬性的值setattr(emp1, ‘age‘, 8) # 添加屬性 ‘age‘ 值為 8delattr(emp1, ‘age‘)    # 刪除屬性 ‘age‘----------------------------------------------------------Python內建類屬性__dict__ : 類的屬性(包含一個字典,由類的資料屬性群組成)__doc__ :類的文檔字串__name__: 類名__module__: 類定義所在的模組(類的全名是‘__main__.className‘,如果類位於一個匯入模組mymod中,那麼className.__module__ 等於 mymod)__bases__ : 類的所有父類構成元素(包含了一個由所有父類組成的元組)Python內建類屬性調用執行個體如下:執行個體#!/usr/bin/python# -*- coding: UTF-8 -*-class Employee:   ‘所有員工的基類‘   empCount = 0   def __init__(self, name, salary):      self.name = name      self.salary = salary      Employee.empCount += 1   def displayCount(self):     print "Total Employee %d" % Employee.empCount   def displayEmployee(self):      print "Name : ", self.name,  ", Salary: ", self.salaryprint "Employee.__doc__:", Employee.__doc__print "Employee.__name__:", Employee.__name__print "Employee.__module__:", Employee.__module__print "Employee.__bases__:", Employee.__bases__print "Employee.__dict__:", Employee.__dict__執行以上代碼輸出結果如下:Employee.__doc__: 所有員工的基類Employee.__name__: EmployeeEmployee.__module__: __main__Employee.__bases__: ()Employee.__dict__: {‘__module__‘: ‘__main__‘, ‘displayCount‘: <function displayCount at 0x10a939c80>, ‘empCount‘: 0, ‘displayEmployee‘: <function displayEmployee at 0x10a93caa0>, ‘__doc__‘: ‘\xe6\x89\x80\xe6\x9c\x89\xe5\x91\x98\xe5\xb7\xa5\xe7\x9a\x84\xe5\x9f\xba\xe7\xb1\xbb‘, ‘__init__‘: <function __init__ at 0x10a939578>}

Python初始學習必要掌握知識點(5分鐘速學)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.