第一个程序 输出hello world
1 #这是一个注释 2 print "Hello world!"
数字和数学运算 + - * / % > <
1 #输入 'linuxroot 19' 2 print "linuxroot", 10 + 18 / 2 3 4 #输出 False 19 比15大 5 print 10 + 9 < 10 + 5 6 7 #模运算 取余数 结果是 1 8 print 5 % 2
变量
1 # 下面定义了一些变量 2 3 my_age = 19 4 5 my_name = 'linuxroot' 6 7 my_work = 'student' 8 9 # '\n'是进行换行输出 10 print "My age is: ",age,"\n" 11 print "and my name is:",name 12 13 """变量名要以字母开头。"""
1 my_age = 19 2 my_name = 'linuxroot' 3 my_work = 'student' 4 5 print "My age is: %d " % my_age 6 print "My name is: %s" % my_name 7 8 #下面这句将输出 my work is: student 9 print "My work is: %s" % my_work 10 11 print "my age is: %d and my name is: %s" % (my_age,my_name) 12 #下面这句将输出 my work is: 'student' 13 print "my work is: %r" % my_work 14 15 """%r 用来做 debug 比较好,因为它会显示变量的原始数据(raw data),而其它的符号则是用来向用户显示输出的。 16 """
打印
1 #定义了一个my_print函数 2 my_print = "%r %r %r %r" 3 4 print my_print % (1, 2, 3, 4) 5 print my_print % ("one", "two", "three", "four") 6 print my_print % (True, False, True, False) 7 print my_print % (my_print, my_print, my_print, my_print) 8 9 days = "Mon Tue Wed Thu Fri Sat Sun" 10 months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug" 11 12 print "Here are the days: ", days 13 print "Here are the moinths:", months 14 # 输出效果用上 只是用法不一样 15 print "Here are the days: %s" % days 16 print "Here are the moinths: %s" % months 17 18 print """ 19 三个双引号是为了书写多行输出 20 这是第三行 21 第四行 22 """
转义字符
PS:需要的时候在去看看吧