birthday = [2011,11,27]
print(“-“.join([str(n) for n in birthday]))
タグ: programming
Python シーケンス
scores = [10, 20, 30, 20, 40]
tokyo = (“JPY”, 36, 140)
name = “Taro Yamada”
replaced_string = name.replace(“Taro”, “Jiro”)
upper_string = name.upper()
print(replaced_string)
print(upper_string)
Python タプルをアンパックする
tokyo = (“JPY”, 36, 140)
currency, *_ = tokyo
print(currency)
python タプル
tokyo = “JPY”, 36, 140
tokyo = list(tokyo)
tokyo[0] = “YEN”
tokyo = tuple(tokyo)
print(tokyo)
print(tokyo[0])
python リスト内表記とifを組み合わせる
prices = [100, 200, 150, 200, 100]
prices_with_tax = []
for price in prices:
if price != 200:
prices_with_tax.append(price * 1.1)
prices_with_tax = [price * 1.1 for price in prices if price != 200]
print(prices_with_tax)
python リスト内包表記
prices = [100, 200, 150, 200, 100]
prices_with_tax = [price * 1.1 for price in prices]
print(prices_with_tax)
Python リストとループ
prices = [100,200,150,200,100]
for price in prices:
print(price * 1.1)
for index, price in enumerate(prices):
print(f”{index}: {price * 1.1:.2f}”)
Python スライス記法
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
sliced_list = nums[::-1]
print(nums)
print(sliced_list)
Python スライス記法
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
nums[3:] = []
print(nums)
Python リストの要素
scores = [10, 20, 30, 20, 40]
scores_sorted = sorted(scores, reverse=True)
print(scores)
print(scores_sorted)