[For beginners] Summary of standard input in Python (with explanation)

Introduction

__ * This article is for those who are having trouble understanding standard input such as AtCoder and paiza. __

There are many types and it may seem difficult, but in reality, we are just selecting the one that matches the type of input.

At first, you don't have to remember it at all, so let's get used to it little by little, checking each time.

Note) __ This article is for beginners. __ Since we give priority to the understanding of beginners, some inadequate wording etc. are included, but we appreciate your understanding.

Which code to use

In Atcoder and paiza problems, there are two main considerations when choosing which code to receive standard input:

--What kind of input is (one line or multiple lines, character string or number) --How do you want to receive that input (put it in a variable, list it, etc.)

Of these, the second one will gradually become clear as you solve the problem repeatedly, so this article will focus on the first one.

One line input

input

abc  #String
12   #String(Even if it looks like a number, it will be treated as a character string as it is)

code

source.py


a = input()
a = int(input())

print

output.py


abc  #String
12   #Numbers

__input () is a function that receives input as a one-line string. __ (Since a in a = input () is a variable name, you can change it freely.) In a = input (), the string abc received by input () is stored in the variable a.

__int () is a function that converts the inside of () to a number (integer). __ If you want to receive numbers, use __int () __. Int (input ()) converts what was received by input () (still a string) to an integer with int ().

Development string → list

input

abc

code

source.py


list_a = list(input())

print

output.py


['a', 'b', 'c']

You can convert a string to a list with __list () __. It's not very frequent, but it's easy, so let's make it available.

Multiple inputs per line

input

apple lemon grape

code

source.py


list_a = input().split()
red, yellow, purple = input().split()

print

output.py


['apple', 'lemon', 'grape']
apple #print(red)

__split () is a function that splits a string into a list. __ If multiple inputs are separated by spaces, split () can be used to make each element a list. If you want to receive them one by one, you need to prepare variables for each number. (If there are not enough numbers, an error will occur.)

If you want to make development numbers

input

1 2 3

code

source.py


list_a = list(map(int,input().split()))
x, y, z = map(int,input().split())

print

output.py


[1, 2, 3]
1 #print(x)

__map () is a function that applies another function to a list or the like. __ (Explained in a very simplified way) Use map () like map (function you want to use, list of targets, etc.). It's a little complicated, so let's break down the process of list (map (int, input (). Split ())).

  1. First, take the input with input () ('1 2 3') and split it with split () (['1', '2', '3']).
  2. __map (__function, list, etc. __) __ applies "function" to "list, etc." This time, apply int () to input (). split () --- (['1', '2', '3']) to make it a number.
  3. However, map () returns a map object as a result and cannot be handled as it is.
  4. So, in order to be able to handle it, I finally converted it to a list. (If you don't do this, you'll see something like map object at 0x7fbff01922e8.)

You can also receive them individually in variables instead of listing them as you would for strings.

Multiple lines, one for each line

input

3
apple
lemon
grape

code

source.py


n = int(input())
list_a = [input() for i in range(n)]

print

output.py


['apple', 'lemon', 'grape']

For multi-line input, the number of lines of input is often given first. This code receives the number of input lines as n, and then stores n lines of input (3 lines this time) in the list.

Writing like [input () for i in range (n)] is called __list comprehension __. It repeats input () n times and stores it in order in the list. It's often used because it makes the list simple, but it looks complicated until you get used to it, so you don't have to learn how it works. (If you tackle the problem many times, you will naturally understand it.)

The above code has the same content as the following code.

source.py


n = int(input())
list_a = []

for i in range(n):
    kudamono = input()
    list_a.append(kudamono)

If you want to make development numbers

input

3 #Represents the number of input lines
1
2
3

code

source.py


n = int(input())
list_a = [int(input()) for i in range(n)]

print

output.py


[1, 2, 3]

It's okay if you convert the input () part of the previous code to an int (input ()) and convert each input to a number.

Multiple lines input is multiple for each line

input

3
a b
c d
e f

code

source.py


n = int(input())
list_a = [input().split() for i in range(n)]

print

output.py


