python3

python でリストに、コロン演算子2つ(::)を使う

投稿日:2020年12月23日 更新日:

リストのコロンで指定できるパラメータには、「start, end, step」という意味がある。

以下の例では、:: 5 とした場合には、5 をステップとして、次に進んでいく。

::-5 とした場合には、5 をステップとして、前に戻っていく。

print('range(100)[::-5] の結果を表示')
for i in range(100)[::-5]:
    print(i, end=' ')

print('\nrange(100)[::-5] の結果を表示')
for i in range(100)[::5]:
    print(i, end=' ')
print('\n')

結果

range(100)[::-5] の結果を表示
99 94 89 84 79 74 69 64 59 54 49 44 39 34 29 24 19 14 9 4 
range(100)[::-5] の結果を表示
0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95

-python3
-,

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

python のプロンプトについて

UNIX で python を起動する ターミナルで $ python インタープリターのプロンプトは >>> となっている。 インタープリタを終了させるときは、Ctrl + D を押す。

no image

matplotlib で、グラフの点の見た目を変更する方法

「marker」で指定すればよい。 次のページを参考に、plotの「marker」を変更する。 https://matplotlib.org/stable/api/markers_api.html 例 …

no image

python3 で、すべて小文字かどうかを判定する方法

islower() 関数を使う。 例 print(‘abcde’.islower()) print(‘abcDe’.islower()) 結果 True False

no image

python3 で、dictionary のキー一覧を取得する方法

dict.keys() でキーを取得することができる。 参考:dictionary の値一覧を取得するときは、dict.values() を使う。 例 print(dict.keys())

no image

matplotlib で、縦横の線(グリッド)を描く方法

pyplot grid で縦横線(罫線のようなもの)を引くことができる。 例 from matplotlib import pyplot as plt data_x = [25,26,27] …