Python--输入输出及数据类型

    xiaoxiao2023-10-08  145

    一.输入与输出 1.python3.x与python2.x的区别 在python3中: input() ##接收任意数据类型 在python2.x中: input() ##只支持正确的数值类型 raw_input() ##支持数值类型和字符串类型 (1)python3中

    [kiosk@foundation4 bin]$ /usr/local/python3/bin/python3 Python 3.6.4 (default, May 23 2019, 15:45:30) [GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> input('Num:') ##输入一个数字 Num:2 '2' >>> input('Num:') Num:abc 'abc' >>> import getpass >>> num = getpass.getpass('请输入密码:') ##对num进行赋值 请输入密码: >>> num 'hello' >>> type(num) ##类型为字符串型 <class 'str'>

    (2)python2.x中

    >>> input('Num:') ##输入一个数字 Num:2 2 >>> a = input('Num') Num2 >>> type(a) <type 'int'> ##为整型 >>> 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 ##字符串型不支持 >>> a = raw_input('num:') num:westos >>> type(a) <type 'str'> ##支持字符串型 >>> b = raw_input('num:') num:20 ##支持整型

    2.接收的数值进行比较时,须先转化为同一类型

    >>> value = input('num:') ##输入一个数字 num:20 ##字符串型 >>> value < 10 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: '<' not supported between instances of 'str' and 'int' >>> value = int(input('num:')) ##转换为整型 num:20 >>> value > 10 ##比较成功 True

    3.格式化的输出 (1)%s ##字符串的占位 %d ##整型的占位

    >>> name = 'lucky' >>> name 'lucky' >>> age = 18 >>> age 18 >>> print('%s的年龄是%d岁' %(name,age)) lucky的年龄是18岁 >>> age = '22' >>> print('%s的年龄是%d岁' %(name,age)) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: %d format: a number is required, not str

    (2)%f ##浮点型的占位 %.xf(x:1 2 3 4 5 ) ##保留小数点后几位,默认为6位

    >>> name = 'mike.angleo' >>> money = 52157853.487516488 >>> print(% >>> >>> print('%s的工资是%f' %(name,money)) mike.angleo的工资是52157853.487516 >>> print('%s的工资是%.5f' %(name,money)) mike.angleo的工资是52157853.48752 >>> print('%s的工资是%.4f' %(name,money)) mike.angleo的工资是52157853.4875 >>> print('%s的工资是%.3f' %(name,money)) mike.angleo的工资是52157853.488 >>> print('%s的工资是%.2f' %(name,money)) mike.angleo的工资是52157853.49 >>> print('%s的工资是%.1f' %(name,money)) mike.angleo的工资是52157853.5

    二.数据的类型 1.int型 ##整型

    >>> a = 8 >>> type(a) <class 'int'>

    2.float ##浮点型

    >>> b = 8.8 >>> type(b) <class 'float'>

    3.str ##字符串型

    >>> c = 'successfully' >>> type(c) <class 'str'> >>> c = "what's" >>> type(c) <class 'str'> >>> c = 'what\'s' >>> type(c) <class 'str'>

    4.bool型 ##只有两个值:True False 非0即真

    >>> a = 5 >>> bool(a) True >>> a = 0 >>> bool(a) False >>> bool(0) False >>> a = '' >>> bool(a) False >>> bool('') False

    5.数据类型的转换

    >>> a = 2 >>> type(a) <class 'int'> >>> b = 2.1 >>> type(b) <class 'float'> >>> float(a) ##整型转浮点型 2.0 >>> int(b) ##浮点型转整型 2 >>> c = 'class' >>> type(c) <class 'str'> >>> int(c) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'i' is not defined >>> c = '5' >>> type(c) <class 'str'> >>> int(c) ##字符串型转整型 5 >>> str(b) ##浮点型转字符串型 '2.1' >>> str(a) ##整型转字符串型 '2' >>> float(c) ##字符串型转浮点型>>> str(a) '2' >>> float(c) 5.0 5.0

    6.变量的删除 del进行删除

    >>> del a >>> a Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined >>> del b >>> b Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'b' is not defined >>> del c >>> c Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'c' is not defined

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

    [root@foundation4 kiosk]# vim /mnt/成绩.py

    [kiosk@foundation4 bin]$ /usr/local/python3/bin/python3 /mnt/成绩.py Please input student name:luck Please input Chinese score:89 Please input Math score:97 Please input English score:87 luck的平均成绩是91.0 luck的语文成绩占总成绩的32%
    最新回复(0)