Python is a dynamic type language, and the language characteristic of a type language determines how complicated he is. #简单的输出打印 #coding=utf-8import time; # This was 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 can not be assigned only readtuple = (' ABCD ', 786, 2.23, ' John ', 70.2) Tinytuple = (123, ' Jo HN ') print tuple + tinytuple# array Operation dict = {}dict[' one '] = "This was one" dict[2] = "This is one" tinydict = {' name ': ' John ', ' Co De ': 6734, ' dept ': ' Sales '}print tinydictprint dictprint chr #运算符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 has same identity "else:print" line 1- A and B do not have same identity "#for loop for letter in" Abcdefghi ": Print Letter #遍历Listfruits = [' banana ', ' apple ' , ' Mango ']for iNdex in range (len (fruits)): print ' current fruit: ', Fruits[index] #遍历trupl by index loop TP = (' banana ', ' apple ', ' mango ') for Index in range (len): 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# definition function 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# even if an exception occurs finally or can be performed #raise trigger an exception 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", one-to-one) Student.showinfo () #获取类的属性 #coding=utf-8#!/usr/bin/pythonclass Employee: ' base class for all employees ' Empcount = 0 def __init__(Self, name, salary): Self.name = name Self.salary = Salary Employee.empcount + = 1 def displaycount (SE LF): print "Total Employee%d"% Employee.empcount def displayemployee (self): print "Name:", Self.name, ", S Alary: ", 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__# dynamic type operation #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=d ATA ("Tom") def F (): print "AAAAAAA" Data.fun=fdata.fun () data.age=100print data.age# custom __str__ output print data
Python Basics Exercises