This article will learn python's built-in function input. Before talking about input, we need to introduce what is a built-in function.
Article Directory
1. Built-in functions
-
What are built-in functions?
In python.exe, there are some ready-to-use functions from the start of operation. We call them "built-in functions". The
print we used before is a built-in function, and its function is to output the content to the console. -
We can use the dir built-in function
dir (__builtins__)
to view all the built-in functions in python, the following should only be part of it
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. Built-in function input
print
and input
are the first two built-in functions we study
- print: output function, output data to the console
- input: input function, save keyboard input to a variable
Example of use
In [7]: myname=input("请输入你的姓名")
请输入你的姓名123
When the keyboard is entered, the entered content will be stored in the variable myname
, and it will be printed below
In [8]: print(myname)
123
With input and output, we can process data more flexibly
The following is what the script runs in VSCode:
name1 =input("请问你的名字是?")
name2 =input("请问你的名字是?")
slogan="{0},加油,加油"
print(slogan.format(name1))
print(slogan.format(name2))
operation result:
请问你的名字是?张三
请问你的名字是?李四
张三,加油,加油
李四,加油,加油
2. The returned content of input cannot be directly calculated numerically
Please see the following code
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
The reasons for the above are:
- The variable type that input can return is actually a string type
- num1 is a string
- Only digital data supports digital-related operations such as addition, subtraction, multiplication, and division.
In the above code:
num1+100
In fact, it is to add a number 100 to a string. Their types are inconsistent and cannot be calculated. If calculation is required, type conversion will be involved, which will be introduced in the next article.
3. Learning video address: data input input