본문 바로가기

Python 배우기50

break_keyword.py count = 0 while True: # this condition cannot possibly be false print(count) count += 1 if count >= 5: break # exit loop if count >= 5 zoo = ["lion", 'tiger', 'elephant'] while True: # this condition cannot possibly be false animal = zoo.pop(2) # extract one element from the list end print(animal) if animal is 'elephant': break # exit loop 산출물 (Output)0 1 2 3 4 elephant * An infinite loop is a l.. 2016. 3. 10.
while_loop.py square = 1 while square 2016. 3. 10.
for_string.py hello_world = "Hello, World!" for ch in hello_world: # hello_world로부터 각 글자를 프린트 하기 print(ch) length = 0 # length variable 초기화 하기 for ch in hello_world: # hello_world에 포함된 글자수를 계산하기 위하여 loop를 사용하세요. # 이 숫자를 "length" 변수에 저장하세요. length += 1 # 각 iteration 마다 length에 1을 더하기 print(len(hello_world) == length) 산출물 (Output) H e l l o , W o r l d ! True * Strings는 파이썬에서 lists와 비슷합니다. 반복하기 위하여 string을 사용할 .. 2016. 3. 9.
for_loop.py for i in range(5): # for each number i in range 0-4. range(5) function returns list [0, 1, 2, 3, 4] print(i) # this line is executed 5 times. First time i equals 0, then 1, ... primes = [2, 3, 5, 7] # create new list for prime in primes: print(prime)# Print each prime number from the "primes" list using the "for" loop. # A prime number is a natural number greater than 1 that has no positive divi.. 2016. 3. 9.
else_elif.py x = 28 if x < 0: print('x < 0') # executes only if x < 0 elif x == 0: print('x is zero') # if it's not true that x < 0, check if x == 0 elif x == 1: print('x == 1') # if it's not true that x < 0 and x != 0, check if x == 1 else: print('non of the above is true') name = "John" if name == "John": # Print True if name is equal to "John" and False otherwise. print(True) else: print(False) 산출물 (Outpu.. 2016. 3. 9.
if_statement.py name = "John" age = 17 if name == "John" or age == 17: # check that name is "John" or age is 17. If so print next 2 lines. print("name is John") print("John is 17 years old") tasks = ['task1', 'task2'] # create new list if tasks == []: # Print "empty" if the "tasks" list is empty. print("empty") 산출물 (Output) name is John John is 17 years old * The "if" keyword is used to form a conditional state.. 2016. 3. 8.