python3

python3 でコンソールからの入力をリストに格納する方法

投稿日:2020年11月28日 更新日:

入力を受け取るには、input() を使う。
range(0,5) で、5個の文字列を順に受け取り、リストに入れてそれを表示する。

list1 = []   
for i in range(0, 5): 
    element = str(input(str(i) + '番目を入力: ')) 
    list1.append(element)      
print(list1) 

結果

0番目を入力: hello
1番目を入力: world
2番目を入力: good
3番目を入力: morning
4番目を入力: Taro
['hello', 'world', 'good', 'morning', 'Taro']

-python3
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

matplotlib でエラーバーを設定する方法

pyplot.errorbar を使って設定する。 オプションの詳細は https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.erro …

no image

matplotlib で、グラフの点の見た目を変更する方法

「marker」で指定すればよい。 次のページを参考に、plotの「marker」を変更する。 https://matplotlib.org/stable/api/markers_api.html 例 …

no image

python3 で csv ファイルを読み込んで、最初の数行を表示する方法

以下の例では、空の配列 data を用意しておき、最初の3行を読み込んでおく。 例 import csv with open(‘data.csv’,’r’) as csv_file: csv_read …

no image

matplotlib で、軸に垂直・平行な線を書く方法

axhline, axvline を使ってx,y軸に平行な線を描くことができる。 https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot …

no image

python3 で、csv ファイルの日付・数字を読み取る方法

csv をインポートすると、row in reader はリストになっている。 次のようにして、各行の要素を取り出して表示することができる。 例 import csv with open(‘data. …