掌握Python的X篇_7_数据的输入input(内置函数;input的使用)

本篇将会学习python的内置函数input,谈到input之前,需要介绍什么是内置函数。

1. 内置函数

  • 什么是内置函数
    python.exe中,从运行开始,就有一些可以现成使用的函数,我们称为“内置函数”
    我们之前使用的print,就是一个内置函数,它的作用就是将内容输出到控制台

  • 我们可以通过dir内置函数dir (__builtins__),查看python中的所有内置函数,下面应该只是一部分的

In [5]: dir (__builtins__)
Out[5]:
['ArithmeticError',
 'AssertionError',
 'AttributeError',
 'BaseException',
 'BlockingIOError',
 'BrokenPipeError',
 'BufferError',
 'BytesWarning',
 'ChildProcessError',
 'ConnectionAbortedError',
 'ConnectionError',
 'ConnectionRefusedError',
 'ConnectionResetError',
 'DeprecationWarning',
 'EOFError',
 'Ellipsis',
 'EnvironmentError',
 'Exception',
 'False',
 'FileExistsError',
 'FileNotFoundError',
 'FloatingPointError',
 'FutureWarning',
 'GeneratorExit',
 'IOError',
 'ImportError',
 'ImportWarning',
 'IndentationError',
 'IndexError',
 'InterruptedError',
 'IsADirectoryError',
 'KeyError',
 'KeyboardInterrupt',
 'LookupError',
 'MemoryError',
 'ModuleNotFoundError',
 'NameError',
 'None',
 'NotADirectoryError',
 'NotImplemented',
 'NotImplementedError',
 'OSError',
 'OverflowError',
 'PendingDeprecationWarning',
 'PermissionError',
 'ProcessLookupError',
 'RecursionError',
 'ReferenceError',
 'ResourceWarning',
 'RuntimeError',
 'RuntimeWarning',
 'StopAsyncIteration',
 'StopIteration',
 'SyntaxError',
 'SyntaxWarning',
 'SystemError',
 'SystemExit',
 'TabError',
 'TimeoutError',
 'True',
 'TypeError',
 'UnboundLocalError',
 'UnicodeDecodeError',
 'UnicodeEncodeError',
 'UnicodeError',
 'UnicodeTranslateError',
 'UnicodeWarning',
 'UserWarning',
 'ValueError',
 'Warning',
 'WindowsError',
 'ZeroDivisionError',
 '__IPYTHON__',
 '__build_class__',
 '__debug__',
 '__doc__',
 '__import__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 'abs',
 'all',
 'any',
 'ascii',
 'bin',
 'bool',
 'breakpoint',
 'bytearray',
 'bytes',
 'callable',
 'chr',
 'classmethod',
 'compile',
 'complex',
 'copyright',
 'credits',
 'delattr',
 'dict',
 'dir',
 'display',
 'divmod',
 'enumerate',
 'eval',
 'exec',
 'filter',
 'float',
 'format',
 'frozenset',
 'get_ipython',
 'getattr',
 'globals',
 'hasattr',
 'hash',
 'help',
 'hex',
 'id',
 'input',
 'int',
 'isinstance',
 'issubclass',
 'iter',
 'len',
 'license',
 'list',
 'locals',
 'map',
 'max',
 'memoryview',
 'min',
 'next',
 'object',
 'oct',
 'open',
 'ord',
 'pow',
 'print',
 'property',
 'range',
 'repr',
 'reversed',
 'round',
 'set',
 'setattr',
 'slice',
 'sorted',
 'staticmethod',
 'str',
 'sum',
 'super',
 'tuple',
 'type',
 'vars',
 'zip']

2. 内置函数input

printinput是我们学习的头两个内置函数

  • print:输出函数,将数据输出到控制台
  • input:输入函数,将键盘输入保存到变量中

使用示例

In [7]: myname=input("请输入你的姓名")
请输入你的姓名123

当键盘输入后,输入的内容,将被存储到变量myname中,以下将其进行打印

In [8]: print(myname)
123

有了输入输出之后,我们可以更灵活的处理数据

以下是在VSCode中脚本运行的:

name1 =input("请问你的名字是?")
name2 =input("请问你的名字是?")
slogan="{0},加油,加油"
print(slogan.format(name1))
print(slogan.format(name2))

运行结果:

请问你的名字是?张三
请问你的名字是?李四
张三,加油,加油
李四,加油,加油

2. input的返回内容是不能直接进行数值计算的

请看以下代码

In [12]: num1=input("请输入num1")
请输入num1345
In [13]: print(num1*10)
345345345345345345345345345345

In [14]: print(num1+100)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[14], line 1
----> 1 print(num1+100)

TypeError: can only concatenate str (not "int") to str

以上的原因是:

  • input能够返回的变量类型,其实是字符串类型的
  • num1是一个字符串
  • 只有数字型的数据,才支持数字有关的加减乘除等运算

以上代码中:

num1+100

其实是让一个字符串加上一个数字100,他们类型不一致,无法计算,如果需要计算那就要涉及类型转换了,下篇将会介绍。

3. 学习视频地址:数据的输入input

猜你喜欢

转载自blog.csdn.net/Dasis/article/details/131619513