-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter04_01.py
More file actions
171 lines (136 loc) · 3.19 KB
/
chapter04_01.py
File metadata and controls
171 lines (136 loc) · 3.19 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Chapter04-1
# 파이썬 제어문
# IF 실습
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.detach(), encoding = 'utf-8')
sys.stderr = io.TextIOWrapper(sys.stderr.detach(), encoding = 'utf-8')
# 기본 형식
print(type(True)) # 0이 아닌 수, "abc", [1,2,3..], (1,2,3...) ... 등은 True
print(type(False)) # 0, "", [], (), {} ... 등은 False
# 쉽게 생각해서 0이 아니니까 당연히 뭔가 존재를 하겠구나 --> True
# 0, 비어있거나 뭔가 약간 부정적인 느낌이면 --> False
print()
# 예1
if True:
print("Good") # 들여쓰기(Indent)
if False:
# 실행 X
print("Bad")
# 예2
if False:
# 여기는 실행되지 않음.
print("Bad!")
else:
# 여기가 실행된다.
print("Good!")
print()
# 관계연산자 종류
# >, >=, <, <=, ==, !=
x = 15
y = 10
# == 양 변이 같을 때 참.
print(x == y)
# != 양 변이 다를 때 참.
print(x != y)
# > 왼쪽이 클때 참.
print(x > y)
# >= 왼쪽이 크거나 같을 때 참.
print(x >= y)
# < 오른쪽이 클 때 참.
print(x < y)
# <= 오른쪽이 크거나 같을 때 참.
print(x <= y)
# 참 거짓 판별 종류
# 참 : "values", [values], (values), {values}, 1
# 거짓 : "", [], (), {}, 0, None
print()
# 예3
city = ""
if city:
print("You are in:", city)
else:
# 출력
print("Please enter your city")
# 예4
city2 = "Seoul"
if city2:
print("You are in:", city2)
else:
# 출력
print("Please enter your city")
print()
# 논리연산자(중요)
# and, or, not
# 참고 : https://www.tutorialspoint.com/python/python_basic_operators.htm
a = 75
b = 40
c = 10
print('and : ', a > b and b > c) # a > b > c
print('or : ', a > b or b > c)
print('not : ', not a > b)
print('not : ', not b > c)
print(not True)
print(not False)
print()
# 산술, 관계, 논리 우선순위
# 산술 > 관계 > 논리 순서로 적용
print('e1 : ', 3 + 12 > 7 + 3)
print('e2 : ', 5 + 10 * 3 > 7 + 3 * 20)
print('e3 : ', 5 + 10 > 3 and 7 + 3 == 10)
print('e4 : ', 5 + 10 > 0 and not 7 + 3 == 10)
print()
score1 = 90
score2 = 'A'
# 예5
# 복수의 조건이 모두 참일 경우에 실행.
if score1 >= 90 and score2 == 'A':
print("Pass.")
else:
print("Fail.")
print()
# 예6
id1 = "vip"
id2 = "admin"
grade = 'platinum'
if id1 == "vip" or id2 == "admin":
print("관리자 인증")
if id2 == "admin" and grade == "platinum":
print("최상위 관리자")
print()
# 예 7
# 다중 조건문
num = 90
if num >= 90:
print('Grade : A')
elif num >= 80:
print('Grade : B')
elif num >= 70:
print('Grade : C')
else:
print('과락')
print()
# 중첩 조건문
grade = 'A'
total = 95
if grade == 'A':
if total >= 90:
print("장학금 100%")
elif total >= 80:
print("장학금 80%")
else:
print("장학금 70%")
else:
print("장학금 50%")
print()
# in, not in
q = [10, 20, 30]
w = {70, 80, 90, 90}
e = {"name": 'Lee', "city": "Seoul", "grade": "A"}
r = (10, 12, 14)
print(15 in q)
print(90 in w)
print(12 not in r) # r에는 12가 있지만(True) 앞에 not이 있으니 False가 된다
print("name" in e) # key 검색
print("seoul" in e.values()) # value 검색
# values함수를 사용하면 key는 버리고 value값만 가져온다