print("hello world")print("Hens",25+30/6)print(3+2+1-5+4%2-1/4+6)print("Is it greater ?",5>=-2)print("It's fleece was white as %s."%'snow')print("."*10)
formatter ="%s %s %s %ss"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","but it did","so i said"))print("""
there is something going on here.
with the three double-quotes.
we will be able to type as much as we like
even 4 line if we want, or 5, or 6.
"""
输入
print("how old are you?", end=' '),
age =input()
age =input("How old are you? ")
参数
from sys import argv
script, first, second, third = argv
print("script is: ", script)print("1st var is: ", first)print("2nd var is: ", second)print("3rd var is: ", third)
读写文件
# 读取from sys import argv
script, filename = argv
txt =open(filename)print("here is you file %r"% filename)print(txt.read())
txt.close()# 读写from sys import argv
script, filename = argv
print("we are going to erase %r."% filename)print("if you don't want that, hit ctrl + c.")print("if you do want that, hit enter")input("?")print("opening the file")
target =open(filename,'w')print("truncating the file. goodbye!")
target.truncate()print("now i am going to ask you for three lines.")
line1 =input("1: ")
line2 =input("2: ")
line3 =input("3: ")print("i am going to write these lines to file.")
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")print("finally, we close it")
target.close()# 文件复制from sys import argv
from os.path import exists
script, from_file, to_file = argv
print("copying from %s to %s"%(from_file, to_file))raw_input=open(from_file)
indata =raw_input.read()print("the input file is %d bytes long"%len(indata))print("does the output file exist? %r "% exists(to_file))print("ready, hit enter to continue, ctrl+c to abort.")input()
output =open(to_file,'w')
output.write(indata)print("ok, all done")
output.close()raw_input.close()
定义函数
# 简单函数defprint_two(*args):
arg1, arg2 = args
print("arg1: %r, arg2: %r"%(arg1, arg2))defprint_two_again(arg1, arg2):print("arg1: %r, arg2: %r"%(arg1, arg2))defprint_one(arg1):print("arg1: %r"% arg1)defprint_none():print("i got nothing")
print_two("zed","shaw")
print_two_again("zed","shaw")
print_one("first")
print_none()# 实现算术运算defadd(a, b):print("add %d + %d"%(a, b))return a + b
defsub(a, b):print("sub %d - %d"%(a, b))return a - b
defmul(a, b):print("mul %d * %d"%(a, b))return a * b
defdiv(a, b):print("div %d / %d"%(a, b))return a / b
print("let's do some math with just functions")
age = add(30,5)
height = sub(78,4)
weight = mul(30,2)
iq = div(100,2)print("values: ", age, height, weight, iq)print("here is a puzzle")
what = add(age, sub(height, mul(weight, div(iq,2))))print("values: ", what," you right?")
分支
people =20
cats =30
dogs =15if people < cats:print("too many cats")if people > cats:print("not too many cats")if people < dogs:print("too many dogs")if people > dogs:print("ok")
dogs +=5if people >= dogs:print("people are greater than equal to dogs")if people <= dogs:print("people are less than or equal to dogs")if people == dogs:print("people are dogs")
循环
the_count =[1,2,3,4,5]
fruits =['apples','oranges','pears','apricots']
change =[1,'pennies',2,'dimes',3,'quarters']for number in the_count:print("this is count %d"% number)for fruit in fruits:print("a fruit of type: %s"% fruit)for i in change:print("i got %r"% i)
elements =[]for i inrange(0,6):print("adding %d to the list."% i)
elements.append(i)for i in elements:print("element was: %d"%i)