LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

Python 変数とデータ型 演習問題回答

Last updated at Posted at 2017-12-05

1. 2017年から2021年までの要素を持ったyears_listという名前のリストを作ってください。

sample.py
years_list = [2017, 2018, 2019, 2020, 2021]

2. years_listから2020の年を取り出して、printで出力してください

sample.py
print(years_list[3])

3. years_listから2021年をprintで出力してください

sample.py
print(years_list[4])

# 下記でも可能
# years_list[-1]
# years_list[4:]
# years_list[4:5]

"""
years_list = [2017, 2018, 2019, 2020, 2021]

for i in years_list:
    if i == 2021:
        print(i)
"""

4. 好きな文字列3つを要素にして、自由な変数名でリストを作ってください

sample.py
pro_lang = ["python", "ruby", "java"]

5. 4で作った文字列3つを持つリストの末尾に新しく文字列を1つ追加してください。

ただし、普通に追加するのではなくある関数を使って末尾に追加してください。

sample.py
pro_lang = ["python", "ruby", "java"]
# 下記を追加
pro_lang.append("php") # ['python', 'ruby', 'java', 'php']

6. 名前と年齢のペアが3つある辞書型を作ってください。(名前と年齢は自由につけてください)keyが名前で、valueが年齢とする

sample.py
user_data = {"田中": 35, "山田": 42, "鈴木": 28} # {'山田': 42, '鈴木': 28, '田中': 35}

7. 7.下記の辞書profileからメールアドレス部分(test@aiacademy.jp)を出力してください。


profile = {"name": "tanaka", "email": "test@aiacademy.jp" }

想定出力結果

test@aiacademy.jp
profile = {"name": "tanaka", "email": "test@aiacademy.jp" }
print(profile["email"]) # print(profile['email'])でも可
1

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