0
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 1 year has passed since last update.

for文

Posted at
py.qiita.py
#  5 制御構文for
#  同じ処理の繰り返しを行うときに、for文を用いて処理を繰り返す

#  5-1 range関数について
j = range(5)
print('range関数の型は、',type(j))
#  range型をrange → list型にキャスト
j_list = list(j)
print(j_list)

# range関数は正数のリストを作成して返す関数
#  range(1, 5)とすると最初の数を指定できる
#  range(1,10, 2)こうすると、1個飛びで整数のリストを作成できる。
#  range(1, 10, 2) →  [1, 3, 5, 7, 9]
k = list(range(1, 10, 2))
print(k)

py.qiita.py
#  5-2 forを使う
#  0~4までの数字を出力
for i in range(5):
    print(i) 

#  1~9までの数字を1個とびで出力
for i in range(1, 10, 2):
    print(i)

#  5-3 for文を用いて遊ぶ
#  len()関数は文字数をカウントする
word = 'Python'
print(len(word))

for i in range(len(word)):
    print(word[i])
    
#  rangeを使わずにfor文を回す
for s in word:
    print(s)

#  5-4 リスト
l = ['Python', 'GoLang', 'Java']

# 値を取り出す *インデックスを使用しないこっちの方がおすすめ
for t in l:
    print(t)
    
# インデックスの指定で値をとりだす
for t in range(len(l)):
    print(f'インデックス{t}の要素は', l[t])
    
0
0
0

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