[['a', 'b'], ['c', 'd'], ['e', 'f']]
['c', 'd']  #print(list_a[1])
e           #print(list_a[2][0]) 

If you have multiple lines of input like this, in most cases it will work if you receive it as a two-dimensional array. As the content of "Multiple inputs per line", input (). Split () is a list by itself, so it is easy to imagine if you think that the work of entering a large list_a list is repeated 3 times. think.

If you want to make development numbers

input

3 #Represents the number of input lines
1 2
3 4
5 6

code

source.py


n = int(input())
list_a = [list(map(int,input().split())) for i in range(n)]

print

output.py


[[1, 2], [3, 4], [5, 6]]

It looks complicated, but I'm repeating the work when the input is one line. You can see that this also repeats the work of "multiple inputs per line (when you want to make numbers)" for the number of input lines.

Finally

As I mentioned at the beginning, you don't have to try to remember everything all at once. __ Even with copy-paste at first, you will naturally come to your mind as you challenge various problems and deepen your understanding of the language. I hope this page helps you.

If you find something difficult to understand, please leave it in the comment section.

Recommended Posts

[For beginners] Summary of standard input in Python (with explanation)
Summary of various for statements in Python
Atcoder standard input set for beginners (python)
[Introduction for beginners] Working with MySQL in Python
Basic story of inheritance in Python (for beginners)
Standard input / summary / python, ruby
Summary of tools for operating Windows GUI with Python
Summary of pre-processing practices for Python beginners (Pandas dataframe)
Data analysis in Python Summary of sources to look at first for beginners
Python3 standard input for competitive programming
Matrix representation with Python standard input
Run unittests in Python (for beginners)
[For beginners of competitive pros] Three input methods to remember when starting competitive programming in Python
A brief summary of Graphviz in python (explained only for mac)
The story of making a standard driver for db with python.
Python standard input summary that can be used in competition pro
INSERT into MySQL with Python [For beginners]
[Python] Minutes of study meeting for beginners (7/15)
Tips for dealing with binaries in Python
Process multiple lists with for in Python
UnicodeEncodeError struggle with standard output of python3
[Python] Read images with OpenCV (for beginners)
Summary of built-in methods in Python list
Summary of useful techniques for Python Scrapy
WebApi creation with Python (CRUD creation) For beginners
[For beginners] Try web scraping with Python
[Python] Standard input
Easy-to-understand explanation of Python Web application (Django) even for beginners (5) [Introduction to DB operation with Django shell]
Standard input summary
Basic summary of scraping with Requests that beginners can absolutely understand [Python]
Easy-to-understand explanation of Python Web application (Django) even for beginners (2) [Project creation]
Explanation of creating an application for displaying images and drawing with Python
Easy-to-understand explanation of Python Web application (Django) even for beginners (1) [Environment construction]
Notes for Python beginners with experience in other languages 12 (+1) items by function
Causal reasoning and causal search with Python (for beginners)
Summary of how to import files in Python 3
Summary of how to use MNIST in Python
~ Tips for Python beginners from Pythonista with love ① ~
Try to calculate RPN in Python (for beginners)
Easy understanding of Python for & arrays (for super beginners)
[Python] Summary of S3 file operations with boto3
Summary of frequently used Python arrays (for myself)
Prime factorization ver.2 of integers input in Python
~ Tips for Python beginners from Pythonista with love ② ~
Explanation of edit distance and implementation in Python
Summary of Excel operations using OpenPyXL in Python
Settings for getting started with MongoDB in python
Competitive Pro with Python and VSCode-Simplification of standard input and automation of sample case judgment-
Japanese translation of self-study "A Beginner's Guide to Getting User Input in Python"
Easy-to-understand explanation of Python Web application (Django) even for beginners (3) [Application creation / DB setting]
Key input in Python
python textbook for beginners
Summary of Python arguments
Key input in Python
[Python] About standard input
OpenCV for Python beginners
Standard input / output summary
Rock-paper-scissors poi in Python for beginners (answers and explanations)
[For beginners] How to use say command in python!
Specific sample code for working with SQLite3 in Python
Key input that does not wait for key input in Python