📗
JunegLee's TIL
  • TIL
  • python
    • class
    • String Basic
    • regularExpression
    • String function
    • Generator
    • String format
    • getset
    • module
    • while
    • numpy
    • print()
    • matplotlib
    • for
    • Boolean
    • tuple
    • package
    • input(variable)
    • list
    • if
    • file
    • type()
    • pandas
    • function
    • dictionary
    • 구문 오류와 예외
    • builtinFunction
    • Constructor
  • algorithm
    • sort
      • mergeSort
      • insertionSort
      • bubbleSort
      • heapSort
      • quickSort
      • selectionSort
    • recursion
    • Greedy
    • DepthFirstSearch
    • basic
      • DataStructure
    • hash
    • BreadthFirstSearch
  • tensorflow
    • keras
      • layers
        • Flatten
        • Flatten
        • Dense
        • Dense
        • Conv2D
        • Conv2D
    • tensorflow1x
    • tensorflow2x
  • DB
    • setting
    • join
    • subQuery
    • overview
  • deep-learning
    • neuralNetwork
    • perceptron
    • neuralNetworkLearning
    • convolution neural network
    • Gradient Descent
    • Linear Regression
    • backPropagation
    • logistic regression
    • overview
  • textPreprocessing
    • overview
  • java
    • basics
      • generic
      • Variable
      • String
    • theory
      • Object Oriented Programing
  • NLP
    • Embedding
    • Natural Language Processing
Powered by GitBook
On this page
  • while 반복문 : for 반복문처럼 사용하기
  • while 반복문 : 상태를 기반으로 반복하기
  • while 반복문 : 시간을 기반으로 반복하기
  • while 반복문 : break 키워드 / continue 키워드
  • 무한반복 while True:

Was this helpful?

  1. python

while

기본구조

while <조건문>:
    <수행할 문장1>
    <수행할 문장2>
    <수행할 문장3>
    ...

while 반복문 : for 반복문처럼 사용하기

i = 0
while i < 10:
    print("{}번째 반복입니다.".format(i))
    i += 1

while 반복문 : 상태를 기반으로 반복하기

list_test = [1, 2, 1, 2]
value = 2
while value in list_test: # list_test 내부에 value가 있다면 반복
    list_test.remove(value)
print(list_test) # [1, 1]

while 반복문 : 시간을 기반으로 반복하기

import time
number = 0
# 5초 동안 반복합니다.
target_tick = time.time() + 5 
while time.time() < target_tick:
    number += 1
print("5초 동안 {}번 반복했습니다.".format(number)) # 5초 동안 25604106번 반복했습니다.

while 반복문 : break 키워드 / continue 키워드

  • break 키워드 : while문을 강제로 빠져나가고 싶을 때 사용

    i = 0
    while True:
     print("{}번째 반복문입니다.".format(i))
     i = i + 1
     input_text = input("> 종료하시겠습니까?(y): ")
     if input_text in ["y", "Y"]:
         print("반복을 종료합니다.")
         break
  • continue 키워드 : 조건이 맞지 않으면 맨 처음(조건문)으로 돌아가게 만드는 것

    numbers = [5, 15, 6, 20, 7, 25]
    for number in numbers:
      if number < 10: # number가 10보다 작으면 다음 반복으로 넘어갑니다.
          continue
      print(number) # 15 20 25

무한반복 while True:

  • while문의 조건문이 True이므로 항상 참이 된다. 따라서 while문 안에 있는 문장들은 무한하게 수행될 것이다.

    while True:
      print(".", end="") 
    # "."을 출력합니다.
    # 기본적으로 end가 "\n"이라 줄바꿈이 일어나는데, 
    # 빈 문자열 ""로 바꿔서 줄바꿈이 일어나지 않게 합니다.
  • 이때는 Ctrl+C를 눌러서 while문을 빠져나갈 수 있다.

PreviousmoduleNextnumpy

Last updated 4 years ago

Was this helpful?