Python常用知识点

来源:互联网 发布:穷人该不该谈恋爱 知乎 编辑:程序博客网 时间:2024/05/16 07:58

1. for

#!/usr/bin/pythonfor letter in 'chinese':if letter == 'n':passprint "this is a pass block"print 'Current Letter:',letterfor zimu in 'Python':if zimu == 'h':breakprint "current zimu = ",zimuprint "Good bye"

2. while

#!/usr/bin/pythonval = 11while val > 0:print "current value = ",valval = val - 2if val == 3:breakvar = 11while var > 0:print "value = ",varvar = var - 2if var == 3:continueprint "Over end"

3. function

def关键字

#!/usr/bin/pythondef printstr(strs):"this function is passing into a string"print strs;return;printstr("cat and dog")printstr("I like it")

4. list

#!/usr/bin/pythonli=["dog","cat","bull","sheep","pig","chicken","horse"]print li #prints all elementsprint li[0] #print first elementprint li[1] #print second elementprint li[1:3] #print element which index is 1 and 2

5. dict

#!/usr/bin/pythonwords = {}words["yes"] = "1"words["no"] = "0"words["age"] = "25"print words["age"]print words["no"]print wordswords["yes"] = "dui"words["name"] = "yanxia"del words["no"]print words

6. string

#!/usr/bin/pythonsen="this is a book"qa="book"if qa == sen:print('strings equal')if qa in sen:print(qa + " found in " + sen)str="In Python,\nyou can use special characters in strings.\nThese special characters can be..."print(str)

7. tuple

#!/usr/bin/pythonx = (2,3,4,5)x = x + (7,8,1)print(x)lis = ["book","cup","pen","eraser","tea"]tu = tuple(lis) #list->tupleprint(tu)y = (4,5)lis = list(y)   #tuple->listprint(lis)animal = ("cock","rabbit","mouse","hen","fog")str1 = ' '.join(animal)print(str1)per = tuple(sorted(animal))print(per)

8. class

#!/usr/bin/pythonclass JustCounter:__secretCount = 0def count(self):self.__secretCount +=1print self.__secretCountcounter = JustCounter()counter.count()counter.count()


0 0
原创粉丝点击