1、

#-*- coding:utf-8 -*-#练习1,主要练习使用print以及单双引号,若有中文等需要添加第一行指明utf-8print "Hello World!"print "Hello Again"#print "I like typing this."print  "This is fun.",print  'Yay! Print.'print "I'd much rather you 'not'."print "                       "print 'I "said" do not touch this.'print '嘿!“外星人”~'

2、

#说明注释#的作用# A comment, this is so you can read your program later.# Anything after the # is ignored by python.print "I could have code like this." # and the comment after is ignored# You can also use a comment to "disable" or comment out a piece of code:# print "This won't run."print "This will run."

3、

#演示各种运算符#-*- coding:utf-8 -*-print "我要数我的小鸡们:"print "母鸡们" ,25+30/6print "公鸡们" , 100-25*3%4print "现在我要数我的鸡蛋们:"print 3+2+1-5+4%2-1/4+6print "Is it true that 3+2<5-7?"print 3+2<5-7print "What is 3 + 2?", 3 + 2print "What is 5 - 7?", 5 - 7print "Oh, that's why it's False."print "How about some more."print "Is it greater?", 5 > -2print "Is it greater or equal?", 5 >= -2print "Is it less or equal?", 5 <= -2

4、

#变量#!/usr/bin/python#-*- coding:utf-8 -*-my_name = 'tom'my_age = 2     #Too Oldmy_height = 171 #米my_weight = 67 #kgmy_eyes = "黑色"my_teeth = "白色"my_hair = "黑色"print "Let's talk about %s." % my_nameprint "%s的身高%d米。" %(my_name, my_height),print "%s的体重%d公斤。" %(my_name, my_weight)# this line is tricky, try to get it exactly rightprint "If I add %d, %d, and %d I get %d." % (            my_age, my_height, my_weight, my_age + my_height + my_weight)

5、

#变量#!/usr/bin/python#-*- coding:utf-8 -*-x = "There are %d types of people." % 10 binary = "binary"do_not = "don't"y = "There who know %s and those who %s." % (binary,do_not)print xprint yprint "I said: %r." % xprint "I also said : '%s'." % yhilarious = Falsejoke_evaluation = "Isn't that joke so funny?! %r"print joke_evaluation % hilariousw = "This is the left side of..."e = "a string with a right side."print w + e

6、

#!/usr/bin/python#-*- coding:utf-8 -*-text = "I am %d years old" %22print "I said:%s." % textprint "I said:%r."% text

7、

#!/usr/bin/python#-*- coding:utf-8 -*-print "Mary had a little lamb."print "It's fleece was white as %s." % 'snow'print "And everywhere that Mary went."print "@" * 10 #what'd that do?end1 = "c"end2 = "h"end3 = "e"end4 = "e"end5 = "s"end6 = "e"end7 = "B"end8 = "u"end9 = "r"end10 = "g"end11 = "e"end12 = "r"#watch that comma at the end.try moving it to see what happensprint end1 + end2 + end3 + end4 + end5 + end6 ,print end7 + end8 + end9 + end10 + end11 + end12

8、

#!/usr/bin/python#-*- coding:utf-8 -*-formatter = "%s %s %s %s"print formatter % (1,2,3,4)print formatter % ("one","two","three","four")print formatter % (True,False,False,True)print formatter % (formatter,formatter,formatter,formatter)print formatter % (    "I had this thing.",    "That you could type up right.",    "But it didn't sing.",    "So I said goodnight")print formatter % ("双引号","单引号","字符","用引号")

9、

#转义符#!/usr/bin/python#-*- coding:utf-8 -*-#Here's some new strange stuff,remember type it exactly.days = "Mon Tue Wed Thu Fri Sta Sun"months  = "\nJan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"print "Here are the days: ", daysprint "Here are the months: ", monthsprint """There's something going on here.With the three double-quotes.We'll be able to type as much as we like.Even 4 lines if we want, or 5, or 6."""

10、

#!/usr/bin/pythontabby_cat = "\tI'm tabbed in."persian_cat = "I'm split\non a line."backslash_cat = "I'm \\a \\ cat."fat_cat = '''I'll do a list:\t* Cat food\t* Fishies\t* Catnip\n\t* Grass'''print tabby_catprint persian_catprint backslash_catprint fat_catwhile True:    #死循环    for i in ["/","-","|","|","@","!","1"]: #什么时间用冒号?        print "%s\r" % i,   #不加逗号是不一样的,改%s为%r也不一样