0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

スペース区切りで入力された数値を変数に格納する方法

0
Posted at

はじめに

初学者の備忘録として書いています。 内容に不正確な部分があるかもしれませんが、温かい目で見ていただけると幸いです。

学んだこと

Pythonでスペース区切りで入力された数値を変数に格納する方法

きっかけ・背景

AtCoderの問題を解くのに必要になったため

詳細

結論から書くと、

A, B = map(int, input().split())

これで標準入力 2 4 をA,Bに格納することができます。

必要な知識は以下の通りです。

  • input
  • split
  • map
  • int

処理の流れは以下の通りです。

  1. input()"2 4"を受け取る
  2. "2 4".split()でスペース区切りで分割し、リストにする
  3. map(int, ["2","4"])でリストの各要素に対して、int関数を実行する
  4. A, B = [2,4]でアンパック代入する (正確には右辺はイテレータ)

フリーコメント・所感

  • 書き方はいくらでもありそうだと思った。
  • 最初にこのコードを見た時は、何をやってるのか全く分からなかったが、ひとつひとつ要素に分解していくことで、理解することができた。

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?