-
ํ์ด์ฌ | #9 ํํ (๋ฐ๋ณต๋ฌธ, ๋ฉ์๋)์นดํ ๊ณ ๋ฆฌ ์์ 2023. 10. 11. 01:23
ํํ ๋ค๋ฃจ๊ธฐ
ํํ ์ํํ๊ธฐ( for loop )
fruits = ("apple", "banana", "cherry") for item in fruits: print(fruits)
์ธ๋ฑ์ค๋ฒํธ๋ก๋ for ๋ฐ๋ณต๋ฌธ์ ๋๋ฆด ์ ์๋ค.
fruits = ("apple", "banana", "cherry") for i in range(len(fruits)): print(fruits[i]) #apple banana cherry
while ๋ฐ๋ณต๋ฌธ ๋๋ฆฌ๊ธฐ
fruits = ("apple", "banana", "cherry") i = 0 while i < len(fruits): print(fruits[i]) i = i + 1 #apple banana cherry
ํํ๋ผ๋ฆฌ ํฉ์น๊ธฐ
fruits = ("apple", "banana", "cherry") fruits2 = ("kiwi", "watermelon") fruits3 = fruits + fruits2 print(fruits3)
ํํ ์์ดํ 2๊ฐ์ฉ ๋๋ฆฌ๊ธฐ
fruits = ("apple", "banana", "cherry") fruits2 = fruits * 2 print(fruits2) #('apple', 'banana', 'cherry', 'apple', 'banana', 'cherry')
ํํ ๋ฉ์๋ ์ฌ์ฉํ๊ธฐ
count() : ํํ์์ ํด๋น ์์ดํ ์ด ๋ช ๊ฐ ๋ค์ด์๋์ง ์๋ ค์ค๋ค
fruits = ("apple", "banana", "cherry", "apple") x = fruits.count("apple") print(x) #2
index() : ์ฒ์์ผ๋ก ํด๋น ์์ดํ ์ด ์๋ ์ธ๋ฑ์ค ๋ฒํธ๋ฅผ ์๋ ค์ค๋ค.
fruits = ("apple", "banana", "cherry", "apple") x = fruits.index("apple") print(x) #0