If
question = input('how\'s the wheather? : ')
if question == 'rain' or question == 'snow':
print('take a umbrella')
elif question == 'fine dust':
print('take a mask')
else:
print('need no anything')
temp = int(input('What\'s the temperature? : '))
if 30 <= temp:
print('hot')
elif 10 <= temp < 30:
print('good')
elif 0 <= temp < 10:
print('cold')
else:
print('don\'t go out')
For
for wait in [0, 1, 2, 3, 4, 5, 6]:
print('{0}'.format(wait))
for wait in range(5):
print('{0}'.format(wait))
for wait in range(1, 6):
print('{0}'.format(wait))
abc = ['a', 'b', 'c']
for ddd in abc:
print('{0}'.format(ddd))
for i in range(5):
print('i:', i, sep = '')
for j in range(5):
print('j:', j, sep = '', end = ' ')
print('\n')
for i in range(5):
for j in range(i + 1):
print('*', end = ' ')
print()
for i in range(5):
for j in range(5 - i):
print('*', end = ' ')
print()
While
aaa = 'A'
index = 5
while index >= 1:
print('{0}, {1}'.format(aaa, index))
index -= 1
if index == 0:
print('index : 0')
bbb = 'B'
person = 'Unknown'
while person != bbb:
print('{0}'.format(bbb))
person = input('What is your name? : ')
print('input = B')
Continue, Break
absent = [2, 5]
no_book = [7]
for student in range(1, 11):
if student in absent:
print('absent : ', student)
continue
elif student in no_book:
print('no_book : {0} and Break'.format(student))
break
print('{0}'.format(student))
print('for문 탈출')
한 줄 For문
alist = [1, 2, 3, 4, 5]
alist = [i + 100 for i in alist]
print(alist)
blist = ['a', 'bb', 'ccc', 'dd', 'e']
blist = [len(i) for i in blist]
print(blist)
clist = ['Abcd', 'aBcd', 'abCd']
clist = [i.upper() for i in clist]
print(clist)
print(all(['a', False]))
print(all([]))
alist = [True, True, False]
print(all(alist))
alist = [23, 66, 36, 53, 19]
if all(60 > i for i in alist):
print('Y')
else:
print('N')
+ Quiz
1. '1 ~ N' 홀수 출력
N = int(input('N : '))
inlist = list(range(1, N + 1))
outlist = []
for i in inlist:
if i % 2 == 0:
continue
outlist.append(i)
print(outlist)
def Odd(N):
outlist = []
for i in range(1, N + 1):
if i % 2 == 0:
continue
outlist.append(i)
print(outlist)
Odd(int(input('N : ')))
2. '1 ~ N' 총합
S = int(input('S : '))
inlist = list(range(1, S + 1))
k = 0
for i in inlist:
k = k + i
if i == S:
print(k)
S = int(input('S : '))
k = 0
for i in range(1, S + 1):
k = k + i
if i == S:
print(k)
def Sum(S):
k = 0
for i in range(1, S + 1):
k = k + i
if i == S:
print(k)
Sum(int(input('S : ')))
3. N의 약수 출력
N = int(input('D : '))
divisors = []
for i in range(1, N + 1):
if N % i == 0:
divisors.append(i)
print(divisors)
def find_divisors(n):
divisors = []
for i in range(1, n + 1):
if n % i == 0:
divisors.append(i)
return divisors
find_divisors(int(input('D : ')))