python基礎練習

來源:互聯網
上載者:User

標籤:

Python是動態類型語言 ,也是若類型語言這種 語言特性就決定了 他不會有多麼的複雜。。 #簡單的輸出列印#coding=utf-8import time;  # This is required to include time module.word = 'word'sentence = "This is a sentence."paragraph = """This is a paragraph. It ismade up of multiple lines and sentences."""print paragrapha,b,c=1,2,"aaa"#List類似數組list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]if word=="word":    print paragraph[1:10]*2list[1]="aaa"print list[1:2]#tuple類似 list 不可以賦值 只能readtuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )tinytuple = (123, 'john')print tuple + tinytuple#元數組操作dict = {}dict['one'] = "This is one"dict[2] = "This is two"tinydict = {'name': 'john','code':6734, 'dept': 'sales'}print tinydictprint dictprint chr(100)#運算子print 5**100if not (1>1 and 2<2):    print "aaaa"else:    print "hjjjjjjj"a = 20b = 20if ( a is b ):   print "Line 1 - a and b have same identity"else:   print "Line 1 - a and b do not have same identity"#for 迴圈  for letter in  "abcdefghi":    print letter  #遍曆Listfruits = ['banana', 'apple',  'mango']for index in range(len(fruits)):   print 'Current fruit :', fruits[index]#遍曆trupl  通過索引迴圈tp = ('banana', 'apple',  'mango')for index in range(len(tp)):   print 'Current fruit :', tp[index]#遍曆元欄位dict={"a":1,"b":2}  for index in dict:   print index,':', dict[index]    #字典長度print len(dict)  #列出banana元素出現的次數print fruits.count('banana')#timeticks = time.time()print ticks#定義函數def  printList(list):    for i in range(len(list)):        print list[i]    print locals()printList(fruits)#異常處理try:    print 2/0except Exception:    print "ssssssss"else:    print "no exception"#try finally#即使出現異常finally還是能執行的#raise觸發異常try:    print 1/0  finally:    print "finally"#元組進行格式化    #coding=utf-8class Student:    name=""    age =0    def showInfo(self):        print "Name:%s,Age:%s" % (self.name,self.age)    def __init__(self,name,age):        self.name=name        self.age=agestudent=Student("aaa",11)  student.showInfo()#擷取類的屬性#coding=utf-8#!/usr/bin/pythonclass 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__#動態類型操作       #codeing=utf-8class Data:    name =""    def __init__(self,name):        self.name=name        print "__init__"    def __del__(self):        print "__del__"    def __str__( self ):        return self.namedata=Data("tom")def F():    print "aaaaaaa"data.fun=Fdata.fun()data.age=100print data.age#自訂__str__輸出print data


python基礎練習

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.