1. for
#!/usr/bin/python
for letter in 'chinese':
if letter == 'n':
pass
print "this is a pass block"
print 'Current Letter:',letter
for zimu in 'Python':
if zimu == 'h':
break
print "current zimu = ",zimu
print "Good bye"
2. while
#!/usr/bin/python
val = 11
while val > 0:
print "current value = ",val
val = val - 2
if val == 3:
break
var = 11
while var > 0:
print "value = ",var
var = var - 2
if var == 3:
continue
print "Over end"
3. function
def关键字
#!/usr/bin/python
def printstr(strs):
"this function is passing into a string"
print strs;
return;
printstr("cat and dog")
printstr("I like it")
4. list
#!/usr/bin/python
li=["dog","cat","bull","sheep","pig","chicken","horse"]
print li #prints all elements
print li[0] #print first element
print li[1] #print second element
print li[1:3] #print element which index is 1 and 2
5. dict
#!/usr/bin/python
words = {}
words["yes"] = "1"
words["no"] = "0"
words["age"] = "25"
print words["age"]
print words["no"]
print words
words["yes"] = "dui"
words["name"] = "yanxia"
del words["no"]
print words
6. string
#!/usr/bin/python
sen="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/python
x = (2,3,4,5)
x = x + (7,8,1)
print(x)
lis = ["book","cup","pen","eraser","tea"]
tu = tuple(lis) #list->tuple
print(tu)
y = (4,5)
lis = list(y) #tuple->list
print(lis)
animal = ("cock","rabbit","mouse","hen","fog")
str1 = ' '.join(animal)
print(str1)
per = tuple(sorted(animal))
print(per)
8. class
#!/usr/bin/python
class JustCounter:
__secretCount = 0
def count(self):
self.__secretCount +=1
print self.__secretCount
counter = JustCounter()
counter.count()
counter.count()