1. Loops
Python의 for loop문은 특정 코드를 반복적으로 수행시킨다.
planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
for planet in planets :
print(planet, end = ' ')
# result is 'Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune'
- 사용할 변수의 이름 (in this case, planet)
- 반복하고자 하는 변수의 집합 (in this case, planets)
두 개를 결합하기 위해 'in' 을 사이에 작성한다.
2. range()
range()는 숫자의 수열을 출력하는 함수이다. loop문과 사용할시 유용하다.
for i in range(5) :
print("Doing important work. i = ", i)
3. while loops
loop문의 또 다른 형태는 'while' 문이 있는데, 조건문을 만족시킬 때까지 코드를 반복 실행하게 된다.
i = 0
while i < 10 :
print(i, end = ' ')
i += 1
# result is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
while loop의 argument는 boolean statement를 사용되며, boolean statement가 False가 될때까지 실행되게 된다.
4. List Comprehensions
squares = [n**2 for n in range(10)]
squares
# result is [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
List Comprehension은 for문과 list를 동시에 생성하는 아주 좋은 작업이다.
# Extract columns which has "Num"
df[[col for col in df.columns if "Num in col]].head()
[(List안에 저장하고 싶은 형식) for 원소 in 집합 if 조건문]
Source of the course : Kaggle Course _ Loops and List Comprehensions
'Course > [Kaggle] Data Science' 카테고리의 다른 글
[Python] Working with External Libraries (0) | 2022.02.11 |
---|---|
[Python] Strings and Dictionaries (0) | 2022.02.11 |
[Python] Lists (0) | 2022.02.11 |
[Python] Booleans and Conditionals (0) | 2022.02.11 |
[Python] Functions and Getting help (0) | 2022.02.11 |