Repository navigation
Expand file tree
/
Copy pathJumpToPython.py
More file actions
277 lines (241 loc) · 5.19 KB
/
Copy pathJumpToPython.py
File metadata and controls
277 lines (241 loc) · 5.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
1+2
# 3
3 / 2.4
# 1.25
3*9
# 27
a = 1
b = 2
a+b
a-b
a*b
a**b
a % b
a/b
a//b
# 3 -1 2 1 1 0.5 0
a = "Python"
print(a)
# Pythong
a = 3
if a > 1:
print("a is bigger than 1")
# a is bigger than 1
for a in [1, 2, 3]:
print(a)
# 1;2;3
def add(a, b):
return a+b
add(3, 5)
# 8
print("""
주석문이
여러 줄 이라면
이러한 방법을 사용하면
편리하다
""")
#
# 주석문이
# 여러 줄 이라면
# 이러한 방법을 사용하면
# 편리하다
#
# cmd 에서 파이썬 출력
print("C:\'Users\'allofdaniel -> cd c:\doit -> python hello.py")
#print("C:\'Users\'allofdaniel -> cd c:\doit -> python hello.py")
# 소수점 표현
a = 4.24E10
b = 4.24e-10
print(a, b)
# 42400000000.0 4.24e-10
# 8진수 16진수
a = 0o177
print(a)
# 127
b = 0x8ff
c = 0xABC
print(b, c)
# 2303 2748
# 따옴표 문법
print("\"Python is very easy.\" he says.")
# "Python is very easy." he says.
c = "Life is too short\nYou need python"
print(c * 3)
# Life is too short
# You need pythonLife is too short
# You need pythonLife is too short
# You need python
print(c[0], c[-0], c[0:4])
# L L Life
# 문자열 응용
print("="*50)
print("My Program")
print("="*50)
# ==================================================
# My program
# ==================================================
# 문자열 포매팅
"I eat %d apples." % 3
#'I eat 3 apples.'
"I eat %s apples." % "five"
#'I eat five apples.'
number = 3
"I eat %d apples." % number
#'I eat 3 apples.'
# %s string문자열 %d integer 정수 %f floating 소수
# 공백
"%10s" % "hi"
#' hi'
"%-10s.jane" % "hi"
#'hi .jane'
"%0.4f" % 3.42134234
# '3.4213'
"I eat {0} apples".format(3)
#'I eat 3 apples'
"I eat {0} apples".format("three")
#'I eat three apples'
"I eat {0} apples for {1} days".format(number, "three")
#'I eat 3 apples for three days'
"I eat {number} apples for {day} days".format(number=10, day="three")
"I eat {0} apples for {day} days".format(10, day="three")
#'I eat 10 apples for three days'
# 정렬
"{0:<10}".format("hi")
# 'hi ' 왼쪽정렬
"{0:>10}".format("hi")
# ' hi' 오른쪽 정렬
"{0:^10}".format("hi")
# ' hi ' 가운데 정렬
"{0:=^10}".format("hi")
# '====hi====' 가운데 정렬
"{{괄호표현}}".format()
# '{괄호표현}'
# f문자열 포매팅
name = '김다니엘'
age = 25
f'나의 이름은 {name}입니다. 나이는 {age}입니다. 내년에 {age+1}이 됩니다.'
'나의 이름은 김다니엘입니다. 나이는 25입니다. 내년에 26이 됩니다.'
# dictionary 응용
d = {'name': '김다니엘', 'age': 25}
f'나의 이름은 {d["name"]}입니다. 나이는 {d["age"]}입니다. '
#'나의 이름은 김다니엘입니다. 나이는 25입니다. '
# 문자열 관련 함수
name.count('다')
# 1 숫자세기
name.find('니')
# 2 위치찾기
name.index('엘')
# 3 위치찾기
name.join('5252')
# '5김다니엘2김다니엘5김다니엘2' 사이사이 끼어넣기
eng = "Daniel"
eng.upper()
eng.lower()
#'DANIEL' 'daniel'
eng.lstrip()
eng.rstrip()
eng.strip()
# 왼쪽 공백제거 오른쪽 공백제거 모두공백제거
eng.replace('Da', 'KimDa')
# KinDaniel
a = "Life is short"
b = "a:b:c"
a.split()
b.split(":")
#['Life', 'is', 'short']
#['a', 'b', 'c']
a = [1, 2, 3, ['a', 'b', 'c']]
a[0]
# 1
a[-1][0]
# 'a'
a = [1, 2, 3]
b = [4, 5, 6]
a+b
# [1,2,3,4,5,6]
a*3
# [1,2,3,1,2,3,1,2,3]
len(a)
# 3
str(a[2])+"hi"
# '3hi' str없이 작성시 오류발생.
a[2] = 4
# [1,2,4]
del a[1]
# [1,3]
a.append(4)
# [1,3,4]
a.append([5, 6])
# [1,3,4,[5,6]]
a = [1, 4, 3, 2]
a.sort()
# [1,2,3,4]
a = ['a', 'c', 'b']
a.sort()
# ['a','b','c']
a.reverse()
# ['c','b','a']
a.index('a')
# 2
a.pop()
# ['c','b']
a.count('b')
# 1
a.extend(['d', 'e'])
# ['c','b','d','e']
##tuple 자료형 ##
t1 = (1, 2, 'a', 'b')
t1[0]
# 1
t1[1:]
# (2,'a','b')
t2 = (3, 4)
t1+t2
# (1,2,'a,'b',3,4)
t2*3
# (3,4,3,4,3,4)
len(t1)
# 4
## dictionary 자료형##
a = {1: 'a'}
a[2] = 'b' # {1: 'a', 2: 'b'}
a['name'] = 'pey' # {1: 'a', 2: 'b', 'name': 'pey'}
a[3] = [1, 2, 3] # {1: 'a', 2: 'b', 'name': 'pey', 3: [1, 2, 3]}
del a[1] # {2: 'b', 'name': 'pey', 3: [1, 2, 3]}
a['name'] # 'pey'
# 동일 키에 2개 값 중복 시 앞의쌍 무시됨.
a = {1: 'a', 1: 'b'} # {1:'b'}
# key 에는 list 쓸 수 없지만, tuple은 쓸 수 있음.
a = {[1, 2]: 'hi'} # 오류
a = {(1, 2): 'hi'} # {(1, 2): 'hi'}
a.keys() # dict_keys([(1, 2)])
for k in a.keys():
print(k) # (1,2)
a.values() # dict_values(['hi'])
a.items() # dict_items([((1, 2), 'hi')])
a.clear() # {}
a = {'name': 'pey', 'phone': '0119993323', 'birth': '1118'}
a.get('name') # 'pey
a.get('phone') # '0119993323'
'name' in a # True
# 집합자료형
s1 = set([1, 2, 3]) # {1,2,3}
s2 = set("Hello") # {'H', 'e', 'o', 'l'}
s1 = set([1, 2, 3])
l1 = list(s1)
l1 # [1,2,3]
l1[0] # 1
t1 = tuple(s1) # 튜플로 변환
t1 # (1,2,3)
t1[0] # 1
s1 = set([1, 2, 3, 4, 5, 6])
s2 = set([4, 5, 6, 7, 8, 9])
s1 & s2 # {4,5,6} 교집합
s1.intersection(s2) # {4,5,6} 교집합
s1 | s2 # {1, 2, 3, 4, 5, 6, 7, 8, 9} 합집합
s1.union(s2) # {1, 2, 3, 4, 5, 6, 7, 8, 9} 합집합
s1-s2 # {1,2,3} 차집합
s1.difference(s2) # {1,2,3} 차집합
s1.add(7) # {1, 2, 3, 4, 5, 6, 7}
s1.remove(2) # {1, 3, 4, 5, 6, 7} remove는 한개밖에 안됨
# bool 자료형 102p