-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.py
More file actions
252 lines (176 loc) · 6.43 KB
/
basic.py
File metadata and controls
252 lines (176 loc) · 6.43 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
# print("hello world")
# 파이썬은 코드를 위에서부터 아래로 읽는다
# a=2
# b=3
# c=a+bs
# print(c)
# 이런 코드를 작성했다고 했을 때, c는 5가 될 것이지만
# 만약 c 변수 다음에 a와 b의 값을 변경해도, 파이썬의 코드 처리 순서 때문에 c는 여전히 5로 출력이 된다.
# snake case를 써야 댐 ex. my_age (myAge는 camel case, js에서 많이 씀)
# 변수
# my_name = "ye eun"
# print(my_name)
# dead = True
# 변수가 필요한 이유:
# 1. 프로그램에 데이터를 넣으려고
# 2. 인간이 이해할 수 있는 이름으로 데이터에 접근하려고
# my_name = "ye eun"
# age= 22
# dead = False
# print("hello my name is ", my_name)
# print("and i'm", age, " years old")
# functions
# 코드를 재사용할 수 있게 해 줌
# def say_hello():
# print("hello how r u?")
# def say_bye():
# print("bye")
# say_hello()
# def == define, 즉 함수를 정의하다라는 뜻이 됨 functions도 기능, 작동하다의 뜻을 가짐
# 함수를 호출하려면 함수 이름 뒤에 괄호를 붙여야 함
# 파이썬에서는 공백이 아주 중요함. 들여써 줘야 어떤 것 안에 들어가있는 코드라는 걸 파이썬이 알아차릴 수 있음
# 다른 언어들이 중괄호로 하는 것들을 파이썬에서는 공백으로 처리함
# def say_hello(user_name, user_age): #say_hello가 값을 받음, 이를 파라미터
# print("hello", user_name)
# print("you are", user_age, "years old")
# say_hello("yeeun", 22) #얘는 argument
# def tax_calculator(money):
# print (money * 0.35)
# tax_calculator(15000000000)
# def say_hello(user_name="anonymous"): #user_name에 기본값 넣기
# print("hello", user_name)
# say_hello("yeeun")
# say_hello()
# def plus(a=0, b=0):
# print(a+b)
# def minus(a=0, b=0):
# print(a-b)
# def multiplication(a=0, b=0):
# print(a*b)
# def division(a=0, b=1):
# print(a/b)
# def power(a=0, b=0):
# print(a**b)
# plus(3, 4)
# minus(3, 4)
# multiplication(3, 4)
# division()
# power(3, 4)
# return
# def tax_calculator(money):
# return money * 0.35
# def pay_tax(tax):
# print("thank you for paying", tax)
# pay_tax(tax_calculator(15000000000))
# 함수 바깥으로 값을 저장하고 싶을 때 사용하는 것이 return
# return은 함수를 끝내버린다
# my_name = "yeeun"
# my_age= 22
# my_color_eyes="brown"
# print(f"hello im {my_name},
# i have {my_age} years in the earth, {my_color_eyes} is my eye color")
# f == format, 변수와 문자열을 같이 사용할 수 있다
# def make_juice(fruit):
# return f"{fruit}+cup..."
# def add_ice(juice):
# return f"{juice}+ice"
# def add_sugar(iced_juice):
# return f"{iced_juice}+sugar"
# juice = make_juice("apple")
# cold_juice = add_ice(juice)
# perfect_juice = add_sugar(cold_juice)
# print(perfect_juice)
# if 조건문
# if 10 == 10:
# print("correct")
# pw_correct= True
# if pw_correct:
# print ("Here is your Money")
# else :
# print("")
# elif
# winner = 10
# if winner > 10:
# print("winner is greater than 10")
# elif winner < 10:
# print("winner is less than 10")
# else:
# print("winner is 10")
# input은 한 개의 argument만 받고, return 한다 이런 함수를 built-in 함수라고 함
# age = input("how old are you")
# print("user answer", age)
# print(type(age))
# age = int(input("how old are you")) # 강제 형변환
# if age<18:
# print("you cant drink")
# elif age >= 18 and age <= 35: # and == &&
# print("you drink beer")
# elif age == 60 or age == 70: # or == ||
# print("birthday party")
# if age >=14 and age <=16:
# print("middle school")
# elif age ==17 or age == 18 or age ==19:
# print("high school")
# else:
# print("go ahead")
# import random
# user_choice = int(input("Choose number"))
# pc_choice = random.randint(1, 50)
# if user_choice == pc_choice:
# print("you won")
# elif user_choice > pc_choice:
# print("lower Computer chose", pc_choice)
# elif user_choice < pc_choice:
# print("higher computer chose", pc_choice)
# While 반복문
# distance = 0
# while distance < 20:
# print("im running", distance, "km")
# distance += 1
# import random
# print("Welcome to Python Casino")
# playing = True
# pc_choice = random.randint(1, 100)
# while playing:
# user_choice = int(input("Choose number"))
# if user_choice == pc_choice:
# print("you won")
# playing = False
# elif user_choice > pc_choice:
# print("lower")
# elif user_choice < pc_choice:
# print("higher")
## data structure = 데이터를 구조화하고 싶을 때 사용 (그 중 list, tuple, dictionary가 있음)
## list
# days_of_week = "Mon, Tue, Wed, Thu, Fri" 이런 코드는 하나하나 써먹을 수 없기 때문에 list를 사용함
# days_of_week = ["Mon", "Tue", "Wed", "Thu", "Fri"]
# print(days_of_week) 이렇게 사용하면 list의 전체 값이 print 되는데, 이때 method를 사용할 수 있음
# name = "nico"
# print(name.upper()) s
# print는 function이고, method는 데이터 뒤에 결합/연결된 function임
# name이라는 변수 안에 method가 존재하는 개념
# days_of_week = ["Mon", "Tue", "Wed", "Thu", "Fri"]
# print(days_of_week.clear()) 이 코드는 리스트를 비우고 아무것도 반환하지 않음. 그래서 none으로 찍힘
# days_of_week = ["Mon", "Tue", "Wed", "Thu", "Fri"]
# days_of_week.clear()
# print(days_of_week) 하지만 이 코드는 비워진 리스트대로 반환되기 때문에 비워진 리스트를 출력함
## tuple
# days = ("Mon", "Tue", "Wed")
# 튜플은 불변성을 가지기 때문에 변경할 수 없음. 메소드도 얼마 없음... 변경할 수 없기 때문!
# 바로 튜플과 리스트의 차이점. 리스트는 뭐든 변경할 수 있지만, 튜플은 그게 불가능함.
# 그거 말고는 다 똑같음
## dicts
# dictionary는 진짜 책처럼 키-값 쌍을 가지기 때문에 dictionary라고 부름
player = {
'name': 'nico',
'age': 12,
'alive': True,
'fav_food': ["pizza", 'burger']
}
# print(player.get('age'))
# print(player.get('fav_food')) == print(player['fav_food'])
# clear(), pop(key)이라는 메소드로 dicts를 지울 수도 있음
# player['xp'] = 1500 추가하기!
# player['fav_food'].append("noodle") fav_food는 리스트기 때문에 리스트에 메소드를 당연히 사용할 수 있음
print(player.get('fav_food'))
print(player)