python的输入输出

 

一、python中的输入输出

 1、python3.x的输入输出

# python3.x
# input():接收任意数据类型 ---str
# 没有raw_input()
>>> input('Num:')
Num:2
'2'
>>> input('Num:')
Num:abc
'abc'
>>> input('Num:')
Num:1.2
'1.2'


 

 

 

 

2、输入信息不回显

 

# 输入信息不回显
>>> import getpass
>>> num = getpass.getpass('请输入密码:')
请输入密码:
>>> num
'123'

 

 

3、 python2.x的输入格式特点


# python2.x
# input():只支持输入正确的数据类型
# raw_input():接收任意数据类型 ---str
>>> input('Num:')
Num:2
2
>>> input('Num:')
Num:redhat
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'redhat' is not defined
>>> input('Num:')
Num:'redhat'
'redhat'
>>> input('Num:')
Num:True
True
>>> raw_input('Num:')
Num:redhat
'redhat'
>>> raw_input('Num:')
Num:1.2
'1.2'
>>> raw_input('Num:')
Num:2
'2'

 

 

 

4、不同数据之间的转换

# 如果接收的数值要进行比较的时候,一定要转换为同一数据类型
>>> age = input('age:')
age:19
>>> age
'19'
>>> age > 19
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'
>>> age = int(input('age:'))
age:19
>>> age > 18
True

 

 

 

 

 

 

二、 python的格式化输出

1、 %s:代表字符串的占位 %d:整型

# %s:代表字符串的占位 %d:整型
>>> name ='westos'
>>> age = 18
>>> print("%s的年龄是%d" %(name,age))
westos的年龄是18
>>> name ='python'
>>> print("%s的年龄是%d" %(name,age))
python的年龄是18
>>> age = 10
>>> print("%s的年龄是%d" %(name,age))
python的年龄是10
>>> print("%d的年龄是%d" %(name,age))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str
>>> print("%s的年龄是%s" %(name,age))
python的年龄是10

 

2、 %f:浮点型
      %.xf (x:1,2,3,4,5...)保留小数点后多少位

>>> money='4342432.12'
>>> name = 'Tom'
>>> print("%s的工资为%f" %(name,money))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must be real number, not str
>>> print("%s的工资为%f" %(name,monr))
KeyboardInterrupt
>>> money=4342432.12
>>> print("%s的工资为%f" %(name,money))
Tom的工资为4342432.120000
>>> print("%s的工资为%.2f" %(name,money))
Tom的工资为4342432.12
>>> print("%s的工资为%.3f" %(name,money))
Tom的工资为4342432.120
>>> print("%s的工资为%.1f" %(name,money))
Tom的工资为4342432.1
>>> print("%s的工资为%s" %(name,money))
Tom的工资为4342432.12
>>> print("%s的工资为%s" %(name,money))
Tom的工资为4342432.12

3、 整数的占位:不够的位数 前面补0

>>> sid = 1
>>> name = 'lily'
>>> print("%s的学号是%d" %(name,sid))
lily的学号是1
>>> print("%s的学号是103%d" %(name,sid))
lily的学号是1031
>>> print("%s的学号是0000%d" %(name,sid))
lily的学号是00001
>>> print("%s的学号是.5%d" %(name,sid))
lily的学号是.51
>>> print("%s的学号是%.5d" %(name,sid))
lily的学号是00001
>>> print("%s的学号是%.6d" %(name,sid))
lily的学号是000001
>>> print("%s的学号是%.7d" %(name,sid))
lily的学号是0000001

 

 

4、# 百分数的实现

>>> scale = 0.1
>>> print('数据的比例是:%.2f' %(scale))
数据的比例是:0.10
>>> print('数据的比例是:%.2f' %(scale * 100))
数据的比例是:10.00
>>> print('数据的比例是:%.2f%' %(scale * 100))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: incomplete format
>>> print('数据的比例是:%.2f%%' %(scale * 100))
数据的比例是:10.00%

 

 

输入输出练习

# 求平均成绩(python3解释器)
#- 输入学生姓名;
#- 依次输入学生的三门科目成绩;(语文 数学 英语)
#- 计算该学生的平均成绩, 并打印;
#- 平均成绩保留一位小数点;
#- 计算该学生语文成绩占总成绩的百分之多少?并打印。eg: 78%;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值