Python

Pythonで辞書型の演習問題(回答)

By 2021年8月23日No Comments
Know This First

プログラミング初心者対象!Python入門セミナー

プログラミング初心者がPythonの初歩部分を突破するためのセミナーです。

[回答1]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(a.keys())

[回答2]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(a.values())

[回答3]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(list(a.keys()))

[回答4]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(list(a.values()))

[回答5]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(list(a.items()))

[回答6]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(a['b'])

[回答7]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
a['f'] = 6
print(a)

[回答8]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
b = {"A": 1, "B": 2, "C":3, "D": 4,"E": 5}
a['f'] = b
print(a)

[回答9]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
a['a'] = "77"
print(a)

[回答10]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print('a' in a.keys())

[回答11]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(1 in a.values())

[回答12]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(('a', 1) in a.items())

[回答13]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
for aa in a.keys():
    print(aa)

[回答14]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
for aa in a.values():
    print(aa)

[回答15]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
for aa, ab in a.items():
    print(aa, ab)

[回答16]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
for i, aa in enumerate(a.keys()):
    print(i, aa)

[回答17]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
for i, aa in enumerate(a.values()):
    print(i, aa)

[回答18]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
for i, (aa, ab) in enumerate(a.items()):
    print(i, aa, ab)

[回答19]

a = dict(a=1, b=2, c=3)
print(a)

[回答20]

c = [('a', 1), ('b', 2), ('c', 3)]
print(dict(c))

[回答21]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(a.get('c'))

[回答22]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(a.get('g', '存在しません。'))

[回答23]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
b = {"A": 1, "B": 2, "C":3, "D": 4,"E": 5}
a.update(b)
print(a)

[回答24]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(max(a))

[回答25]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(min(a))

[回答26]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(max(a.values()))

[回答27]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(min(a.values()))

[回答28]

de = d | e
print(de)

もしくは

d = {1, 3, 4, 5, 6, 9}
e = {0, 2, 4, 6, 7, 8, 10}
print(d.union(e))

[回答29]

d = {1, 3, 4, 5, 6, 9}
e = {0, 2, 4, 6, 7, 8, 10}
de = d - e
print(de)

もしくは

d = {1, 3, 4, 5, 6, 9}
e = {0, 2, 4, 6, 7, 8, 10}
print(d.difference(e))

[回答30]

d = {1, 3, 4, 5, 6, 9}
e = {0, 2, 4, 6, 7, 8, 10}
de = d & e
print(de)

[回答31]

d = {1, 3, 4, 5, 6, 9}
e = {0, 2, 4, 6, 7, 8, 10}
de = d ^ e
print(de)

もしくは

d = {1, 3, 4, 5, 6, 9}
e = {0, 2, 4, 6, 7, 8, 10}
print(d.symmetric_difference(e))

[回答32]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
print(len(a))

[回答33]

a = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
a.clear()
print(a)

Udemyの動画学習でもPythonを勉強しよう!

「平日の夜の勉強会には時間が間に合わなくて参加できない」「通勤時間のわずかな隙間時間を勉強時間にあてたい」「本ではよく分からないところを動画で理解を深めたい」そんなあなたはUdemyの動画学習がお勧めです!

UdemyのPythonおすすめ33講座レビューリスト
HOSL

Author HOSL

More posts by HOSL