Python

Pythonでリスト内包表記の演習問題(回答)

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

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

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

[回答1]

a = [1, 2, 3, 4, 5]
aa = [i for i in a]
print(aa)

[回答2]

a = [1, 2, 3, 4, 5]
aa = [i * 2 for i in a]
print(aa)

[回答3]

a = [1, 2, 3, 4, 5]
aa = [i ** 2 for i in a]
print(aa)

[回答4]

a = [1, 2, 3, 4, 5]
aa = [i for i in a if i % 2 == 0]
print(aa)

[回答5]

a = [1, 2, 3, 4, 5]
aa = [i for i in a if i <= 3]
print(aa)

[回答6]

a = [1, 2, 3, 4, 5]
aa = [i ** 2 for i in a if i <= 3]
print(aa)

[回答7]

a = [1, 2, 3, 4, 5]
aa = [(i, ii) for i, ii in enumerate(a)]
print(aa)

[回答8]

e = ['ヤマダ', 'ウエダ', 'タナカ']
ee = [len(i) for i in e]
print(ee)

[回答9]

a = [1, 2, 3, 4, 5]
b = ['a', 'b', 'c', 'd', 'e']
aa = [(i, ii) for i, ii in zip(a, b)]
print(aa)

[回答10]

aa = [(i, ii) for i, ii in zip(a, b) if i == 5]
print(aa)

もしくは

aa = [(i, ii) for i, ii in zip(a, b) if ii == 'e']
print(aa)

[回答11]

a = [1, 2, 3, 4, 5]
c = [10, 20, 30, 40, 50]

aa = [cc - aa for aa, cc in zip(a, c)]
print(aa)

[回答12]

d = [[1, 2, 3], [10, 20, 30], [100, 200, 300]]

x = [ddd for dd in d for ddd in dd]
print(x)

[回答13]

a = [1, 2, 3, 4, 5]
c = [10, 20, 30, 40, 50]

aa = [(aa, cc) for aa in a for cc in c]
print(aa)

[回答14]

a = [1, 2, 3, 4, 5]
c = [10, 20, 30, 40, 50]

aa = [(aa, cc) for aa in a if aa == 2 for cc in c]
print(aa)

[回答15]

a = [1, 2, 3, 4, 5]
c = [10, 20, 30, 40, 50]

aa = [(aa, cc) for aa in a if aa == 1 for cc in c if cc == 10]
print(aa)

[回答16]

a = [1, 2, 3, 4, 5]
print(sum([aa ** 2 for aa in a]))

[回答17]

a = [1, 2, 3, 4, 5]
aa = "".join([str(i) for i in a])
print(aa)

[回答18]

e = ['ヤマダ', 'ウエダ', 'タナカ']
ee = [i[0] for i in e]
print(ee)

[回答19]

print(["FizzBuzz" if i % 6 == 0 else "Fizz" if i % 2 == 0 else "Buzz" if i % 3 == 0 else i for i in range(1 , 31)])

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

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

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

Author HOSL

More posts by HOSL