pythonでは配列のことを「リスト」という
インデックスの概念は同じ
0から始まる
print(team[0])
print(team[n+1]
print(len(team))
など
# coding: utf-8
# リストを作成する
player_1 = "yusya"
player_2 = "mahou"
team = ["yusya","mahou",100,player_1]
print(team)
-->出力結果
['yusya', 'mahou', 100, 'yusya']
# 変数で、リストに代入する
player_1 = "勇者"
player_2 = "魔法使い"
player_3 = "戦士"
# player_1 ~ 3を、リストに記述して、print関数で出力してください。
list = [player_1,player_2,player_3]
print(list)
# coding: utf-8
# リストの要素を取り出す
team = ["勇者", "魔法使い"]
print(team)
num = 0
print(team[num+1])
print(len(team))
# coding: utf-8
# リストの要素を操作する
team = ["勇者", "魔法使い"]
print(team)
print(team[0])
team.append("戦士") #要素を追加
print(team)
team.append("ドラゴン")
print(team)
team.pop(2) #要素を消す
print(team)
# coding: utf-8
# ループでリストを操作する
team = ["勇者", "戦士", "魔法使い"]
print(team)
for i in team:
print(i)
# coding: utf-8
# ループでリストを操作する
team = ["勇者", "戦士", "魔法使い"]
print("<select name = 'job'>")
for job in team:
print("<option>" + job + "</option>")
print("</select>")
--->html出力
listの要素の合計値:
print(sum(list))
カンマ区切りの文字列を配列に入れる
# coding: utf-8
# 取り込んだデータをリストに格納する
line = input().rstrip().split(",")
print(line)
# coding: utf-8
# 取り込んだデータをリストに格納する
line = input().rstrip().split(",")
print(line)
print(len(line))
for i in line:
print(i+"が現れた")
-->出力結果
['q', 'q', 'w']
3
qが現れた
qが現れた
wが現れた
# coding: utf-8
#文字列をカンマで分割する
team_str = "勇者,戦士,忍者,魔法使い"
list = team_str.split(",")
print(list)
# coding: utf-8
#英文の単語数を数える
str = "One cold rainy day when my father was a little boy he met an old alley cat on his street"
list = str.split(" ") #半角スペースで区切る
print(len(list))