Python Paiza-Various skill checks and standard input

Introduction

I summarized the "input function" that is always used when doing skill check of Paiza. There are various ways to use it, so please see it when you want to check how to use it properly.

How to use Paiza's skill check

Before getting into the text, I'll write something to do before solving the skill check problem. I didn't know what to do before solving the problem at first, and I was struggling because I couldn't type in the code for about 30 minutes. I hope it helps those who solve the skill check for the first time.

① First, click Paiza_Skill Check to jump to Paiza's skill check. (2) Select and click the one you like from the list of questions for each rank on the left. (D is the gentlest and S is the most difficult) ③ After that, select your favorite question from the list and press "Challenge". ④ Then, a confirmation screen will appear, so press "Challenge the problem". ⑤ Do so in order from the top -"Problem statement" -"Evaluation points": About points -"Value to be input": Explanation of the value brought by input -"Expected output": Explanation of what you want to output -"Conditions": Lower and upper limits of numerical values -Specific input example described in "Input Value" and Specific Output Example described in "Expected Output" -"Answer column": A place to write the actual code Since it is a style, read everything and understand it. ⑥ Select the language you use from "Language" at the top right of the answer column. Then you will be able to write code. This is the preparation, so please solve your own problems from here.

What is "input"

I think some people think of "input" in the first place.

"Input" is to receive the data entered on the keyboard. You can receive what is in the part written as input example in Paiza's skill check by using "input". Since "print" is the output, it may be easier to understand if you think the opposite.

Basic usage of "input"

I will introduce the basic usage of "input".

>>>Variable name= input()
#Prompted for input
>>>print(Variable name)
#What you type is output

You can receive what you have entered in this way. It's hard to know where to type at first, but most of the time you can type it in the next line and it will be received. You can write a sentence that indicates what you want to input in "input" as shown below.

>>>Variable name= input('Instructions on what you want to enter:')

In paiza, by inputting as above, you can pull what is written in the input example. Also, what you enter will be received as a character string. In other words, even if you enter "1", it will be "'1'".

>>>number = input()
1
>>>number
'1'
>>>int(number)
1

By doing this, you can convert from a string type to a number.

Receive multiple values

In basic usage, you can only receive one value. So how do you get multiple values?

>>>a,b=input().split()
one two
>>>a
one
>>>b
two

In this way, "a" will contain "one" and "b" will contain "two". Of course, you can increase the number of "a, b, c, d ..." you can receive, but be careful as it will be difficult to see if it is too long.

Receive as a number

And when you receive it this time, it is a character string, so if you want to receive it as a number, you should use "int".

>>>a,b=input().split()
1 2
>>>a
'1'
>>>b
'2'
>>>int(a)
1
>>>int(b)
2

But it's a little annoying if you need 3 lines to make numbers. In such a case, use "list comprehension".

Receive multiple values in list comprehension

It may be difficult to hear only the words, but I will explain them in order.

>>>a,b=[int(x) for x in input().split()]
1 2
>>>a
1
>>>b
2

(1) First, use the for statement to extract the elements contained in the list in "input (). Split ()]" one by one. (2) The extracted element enters "x", and the "x" is converted to a number with "int". (It's easier to understand if you look at it from behind) ③ Finally, put the converted numbers in "a" and "b". It's fascinating to be able to write in just one line what you used to write in three lines.

There is another way, so I will introduce it.

A slightly different version

>>>N = int(input())
#Specify the number of inputs
>>>a,b = [int(input()) for i in range(N)]
1
2
>>>a
1
>>>b
2

First, ask them to specify the number of inputs, and then rotate the for statement the number of times. After that, "int (input ())" receives the elements line by line, converts them into numbers one by one, and puts them in "a" and "b". The difference from the previous one is "Is there multiple values in one line?" And "Is there one value in each line?"

Use map function

>>>a,b=map(int,input().split())
1 2
>>>a
1
>>>b
2

The "map function" draws the change target in the second argument (input (). Split ()) and processes it with the first argument (int). This one is shorter and may be easier to use.

Finally

This time, I summarized "input". Paiza's skill check is essential knowledge, so it's worth remembering. Let's utilize it steadily.

Recommended Posts

Python Paiza-Various skill checks and standard input
[Python] Standard input
python input and output
[Python] About standard input
[Python3] Standard input [Cheat sheet]
Python3 standard input (competition pro)
Standard input / summary / python, ruby
Receiving standard input tips @ python
Python3 standard input for competitive programming
Matrix representation with Python standard input
Calculate and display standard weight with python
I compared python3 standard argparse and python-fire
Python3 standard input I tried to summarize
Atcoder standard input set for beginners (python)
[Python] Add comments to standard input files
Competitive Pro with Python and VSCode-Simplification of standard input and automation of sample case judgment-
Standard input summary
Python: Use zipfile to unzip from standard input
Calculation of standard deviation and correlation coefficient in Python
Have python parse the json entered from the standard input
Receives and outputs standard output of Python 2 and Python 3> C implementations
[Python] Change standard input from keyboard to text file
Which is better, python standard input receiving input () or sys.stdin?
[python] Compress and decompress
Key input in Python
Python and numpy tips
[Python] pip and wheel
Batch design and python
Python iterators and generators
Python packages and modules
Vue-Cli and Python integration
Ruby, Python and map
Python and Ruby split
Python audio input / output
Memorize Python commentary 4 --Input
Key input in Python
Python3, venv and Ansible
Python asyncio and ContextVar
Standard input / output summary
[Python of Hikari-] Chapter 08-03 Module (Import and use of standard library)
[For beginners] Summary of standard input in Python (with explanation)
To go back and forth between standard python, numpy, pandas ①
Python application: Data handling Part 1: Data formatting and file input / output
A standard way to develop and distribute packages in Python