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

python3 で切り捨て割り算(除算)

python3 で整数の割り算をして、商(の整数部)を表示するには // 演算子を使う。余りが発生した場合、商は切り捨てた整数部となる。 例 n = 201 print(str(n//2)) 結果 1 …

no image

pythonで配列(リスト)の、ある要素がわかっているときにその次の要素を取得する方法。

next() を使う。 例 my_arr = [‘春’,’夏’,’秋’,’冬’] my_iter = iter(my_arr) print(next(my_iter,’なし’)) print(next …

no image

matplotlib で、グラフの線の幅を変更する方法

lw で、線幅を数字で指定すればよい。 例 from matplotlib import pyplot as plt data_x = [25,26,27] data_y = [39, …

no image

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

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

no image

matplotlib でグラフ表示ウィンドウの画面上の位置を自由に設定する方法

matplotlib.use(‘TkAgg’) としておき、 get_current_fig_manager().window.wm_geometry(“+20+50”) として、(+20+50)のと …