python之数据类型、输入、输出

一、python中的数据类型:整型、浮点型、字符串型、布尔型
# 整型
>>> a = 1
>>> print(a)
1
>>> a
1
# 查看变量的数据类型
>>> type(a)
<type 'int'>

# 浮点型
>>> b = 1.2
>>> b
1.2
>>> type(b)
<type 'float'>

# 字符串型
>>> c = westos
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'westos' is not defined
>>> c = "westos"
>>> c
'westos'
>>> c = 'westos'
>>> c
'westos'
>>> c = 'what's'
  File "<stdin>", line 1
    c = 'what's'
              ^
SyntaxError: invalid syntax
>>> c = 'what\'s'
>>> c
"what's"
>>> c = "what's"
>>> c
"what's"

# bool型(只有两个值:True False 非0即真)
>>> a = 1
>>> bool(a)
True
>>> bool(0)
False
>>> bool('')
False
>>> bool(' ')
True
>>> bool('redhat')
True

# 变量类型的转换
>>> a = 1
>>> type(a)
<type 'int'>
>>> float(a)
1.0
>>> type(a)
<type 'int'>
>>> b = float(a)
>>> a
1
>>> b
1.0
>>> b = 2.0
>>> int(b)
2
>>> b = 2.3
>>> int(b)
2
>>> b = 123
>>> str(b)
'123'
>>> b = 12.3
>>> str(b)
'12.3'
>>> c = 'westos'
>>> int(c)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'westos'
>>> c = '123'
>>> int(c)
123

二、python中的输入输出
# input():接收任意数据类型
>>> input('Num:')
Num:2
'2'
>>> input('Num:')
Num:redhat
'redhat'
>>> input('Num:')
Num:1.2
'1.2'
>>> input('Num:')
Num:False
'False'
>>> import getpass
>>> num = getpass.getpass('请输入密码:')
请输入密码:
>>> num    
'123'

# 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
>>> input('Num:')
Num:1.2
1.2
>>> raw_input('Num:')
Num:2
'2'
>>> raw_input('Num:')
Num:redhat
'redhat'
>>> raw_input('Num:')
Num:3.0
'3.0'
>>> raw_input('Num:')
Num:False
'False'

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

三、python中的格式化输出 
# 格式化输出
# %s:代表字符串的占位 %d:整型的占位
>>> name = 'westos'
>>> name
'westos'
>>> age = 11
>>> print('%s的年龄是%d' %(name,age))
westos的年龄是11
>>> name = 'tom'
>>> age = 18
>>> print('%s的年龄是%d' %(name,age))
tom的年龄是18
>>> print(name '的年龄是',age)
  File "<stdin>", line 1
    print(name '的年龄是',age)
                            ^
SyntaxError: invalid syntax
>>> print(name '的年龄是' age)
  File "<stdin>", line 1
    print(name '的年龄是' age)
                            ^
SyntaxError: invalid syntax
>>> print(name +'的年龄是'+age)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
>>> 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的年龄是%d' %(name,age))
tom的年龄是18
>>> print('%s的年龄是%d' %(name,age))
tom的年龄是18
>>> print('%s的年龄是%s' %(name,age))
tom的年龄是18

# %f浮点型
# %.xf(x:1,2,..num) 保留小数点后多少位
>>> money = 234251.4124
>>> name = 'tom''
  File "<stdin>", line 1
    name = 'tom''
                ^
SyntaxError: EOL while scanning string literal
>>> name = 'tom'
>>> print('%s的工资为%f' %(name,money))
tom的工资为234251.412400
>>> money = 60000
>>> print('%s的工资为%f' %(name,money))
tom的工资为60000.000000
>>> print('%s的工资为%.2f' %(name,money))
tom的工资为60000.00
>>> print('%s的工资为%.3f' %(name,money))
tom的工资为60000.000
>>> print('%s的工资为%.7f' %(name,money))
tom的工资为60000.0000000
>>> print('%s的工资为%.10f' %(name,money))
tom的工资为60000.0000000000


# 整数的占位:不够的位数 前面补0
>>> sid = 1
>>> name = 'lily'
>>> print('%s的学号为%d' %(name,sid))
lily的学号为1
>>> print('%s的学号为103%d' %(name,sid))
lily的学号为1031
>>> print('%s的学号为000%d' %(name,sid))
lily的学号为0001
>>> print('%s的学号为%.5d' %(name,sid))
lily的学号为00001
>>> print('%s的学号为%.6d' %(name,sid))
lily的学号为000001
>>> print('%s的学号为%.4d' %(name,sid))
lily的学号为0001
>>> sid = 10
>>> print('%s的学号为%.4d' %(name,sid))
lily的学号为0010

# 百分数的实现
>>> 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%



 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值