PYTHON PROGRAMMING FUNDAMENTALS¶
우리가 배울것 : 반복문 - Loop¶
- For Loops
- Break a loop
- Continue statement
- Range
- While Loops
- Nested loops
- List Comprehension
실행 순서에 대한 문법을 배웁시다.¶
In [1]:
score = [90, 45, 77, 83, 39]
In [7]:
score[0] -5
Out[7]:
85
In [8]:
score[1]-5
Out[8]:
40
In [11]:
new_score = [score[0]-5, score[1]-5]
In [12]:
new_score
Out[12]:
[85, 40]
In [ ]:
#데이터 스트럭쳐에 저장된 데이터를 처음부터 끝가지 다 가져와서 작업하고
#싶을때 => for 반복문을 사용한다.
In [14]:
for data in score :
print(data)
90 45 77 83 39
In [18]:
for data in score :
print(data)
90 45 77 83 39
In [21]:
data #for 변수는 for 밖에서 사용하지 말라.
Out[21]:
39
In [23]:
for data in score :
print(data - 5 )
85 40 72 78 34
In [25]:
new_list = []
In [29]:
for data in score :
print(data - 5 )
new_list.append(data-5)
85 40 72 78 34
In [30]:
new_list
Out[30]:
[85, 40, 72, 78, 34]
FOR LOOPS¶
영어가 설명이 정확합니다. For loops are used for iterating over a sequence (a list, a tuple, a dictionary, a set, or a string).
한번에 하나의 항목만 가져와서, 원하는 대로 처리합니다.
리스트 데이터를 loop 돌면서 처리한다.¶
문자열 데이터를 루프 돌면서 처리한다.¶
In [31]:
sentence = "Hello World~"
In [32]:
for i in sentence :
print(i)
H e l l o W o r l d ~
리스트를 루프 돈다.¶
In [33]:
fruits = ['apple', 'blueberries', 'mango', 'watemelon']
In [34]:
# 리스트의 데이터를, 모두 대문자로 바꿔서 화면에 출력하고 ,
# 대문자로 되어있는 새로운 리스트를 만들어 주세요
In [35]:
Fru = []
In [38]:
for i in fruits :
Fru.append(i.upper())
print(i)
print(Fru)
apple ['APPLE'] blueberries ['APPLE', 'BLUEBERRIES'] mango ['APPLE', 'BLUEBERRIES', 'MANGO'] watemelon ['APPLE', 'BLUEBERRIES', 'MANGO', 'WATEMELON']
딕셔너리 데이터를 for 루프 : key¶
리스트의 인덱스값과, 매칭되어 저장되어있은 값을 함께 출력¶
In [40]:
my_phone = {"brand" : "apple", "model" : "iphone 12", "color" : "red", "year" : 2021}
In [46]:
for i in my_phone :
print(i)
brand model color year
딕셔너리 value 값을 for 루프¶
In [47]:
for i in my_phone.values() :
print(i)
apple iphone 12 red 2021
키 밸류를 튜플 로 프린트¶
In [50]:
for i in my_phone.items() :
print(i)
('brand', 'apple') ('model', 'iphone 12') ('color', 'red') ('year', 2021)
키, 밸류 값을 각각 가져와서 원하는 처리를 함¶
In [51]:
for i, j in my_phone.items() :
print(i,j)
brand apple model iphone 12 color red year 2021
결론은? 데이터가 나열되어 있는 것들(리스트 등등)은, for 를 통해 쉽게 원하는 값을 가져올 수 있다.¶
In [ ]:
누가????? 코드의 실행 순서가!!!!!!!!!!!!! 바뀐다!!!!!!!!!!!!!!!!!!!!!!!!!¶
In [52]:
fruits
Out[52]:
['apple', 'blueberries', 'mango', 'watemelon']
In [ ]:
# fruits 리스트 안에 들어있는, 과일 이름을 ,
# 하나씩 출력하되, mango 가 나오면, 반복을 끝낸다.
In [61]:
for i in fruits :
print(i)
if i == "mango":
break
apple blueberries mango
In [62]:
for i in fruits :
print(i)
if i == "mango":
break
print("nice~")
apple blueberries mango nice~
In [63]:
for i in fruits :
print(i)
if i == "mango":
print("heollo")
break
print("nice~")
apple blueberries mango heollo nice~
In [64]:
for i in fruits :
print(i)
if i == "mango":
print("heollo")
break
print("Bye~")
print("nice~")
apple blueberries mango heollo nice~
RANGE 함수¶
- range() 함수는, 숫자 리스트를 만들어 준다.
- range() 함수는, 인덱스가 0 부터 시작한다.
- 레인지의 괄호 안에 적는 숫자 갯수만큼 만든다.
- Example: range(0, 10) generates integers from 0 up to, but not including, 10.
In [67]:
range(10)
Out[67]:
range(0, 10)
In [68]:
# 실체, 눈에 안보이지만 이렇게 동작한다.
list(range(10))
Out[68]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [69]:
# range 함수는? 지정한 범위 안에 들어있는 정수들의 리스트를 만들어 준다.
In [70]:
range(7)
Out[70]:
range(0, 7)
In [72]:
# 우리 눈으로 확인하고 싶으면, 리스트로 변경해 주면 된다.
list(range(7))
Out[72]:
[0, 1, 2, 3, 4, 5, 6]
In [73]:
range(26)
Out[73]:
range(0, 26)
In [74]:
list(range(26))
Out[74]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
In [75]:
range(5, 14+1)
Out[75]:
range(5, 15)
In [76]:
list(range(5, 14+1))
Out[76]:
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
In [77]:
# 3부터 9 까지의 정수 리스트를 만들어 주세요
In [79]:
range(3,9+1)
Out[79]:
range(3, 10)
In [80]:
list(range(3,9+1))
Out[80]:
[3, 4, 5, 6, 7, 8, 9]
In [81]:
# 2부터 18까지 짝수 리스트를 만들어 주세요
In [86]:
range(2, 18+1 , 2)
Out[86]:
range(2, 19, 2)
In [87]:
list(range(2, 18+1 , 2))
Out[87]:
[2, 4, 6, 8, 10, 12, 14, 16, 18]
In [88]:
# range 함수와 for 문의 조합
In [ ]:
# hello 를 7 번 화면에 출력
hello
hello
hello
hello
hello
hello
hello
In [91]:
for i in range(7):
print("hello")
hello hello hello hello hello hello hello
'DataScience' 카테고리의 다른 글
Python 4.함수 (0) | 2022.11.21 |
---|---|
Python 2.비교연산자와 if문 (0) | 2022.11.17 |
Python 1.기본 자료구조 D)튜플, 셋 (0) | 2022.11.16 |
Python 1.기본 자료구조 C)딕셔너리, 불리안 (0) | 2022.11.16 |
Python 1.기본 자료구조 B)리스트 (0) | 2022.11.16 |