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 で、凡例を表示する位置を変更する方法

凡例(legend)の表示位置を変更するには、axis の legend の loc を設定する。 次のリンクhttps://matplotlib.org/stable/api/_as_gen/mat …

no image

python3 で、すべて小文字かどうかを判定する方法

islower() 関数を使う。 例 print(‘abcde’.islower()) print(‘abcDe’.islower()) 結果 True False

no image

python3 の辞書で、キー一覧を取り出して表示する方法

keys()メソッドでは、キー一覧を取得できる。values() では、値を取得できる。items() では、キーと値をタプルとして取得できる。 リストに変換すると、print() でコンソールに表示 …

no image

python3 で、csv ファイルを読み込んで条件をみたす行の内容を表示する方法

csv.reader で読み込み、各行を読み込んで判定する。 例 import csv with open(‘data.csv’, ‘r’) as csv_file: reader = csv.rea …

no image

matplotlib で、縦横の線(グリッド)を描く方法

pyplot grid で縦横線(罫線のようなもの)を引くことができる。 例 from matplotlib import pyplot as plt data_x = [25,26,27] …