In [ ]:
money = 1 #0이 아닌 숫자는 True, 0일 경우 False
if money:
print("택시를 타고 가라")
else:
print("걸어 가라")
In [7]:
money = 2000
if money >= 3000:
print("택시를 타고 가라")
else:
print("걸어 가라")
In [11]:
money = 2000
card = 1
if money >=3000 or card: # 둘중하나면 참이면 참
print("택시를 타고 가라")
else:
print("걸어 가라")
In [12]:
pocket = ['paper', 'cellphone', 'money']
if 'money' in pocket:
print("택시를 타고 가라")
else:
print("걸어 가라")
In [13]:
pocket = ['paper', 'money', 'cellphone']
if 'money' in pocket:
pass # 맞으면 아무값도 나타내지 않는다.
else:
print("카드를 꺼내라")
In [14]:
pocket = ['paper', 'cellphone']
card = 1
if 'money' in pocket:
print("택시를 타고 가라")
else:
if card:
print("택시를 타고 가라")
else:
print("걸어 가라")
In [15]:
#위의 구문을 elif를 사용해서
pocket = ['paper', 'cellphone']
card = 1
if 'money' in pocket:
print("택시를 타고 가라")
elif card:
print("택시를 타고 가라")
else:
print("걸어 가라")
In [17]:
treeHit = 0
while treeHit < 10:
treeHit = treeHit + 1
print("나무를 %d번 찍었습니다." %treeHit)
if treeHit == 10:
print("나무가 넘어갑니다.")
In [19]:
coffee = 10
money = 300
while money:
print("돈을 받았으니 커피를 줍니다")
coffee = coffee - 1
print("남은 커피의 양은 %d개 입니다" %coffee)
if not coffee:
print("커피가 다 떨어졌습니다. 판매를 중지합니다")
break
In [20]:
coffee = 10
while True:
money = int(input("돈을 넣어주세요:"))
if money == 300:
print("커피를 줍니다")
coffee = coffee - 1
elif money > 300:
print("거스름돈 %d를 주고 커피를 줍니다" %(money-300))
coffee = coffee - 1
else:
print("돈을 다시 돌려주고 커피를 주지 않습니다")
print("남은 커피의 양은 %d개 입니다" % coffee)
if not coffee:
print("커피가 다 떨어졌습니다. 판매를 중지합니다")
break
In [21]:
a = 0
while a < 10:
a = a + 1
if a%2 == 0:
continue
print(a)
In [25]:
test_list = ['one', 'two', 'three']
for i in test_list:
print(i)
In [26]:
a = [(1,2), (3,4), (5,6)]
for (first, last) in a:
print (first + last)
In [27]:
#총 5명의 학생이 시험을 보았는데 시험 점수가 60점이 넘으면 합격이고 그렇지 않으면 불합격이다. 합격인지 불합격인지 결과를 보여주시오.
marks = [90, 25, 67, 45, 80]
number = 0
for mark in marks:
number =+ 1
if mark >= 60:
print("%d번 학생은 합격입니다." %number)
else:
print("%d번 학생은 불합격입니다." %number)
In [29]:
#합격인 사람만 출력
marks = [90, 25, 67, 45, 80]
number = 0
for mark in marks:
number = number + 1
if mark < 60:
continue
print("%d번 학생은 합격입니다." %number)
In [30]:
sum = 0
for i in range(1, 11):
sum = sum + i
print(sum)
In [32]:
marks = [90, 25, 67, 45, 80]
for number in range(len(marks)):
if marks[number]<60:
continue
print("%d번 학생은 합격입니다" %number)
In [36]:
a = [1,2,3,4]
result = []
for num in a:
result.append(num * 3)
print(result)
In [37]:
result = [num * 3 for num in a]
print(result)
In [38]:
result = [num * 3 for num in a if num % 2 ==0 ]
print(result)
In [40]:
# Q1, if
a = "Life is too short, you need"
if "wife" in a:
print("wife")
elif "python" in a and "you" not in a:
print("python")
elif "shirt" not in a:
print("shirt")
elif "need" in a:
print("need")
else:
print("none")
In [41]:
# Q2, while
i = 0
while True:
i += 1
if i > 5:
break
print(i * '*')
In [42]:
# Q3, for
A = [70, 60, 55, 75, 95, 90, 80, 80, 85, 100]
total = 0
for score in A:
total += score
average = total/len(A)
print(average)
In [ ]:
In [ ]:
In [ ]: