一、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%
来源:CSDN
作者:silence-1
链接:https://blog.csdn.net/qq_41871875/article/details/103579077