A memorandum of using Python's input function

For some reason, the book Introduction to Python3 that I read to study Python did not contain a description of standard input functions. : scream: However, it is a very convenient function to use, so I will leave a memorandum on how to use it.

The most basic usage

The most basic usage is as follows. When executed, it will be in a state of waiting for input from standard input (usually keyboard). Enter the content you want to assign to the variable x, and finally enter Return to assign the input content to x.

Input example


>>> x = input()
5
>>> print(x)
5

Display a message prompting you to enter

It is also possible to display a message prompting for input. Pass the content you want to display as an argument of the input function.

Input example


>>> x = input("Enter a number: ")
Enter a number: 2
>>> print(x)
2

Extract as an integer

First, let's look at the return type of the input function. It is output as a character string type (str).

>>> x = input()
2
>>> print(type(x))
<class 'str'>

To extract it as an integer, cast it using the int function (float for floating point).

Input example


>>> x = int(input())
2
>>> print(type(x))
<class 'int'>

Enter multiple values at once

Basically a string

There are many cases where you want to enter multiple values from standard input at once. If you enter multiple values separated by spaces, they will be assigned to variables as shown below.

Input example


>>> x = input()
2 3 4
>>> print(x)
2 3 4

Decompose a string to retrieve multiple values

It is assigned as a string of multiple values concatenated with spaces. Therefore, multiple values can be retrieved at once by using the inclusion notation as shown below. (Casted with the int function and extracted as an integer type.)

Input example


>>> x, y, z = (int(x) for x in input().split())
2 3 4
>>> print(x, y, z)
2 3 4
>>> print(type(x), type(y), type(z))
<class 'int'> <class 'int'> <class 'int'>

Convert to list

You can assign it as a list by doing the following.

Input example


>>> x = input().split()
2 3 4
>>> print(x)
['2', '3', '4']

In this case, you end up with a list of strings. List comprehension is used to make a list of integer values.

Input example


>>> x = [int(x) for x in input().split()]
2 3 4
>>> x
[2, 3, 4]

Combination with if statement

You can control the number of values to retrieve by adding an enumerate statement and an if statement.

Input example


>>> x = [int(x) for i, x in enumerate(input().split()) if i < 3]
1 2 3
>>> print(x)
[1, 2, 3]
>>> x = [int(x) for i, x in enumerate(input().split()) if i < 3]
1 2 3 4
>>> print(x)
[1, 2, 3]

Reference

Recommended Posts

A memorandum of using Python's input function
A memorandum of using eigen3
A memorandum of kernel compilation
A small memorandum of openpyxl
A memo of writing a basic function in Python using recursion
A memorandum when using beautiful soup
[Python] A memorandum of beautiful soup4
A memorandum of files under conf.d
A story about using Python's reduce
A memorandum of closure survey contents
Finding the optimum value of a function using a genetic algorithm (Part 1)
Getting a combination of elements using itertools
A complete understanding of Python's asynchronous programming
A memorandum of speed of arbitrary degree diagonalization
Impressions of using Flask for a month
A memorandum of understanding about django's QueryDict
A complete understanding of Python's object-oriented programming
Equipped with a card function using payjp
A memorandum of python string deletion process
A memorandum of trouble when formatting data
Draw a graph of a quadratic function in Python
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
Memorandum of sed
Get the caller of a function in Python
A memorandum of calling Python from Common Lisp
A memorandum of studying and implementing deep learning
A memorandum of extraction by python bs4 request
[Linux command] A memorandum of frequently used commands
A memorandum of method often used in machine learning using scikit-learn (for beginners)
The story of introducing a multi-factor authentication function using a one-time password into a Java application
[Python] Implementation of clustering using a mixed Gaussian model
Development of tracking function using small educational drone TELLO
Cut a part of the string using a Python slice
# Function that returns the character code of a string
Drawing on Jupyter using the plot function of pandas
Read a large amount of securities reports using COTOHA
Implementation of a convolutional neural network using only Numpy
I tried using Python (3) instead of a scientific calculator
Memorandum of fastText (editing)
memorandum of vi command
A memorandum about matplotlib
Function fitting (using Keras)
Differentiate a two-variable function
Make a function decorator
A memorandum about Nan.
elasticsearch_dsl Memorandum of Understanding
Try using Python's feedparser.
Try using Python's Tkinter
Example of using lambda
Set function of NumPy
Implementation of VGG16 using Keras created without using a trained model
Let's create a function for parametrized test using frame object
Avoiding the pitfalls of using a Mac (for Linux users?)
A memorandum of stumbling on my personal HEROKU & Python (Flask)
Reuse the behavior of the @property method by using a descriptor [16/100]
Summary of execution of anonymous recursive function using fixed point combinator
Add a function to heat transfer + heat input by temperature to heatrapy
How to print characters as a table with Python's print function
I made a function to check the model of DCGAN
[Introduction to AWS] A memorandum of building a web server on AWS
To return char * in a callback function using ctypes in Python