1.python程序基本组成练习

       最近开始学习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 name
please input your name: hello

hello

x= raw_input("please input x:")
y= raw_input("please input y:")
z=x+y
print z
please 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 z
please input x: 12
please input y: 13

25

      如果觉得上述函数比较麻烦的话,还有一个简单的input函数也可以直接输入数据,代码如下:

x= input('please input an int:')
print x
y= input('please input a float:')
print y
please input an int: 456
456
please input a float:34.8

34.8



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值