1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

range関数の使い方と例外処理

Last updated at Posted at 2019-05-19

##range関数を使うと
while文に比べて少ない記述で同じ内容を実行できる

【While文で書いた場合】

n = 0

while n < 5:
    print(n)
    n += 1

【for文 + range関数で書いた場合】


for i in range(5):
    print(i)

rangeは引数を3つまで取ることができる

for i in range (x, y, z)とあった場合

x = 始まりの値
y = 最後の値
z = 増加量

for i in range(1, 10, 2)
    print(i)

とあった場合、「1から(10-1)までの行列が2間隔で出力される」ので実行結果は

1
3
5
7
9

行列の中に文字列があった場合は、tryexcept文で例外処理を行う

L = [1, "", 4, 2]
for i in L:
    try:
        print(i/10)
    except:
        print("Oh...")

※参照元: Pythonによる教育データ分析入門:Pythonの基礎から回帰分析・項目分析まで

1
0
6

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?