最近开始学习python,而对于一门编程语言的学习最重要的是进行相应的练习,今天将对于最简单的数据输入与输出问题进行演练,以下所有的代码训练均基于python2.7进行。
print 'hello' print 'hello'+'word' print 'hello','word' print """he said "hello" to me"""
结果输出:
hello
helloword
hello word
he said "hello" to me
x=12 print 'hello x= ',x y= 12.45 z= "word" print "output %d %f %s" %(x,y,z) # 格式化控制字符%d,%f,%s整形,浮点型与字符串型
结果输出:
hello x= 12
output 12 12.450000 word
raw_input(<prompt>)函数的形参prompt是一个字符串用于提示用户输入,当用户输入数据以后函数会把输入的数据传给等号左边的变量来进行保存,函数的返回值是字符串型的。
name=raw_input("please input your name:") print nameplease input your name: hello
hello
x= raw_input("please input x:") y= raw_input("please input y:") z=x+y print zplease input x: 12
please input y: 13
1213
如果想要输出的结果是整数加减运算之后的结果,需要将字符串修改为整型或者浮点型的数值形式,如下:
x=int(raw_input("please input x:")) y=int(raw_input("please input y:")) z=x+y print zplease input x: 12
please input y: 13
25
如果觉得上述函数比较麻烦的话,还有一个简单的input函数也可以直接输入数据,代码如下:
x= input('please input an int:') print x y= input('please input a float:') print yplease input an int: 456
456
please input a float:34.8
34.8