When storing an integer in multiple variables, store it with variable name A and variable name B = map (int, input (). Split ()).
input
1 2
Sample code
a,b=map(int,input().split())
print(a)
print(b)
Execution result
1
2
input
1 2 3 4 5
Sample code
List=list(map(int,input().split()))
print(List)
Execution result
[1, 2, 3, 4, 5]
When storing an integer in the list, store it with the list name = [int (input ()) for variable in range (number of variables)].
input
1
2
3
4
5
Sample code
List=[int(input()) for i in range(5)]
print(List)
Execution result
[1, 2, 3, 4, 5]
When storing an integer in a two-dimensional list, the list name = [list (map (int, input (). Split ())) for variable in range (number of input lines)].
input
3
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
Sample code
Row=int(input())
List=[list(map(int,input().split())) for i in range(Row)]
print(List)
Execution result
[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
if / else statement
When the conditional expression is described by a ternary operation, it becomes print (processing where the conditional expression is TRUE if conditional expression else processing when the conditional expression is FALSE).
Sample code
a=1
print("a is 1" if a==1 else "a is not 1")
Execution result
a is 1
This article is easy to understand!
Recommended Posts