YouTube2Text

파이썬 무료 강의 100분 완성 (1분 파이썬 모음) — Transcript

by 나도코딩 · 22,142 words · 3,272 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Hello, this is Rado Coding. Yes. I have
  2. 0:02prepared a new course for those who
  3. 0:03want to study Python but find the basic
  4. 0:05videos too long and overwhelming, or
  5. 0:06for those who are too busy to make time
  6. 0:08. The title of this content is "1-
  7. 0:10Minute Python." We proceed by investing
  8. 0:12just one minute a day to learn the
  9. 0:14Python language comprehensively and
  10. 0:16solve test questions. Python is a
  11. 0:18programming language. It is loved by
  12. 0:20many people because its syntax is very
  13. 0:22easy to learn, yet it can be applied in
  14. 0:24a wide variety of fields. It can be
  15. 0:26used in various fields even by those
  16. 0:27who are not majoring in
  17. 0:28computer-related subjects, and it is a
  18. 0:30language I strongly recommend to
  19. 0:31students who want to learn coding. Rado
  20. 0:33Coding will conduct the lectures in a
  21. 0:35very easy and fun way. If you just
  22. 0:37trust and follow along without any
  23. 0:38preparation, you will find yourself
  24. 0:39familiar with Python before you know it
  25. 0:40, just like many others have already
  26. 0:41done. In "1-Minute Python," each
  27. 0:43lecture will cover the core concepts of
  28. 0:45the topic in just one minute, and we
  29. 0:46will present test questions to help you
  30. 0:48check your understanding. Since the
  31. 0:50test questions are displayed for 5
  32. 0:52seconds before the answers and
  33. 0:53explanations are revealed, please pause
  34. 0:55the screen briefly when the questions
  35. 0:56appear, solve the problems yourself,
  36. 0:58and then continue. Okay. Shall we give
  37. 1:00it a try? We will first proceed with
  38. 1:12the environment setup necessary for
  39. 1:13studying Python. For Japanese Python,
  40. 1:15we will use Microsoft's Visual Studio
  41. 1:17Code. First, to install Python, search
  42. 1:20for "Python" on Google and visit the
  43. 1:21official website. Download the
  44. 1:23installation file and run it. Select "
  45. 1:26Add Python 3,""Customize Installation"
  46. 1:27under the "Pass to" section, click Next
  47. 1:29, click Install on the Python 3 version
  48. 1:31information located right under the C
  49. 1:33drive, and click Close once the
  50. 1:35installation is complete. Now, let's
  51. 1:37try installing Visual Studio Code.
  52. 1:40Search for "VS Code" on Google, go to
  53. 1:41the website, and download the
  54. 1:42installation file. Run it, select "
  55. 1:44Agree to the terms," and keep clicking
  56. 1:46the Next button. Okay. Click Install,
  57. 1:48and then click the Finish button.
  58. 1:49Create a folder named "Wonmin Python"
  59. 1:51and click "Open Folder." Select the
  60. 1:53Wonmin Python folder. Click the New
  61. 1:55File button. Yes. Fract. Spy. Yes.
  62. 1:58Install the Python extension from the
  63. 2:00popup that appears at the bottom right.
  64. 2:01Please type it exactly as it appears.
  65. 2:02Yes. Open the Print parentheses, type "
  66. 2:04Hello World", and click the triangle
  67. 2:06button to save it. Yes. If the text "
  68. 2:08Hello World" is displayed correctly
  69. 2:09like this, the environment setup is
  70. 2:11ready. Earlier, we saw that "Hello
  71. 2:24World" was displayed when we entered
  72. 2:25the "Print Hello World" problem and
  73. 2:27clicked this button to run it. The
  74. 2:29meaning of this code is to print, or
  75. 2:31output, the content inside the
  76. 2:32parentheses. Now, the part "Hello World
  77. 2:34" enclosed in parentheses is a
  78. 2:36character data type, which is one of
  79. 2:37several data types. Programming
  80. 2:39languages have various forms of data
  81. 2:40types. Let's assume that we are
  82. 2:42entering our name, address, phone
  83. 2:44number, and year of birth to sign up
  84. 2:45for a website. There would also be a
  85. 2:47checkbox to select the consent option.
  86. 2:49If you input information in this way,
  87. 2:50names and addresses consisting of
  88. 2:52letters are of the character data type,
  89. 2:53phone numbers and birth dates
  90. 2:54consisting of numbers are of the number
  91. 2:56data type, and consent statuses where
  92. 2:57you can choose between "yes" and "no"
  93. 2:58are of the disadvantageous data type.
  94. 3:00In Python, number data types can be
  95. 3:02written as integers or real numbers.
  96. 3:04Character data types refer to text such
  97. 3:06as Korean or English words like "Hello
  98. 3:07World" or "안녕하세요," and you
  99. 3:09simply enclose them in single or double
  100. 3:11down arrows. Please note that if a
  101. 3:13number like 10 is also enclosed in down
  102. 3:15arrows, it becomes a character data
  103. 3:16type. Disadvantage data types are "True
  104. 3:18" and "F." In this case, the T and F at
  105. 3:20the beginning of the word must be a
  106. 3:33space to store a variable value. If you
  107. 3:35are an adult and your nieces and
  108. 3:36nephews bow three times on Lunar New
  109. 3:38Year's Day, you would give them New
  110. 3:39Year's money. Usually, people prepare
  111. 3:40envelopes in advance and put an
  112. 3:42appropriate amount of money inside.
  113. 3:43Let's assume you have three children.
  114. 3:45You met them on Lunar New Year's Day.
  115. 3:47So, you prepare three envelopes in
  116. 3:49order, calculate the appropriate amount
  117. 3:51, put it inside, and label them
  118. 3:52Envelope 1, 2, and 3 respectively.
  119. 3:54Since Jikjang earns money, he just
  120. 3:55sends it by letter. Then, after
  121. 3:56receiving the New Year's bows, you
  122. 3:57simply give the prepared envelopes in
  123. 3:59that order. You don't need to remember
  124. 4:01how much is inside each envelope, as it
  125. 4:03is already there. A space that stores a
  126. 4:05value like this is called a variable.
  127. 4:06In other words, in the previous example
  128. 4:08, the envelopes are the variables, and
  129. 4:10"dawn" is the value. Creating a
  130. 4:12variable and assigning a value to it is
  131. 4:14called variable declaration; you write
  132. 4:15the variable name on the left first,
  133. 4:17write an equal sign, and then write the
  134. 4:19value you want to assign to that
  135. 4:20variable. To create three variables
  136. 4:22corresponding to "Gongtu" and put
  137. 4:2310,000 won, 20,000 won, and a letter
  138. 4:24with the word "Fighting" written on it
  139. 4:26into each one, you would do it like
  140. 4:27this. And to print the value in these
  141. 4:29variables, you can use the print
  142. 4:30function and put each variable inside
  143. 4:32parentheses like this. You can create
  144. 4:45as many variables as you want, but
  145. 4:47there are a few rules you must follow
  146. 4:48when naming variables. First, the name
  147. 4:51must start with a letter or an
  148. 4:53underscore. After starting like this,
  149. 4:55the variable name must consist of
  150. 4:56letters, numbers, or underscores.
  151. 4:57Spaces or special characters cannot be
  152. 4:59used in variable names. Also, since
  153. 5:01case is sensitive, names composed
  154. 5:02entirely of lowercase letters, names
  155. 5:04with only the first letter capitalized,
  156. 5:05and names composed entirely of
  157. 5:07uppercase letters are all recognized as
  158. 5:08different variables. There are also
  159. 5:10terms called keywords or reserved words
  160. 5:11. These refer to words that are
  161. 5:12reserved in advance to perform specific
  162. 5:14functions. Remember "True" and "Press"
  163. 5:16when we studied data analysis? These
  164. 5:18are keywords, and there are over 30
  165. 5:19keywords in total, such as "4,""Il,""if
  166. 5:21," and "P," which we will learn later.
  167. 5:23Since these keywords are already
  168. 5:25reserved, they cannot be used as
  169. 5:26variable names. You will naturally
  170. 5:28become familiar with keywords as you
  171. 5:29study, so there is no need to memorize
  172. 5:30them. Furthermore, while not strictly
  173. 5:32mandatory, there is a recommended
  174. 5:33practice to improve readability—that
  175. 5:35is, to make the code easy to read.
  176. 5:36Generally, it is best to create
  177. 5:38variables using a single lowercase word
  178. 5:40or a combination of words separated by
  179. 5:42underscores, such as "Ah-Under Name" or
  180. 5:43"Your-Under Name." Data types can be
  181. 5:56converted between each other as needed.
  182. 5:57This is called type casting. In Oxygen,
  183. 6:00when we perform 2 + 1, the result is
  184. 6:02naturally 3. However, what would the
  185. 6:04result be if we did 2 + Honey Bag?
  186. 6:07Since we added Honey Bag to it, it
  187. 6:08becomes Honey Bag Bag. In Oxygen, such
  188. 6:10a calculation is impossible. It is the
  189. 6:11same in Python. As mentioned earlier, 2
  190. 6:13is a numeric data type, and if you put
  191. 6:15down brackets around 2, it becomes a
  192. 6:16character data type. Therefore, because
  193. 6:18the numeric 2 and the character 2 are
  194. 6:20different, adding the two is impossible
  195. 6:22. In this case, you can use something
  196. 6:23called type casting. You only need to
  197. 6:25know these three main types of type
  198. 6:26casting, which convert to integers,
  199. 6:27floating-point numbers, and characters,
  200. 6:29respectively. For reference, numeric
  201. 6:30data types can be distinguished into
  202. 6:31integers and floating-point numbers; `
  203. 6:32int` refers to an integer, and ` float`
  204. 6:34refers to a floating-point number with
  205. 6:35a decimal point. After writing it like
  206. 6:36this, if you put the value or variable
  207. 6:38you want to convert inside the
  208. 6:39parentheses, the conversion is
  209. 6:40performed. For example, if you want to
  210. 6:42convert the character '2' into an
  211. 6:43integer, you would write it like this.
  212. 6:44If you want to convert the string "1.5"
  213. 6:46into a floating-point number, you
  214. 6:47simply use ` float 1.5`. Conversely, if
  215. 6:49you want to convert the number 2 into a
  216. 6:51string, you simply put 2 inside the
  217. 6:53parentheses. There is something to be
  218. 6:54careful about here: what goes inside
  219. 6:56the parentheses must be in a form that
  220. 6:57the data type you are converting to can
  221. 6:58recognize. For example, the English
  222. 7:00letter 2 cannot be converted because it
  223. 7:02cannot be recognized as an integer.
  224. 7:04Also, if a floating-point value like
  225. 7:052.5 is enclosed in a string, direct
  226. 7:07conversion to an integer is not
  227. 7:08possible; you must first convert the
  228. 7:09string 2.5 into a floating-point form
  229. 7:11using ` float`, and then enclose it in
  230. 7:12an integer to convert it. Note that
  231. 7:14when converting a floating-point number
  232. 7:16to an integer, the decimal part is
  233. 7:17truncated rather than rounded, so the
  234. 7:18result in this example is 2, not 3.
  235. 7:31Let's study the various operators
  236. 7:33available in Python. First, there are
  237. 7:35arithmetic operators, which allow us to
  238. 7:37calculate values through addition,
  239. 7:38subtraction, multiplication, and
  240. 7:39division. Please note that while the
  241. 7:41symbols for addition and subtraction
  242. 7:42are the same, multiplication uses an
  243. 7:44asterisk (*) instead of a square (*)
  244. 7:45symbol, and division uses a slash (*)
  245. 7:47instead of a division symbol. There is
  246. 7:48also an operator called the percentage
  247. 7:49operator, which calculates the
  248. 7:50remainder. If you perform 5%, the
  249. 7:52result is the remainder of dividing 5
  250. 7:53by 2, which is 1. There is also an
  251. 7:55operator to calculate the quotient. You
  252. 7:57simply write two slashes for the
  253. 7:59quotient. For example, 5/2 results in
  254. 8:01the quotient (*2*) when dividing 5 by 2
  255. 8:03, and the remainder 1 is simply
  256. 8:05discarded. Additionally, writing two
  257. 8:07asterisks (*) for multiplication
  258. 8:09performs a power. That is, 5*asterisk*1
  259. 8:11results in 5 raised to the power of 2,
  260. 8:13or 5*5, which equals 25. There is also
  261. 8:15a comparison function that allows you
  262. 8:17to compare values. You can compare the
  263. 8:19left and right sides to determine if
  264. 8:21the left side is greater than, greater
  265. 8:22than or equal to, or less than or equal
  266. 8:24to the right side. Comparison operators
  267. 8:26produce a result that is either True or
  268. 8:28False. For example, 5 being greater
  269. 8:30than 2 is True, so the result is True.
  270. 8:33You can also compare whether the left
  271. 8:35and right sides are equal; to say they
  272. 8:37are equal, you write two equal signs.
  273. 8:39This is a bit different from the
  274. 8:39mathematical operator. For example, to
  275. 8:41compare if 5 is equal to 2, you write 5
  276. 8:44and 2. When comparing them together,
  277. 8:46you write an exclamation mark; if you
  278. 8:49write 5 and 2, 5 is not equal to 2, so
  279. 8:52the result is True. There is also the
  280. 8:54mathematical operator. Using 'END'
  281. 8:55means True if both the left and right
  282. 8:57sides are True, while 'A' means True if
  283. 9:00at least one of them is True. 'NOT'
  284. 9:02inverts a value. For example, 3 being
  285. 9:05less than 5 is True. However, since 7
  286. 9:07is less than 5 is false, the result is
  287. 9:09false because neither condition is true
  288. 9:11. In the case of 5, 3 being less than 5
  289. 9:13is true, and 7 being less than 5 is
  290. 9:15false; since at least one of them is
  291. 9:17true, the result is true. And here, 3
  292. 9:19being less than 5 is true. However,
  293. 9:21since there is a 'day' before 'true',
  294. 9:23we invert 'true', so yes, it comes out
  295. 9:24as false. There is also a member
  296. 9:26operator that allows you to check if a
  297. 9:28value is included—that is, to check
  298. 9:29if it is a member. If you use 'in', you
  299. 9:32can check if a value is included in
  300. 9:34what follows it. Since the letter 'C'
  301. 9:37is currently included in the word '
  302. 9:39Cache', the result is true. If you use
  303. 9:41'not in', it means it is not included.
  304. 9:43So, 'c not in cas' would mean that 'c'
  305. 9:45is not included in the word 'Cache',
  306. 9:48but this is incorrect. Yes, because 'C'
  307. 9:50is here, the result is false. A
  308. 10:03delimited data type holds one of two
  309. 10:05values: true or false. We have
  310. 10:07previously practiced how outputting
  311. 10:08this in comparison operators results in
  312. 10:10either true or false. Additionally,
  313. 10:11while we practiced converting values to
  314. 10:13int, float, and string when learning
  315. 10:14type casting, there is actually also a
  316. 10:16type casting to delimited types. In
  317. 10:17this case, you only need to specify up
  318. 10:19to 'boa' within the delimited type. A
  319. 10:21boolean converts to true if a value
  320. 10:22exists and false if it does not. The
  321. 10:24criterion for determining whether a
  322. 10:25value exists or not is as follows: for
  323. 10:27character data types, if you assign the
  324. 10:28string "Hello" to variable 'a', this
  325. 10:30indicates that a value exists.
  326. 10:31Similarly, if there are multiple spaces
  327. 10:33within a bracket like 'b', it indicates
  328. 10:34that a space has a value. However, if
  329. 10:36nothing is placed between the brackets,
  330. 10:38as in C, it indicates that there is no
  331. 10:40value. If you write this and run it,
  332. 10:41you can see that in the case of C, only
  333. 10:43'False' is returned. For numbers, all
  334. 10:45non-zero numbers are true, while zero
  335. 10:47is false. This is because zero
  336. 10:48literally means nothing. In the example
  337. 10:50, a and b have values of 1 and -2
  338. 10:52respectively, so they are True, but c
  339. 10:54is 0, so it is False. Also, there is a
  340. 10:56keyword in Python called 'Nun' which
  341. 10:58signifies the absence of a value. As
  342. 11:00you may have noticed, if you use 'Bul
  343. 11:02Nunn', there is no value, so it becomes
  344. 11:03False. To summarize, separation is True
  345. 11:06if true and False if false; it is True
  346. 11:07if there is a value and False if there
  347. 11:09is no value. When hosting a wedding,
  348. 11:22the master of ceremonies often follows
  349. 11:24a pre-written script exactly rather
  350. 11:25than proceeding spontaneously to
  351. 11:26minimize mistakes. Additionally, the
  352. 11:28script contains supplementary
  353. 11:30explanations to regulate the flow,
  354. 11:31indicating where to pause and when to
  355. 11:33resume. For example, if the sequence is
  356. 11:35greeting both sets of parents, the
  357. 11:37script is prepared like this. Of this,
  358. 11:38the part where the sound is actually
  359. 11:40played is right here. The part written
  360. 11:42in the script that is not read but is
  361. 11:43only referenced is here. During the
  362. 11:45wedding ceremony, guests do not hear
  363. 11:46these supplementary explanations. If
  364. 11:48there is no need for further
  365. 11:49explanation, there would be no problem
  366. 11:51if the master of ceremonies is
  367. 11:52experienced and knowledgeable about
  368. 11:53weddings; however, if it is their first
  369. 11:55time hosting, they might make mistakes.
  370. 11:57Similarly, explanations or notes
  371. 11:58written for reference, which are not
  372. 11:59actually read but are included for
  373. 12:01context, are called "comments" in
  374. 12:02programming. This is referred to as "
  375. 12:04commenting out" the written source code
  376. 12:05. In Python, there are two ways to
  377. 12:07comment out code. First, to comment out
  378. 12:09a single statement, or line, simply
  379. 12:11place a hash symbol (#) at the
  380. 12:12beginning of the statement. For example
  381. 12:13, if the code is written like this, the
  382. 12:15second statement is a comment, so it
  383. 12:16will simply be ignored and the program
  384. 12:17will proceed. To comment out one or
  385. 12:19more statements, you can enclose the
  386. 12:21area with three down symbols (*) at the
  387. 12:22beginning and end. By doing this, when
  388. 12:25the program runs, these two lines in
  389. 12:26green will simply be ignored and
  390. 12:27skipped over. Comments are useful when
  391. 12:30you need to provide additional
  392. 12:31explanations for complex code, when
  393. 12:32leaving listings for other developers,
  394. 12:34or when you need to temporarily block
  395. 12:35execution for testing purposes. A
  396. 12:48parking lot at a villa is displayed
  397. 12:49like this. This parking lot is a space
  398. 12:51exclusively for villa residents, and
  399. 12:52parking spots are designated by
  400. 12:54category. If you live in an office, you
  401. 12:56should park in the third spot from the
  402. 12:57left. Once, a friend came to visit.
  403. 13:00From the friend's perspective, since
  404. 13:01all the spots were empty for the second
  405. 13:02reason, it seemed like they could park
  406. 13:04anywhere, but you told them to park in
  407. 13:05the third spot. The friend successfully
  408. 13:07parked where you instructed. Although
  409. 13:09the parking lot was actually empty, the
  410. 13:11friend was able to communicate through
  411. 13:12the information of which spot it was.
  412. 13:14Python has the same concept. It is
  413. 13:16called an index. There is a position in
  414. 13:18Python strings, and also in lists,
  415. 13:20which we will learn about later.
  416. 13:21However, please remember that here, the
  417. 13:24starting point is 0, not 1. For example
  418. 13:26, when declaring a variable named 'Ray'
  419. 13:28, the first character would be 'p'. To
  420. 13:30print 'p', you would write 'Ray' and
  421. 13:32then write the position you want to
  422. 13:33fill inside; since the index value here
  423. 13:36is 0, you would write '0'. To print the
  424. 13:38last problem, the last index value is 5
  425. 13:40, so you would write 'Ray 5'. While
  426. 13:42this works, in line, you can also use
  427. 13:44-1 for the last position. In other
  428. 13:46words, you can use index values that
  429. 13:48start from 0 and increase by 1, or you
  430. 13:50can use indices that start at -1 and
  431. 13:52decrease by 1, such as -2 and -3. There
  432. 13:54is also something called slicing. This
  433. 13:56serves to cut a range based on an index
  434. 13:58value. As you saw earlier, all the
  435. 14:00positions starting from the second one
  436. 14:02were empty. To apply this to Python and
  437. 14:04find the range from the second to the
  438. 14:06sixth position—that is, from 1 to 5
  439. 14:08based on the index—you simply insert
  440. 14:09a colon at the start index and a value
  441. 14:11equal to the end index plus 1 inside
  442. 14:13the lang. Doing this cuts out the range
  443. 14:15from index 1 up to 5, excluding 6. For
  444. 14:17reference, in Python, if you want to
  445. 14:19cut from a specific starting index to
  446. 14:21the end, you can omit the latter part
  447. 14:23like this. That is, writing only up to
  448. 14:251 and the colon cuts from 1 to the end,
  449. 14:27including the end. Conversely, if you
  450. 14:30want to cut from the very beginning to
  451. 14:31a specific index, you simply leave the
  452. 14:33beginning part empty. If you use a
  453. 14:35colon 4, it will cut the value up to 3
  454. 14:37(which is just before index 4) from the
  455. 14:39beginning up to 'pyth'. To work a bit
  456. 14:41more and include the entire range from
  457. 14:42start to finish, simply write a colon
  458. 14:44at both the beginning and end without
  459. 14:45any indices. Do you remember that I
  460. 14:58mentioned it is impossible to add '
  461. 15:00honey bagi' to the number 2? However,
  462. 15:02if you convert the number to a string,
  463. 15:04'honey bagi' becomes possible. If the
  464. 15:06variable 'snack' contains the text '
  465. 15:07honey bagi' and the variable '2'
  466. 15:09contains the text 'two', and you want
  467. 15:10to combine them to make 'honey bagi two
  468. 15:12', you can simply handle it by using
  469. 15:13the addition operator to combine them.
  470. 15:15Let's try adding the text 'please' here
  471. 15:17. The variable 'please' is 'please plus
  472. 15:20please', and while it works like this,
  473. 15:21the variable 'please' is used twice. In
  474. 15:24Python, to make it more convenient, you
  475. 15:26can also express it using the addition
  476. 15:27function like this. In other words, the
  477. 15:29value obtained by adding the text '
  478. 15:30please' to the variable 'please' is
  479. 15:32assigned to the 'please' variable like
  480. 15:33this. The final value assigned to the '
  481. 15:34please' variable becomes this. In fact,
  482. 15:36it can be used with data other than
  483. 15:38text. If the variable 'Num' contains 3,
  484. 15:41you can add 2 to 'Num' by doing this.
  485. 15:44However, if you use 'add' to express it
  486. 15:46as 'Num is 2', it performs exactly the
  487. 15:48same action as the sentence above. You
  488. 15:50can use other functions besides
  489. 15:51addition as well. Currently, 'Num' is 5
  490. 15:53after adding 1, and if you want to
  491. 15:54subtract 1 from this state, you would
  492. 15:56use 'Num is 1'. If you want to multiply
  493. 15:58by 2, you would use 'Num is 2', and if
  494. 16:00you want to divide by 4, you would use
  495. 16:02'Divide by 4'. Returning to text, there
  496. 16:04is a way to determine the length of a
  497. 16:06string. This is the 'Lan' function,
  498. 16:07which stands for length. Yes. We will
  499. 16:09learn about functions later, but for
  500. 16:10now, if you put the string whose length
  501. 16:12you want to know inside the 'Lan'
  502. 16:13parentheses, you can find out how many
  503. 16:14characters it is. 'Honey price' is four
  504. 16:15characters long. Sometimes, you need a
  505. 16:17string that spans multiple lines. In
  506. 16:19such cases, when declaring a variable,
  507. 16:21you simply wrap the string in three
  508. 16:22parentheses at the beginning and end.
  509. 16:23It is similar to commenting out
  510. 16:25multiple lines. There is also a method.
  511. 16:38A method refers to a group of code
  512. 16:40defined within a class that performs a
  513. 16:41specific action or function; you can
  514. 16:43simply understand it as a "function."
  515. 16:44There are various functions available
  516. 16:46for handling strings. To use them, you
  517. 16:47enter the string, a period, the method
  518. 16:49name, and open and close parentheses.
  519. 16:51Sometimes, content is placed inside the
  520. 16:52parentheses. For example, the variable
  521. 16:54` Letter` contains the word "Ha-ryu."
  522. 16:56Only the "Yu" part is capitalized.
  523. 16:57However, if you want to change the
  524. 16:58entire text to lowercase, there is a
  525. 17:00method called ` Lower`. By opening `
  526. 17:02Letter` and typing ` Da-go`, you can
  527. 17:04easily convert all characters to
  528. 17:05lowercase. Conversely, if you want to
  529. 17:07convert the entire content to uppercase
  530. 17:08, you can use the ` Upper` method. If
  531. 17:10you want to capitalize only the first
  532. 17:11letter and keep the rest lowercase, you
  533. 17:13can use the ` Capitalize` method. This
  534. 17:14results in the form of a standard
  535. 17:15English sentence. If you want to
  536. 17:17capitalize only the first letter of
  537. 17:18each word, you can use the ` Title`
  538. 17:19method. You can see that the first
  539. 17:21letter of each word has been
  540. 17:22capitalized, just like a newspaper
  541. 17:23headline. There is also a way to swap
  542. 17:25lowercase letters. You can use the `
  543. 17:27letter.dot.case` and ` case.case`
  544. 17:28methods. Then you can see that
  545. 17:29lowercase letters are converted to
  546. 17:30uppercase and uppercase letters to
  547. 17:31lowercase. If you want to split a
  548. 17:33string, you can use the ` split` method
  549. 17:35, which returns the result split by
  550. 17:37spaces as a list. We will learn about
  551. 17:39lists later, so let's just skip over it
  552. 17:41for now. To check how many times a
  553. 17:42character appears within a string—
  554. 17:44that is, how many times "hower" appears
  555. 17:45in the example—use the ` count`
  556. 17:47method. If you input the character "cha
  557. 17:49" like this, you can easily check the
  558. 17:50count. We learned these things this
  559. 17:51time, and since there are so many types
  560. 17:53of methods, we will continue next time.
  561. 17:59I mentioned that methods are functions,
  562. 18:06right? Yes. Let's continue learning the
  563. 18:08main methods. The variable ` s`
  564. 18:10contains the text "I am also a high
  565. 18:11school." To check if this string starts
  566. 18:14with "what problem" or "I am," there is
  567. 18:16` starts with`. You just need to put
  568. 18:17the value you want to check inside the
  569. 18:19parentheses here. The result will come
  570. 18:20out as either "true" or "false" in a
  571. 18:21split form. To check what value it ends
  572. 18:23with, there is ` ends with`. Yes. The
  573. 18:25usage is the same as Start. If there
  574. 18:27are unnecessary parts like dots at the
  575. 18:29beginning or end, you can use Strip to
  576. 18:31remove the unnecessary parts by placing
  577. 18:33the characters you want to remove
  578. 18:34inside the parentheses. Note that if
  579. 18:36you leave nothing inside the
  580. 18:37parentheses, unnecessary spaces at the
  581. 18:38beginning and end of the text will be
  582. 18:40removed. You can also intentionally
  583. 18:41change characters. You can easily do
  584. 18:43this by using Reply and inputting the
  585. 18:44problem to replace the character you
  586. 18:45are looking for. Now, we are finding "
  587. 18:47High School" and changing it to "High
  588. 18:48School." To check where a specific
  589. 18:50character is located, you can use the
  590. 18:51Find method and input a value; it will
  591. 18:53tell you its position based on the
  592. 18:54index. Since "School" is between 0, 1,
  593. 18:572, and 4, the value 4 is displayed. If
  594. 19:00you want to insert a string in the
  595. 19:01middle between other characters, you
  596. 19:03can use the Center method. Here, we
  597. 19:04defined the total length as "ten
  598. 19:06characters," and by using a hyphen, you
  599. 19:07can produce a result corresponding to
  600. 19:09the end of the string, such as wrapping
  601. 19:10"I too High School" with "my Hi." We
  602. 19:12have learned these things so far. There
  603. 19:14are still many more methods available;
  604. 19:16you can search for "Python built-in
  605. 19:17methods" on Google. If you visit the
  606. 19:19official website, press Ctrl + F, and
  607. 19:21search using Find, you will see results
  608. 19:23like this. If you scroll up a bit, you
  609. 19:25will find "String Methods." Below that,
  610. 19:28you will see various different methods
  611. 19:30with explanations of their functions,
  612. 19:31so it would be helpful to refer to this
  613. 19:33section. When the variable 'Python'
  614. 19:45contains 'Python' and the variable '
  615. 19:47Jabal' contains the value 'Jabal', to
  616. 19:48print both using ` print`, you would
  617. 19:50have done it like this using addition.
  618. 19:51We also added a space in the middle for
  619. 19:53separation. However, there is another
  620. 19:55way besides this: using commas. If you
  621. 19:57insert the variables or values you want
  622. 19:59to print, separated by commas, it
  623. 20:01automatically prints them with spaces.
  624. 20:03But what if you want to include
  625. 20:04variable values within another sentence
  626. 20:05, rather than just concatenating them
  627. 20:07as they are? For example, you can
  628. 20:08create a sentence like this. First, you
  629. 20:10can do it like this using addition.
  630. 20:12Here, the colored parts are the string
  631. 20:14values, which have been combined
  632. 20:15through addition. Alternatively, you
  633. 20:17can do it like this using the commas we
  634. 20:19learned earlier. However, since
  635. 20:20addition is output via commas, it looks
  636. 20:22complicated with many columns, doesn't
  637. 20:23it? In such cases, we can use the "
  638. 20:25string format." First, there is a
  639. 20:27method using curly braces and the
  640. 20:28format. When you have a statement like
  641. 20:30this, simply place parentheses inside
  642. 20:31the statement, put a dot after the
  643. 20:33string, write "format," and then put
  644. 20:34the value you want to output inside the
  645. 20:36parentheses. By doing so, the values
  646. 20:37inside the format will be assigned to
  647. 20:38the curly braces inside the string in
  648. 20:40order. The second method is similar to
  649. 20:41the first, but numbers are placed
  650. 20:43inside the curly braces. In this case,
  651. 20:44the numbers increase from 0 to 1, 2,
  652. 20:46and so on. If you write the statement
  653. 20:47this way, the values inside the format
  654. 20:48will be assigned to the positions of
  655. 20:49curly brace 0 and curly brace 1 in
  656. 20:51order. The difference from the first
  657. 20:52method is that you can determine the
  658. 20:54order using numbers rather than curly
  659. 20:55braces. For example, if you write curly
  660. 20:57brace 1 and curly brace 0 instead of
  661. 20:58curly brace 0 and curly brace 1, you
  662. 21:00can see that "Java" goes into the 1
  663. 21:01position and "Python" goes into the 0
  664. 21:03position, changing the order. Finally,
  665. 21:04there is something called an 'f-string'
  666. 21:06. You place an 'f' at the beginning of
  667. 21:07the string and write the characters
  668. 21:09within curly braces; you simply define
  669. 21:10the value you want to output directly
  670. 21:11inside these braces. In other words, if
  671. 21:13you write 'Python Java' like this, it
  672. 21:14directly outputs the value of the
  673. 21:15Python variable or the Java variable.
  674. 21:17It's incredibly convenient, isn't it?
  675. 21:29Consider this sentence: "My friend
  676. 21:31asked, 'Is learning Pi easy?'" If you
  677. 21:33look here, you see large down brackets.
  678. 21:35Yes. To print this sentence, using
  679. 21:36single down brackets at the beginning
  680. 21:38and end will ensure it prints correctly
  681. 21:39. What about this case? "I thought to
  682. 21:42myself, 'It's incredibly difficult.'"
  683. 21:44Here, there are single down brackets
  684. 21:45like this. In this instance, attempting
  685. 21:47to print using single down brackets
  686. 21:49will cause an error. Since a string is
  687. 21:50the part enclosed in single or large
  688. 21:52down brackets, the parts "I thought to
  689. 21:54myself" and "thought" are recognized as
  690. 21:57separate characters. In other words,
  691. 21:58the part "It's incredibly difficult" in
  692. 22:00the middle becomes an unreadable entity
  693. 22:01. In this case, wrapping the entire
  694. 22:03sentence in large down brackets instead
  695. 22:04of single down brackets solves the
  696. 22:05problem. But what about this case?
  697. 22:07However, thinking "I can't be the only
  698. 22:09one suffering," I said, "It's super
  699. 22:10easy." This sentence contains both
  700. 22:12small and large slashes. Since I
  701. 22:14started by writing the large slashes
  702. 22:15first, problems arose starting from the
  703. 22:17part that was "super easy" later on. In
  704. 22:18such cases, you can use escape
  705. 22:20characters. An escape character is used
  706. 22:21to indicate functions or problems that
  707. 22:23cannot be expressed by combining a
  708. 22:24backslash and a specific problem.
  709. 22:25Within the text, you simply write the
  710. 22:27backslash after the backslash like this
  711. 22:28. In other words, if you write this
  712. 22:30sentence like this, both the small and
  713. 22:31large slashes will be printed correctly
  714. 22:32without any issues. In this case, you
  715. 22:34can use either a small or large slash
  716. 22:36for the start and end of the print
  717. 22:37statement. You can also express a
  718. 22:38backslash using escape characters. In
  719. 22:40Windows, a folder path looks like this,
  720. 22:42but an error occurs if you try to print
  721. 22:43the value exactly as is. In this case,
  722. 22:45if you write backslashes twice, it will
  723. 22:46be printed correctly in this form. You
  724. 22:48can also create line breaks using the
  725. 22:49backslash N. When creating text that
  726. 22:51spans multiple lines, you could do this
  727. 22:52by using three slashes. If you change
  728. 22:54this using the escape character/N, you
  729. 22:56can see the result with a line break at
  730. 22:59the escape character. Python provides
  731. 23:11built-in data types for managing
  732. 23:13multiple pieces of data at once. One of
  733. 23:15them is the list, which we discussed a
  734. 23:16few times in previous lectures. Let's
  735. 23:18assume we go to a convenience store to
  736. 23:19buy an OS. We can easily carry just one
  737. 23:21OS. But then we see a Mon Cher next to
  738. 23:23it. We should buy a Mon Cher too, right
  739. 23:25? Well, since we have two hands,
  740. 23:27carrying the Mon Cher isn't a big
  741. 23:28problem so far. But then we see a Choco
  742. 23:31Pie next to it. When I was in the
  743. 23:32military, I used to make a cake out of
  744. 23:34Choco Pies to add to my birthday party.
  745. 23:36That memory came back to me after a
  746. 23:37long time, so I'll buy about three
  747. 23:38Choco Pies. However, trying to carry a
  748. 23:40total of five items, it looks like I
  749. 23:41don't have enough hands. At this point,
  750. 23:43the convenience store employee asks, "
  751. 23:44Is it good? It will definitely be
  752. 23:46easier to carry if you have an empty
  753. 23:47bag." So, we put everything into a
  754. 23:48plastic bag and carry it comfortably
  755. 23:50with one hand. The list we will learn
  756. 23:51about this time is exactly like this
  757. 23:52plastic bag. While variables allow us
  758. 23:54to store a single value, lists can
  759. 23:56store multiple values. They are
  760. 23:58generally used to manage related,
  761. 24:00consecutive data. You can declare a
  762. 24:02list by separating the values you want
  763. 24:04to store inside with commas, like this.
  764. 24:05You just need to put the chocolate
  765. 24:06breads into a list called "My List" in
  766. 24:08order. Oh, I mentioned there are three
  767. 24:09chocolate files, right? Since lists
  768. 24:11allow duplicate values, you just need
  769. 24:13to write "chocolate file" three times
  770. 24:14like this. You can also mix and match
  771. 24:16any values inside the list, such as
  772. 24:18numbers, separated strings, etc. If you
  773. 24:20want to create a list of four plastic
  774. 24:21bags with no values, you simply write "
  775. 24:23List". It becomes an empty plastic bag.
  776. 24:37I mentioned that a list is like a
  777. 24:38plastic bag that can hold multiple
  778. 24:40values. Now, let's look at how to use
  779. 24:42lists. When a list is declared like
  780. 24:44this, to check the value corresponding
  781. 24:45to a specific index, you simply insert
  782. 24:47the index into the list, just like with
  783. 24:49strings, and the value at that position
  784. 24:50is immediately displayed. This is
  785. 24:52possible because the order of values in
  786. 24:54a list is guaranteed. For example, the
  787. 24:56first is OS, the second is Mongsil, and
  788. 24:57so on. So, strictly speaking, a list is
  789. 24:59not a random plastic bag where you just
  790. 25:01throw things in randomly; rather, it is
  791. 25:02a special plastic bag with internal
  792. 25:04compartments that manages items neatly
  793. 25:05according to the order in which they
  794. 25:06are inserted. Because the order is
  795. 25:08guaranteed, slicing is also possible.
  796. 25:09To cut from a specific point to a
  797. 25:11specific point, you simply insert the
  798. 25:12starting value through the colon, just
  799. 25:14like with strings. The result will be
  800. 25:15in the form of a list. To check what
  801. 25:17values are contained in the list, you
  802. 25:19use ` in`. Yes. The result is in a
  803. 25:21separated form; it returns true if
  804. 25:22there are values, and false otherwise.
  805. 25:23To check how many values are in the
  806. 25:25list, you can use the ` ran` function.
  807. 25:27Here, there are three values. Lists can
  808. 25:29be freely modified. To modify a value,
  809. 25:32you specify an index like this and
  810. 25:33insert a new value, just as you would
  811. 25:35change a variable. You can also add a
  812. 25:37new value after the list main using the
  813. 25:39` ahand` method, or delete it by
  814. 25:40entering the value to be removed inside
  815. 25:42the parentheses using ` remove`. To
  816. 25:43merge values from different lists, you
  817. 25:45can use the ` extend` method. Please
  818. 25:47note that there are various other
  819. 25:49methods available in addition to those
  820. 25:50introduced. There is also a built-in
  821. 26:02data type called a tuple that can
  822. 26:04manage multiple data items, similar to
  823. 26:05a list. With lists, values were placed
  824. 26:07inside parentheses and separated by
  825. 26:08commas. Tuples, however, place values
  826. 26:09inside parentheses rather than
  827. 26:10parentheses and separate them by commas
  828. 26:12. The biggest difference between lists
  829. 26:13and tuples is that once a tuple is
  830. 26:15created, it cannot be modified. When
  831. 26:17studying lists, let's assume you first
  832. 26:18buy OS and Mongsil at a convenience
  833. 26:20store, put them in a plastic bag, and
  834. 26:22go home. If you suddenly want to eat a
  835. 26:24Choco Pie and return to the store to
  836. 26:26buy one, you would simply put the Choco
  837. 26:28Pie into the plastic bag. However, as
  838. 26:30mentioned, tuples cannot be modified.
  839. 26:32It is similar to attaching a "Do Not
  840. 26:34Open" sticker to a plastic bag after
  841. 26:35payment; once you buy OS and Mongsil
  842. 26:37and the sticker is attached, you cannot
  843. 26:39buy Choco Pies later, even if you want
  844. 26:41to. Furthermore, since OS and Mongsil
  845. 26:42cannot be removed from the bag, a
  846. 26:44refund is also impossible. Tuples
  847. 26:45cannot be modified like this, but aside
  848. 26:47from that, they are very similar to
  849. 26:48lists, so you can understand them as
  850. 26:49lists dedicated to periodicity. You can
  851. 26:51define a tuple like this using
  852. 26:52parentheses. Just like lists,
  853. 26:54duplicates are allowed. You can put any
  854. 26:56data type into it. Except for
  855. 26:58modification, it is also similar to a
  856. 26:59list in terms of usage. You can access
  857. 27:01values at any position, and the order
  858. 27:03of elements is guaranteed. It is also
  859. 27:05possible to print only the desired
  860. 27:06amount using slashing. You can check
  861. 27:08inclusion using the ` include` keyword,
  862. 27:09and you can also check the length.
  863. 27:11Tuples also have these methods. Let's
  864. 27:24continue studying tuples. You can
  865. 27:26create a tuple by writing values and
  866. 27:28inserting them separated by commas.
  867. 27:30This process is called packing. It
  868. 27:31means putting the values highlighted in
  869. 27:33orange on the right into the 'My Tuple'
  870. 27:35on the left, just like putting an empty
  871. 27:37envelope. However, Python also has a
  872. 27:38concept that is the opposite of this.
  873. 27:40This is called unpacking. The contents
  874. 27:43of the 'my tuple' are taken out one by
  875. 27:44one and stored in the variables Pi 1,
  876. 27:46Pi 2, and Pi 3, respectively. It
  877. 27:47becomes exactly the same as code that
  878. 27:49assigns values to individual variables
  879. 27:51in this way. You can use asterisks
  880. 27:52during unpacking. If a tuple called '
  881. 27:54Numbers' is defined with values from 1
  882. 27:55to 10, writing the code like this would
  883. 27:57result in a mismatch between the number
  884. 27:58of elements in 'Numbers' and the number
  885. 27:59of variables. However, by using an
  886. 28:01asterisk, it means to put the first
  887. 28:03value (1) from 'Numbers' into the
  888. 28:05original variable, 2 into the variable
  889. 28:072, and all the rest into 'Others'.
  890. 28:09Please note that 'Others' contains
  891. 28:10multiple values but is in the form of a
  892. 28:12list, not a tuple. You can change the
  893. 28:13position of the asterisk depending on
  894. 28:14the exam requirements. If you place the
  895. 28:16asterisk at the very beginning of the
  896. 28:17variable like this, 9 will be assigned
  897. 28:18to the value 9, 10 to the value 10, and
  898. 28:20the remaining values—from 1 to 8—
  899. 28:21will be assigned to the three 'Others'.
  900. 28:23If you place a star between other
  901. 28:24variables, the original variable will
  902. 28:26contain 1 and 10, while the variable 10
  903. 28:27will contain 10, and the values in
  904. 28:29between will be from 2 to 9. There is
  905. 28:34something called a Set. Like lists and
  906. 28:43tuples, it can store multiple pieces of
  907. 28:46data. Sets are characterized by the
  908. 28:47fact that the order of the values is
  909. 28:48not guaranteed and duplicates are not
  910. 28:49allowed. Let's say you have decided to
  911. 28:51meet your friends for a meal. You are
  912. 28:52to decide the menu, but first, let's
  913. 28:54list what foods your friends like.
  914. 28:56Friend A says they like pork cutlet,
  915. 28:58bossam, and Jeu tteokbap. Friend B says
  916. 29:00they like jjamppong, sushi, and Jeu
  917. 29:02tteokbap. Now, which menu would be a
  918. 29:04safe bet? That's right. If you choose
  919. 29:06Jeyuk Deopbap, both friends will
  920. 29:08probably enjoy it. You can declare a
  921. 29:09Set like this. You put the values
  922. 29:11inside the curly braces separated by
  923. 29:13commas. First, for convenience, I will
  924. 29:15name Friend A with a capital A and
  925. 29:16create a Set of their favorite foods. I
  926. 29:18will declare Friend B like this as well
  927. 29:19. We already know the answer to what
  928. 29:21food the two friends like in common,
  929. 29:23but let's give it a try. There is a
  930. 29:24method called Intersection, which
  931. 29:26allows us to easily utilize the "Jeu"
  932. 29:27bait. However, actually, you all like
  933. 29:29all the foods listed here. Today, your
  934. 29:32friends told you to choose the food you
  935. 29:33want to eat when you grow up as the
  936. 29:34main character. Then, all the foods
  937. 29:36that the two friends like could be
  938. 29:37candidates, right? Yes. Let's combine
  939. 29:39all the foods using the Union method.
  940. 29:41You can use the Union method like this.
  941. 29:43Since sets do not guarantee order, the
  942. 29:45output result might be slightly
  943. 29:46different from this. Also, since
  944. 29:47duplicates are not allowed, we can see
  945. 29:49that Jeyuk Deopbap appears only once.
  946. 29:50However, suddenly, Friend A says they
  947. 29:52can't really eat other foods except for
  948. 29:54the ones they like. Also, they suggest
  949. 29:56that they want to eat Jeyuk without the
  950. 29:57rice because they have been eating too
  951. 29:59much rice with it recently. So, we have
  952. 30:00no choice but to deduce the food that
  953. 30:02only A likes. Since they said they
  954. 30:03don't want to eat Jeyuk, which they
  955. 30:05like in common with B, we can use the
  956. 30:06difference of sets—that is,
  957. 30:07subtracting Set B from Set A. In this
  958. 30:09case, you can use the ` difference`
  959. 30:10method. This extracts the values found
  960. 30:12only in Set A, subtracting the values
  961. 30:14from Set B. I mentioned that the order
  962. 30:28of values in a set is not guaranteed.
  963. 30:29This means that in a list, we could
  964. 30:31access the first value using an index
  965. 30:33or directly modify the value at index
  966. 30:350. Even with tuples, while value
  967. 30:36modification was impossible, accessing
  968. 30:40values via an index was possible.
  969. 30:43However, since the order of a set is
  970. 30:44not guaranteed, you cannot access
  971. 30:45values via an index. This is a
  972. 30:48significant difference from lists and
  973. 30:48tuples. However, that does not mean it
  974. 30:51is read-only like a tuple. For example,
  975. 30:53let's say a friend originally thought
  976. 30:54they would only eat three dishes, but
  977. 30:55after trying Dakgalbi (spicy stir-fried
  978. 30:56chicken) once, they found it delicious.
  979. 30:58This means the number of dishes they
  980. 30:59can eat has increased by one. In that
  981. 31:01case, they can add a value using the `
  982. 31:02add` method. Or, if they have eaten too
  983. 31:04much Jeyuk Deopbap (spicy pork rice
  984. 31:06bowl) and are sick of it and can't eat
  985. 31:07any more, they can remove a value using
  986. 31:09the ` remove` method. Alternatively, if
  987. 31:11you decide to go on a diet and stop
  988. 31:12eating all food in the future, you can
  989. 31:14use the ` clear` method to completely
  990. 31:15erase all values. Note that doing just
  991. 31:18this makes ` myset` an empty set.
  992. 31:20However, if you type ` set` followed by
  993. 31:22a space (D), which signifies delete, it
  994. 31:24completely erases the ` myset` set
  995. 31:26itself. Consequently, you will not be
  996. 31:27able to use ` myset` at all in the
  997. 31:28future. This applies equally to lists
  998. 31:30and tuples. Sets also have these kinds
  999. 31:32of methods. The last data type we will
  1000. 31:46learn is the dictionary. The word means
  1001. 31:47"statement" or "arbitrary." When we
  1002. 31:49look up a word in a dictionary, it is
  1003. 31:50usually structured like this: there is
  1004. 31:52the word, and its meaning is always
  1005. 31:53included. It is similar in Python. If
  1006. 31:56you are curious about the meaning of
  1007. 31:57the word "study," you would look up "
  1008. 31:58study" in the image. In this case, "
  1009. 32:00study" is called the key, and the part
  1010. 32:01explaining the meaning is called the
  1011. 32:03value. To summarize, a dictionary
  1012. 32:05always consists of key-value pairs.
  1013. 32:07However, duplicate keys are not allowed
  1014. 32:09. Dictionaries are created using curly
  1015. 32:11braces like this, separating keys and
  1016. 32:12values with colons. This constitutes a
  1017. 32:14single piece of data. Then, you
  1018. 32:15separate the next data with commas,
  1019. 32:17writing "Key 2, Value 2," and so on,
  1020. 32:19continuing to write colon-separated
  1021. 32:20data. For example, let's say there is a
  1022. 32:23person. Name, age, height, and weight
  1023. 32:24correspond to height, and the actual
  1024. 32:26values are the values. You can express
  1025. 32:29this in code like this. However,
  1026. 32:30writing it out makes it a bit long and
  1027. 32:32difficult to read. In that case, it is
  1028. 32:34fine to write it using line breaks like
  1029. 32:35this. In the example, the problem is
  1030. 32:37structured as a table containing all
  1031. 32:39key values, but keys can also be
  1032. 32:40defined in other forms, such as numbers
  1033. 32:41. You just need to avoid duplicates.
  1034. 32:44Also, regarding the value part, while
  1035. 32:45keys are characters and other values
  1036. 32:47are integers, dictionaries can use
  1037. 32:49numbers, characters, delimiters, lists,
  1038. 32:51tuples, and anything else as values. To
  1039. 32:53access data defined in this way, you
  1040. 32:55simply insert the key inside. In other
  1041. 32:58words, to print the value corresponding
  1042. 32:59to the key 'Name', you do it like this.
  1043. 33:01To print what corresponds to the key '
  1044. 33:03...', you can do it like this. Let's
  1045. 33:15look at how to use dictionaries step by
  1046. 33:17step. First, when we declared a
  1047. 33:18dictionary named 'Person', we tried
  1048. 33:20checking the value corresponding to a
  1049. 33:21key by inserting the key through the
  1050. 33:23reference like this to retrieve the
  1051. 33:24corresponding value. However, an error
  1052. 33:26occurs if you try to access the key '
  1053. 33:28Alias'. This is because the 'Alias' key
  1054. 33:29does not exist in the dictionary. In
  1055. 33:31this case, if you use the 'get' method,
  1056. 33:33it attempts to access the 'Alias' key
  1057. 33:34in the same way, but even if the key
  1058. 33:36value does not exist, no error occurs
  1059. 33:37and 'None' is printed. Of course, if
  1060. 33:39the key value exists, the corresponding
  1061. 33:40value will be displayed. To add new
  1062. 33:42data, you simply insert a new key into
  1063. 33:43the reference and define the value. If
  1064. 33:46you want to change the value
  1065. 33:47corresponding to a specific key, you do
  1066. 33:48it in the same way. In other words, if
  1067. 33:50it is a new key, the value is modified;
  1068. 33:51if it is an existing key, the value is
  1069. 33:53modified. To change the values
  1070. 33:54corresponding to multiple keys rather
  1071. 33:55than just one, use the update method.
  1072. 33:57Inside the parentheses, define the keys
  1073. 33:59to be changed and the values to be
  1074. 34:00updated in a dictionary format. Here,
  1075. 34:02we are updating the height to 130 and
  1076. 34:04the weight to 26. To delete specific
  1077. 34:07key-value data, use the pop method and
  1078. 34:09pass the key value you want to delete.
  1079. 34:11To delete all data, use clear, just
  1080. 34:13like with other data types. To check
  1081. 34:16which keys are in the dictionary, use
  1082. 34:18the keys method. To check which values
  1083. 34:21are present, use the values method.
  1084. 34:24There is also a way to print all
  1085. 34:25key-value pairs; by using the items
  1086. 34:27method, you can view all data where the
  1087. 34:29key and value are separated by commas.
  1088. 34:31Dictionaries also have these features.
  1089. 34:44So far, we have learned about four data
  1090. 34:46types for managing multiple data at
  1091. 34:48once. Let's compare the characteristics
  1092. 34:50of each data type. First, for lists,
  1093. 34:52you convert them to a list and insert
  1094. 34:53values separated by commas. If you
  1095. 34:56insert them exactly as they are, you
  1096. 34:57can add as many duplicate values as you
  1097. 34:58want. Since order is guaranteed, you
  1098. 35:00can access them by inserting an index
  1099. 35:02inside the list. Slashing is also
  1100. 35:04possible. You can directly modify
  1101. 35:05values through indexing, add values
  1102. 35:07using methods like these, and delete
  1103. 35:09them. Tuples can be declared using
  1104. 35:12parentheses. Like lists, order is
  1105. 35:14maintained and duplicate values are
  1106. 35:16allowed. Accessing them works in the
  1107. 35:17same way. However, tuples are read-only
  1108. 35:19, so modification, addition, and
  1109. 35:20deletion are impossible. This is a very
  1110. 35:22important characteristic of tuples.
  1111. 35:23Sets are declared using curly braces.
  1112. 35:25Unlike lists and tuples, the order of
  1113. 35:27values is not guaranteed. Also, since
  1114. 35:29duplicates are not allowed, even if the
  1115. 35:30same value is inserted multiple times,
  1116. 35:32that value exists only once within the
  1117. 35:33set. Because order is not guaranteed,
  1118. 35:35access via index is impossible. Since
  1119. 35:37direct access is impossible, you cannot
  1120. 35:39directly modify any value within the
  1121. 35:40set either. However, it is possible to
  1122. 35:42add or delete new values within the set
  1123. 35:43itself. Finally, dictionaries are
  1124. 35:45declared using curly braces, and
  1125. 35:46key-value pairs always follow.
  1126. 35:48Additionally, the order of the input
  1127. 35:50values is guaranteed. For reference,
  1128. 35:51order guarantees began in Python
  1129. 35:53version 3.7 and above. Key values do
  1130. 35:55not allow duplicates. Access is done by
  1131. 35:57key, not index, and values can be
  1132. 35:59modified after access. New key-value
  1133. 36:01pairs can be easily added and deleted
  1134. 36:03in this way. While there are various
  1135. 36:05scenarios during development, here is a
  1136. 36:06brief summary of when to use each data
  1137. 36:08type: If you need to manage multiple
  1138. 36:10values in order, simply use a list. Use
  1139. 36:12a tuple if the data will not change
  1140. 36:14once created, or if you want to prevent
  1141. 36:16accidental changes during program
  1142. 36:17execution. Use a set if verifying the
  1143. 36:19existence of a specific value is
  1144. 36:21important and the data does not allow
  1145. 36:23duplicates. If you want to manage data
  1146. 36:25efficiently through keys, use a
  1147. 36:26dictionary consisting of key-value
  1148. 36:28pairs. I advised using tuples when
  1149. 36:41values do not need to change or should
  1150. 36:43not change. This is because tuples are
  1151. 36:45a dedicated data type that cannot be
  1152. 36:46modified, added, or deleted. However,
  1153. 36:48there is actually a way to modify
  1154. 36:50tuples. If you have declared a tuple
  1155. 36:52like this and want to add "Choco Pie,"
  1156. 36:54you can write a list and wrap it in
  1157. 36:55parentheses to convert it into a list.
  1158. 36:57First, define this list as "Mile List."
  1159. 37:00Add a value using the "Ad" values
  1160. 37:01provided by the list, and then wrap "
  1161. 37:03Mile List" again in tuple parentheses
  1162. 37:04to change the data type back to a tuple
  1163. 37:06. You then simply need to put this into
  1164. 37:08"My Tuple." To summarize, to convert a
  1165. 37:10tuple into a list or a list into a
  1166. 37:12tuple, you simply wrap each data type
  1167. 37:13in tuple parentheses or list
  1168. 37:15parentheses like this. Lists allow
  1169. 37:17duplicate values, but sometimes you
  1170. 37:18need to remove duplicate values. You
  1171. 37:21can do this like this: if you have a
  1172. 37:22list that contains "Choco Pie" three
  1173. 37:24times, you can convert it into a set by
  1174. 37:25folding the set and placing the list
  1175. 37:27inside the parentheses. Since sets do
  1176. 37:29not allow duplicates, duplicate values
  1177. 37:30like "Choco Pie" will disappear during
  1178. 37:32this process, leaving only one out of
  1179. 37:33the three. Then, you can convert it
  1180. 37:35back to a list. However, while sets do
  1181. 37:37not allow duplicates, they do not
  1182. 37:39guarantee order either. Therefore, if
  1183. 37:40you print the values, the order might
  1184. 37:42be jumbled like this. If the order is
  1185. 37:44not important for the list, you can
  1186. 37:45just use it as is, but if order is
  1187. 37:47critical, you should not convert it to
  1188. 37:48a set. In that case, there is another
  1189. 37:50method: using a dictionary. A
  1190. 37:52dictionary guarantees order while not
  1191. 37:54allowing duplicate keys. So, if you
  1192. 37:56have a list like this, entering `
  1193. 37:58dict.fromkeys` and placing ` mylist`
  1194. 38:00inside the parentheses creates a new
  1195. 38:01dictionary with keys containing "OS
  1196. 38:03Mongsil Choco" files, with duplicates
  1197. 38:05removed from the list. Note that the
  1198. 38:07values in this case are all null.
  1199. 38:08Furthermore, if you wrap this
  1200. 38:10dictionary in a list, you can extract
  1201. 38:11only the key values and convert them
  1202. 38:13back into a list. This way, the order
  1203. 38:14from when the list was initially
  1204. 38:16declared is guaranteed. In this way, we
  1205. 38:18have studied how to swap data types
  1206. 38:20with different characteristics. Up
  1207. 38:34until now, the code we have written has
  1208. 38:35always executed statements one by one
  1209. 38:37in order from top to bottom. Of course,
  1210. 38:39if we commented them out, those
  1211. 38:40statements were ignored. However, when
  1212. 38:42creating programs, it is not always
  1213. 38:44necessary for every statement to be
  1214. 38:45executed. You might want one piece of
  1215. 38:47code to run in one case and another in
  1216. 38:49another. That is where ` if` statements
  1217. 38:51come in handy. ` if` statements allow
  1218. 38:53you to branch based on a condition like
  1219. 38:55"if something is true." You can ensure
  1220. 38:57that a statement is executed only when
  1221. 38:59the condition is true. If the condition
  1222. 39:01is false, the ` if` statement will not
  1223. 39:03be executed. In code, you can write it
  1224. 39:06in this form: ` if`, write the
  1225. 39:07condition, and add a colon at the end.
  1226. 39:10Then, on the next line, write the
  1227. 39:11statement to be executed when the
  1228. 39:12condition is true. However, please note
  1229. 39:14that you must indent the line before
  1230. 39:16the condition. Indentation is usually
  1231. 39:18done by pressing the spacebar four
  1232. 39:19times or the Tab key once. In Visual
  1233. 39:21Studio Code, if you type a colon after
  1234. 39:23a condition and press Enter, it
  1235. 39:24automatically indents. I wrote the code
  1236. 39:26like this. If you were a student, you
  1237. 39:28would need to study, but what do you do
  1238. 39:29if it is Sunday? You would play a game
  1239. 39:31first and then start studying. So, if
  1240. 39:33the variable 'Today' is Sunday, the
  1241. 39:34condition is true, so you play a game
  1242. 39:36first and then start studying. However,
  1243. 39:39if today is not Sunday—if it is
  1244. 39:40Tuesday—the 'if' condition is false,
  1245. 39:42so you should start studying
  1246. 39:43immediately without playing a game.
  1247. 39:45That is, this statement part—yes,
  1248. 39:46right here. —is not executed, and the
  1249. 39:48program moves directly to the next
  1250. 39:49statement. There is also this form: if
  1251. 39:51the condition is true, this statement
  1252. 39:53is executed, and if the condition is
  1253. 39:55false, that statement is executed. You
  1254. 39:57choose one of the two based on the
  1255. 39:58condition. In code, you simply write '
  1256. 40:00if' below the conditional statement and
  1257. 40:02then a colon. For the statement below
  1258. 40:05that, you should also indent it before
  1259. 40:07writing it. By doing this, if the 'if'
  1260. 40:08condition is satisfied, this statement
  1261. 40:10is executed; otherwise, that statement
  1262. 40:11is executed, and the program moves on
  1263. 40:12to the next statement. For example, if
  1264. 40:14the code looks like this, if today is
  1265. 40:16Sunday, you will play a game and then
  1266. 40:18start studying. In this case, the ` ss`
  1267. 40:20part will not execute at all. If today
  1268. 40:22is not Sunday, the ` if` part will be
  1269. 40:23ignored, and the ` else` part will
  1270. 40:25execute, so you will use your
  1271. 40:26smartphone for exactly 5 minutes before
  1272. 40:27starting to study. Earlier, I explained
  1273. 40:30that ` if` means "if something happens.
  1274. 40:32" You can understand ` if else` like
  1275. 40:33this: "If something happens, the ` if`
  1276. 40:35statements execute," and "If it doesn't
  1277. 40:38happen, the ` if else` statements
  1278. 40:39execute." Let's review briefly. ` if`
  1279. 40:52means "if something happens," so the A
  1280. 40:53statements are executed. ` if s` means
  1281. 40:55"if something happens, if the four `
  1282. 40:57if` statements don't happen, then the s
  1283. 40:59statements execute." However, there is
  1284. 41:01one more thing here: ` l if`. ` l if`
  1285. 41:04is used to check another condition
  1286. 41:05again if the preceding condition is not
  1287. 41:08true. You can insert as many ` l if`
  1288. 41:09statements as you want between ` if`
  1289. 41:11and ` l`. If the initial ` if`
  1290. 41:12condition is false, the orange ` LF`
  1291. 41:14condition is checked; if the orange is
  1292. 41:15false, the green condition is checked;
  1293. 41:16if the green is false, the blue
  1294. 41:17condition is checked; and if the blue
  1295. 41:19condition is false, the ` L` condition
  1296. 41:20is executed. Note that while ` LF` is
  1297. 41:22optional, the ` L` condition does not
  1298. 41:23always have to be present when using
  1299. 41:25this part. Therefore, you can omit the
  1300. 41:27` L` condition if it is not needed. I
  1301. 41:28have set up a flow using ` if L if L`.
  1302. 41:30If the ` if` condition is true, this
  1303. 41:32statement is executed, followed by the
  1304. 41:34next statement; if it is false, the `
  1305. 41:36LF` condition is checked; if it is true
  1306. 41:37, that statement is executed, followed
  1307. 41:39by the next statement; and if even that
  1308. 41:41is false, it executes that statement
  1309. 41:43and moves on to the next statement.
  1310. 41:45Looking at the code, the first ` if`
  1311. 41:46contains the first condition. Below
  1312. 41:48that, you write ` L if flow` and write
  1313. 41:50the second condition in the same format
  1314. 41:51. After that, you can add the ` L`
  1315. 41:53condition to handle the action when
  1316. 41:54none of the conditions listed above are
  1317. 41:56satisfied. In other words, with the
  1318. 41:58code like this, if today is Sunday, it
  1319. 42:00works like this: you play just one game
  1320. 42:02, exit this entire section, and start
  1321. 42:03studying. If today is Saturday, the
  1322. 42:06first condition is not satisfied, so it
  1323. 42:07moves to the next condition. Since the
  1324. 42:09next condition is satisfied, the LFNA A
  1325. 42:11code is executed, and the program exits
  1326. 42:13. In other words, this allows you to
  1327. 42:14take out your phone for just 5 minutes
  1328. 42:16and start studying. If today is neither
  1329. 42:18Sunday nor Saturday, neither the if nor
  1330. 42:19the Lif conditions are satisfied, so
  1331. 42:21the ELS part is executed, meaning you
  1332. 42:23will have to drink a glass of water and
  1333. 42:24start studying immediately. Remember
  1334. 42:26that Lif can be added as many times as
  1335. 42:28you want between L and S. Now, let's
  1336. 42:41learn about nested if statements. Yes.
  1337. 42:42Nested statements, as the name suggests
  1338. 42:43, are cases where one statement is used
  1339. 42:45within another statement. As you can
  1340. 42:46see in the diagram, if the first if
  1341. 42:48statement is true, this statement is
  1342. 42:49executed first, and then the second if
  1343. 42:51statement highlighted in yellow is
  1344. 42:52checked. If the second if statement is
  1345. 42:54true, that statement is executed, but
  1346. 42:56if the second if statement is false,
  1347. 42:57that statement is not executed, and the
  1348. 42:59program moves directly to the next
  1349. 43:00statement. In code, you write this
  1350. 43:02statement after the line following the
  1351. 43:04if statement. The nested statement on
  1352. 43:07the next line should be written after
  1353. 43:09being indented by the same size as this
  1354. 43:11statement. Please note that the
  1355. 43:13statement to be executed when the
  1356. 43:15nested if is true must be indented by
  1357. 43:17this amount relative to the nested if.
  1358. 43:20When committing a foul during a soccer
  1359. 43:21match, depending on the situation, a
  1360. 43:23warning may be given, but if the
  1361. 43:24offense is severe, a yellow or red card
  1362. 43:26may be issued. Receiving a red card
  1363. 43:28results in immediate expulsion, but
  1364. 43:29receiving two yellow cards results in
  1365. 43:30expulsion due to accumulated warnings,
  1366. 43:32just like a red card. Let's try to find
  1367. 43:34a method to handle a player differently
  1368. 43:35when they receive a yellow card,
  1369. 43:37depending on the number of yellow cards
  1370. 43:38accumulated so far. First, there is a
  1371. 43:40player. This player's yellow card count
  1372. 43:42is initially 0, since they have never
  1373. 43:43received a warning. However, they
  1374. 43:44committed a foul while playing the game
  1375. 43:46. It says "Troll" here, right? Yes. For
  1376. 43:48convenience, let's call it a foul
  1377. 43:49equivalent to a yellow card. On the
  1378. 43:51next line, we check for foul status
  1379. 43:52through this part. If it is confirmed
  1380. 43:54to be a foul, you would receive one
  1381. 43:55yellow card. If we represent the
  1382. 43:56current situation graphically, it looks
  1383. 43:58like this. Now, let's add code to check
  1384. 44:00the total number of yellow cards; if
  1385. 44:01there is one, the game continues, and
  1386. 44:03if there are two, the player is ejected
  1387. 44:04. Nesting is required here. After
  1388. 44:07writing the same amount of text, I will
  1389. 44:09write another statement. If it is
  1390. 44:11confirmed that there are two yellow
  1391. 44:13cards, I will indent the next line
  1392. 44:14again and print the phrase "Ejected due
  1393. 44:16to accumulated warnings." Visually, it
  1394. 44:18would look like this. If there are two
  1395. 44:20yellow cards, it prints "Ejected due to
  1396. 44:22accumulated warnings" when the
  1397. 44:23condition is true. If this is the first
  1398. 44:25time receiving a yellow card, it is not
  1399. 44:26an ejection, but the player should be
  1400. 44:27careful going forward. So, I write "
  1401. 44:29Else" and handle the case where there
  1402. 44:30are not two yellow cards like this.
  1403. 44:32Visually, it would look like this.
  1404. 44:33However, if it wasn't a foul—yes, if
  1405. 44:35the foul variable was "Foul"—the code
  1406. 44:37inside here would not execute at all.
  1407. 44:39In other words, if it is a foul that
  1408. 44:41doesn't warrant a yellow card but
  1409. 44:42merely gives a warning, I will use an
  1410. 44:44Else statement at the same position as
  1411. 44:46the first If statement to simply print
  1412. 44:47"Yes, warning." It looks like this in
  1413. 44:49the diagram. Yes. If it is not a foul,
  1414. 44:51it just gives a warning. After that,
  1415. 44:53the subsequent statements outside the
  1416. 44:55second If statement will be executed.
  1417. 44:57In this way, we have learned about
  1418. 44:58nested If statements, which allow us to
  1419. 45:00check another condition when a certain
  1420. 45:01condition is met. Imagine a teacher
  1421. 45:14having the students do floats to warm
  1422. 45:15up during after-school class. However,
  1423. 45:17the teacher doesn't keep saying "Float
  1424. 45:20floats, fly floats, fly floats" every
  1425. 45:22time they do them. Usually, the teacher
  1426. 45:24just says, "Start with 10 floats." Then
  1427. 45:26, the students repeat the same float
  1428. 45:28motion ten times. Programs work the
  1429. 45:30same way. Let's try printing a
  1430. 45:32statement using a Print statement. To
  1431. 45:34print the same statement ten times, you
  1432. 45:35would simply copy and paste it ten
  1433. 45:36times, right? However, if you want to
  1434. 45:38print 1,000 times instead of ten, it
  1435. 45:40seems that a simple loop won't suffice.
  1436. 45:41This is where the ` print` statement
  1437. 45:43comes in. There are various types of
  1438. 45:45loops, but let's learn the most basic
  1439. 45:46one first. The syntax is as follows:
  1440. 45:48You write the loop variable (the range
  1441. 45:50or target), add a colon, indent the
  1442. 45:52next line, and then write the
  1443. 45:54statements to be executed repeatedly.
  1444. 45:56Let's look at an example. Yes. We use
  1445. 45:58the range function 4x, input 10, and
  1446. 45:59then write a colon. On the next line,
  1447. 46:02we write ` print 8 times`. Here, range
  1448. 46:0410 is used to mean repeating a total of
  1449. 46:06ten times, so this statement is 100%
  1450. 46:08identical to the statement on the right
  1451. 46:10. We have produced the same output
  1452. 46:12result in just two lines using the `
  1453. 46:13print` statement. Let's modify the
  1454. 46:14statement slightly. Here, I will use
  1455. 46:16the part I wrote as 'x' inside the `
  1456. 46:18print` statement. The value of 'x'
  1457. 46:19changes every time the loop runs
  1458. 46:21through range 10. If you run the code,
  1459. 46:23you can see that the value of x changes
  1460. 46:24from 0 to 9. A range serves the
  1461. 46:26function of generating numbers within a
  1462. 46:28specific range. So, if you use ` range
  1463. 46:3010`, you can consider it to generate
  1464. 46:32numbers from 0 to 9 (inclusive of 0 and
  1465. 46:34exclusive of 10). If you use ` range
  1466. 46:3610` (4x), you can understand it as
  1467. 46:38assigning the numbers from 0 to 9 to x
  1468. 46:40one by one in order. For reference, X
  1469. 46:42is just a name I gave it; you can
  1470. 46:44freely use any name you want, just like
  1471. 46:46a regular variable. We used ranges when
  1472. 46:59learning about loops. I mentioned that
  1473. 47:01writing ` range 10` generates numbers
  1474. 47:03from 0 to 10 (inclusive of 10). Here,
  1475. 47:05the value 10 represents the stop. In
  1476. 47:07other words, it generates numbers from
  1477. 47:090 to less than the stop. If you write
  1478. 47:11it like this in a loop and run it, it
  1479. 47:12will output numbers from 0 to 9.
  1480. 47:14However, unlike computers, in our daily
  1481. 47:15lives, we start counting from 1. In
  1482. 47:17such cases, you use the range function
  1483. 47:18slightly differently. By entering two
  1484. 47:20values separated by a comma, the first
  1485. 47:22value becomes the starting value and
  1486. 47:24the second becomes the ending value,
  1487. 47:25allowing you to generate numbers from '
  1488. 47:27start' up to 'stop'(exclusive). For
  1489. 47:29example, if you input 1 and 10 into the
  1490. 47:31range function sequentially, it creates
  1491. 47:32numbers from 1 (inclusive) to '10'(
  1492. 47:34exclusive) starting from 1 instead of
  1493. 47:350. However, there is actually one more
  1494. 47:37value that can be included when using
  1495. 47:38range: 'step'. Specifying a step
  1496. 47:40increment increases the interval
  1497. 47:41between numbers by that amount. There
  1498. 47:45are times when we need numbers with
  1499. 47:46regular intervals, such as odd, even,
  1500. 47:48or multiples of 3. If you use range 1
  1501. 47:5010 2, it creates numbers from 1 up to '
  1502. 47:5210'(exclusive), meaning that each digit
  1503. 47:55increases by that amount. This allows
  1504. 47:58you to generate odd numbers like 1 3 5
  1505. 48:007 9. If you change the start value to 2
  1506. 48:02, you can create numbers consisting of
  1507. 48:04even numbers, such as 2, 4, 6, and 8,
  1508. 48:06starting from 1 and increasing by that
  1509. 48:08amount. If you use the range 3, 10, and
  1510. 48:113, you can create numbers such as 3, 6,
  1511. 48:13and 9, which are multiples of 3,
  1512. 48:15starting from 3 and increasing numbers
  1513. 48:21less than 10 by 3. The for loop can be
  1514. 48:24used in this form, iterating through a
  1515. 48:28loop range or target to repeatedly
  1516. 48:31execute the statements within the loop.
  1517. 48:36Previously, we specified the loop range
  1518. 48:37using the range, but this time, let's
  1519. 48:39utilize the loop by using the loop
  1520. 48:41target. Lists, tuples, and dictionaries
  1521. 48:42, which we learned about when studying
  1522. 48:44data types, can be used for the loop
  1523. 48:45target. If you have a list called '
  1524. 48:48myList', you can use the loop target
  1525. 48:49like this: 4 x myList colon. Now, the
  1526. 48:52values 1, 2, and 3 from 'myList' are
  1527. 48:54taken out one by one in order and put
  1528. 48:56into x. If you perform the calculation,
  1529. 48:59the result is 1 2 3. Tuples can be used
  1530. 49:01just like lists, and the result of the
  1531. 49:02example is the same as 'My List'.
  1532. 49:04Dictionaries are a bit different. When
  1533. 49:06a dictionary named 'Person' is declared
  1534. 49:08like this, since dictionaries consist
  1535. 49:10of key-value pairs, if you iterate over
  1536. 49:12'PersonValues', the parts corresponding
  1537. 49:14to the values in the dictionary 'Person
  1538. 49:16' will be printed. If you input 'Key'
  1539. 49:18instead of 'Values', the parts
  1540. 49:19corresponding to the keys will be
  1541. 49:20printed. Alternatively, if you use '
  1542. 49:22Items', the key and value are returned
  1543. 49:24together, allowing you to print
  1544. 49:26everything. Please note that in this
  1545. 49:28case, to receive both 'q' and the
  1546. 49:30values, the variable part after '4d'
  1547. 49:32contains 'k' for the 'q' and 'v' for
  1548. 49:34the values, separated by a comma. In
  1549. 49:36addition to these data types, general
  1550. 49:37strings can also be used as targets for
  1551. 49:39loops. If you declare the string 'Apple
  1552. 49:41' in a variable named 'Pro' and run it,
  1553. 49:43you can see that the characters within
  1554. 49:45the string are printed one by one.
  1555. 49:58Python loops also include something
  1556. 49:59called ` while`. By definition, it
  1557. 50:01means "while doing something." However,
  1558. 50:03` while` is slightly different in
  1559. 50:04nature. While ` while` iterates through
  1560. 50:06a defined range or data, ` while`
  1561. 50:08continues to repeat as long as the
  1562. 50:10condition is true. For example, let's
  1563. 50:11say you have decided to travel by plane
  1564. 50:13. You would need to pack your luggage.
  1565. 50:15Looking at your flight ticket, it says
  1566. 50:17that checked baggage is allowed up to a
  1567. 50:18maximum of 25kg. In this case, we
  1568. 50:20usually check the weight while packing
  1569. 50:22at home. We would pack items up to the
  1570. 50:24limit of 25kg and then lock the
  1571. 50:25suitcase. For convenience, let's assume
  1572. 50:28your total luggage weighs 3kg. Then,
  1573. 50:30you could fit up to eight pieces of
  1574. 50:32luggage within the 25kg limit. Let's
  1575. 50:34implement this in code. First, to use `
  1576. 50:37Y`, enter the condition after ` Y` and
  1577. 50:39write a colon. Below that, indent the
  1578. 50:41text and write the statement to be
  1579. 50:43executed repeatedly. Let's declare
  1580. 50:44three variables first. I set 'Max',
  1581. 50:46which represents the maximum allowable
  1582. 50:47weight, to 25. For 'Weight', which
  1583. 50:48represents the current carrier weight,
  1584. 50:49I will start with 0. I will also fix
  1585. 50:50the weight of each item in the 'Item'
  1586. 50:52variable to 3. Next, I will use a while
  1587. 50:56loop. You simply need to put a
  1588. 50:58condition after 'while'; here, I will
  1589. 51:00check if the sum of the current carrier
  1590. 51:01weight ('Weight') and the weight of the
  1591. 51:03items is less than or equal to the
  1592. 51:04maximum allowable weight. By writing it
  1593. 51:06this way, I am checking whether it is
  1594. 51:07safe to add more items to the carrier.
  1595. 51:09If this condition is true, I will add
  1596. 51:10the items to 'Weight' and then print
  1597. 51:12that items have been added using a
  1598. 51:14print statement. Then, the while loop
  1599. 51:15will continue to execute until the
  1600. 51:17moment it can no longer add items—
  1601. 51:19that is, when the condition becomes
  1602. 51:20false—the loop will exit. Finally, I
  1603. 51:22will print the total weight and
  1604. 51:24terminate the program. Running the code
  1605. 51:26, I can see that a maximum of eight
  1606. 51:28items can be added, and the total
  1607. 51:29weight at that point is 24kg. When you
  1608. 51:42go to the gym, people usually do cardio
  1609. 51:44exercises like using a treadmill or
  1610. 51:45stationary bike first, followed by
  1611. 51:46strength training. I find that I start
  1612. 51:47sweating after using the treadmill for
  1613. 51:49about 20 minutes. However, treadmills
  1614. 51:50have a red emergency stop button. You
  1615. 51:53can press this button if you need to
  1616. 51:54stop the machine suddenly due to an
  1617. 51:56emergency, even if 20 minutes haven't
  1618. 51:58passed yet. Similarly, loops have
  1619. 51:59something like this emergency stop
  1620. 52:00button. It is called a "Break." A Break
  1621. 52:02serves to immediately halt the action
  1622. 52:04currently being executed within a loop
  1623. 52:06and exit the loop. It is usually used
  1624. 52:08in conjunction with an "If" conditional
  1625. 52:09statement. For example, let's assume
  1626. 52:10there is a drama series with seasons 1
  1627. 52:12through 5. Since this drama is in a
  1628. 52:14list format, you can use a loop to
  1629. 52:15watch it in order starting from season
  1630. 52:171. However, a friend who watched the
  1631. 52:19drama first says that seasons 1 and 2
  1632. 52:20were really fun, but seasons 3 onwards
  1633. 52:22are too boring and tells you not to
  1634. 52:24watch them. In this case, you can use
  1635. 52:26an "If" conditional statement and a
  1636. 52:27Break to ensure that if the current
  1637. 52:29season is 3, the loop exits. In other
  1638. 52:31words, the meaning of these statements
  1639. 52:33is: if a certain condition is true,
  1640. 52:35execute the print statement, and then
  1641. 52:36execute the Break command to
  1642. 52:38immediately exit the current loop. If
  1643. 52:40you use a loop, the items within the
  1644. 52:41list are iterated through in order. So,
  1645. 52:43if it is not yet Season 3—that is, if
  1646. 52:45it is Season 1 or 2—I will add a
  1647. 52:46print statement to ensure it is watched
  1648. 52:48. If you run the code, you can see that
  1649. 52:50it only watches Seasons 1 and 2, and
  1650. 52:52does not watch anything afterward. A `
  1651. 52:55break` is used in this way to
  1652. 52:57immediately exit a loop when a certain
  1653. 52:59condition is true. Do you remember the
  1654. 53:12drama with seasons 1 through 5? A
  1655. 53:14friend who watched the drama first told
  1656. 53:16you not to watch from Season 3 onwards
  1657. 53:17because it wasn't interesting, so you
  1658. 53:19only watched up to Season 2. However,
  1659. 53:20you later found out that what your
  1660. 53:21friend meant was not that all seasons
  1661. 53:23from 3 onwards were boring, but that
  1662. 53:24only Season 3 was boring. In other
  1663. 53:26words, they were telling you to skip
  1664. 53:28Season 3 and continue watching Seasons
  1665. 53:294 and 5. Fortunately, they say that the
  1666. 53:31storyline is not a major issue even if
  1667. 53:32you don't watch Season 3. In Python
  1668. 53:33visits, ` continue` is what you can use
  1669. 53:35when you want to skip an action in
  1670. 53:36certain cases. Just like with the `
  1671. 53:38break`, there is a drama like this. We
  1672. 53:40will use a loop to watch them one by
  1673. 53:41one, but this is where it changes. The
  1674. 53:43` if` condition is the same since it
  1675. 53:45checks if it is Season 3, but since
  1676. 53:46Season 3 isn't interesting, we need to
  1677. 53:48skip it and add ` Continue` to continue
  1678. 53:49watching from Season 4. In other words,
  1679. 53:53even though there is a print statement
  1680. 53:55like "Watch [something]," if `
  1681. 53:56Continue` is encountered, it
  1682. 53:58immediately moves to the next iteration
  1683. 53:59. Therefore, the print statement below
  1684. 54:02is not executed at all when it is
  1685. 54:03Season 3, and it jumps to the next
  1686. 54:05iteration element, Season 4. If you run
  1687. 54:07the code, you can see that it watches
  1688. 54:08Seasons 1 and 2, skips Season 3, and
  1689. 54:10then watches Seasons 4 and 5—yes, all
  1690. 54:12the way to the end. When we write, if a
  1691. 54:25new paragraph begins, we leave a space
  1692. 54:28at the beginning of the first sentence;
  1693. 54:30we call this indentation. Unlike other
  1694. 54:32programming languages, indentation
  1695. 54:34plays a very significant role in Python
  1696. 54:36. We have already used indentation when
  1697. 54:37learning about ` if` statements, ` if`
  1698. 54:39statements, and ` if` loops. When you
  1699. 54:41indent, you can consider code lines
  1700. 54:43that are indented by the same amount as
  1701. 54:45a single paragraph. In other words,
  1702. 54:47these three indented statements are all
  1703. 54:49executed together when the subcondition
  1704. 54:50is true. If you remove the indentation
  1705. 54:52before a statement, it will no longer
  1706. 54:54be part of the same paragraph as the
  1707. 54:56statements above. The execution
  1708. 54:57behavior will also change completely.
  1709. 55:00For conditional statements, loops, and
  1710. 55:01the functions, exception handling, and
  1711. 55:03classes we will learn about later, the
  1712. 55:05line following a section ending with a
  1713. 55:06colon must be indented. If you need to
  1714. 55:09separate a paragraph while writing
  1715. 55:11indented statements, such as with
  1716. 55:12nested statements, you must indent by
  1717. 55:14the same amount one more time. Although
  1718. 55:17there is no specific number of spaces
  1719. 55:18required for indentation, Python
  1720. 55:20generally applies four spaces. Since it
  1721. 55:23involves the characters 'Siro', a
  1722. 55:31company decided to recall all of its
  1723. 55:34products because of a problem with
  1724. 55:37certain items. This company makes two
  1725. 55:40types of products, and let's assume
  1726. 55:42that the model names of the problematic
  1727. 55:44products start with 'Siro'. If we
  1728. 55:46organize the names of all products
  1729. 55:47released so far into a list, it looks
  1730. 55:49like this. From this list, let's create
  1731. 55:50a new list by extracting only the
  1732. 55:52products starting with 'Siro' that are
  1733. 55:53subject to recall. First, we create an
  1734. 55:55empty list called 'Recall' to manage
  1735. 55:57the list of recalled products. We then
  1736. 56:00iterate through the 'Product' list
  1737. 56:02using the method, and if a conditional
  1738. 56:04statement checks if the product name
  1739. 56:05starts with 'Siro', we add it to the
  1740. 56:07recall list. After doing this, if we
  1741. 56:10check the result using a print
  1742. 56:11statement, we can see a list consisting
  1743. 56:12of products that start with 'Siro'.
  1744. 56:14However, there is a way to solve this
  1745. 56:16part in just one line. It is called
  1746. 56:19list compression. It can be used to
  1747. 56:21extract only data that meets a specific
  1748. 56:23condition from a list or to create a
  1749. 56:24new list by changing values. The
  1750. 56:26general syntax is like this, but it
  1751. 56:27looks a bit complicated, doesn't it?
  1752. 56:28Let me explain with an example. We have
  1753. 56:30a list called 'My List'. It contains
  1754. 56:32the values 1, 2, 3, 4, and 5. Now, we
  1755. 56:33want to use list compression to extract
  1756. 56:35only the numbers greater than 3 and
  1757. 56:37create a new list. To do this, we first
  1758. 56:39need to iterate through the My List and
  1759. 56:40check if a value is greater than 3. To
  1760. 56:42do this, just like we did in the 4th
  1761. 56:44section, we first write the iteration
  1762. 56:45target, or My List, which is 4x. The
  1763. 56:48List values will be entered into x in
  1764. 56:50order, but we can change x arbitrarily.
  1765. 56:52You just need to write an expression
  1766. 56:54using x right in front of 4 like this.
  1767. 56:55If you simply write x, it means you are
  1768. 56:57using the list values as they are
  1769. 56:59without modification, but if you do x +
  1770. 57:001, it adds 1 to each list value, and if
  1771. 57:02you do x*3, it multiplies each list
  1772. 57:04value by 3. Also, if you convert x into
  1773. 57:05a character set and append the word 'th
  1774. 57:07', a new list is created with the
  1775. 57:08values converted into character sets,
  1776. 57:10such as '1st', '2nd', and so on.
  1777. 57:12Finally, there is an if conditional
  1778. 57:13statement at the very end. Doing this
  1779. 57:14means that when creating a new list,
  1780. 57:15the value should only be used if it is
  1781. 57:16greater than 3. In other words, when we
  1782. 57:17utilized x in various ways earlier, the
  1783. 57:18result appeared as shown in light green
  1784. 57:19; this is actually the result when
  1785. 57:24there is no if condition. However, with
  1786. 57:26an if condition, new list elements are
  1787. 57:27used only when x is greater than 3—
  1788. 57:29that is, for values 4 and 5. Therefore,
  1789. 57:30when utilizing x in various ways, the
  1790. 57:31actual value becomes like this.
  1791. 57:36Breaking this code down into sentences,
  1792. 57:39it means to create a vertical list by
  1793. 57:41using only the values greater than 3
  1794. 57:43from the list. Let's return to the
  1795. 57:45example explained earlier. If we use
  1796. 57:47list compression on the code written
  1797. 57:49using the body and conditions of Method
  1798. 57:514, it becomes like this. If we
  1799. 57:53interpret the code, it creates a
  1800. 57:55vertical list by taking only the
  1801. 57:56product names from the "Product" list
  1802. 57:59if they start with "Silho." If you
  1803. 58:01print the list created in this way, you
  1804. 58:03can confirm that the exact same result
  1805. 58:05is output as follows. In addition to
  1806. 58:07what I have explained, list compression
  1807. 58:08can be applied in various ways like
  1808. 58:10this. You are the owner of a hair salon
  1809. 58:23. This salon only offers haircuts, and
  1810. 58:25the price is fixed at 10,000 won. To
  1811. 58:27reduce labor costs, we use a payment
  1812. 58:28kiosk, and you will write the program
  1813. 58:30for it yourself. The first customer has
  1814. 58:32arrived. We are writing down the code
  1815. 58:33in advance so that we can provide price
  1816. 58:35information when this customer pays
  1817. 58:36later. We will store the name in a
  1818. 58:37variable called "Customer One" and
  1819. 58:39print the price. A little later, a
  1820. 58:41second customer has arrived. We will
  1821. 58:43create a new variable called "Customer
  1822. 58:442," store the customer's name, and
  1823. 58:45write the same code to print the price.
  1824. 58:47Shortly after, a third customer arrives
  1825. 58:49. We just need to write the same code
  1826. 58:50as for the previous customers. However,
  1827. 58:52while cutting hair, Kang-eon realizes
  1828. 58:53that it will be difficult to run the
  1829. 58:55salon for 10,000 won. So, he decides to
  1830. 58:56raise the haircut price to 15,000 won.
  1831. 58:58Consequently, he will need to modify
  1832. 59:00the program he has created so far. He
  1833. 59:01asks the customers for their
  1834. 59:02understanding for a moment, finds all
  1835. 59:03the sections where prices are listed,
  1836. 59:04and edits them one by one. However, I
  1837. 59:06was worried that simply raising prices
  1838. 59:07might burden customers, so I decided to
  1839. 59:09give the haircuts special names. I then
  1840. 59:10went back to the code to find all the
  1841. 59:12sections related to haircuts and
  1842. 59:13modified them one by one. But having to
  1843. 59:14manually change the same code scattered
  1844. 59:16across various places every time a
  1845. 59:18change occurs feels incredibly
  1846. 59:19cumbersome. This is where functions
  1847. 59:21come in handy. A function is a group of
  1848. 59:23code that performs a specific action.
  1849. 59:24It is especially convenient to
  1850. 59:25consolidate code that is used
  1851. 59:26repeatedly in multiple places into a
  1852. 59:28single function. You define a function
  1853. 59:29like this: write "define"(shortened to
  1854. 59:31"def"), leave a space, write the
  1855. 59:32function name, open and close
  1856. 59:33parentheses, and then write a colon.
  1857. 59:35From the next line onwards, you indent
  1858. 59:37the code and write the statements to be
  1859. 59:39executed. If you were to create a
  1860. 59:40function for the price guide code in a
  1861. 59:41hair salon, you could do it like this.
  1862. 59:42This function is named "Shop Ice," and
  1863. 59:44when the function is called, the
  1864. 59:46printouts defined here will be executed
  1865. 59:48. I used the term "call" because a
  1866. 59:50function does nothing when it is simply
  1867. 59:52defined. To execute the statements
  1868. 59:54within the function, a "call" is
  1869. 59:55required. By writing the function name
  1870. 59:57and parentheses somewhere in the code
  1871. 59:58like this, you can call the function.
  1872. 1:00:00Only when this statement is executed do
  1873. 1:00:02the corresponding statements within the
  1874. 1:00:03function begin to run. The hair salon
  1875. 1:00:05program I initially wrote was in this
  1876. 1:00:06form. To save space, I will only leave
  1877. 1:00:08the code for two customers. Applying
  1878. 1:00:10the functions in this code results in
  1879. 1:00:11this. We create a function called "Show
  1880. 1:00:13Price" to display prices, and other
  1881. 1:00:14places call and use this function.
  1882. 1:00:16Furthermore, if you need to modify the
  1883. 1:00:17code in this state, you don't need to
  1884. 1:00:19go around looking for specific parts to
  1885. 1:00:20edit; you just need to modify the
  1886. 1:00:21section inside the function exactly
  1887. 1:00:22once, and that's it. It's difficult,
  1888. 1:00:24right? Yes. Yes. When using a function,
  1889. 1:00:26the execution order of the code is as
  1890. 1:00:27follows. First, it arrives sequentially
  1891. 1:00:28from top to bottom. When the "Show
  1892. 1:00:29Price" function is called, it briefly
  1893. 1:00:31jumps to the function, and the code
  1894. 1:00:32inside it is executed in order. Once
  1895. 1:00:34the function finishes executing, it
  1896. 1:00:35returns to the original code, and the
  1897. 1:00:36code for the second customer is
  1898. 1:00:37executed sequentially. When it
  1899. 1:00:39encounters the function call section,
  1900. 1:00:40the code within the new function is
  1901. 1:00:41executed, after which it returns to the
  1902. 1:00:43original code to continue. This is how
  1903. 1:00:56we created functions earlier. And this
  1904. 1:00:57is how the call was made. Since the
  1905. 1:00:59part that displays haircut prices was
  1906. 1:01:00created as a function, if modifications
  1907. 1:01:01are needed, you only need to edit the
  1908. 1:01:02inside of the function. However,
  1909. 1:01:03looking at the code again, the part
  1910. 1:01:05that prints "Customer [Name]" is
  1911. 1:01:06similar but is still being used in
  1912. 1:01:08multiple places. If you want to add the
  1913. 1:01:10word "beloved" before "Customer [Name],
  1914. 1:01:11" there is the inconvenience of having
  1915. 1:01:13to go around and modify it in several
  1916. 1:01:14places. Let's try to handle this
  1917. 1:01:16together within the function. We
  1918. 1:01:17learned that functions are written like
  1919. 1:01:19this. The parentheses after the
  1920. 1:01:20function name can be left empty like
  1921. 1:01:21this, but if necessary, you can pass a
  1922. 1:01:23value inside them. This is called a
  1923. 1:01:25passed value or a parameter. In the
  1924. 1:01:27hair salon program, when printing "
  1925. 1:01:28Customer [Name]," variable values like
  1926. 1:01:30"Customer" and "Customer 2" are used
  1927. 1:01:32directly, but let's handle this part
  1928. 1:01:33inside the function. As the first step,
  1929. 1:01:35add a print statement within the
  1930. 1:01:36function definition. Here, for the
  1931. 1:01:38variable holding the customer's name, I
  1932. 1:01:39will only write "Customer." As the
  1933. 1:01:41second step, write "Customer" inside
  1934. 1:01:42the parentheses after the function name
  1935. 1:01:44. "Customer" is the value passed from
  1936. 1:01:45the place calling the ` Show Price`
  1937. 1:01:47function, and it allows you to use a
  1938. 1:01:48variable named "Customer" within the
  1939. 1:01:49function. Actually, you can use any
  1940. 1:01:51name you want here, just like with "
  1941. 1:01:53Name." As the third step, since the
  1942. 1:01:55part in the original code that printed
  1943. 1:01:57"Customer [Name]" is no longer needed,
  1944. 1:01:59delete it like this. As the final step,
  1945. 1:02:00to pass the customer name when calling
  1946. 1:02:02the function, simply write "Customer"
  1947. 1:02:03and "Customer 2" inside the parentheses
  1948. 1:02:05of ` Show Price`, and you're done. Once
  1949. 1:02:07you've done this, if you want to add
  1950. 1:02:08the text "Sarang Haneun" before the
  1951. 1:02:10customer name, you just need to change
  1952. 1:02:11it once inside the function. You can
  1953. 1:02:13pass as many values as you want to a
  1954. 1:02:15function. One important feature is that
  1955. 1:02:17passed values can only be used inside
  1956. 1:02:19the function. Please remember that you
  1957. 1:02:21cannot use the passed values outside
  1958. 1:02:22the function. When you pass a value
  1959. 1:02:35when calling a function, you can use
  1960. 1:02:37that value within the function.
  1961. 1:02:39Functions also have "return values."
  1962. 1:02:41After performing an action or operation
  1963. 1:02:42within a function, you can return the
  1964. 1:02:44result to the place that called the
  1965. 1:02:45function. You simply write the keyword
  1966. 1:02:47'return', leave a space, and then write
  1967. 1:02:49the value you want to return. This
  1968. 1:02:50allows the place calling the function
  1969. 1:02:52to receive and use this value. Let's
  1970. 1:02:54modify the hair salon program slightly
  1971. 1:02:56to create a function that returns price
  1972. 1:02:58information. I will name the function '
  1973. 1:03:00GetPrice' and write 'Return 15,000'
  1974. 1:03:01directly without any other statements
  1975. 1:03:03to execute. Then, the place calling
  1976. 1:03:04this function needs to receive the
  1977. 1:03:06value 15,000. Below, I will set the
  1978. 1:03:09variable 'Price' to 'GetPrice'. Since
  1979. 1:03:12the value 15,000 returned via 'Return'
  1980. 1:03:13will be stored in the 'Price' variable,
  1981. 1:03:15this statement will perform the exact
  1982. 1:03:17same action as the statement written
  1983. 1:03:18below. And you can output the value
  1984. 1:03:20like this using a print statement.
  1985. 1:03:22However, while running the hair salon,
  1986. 1:03:24I encountered a concern. It is
  1987. 1:03:25regarding regular customers. Since they
  1988. 1:03:27visit the salon so often, I feel like I
  1989. 1:03:29should offer them a discount for some
  1990. 1:03:30reason. So, I’m going to change the
  1991. 1:03:32pricing policy so that regular
  1992. 1:03:33customers are charged only 10,000 won
  1993. 1:03:34for a haircut, while regular customers
  1994. 1:03:36are charged 15,000 won. First, to check
  1995. 1:03:37for regular status, I will define a
  1996. 1:03:39parameter called 'Easy VIP'. This
  1997. 1:03:40parameter is in a variable form,
  1998. 1:03:42meaning 'True' indicates a regular
  1999. 1:03:43customer and 'False' indicates a
  2000. 1:03:44regular customer. Using this
  2001. 1:03:46conditional variable, I will return
  2002. 1:03:4810,000 won if 'Easy VIP' is true, and
  2003. 1:03:5115,000 won otherwise. The price will
  2004. 1:03:53always be one of these two values. Also
  2005. 1:03:55, to pass the regular status when
  2006. 1:03:56calling this function, I write 'True'
  2007. 1:03:57or 'False' inside the parentheses. If
  2008. 1:03:59it is true, it means the customer is a
  2009. 1:04:00regular customer and receives 10,000
  2010. 1:04:01won; if it is false, it means the
  2011. 1:04:04customer is a regular customer and
  2012. 1:04:05receives 15,000 won, which is then
  2013. 1:04:06stored in the 'Price' variable.
  2014. 1:04:07Functions usually return a single value
  2015. 1:04:09, but if necessary, you can return
  2016. 1:04:10multiple values in a voting format.
  2017. 1:04:12When a value is returned using the '
  2018. 1:04:14return' keyword, the function exits
  2019. 1:04:15immediately. In other words, even if
  2020. 1:04:17there are other statements under the
  2021. 1:04:18return statement, they will not be
  2022. 1:04:19executed. Let's say you decided to
  2023. 1:04:32charge 15,000 won for a haircut to
  2024. 1:04:33regular customers and 10,000 won to
  2025. 1:04:35regular customers at the hair salon you
  2026. 1:04:36run. Usually, there are far more
  2027. 1:04:38regular customers than regular
  2028. 1:04:39customers. Looking at the function we
  2029. 1:04:40previously created to return price
  2030. 1:04:42information, it can be cumbersome to
  2031. 1:04:43always pass a return value to find the
  2032. 1:04:45price for regular customers, who are
  2033. 1:04:46relatively more numerous compared to
  2034. 1:04:48regular customers. In such cases, using
  2035. 1:04:50a default value can make using the
  2036. 1:04:52function very convenient. A default
  2037. 1:04:53value is a value used as the standard
  2038. 1:04:55for a monthly value. For example, there
  2039. 1:04:57is a very famous Jajangmyeon restaurant
  2040. 1:04:58. Although they sell other menu items
  2041. 1:05:01like Jjamppong or Udon, most customers
  2042. 1:05:03order Jajangmyeon. At this restaurant,
  2043. 1:05:04if you order "Two servings of
  2044. 1:05:06Jajangmyeon, please," the waiter simply
  2045. 1:05:08tells the kitchen "Two servings."
  2046. 1:05:09Although it wasn't explicitly stated
  2047. 1:05:11what "two" refers to, the kitchen
  2048. 1:05:12implicitly ends up making two servings
  2049. 1:05:14of the best-selling Jajangmyeon. In
  2050. 1:05:16other words, the default menu item at
  2051. 1:05:17this Jajangmyeon restaurant is
  2052. 1:05:18Jajangmyeon. In functions, you simply
  2053. 1:05:20attach an equal sign after the value
  2054. 1:05:22passed and set the value you want as
  2055. 1:05:24the default. Since customers visiting a
  2056. 1:05:25hair salon are often regular customers,
  2057. 1:05:27if you set the Easy VIP value to false
  2058. 1:05:28as the default like this, the place
  2059. 1:05:29calling the function doesn't need to
  2060. 1:05:30set false every time for a regular
  2061. 1:05:31customer; simply calling the function
  2062. 1:05:36with the parentheses empty will
  2063. 1:05:38automatically set the Easy VIP value to
  2064. 1:05:39false. Now, you only need to pass the
  2065. 1:05:41value to regular customers. When
  2066. 1:05:57creating functions, there are often
  2067. 1:05:58cases where multiple values are
  2068. 1:05:59required. For example, a price function
  2069. 1:06:01used to obtain price information from a
  2070. 1:06:03hair salon might check for regular
  2071. 1:06:04customers to offer discounts, or accept
  2072. 1:06:06discounts for birthdays or mobile
  2073. 1:06:07carrier memberships. Additionally,
  2074. 1:06:09prices can vary depending on various
  2075. 1:06:10conditions such as card discounts,
  2076. 1:06:11review discounts, and new visit
  2077. 1:06:13discounts. However, if there are so
  2078. 1:06:14many different pass values, you would
  2079. 1:06:16have to check them all every time you
  2080. 1:06:18call the function, which would make
  2081. 1:06:19using the function quite difficult. How
  2082. 1:06:21can we use the function a little more
  2083. 1:06:23conveniently? That’s right. If you
  2084. 1:06:24set default values, you won’t need to
  2085. 1:06:26set the pass values individually. In
  2086. 1:06:28other words, you set it so that for all
  2087. 1:06:30cases, it is initially set to "not
  2088. 1:06:31applicable." Then, if a situation
  2089. 1:06:33arises—for example, if a customer
  2090. 1:06:34mentions leaving a review—we can then
  2091. 1:06:36set the "Review" pass value using the
  2092. 1:06:38tool. But what if the code were written
  2093. 1:06:41like this? What we need right now is to
  2094. 1:06:44set only the "Review" pass value to
  2095. 1:06:45True and leave everything else at the
  2096. 1:06:46default value of False. In this case,
  2097. 1:06:48you can use the keyword value. It
  2098. 1:06:50precisely defines the target for the
  2099. 1:06:51pass value. If the code is like this
  2100. 1:06:53and you want to set only the "Review"
  2101. 1:06:55pass value to True and leave the rest
  2102. 1:06:56as is, you can do it exactly like this.
  2103. 1:06:58When using the function, you simply
  2104. 1:06:59write the target "Review" and the pass
  2105. 1:07:01value "True" inside the parentheses
  2106. 1:07:02like this. If you want to set the '
  2107. 1:07:04Birthday' value to True because it
  2108. 1:07:05happens to be a birthday and you are
  2109. 1:07:07offering a review discount, you can add
  2110. 1:07:09keyword values separated by commas like
  2111. 1:07:10this. Please note that the order of the
  2112. 1:07:12keyword values does not matter as it
  2113. 1:07:14differs from what is defined in the
  2114. 1:07:15function. Right now, you are the owner
  2115. 1:07:27of a hair salon, aren't you? Yes. You
  2116. 1:07:29decide to manage the history of which
  2117. 1:07:31customers visit the salon every day.
  2118. 1:07:33Based on this visit history, you plan
  2119. 1:07:34to encourage future visits by
  2120. 1:07:35contacting customers in advance to
  2121. 1:07:36offer discounts when it is time for
  2122. 1:07:38them to get their hair done again after
  2123. 1:07:39a certain amount of time has passed.
  2124. 1:07:41First, let's create a function to
  2125. 1:07:42manage today's date and the visiting
  2126. 1:07:44customers. Let's name the function '
  2127. 1:07:46Business' and receive today's date as
  2128. 1:07:47input. However, I realized there is one
  2129. 1:07:49problem. Customers can change every day
  2130. 1:07:52. While you could create a large number
  2131. 1:07:54of input values in advance to be safe,
  2132. 1:07:56an issue might arise if more customers
  2133. 1:07:59visit than the total number of input
  2134. 1:08:01values. In such cases, a very simple
  2135. 1:08:03method is to use variable arguments. As
  2136. 1:08:04the name suggests, variable arguments
  2137. 1:08:06refer to arguments whose quantity can
  2138. 1:08:07change. In other words, it allows you
  2139. 1:08:08to use a function without worrying
  2140. 1:08:10about the number of values to be passed
  2141. 1:08:11when you call it, even if you don't
  2142. 1:08:12know exactly how many will be passed.
  2143. 1:08:14Using variable arguments is very simple
  2144. 1:08:15. You just need to place an asterisk (*
  2145. 1:08:17) in front of the value. You can neatly
  2146. 1:08:19reduce the function you were previously
  2147. 1:08:21worrying about regarding the number of
  2148. 1:08:22values to pass into a variable called `
  2149. 1:08:24customers`. For reference, ` customers`
  2150. 1:08:26receives values in the form of a tuple.
  2151. 1:08:28Inside the function, we will first
  2152. 1:08:30write today's date. For now, we will
  2153. 1:08:32set it up to print all customers 'names
  2154. 1:08:33in half a won. By using variable
  2155. 1:08:35arguments like this, you can use this
  2156. 1:08:37function exactly the same way
  2157. 1:08:38regardless of whether there is one
  2158. 1:08:40customer or three. If you run these
  2159. 1:08:42statements individually, it looks like
  2160. 1:08:43this for one customer, like this for
  2161. 1:08:45two, and like this for three. The
  2162. 1:08:46number of customers keeps changing.
  2163. 1:08:48There is one thing to keep in mind when
  2164. 1:08:49using variable arguments: if a function
  2165. 1:08:51has multiple values to pass, the
  2166. 1:08:52variable argument can only be used
  2167. 1:08:54exactly once at the end. For example,
  2168. 1:08:55if the values passed to a function
  2169. 1:08:57called' My Function 'are both' star's,
  2170. 1:08:59'the first value, 1, will go into' a '.
  2171. 1:09:01However, when passing the values in
  2172. 1:09:03this way, it is impossible to know how
  2173. 1:09:05many of the remaining values should be
  2174. 1:09:07split and assigned to' b 'and' c '.
  2175. 1:09:19There is a function called' Secret '.
  2176. 1:09:21This function creates a variable called
  2177. 1:09:23' Message 'to secretly store a secret
  2178. 1:09:25message. Can you print the value of
  2179. 1:09:27this' Message 'variable from outside
  2180. 1:09:28the function like this? Or not? The
  2181. 1:09:33answer is no. If you run this code
  2182. 1:09:35directly, you will get an error stating
  2183. 1:09:37that the variable' Message 'is not
  2184. 1:09:39defined. The reason is that the'
  2185. 1:09:40Message 'variable is a local variable.
  2186. 1:09:43A local variable is a variable defined
  2187. 1:09:44within a function, and it can only be
  2188. 1:09:46used within that specific function.
  2189. 1:09:48Therefore, the' Message 'defined within
  2190. 1:09:50the' Secret 'function can be printed
  2191. 1:09:51like this, and you can also modify the
  2192. 1:09:53value as you wish. However, it can
  2193. 1:09:55never be accessed or modified from
  2194. 1:09:56outside the function or from another
  2195. 1:09:57function. If you assign a value to a
  2196. 1:09:59variable with the same name within
  2197. 1:10:01different functions, are they actually
  2198. 1:10:03the same variable? The answer is no.
  2199. 1:10:09They share only the same name; they are
  2200. 1:10:11local variables that can never interact
  2201. 1:10:12with each other, as they are usable
  2202. 1:10:14only within the ` secret` function or
  2203. 1:10:15the ` please` function, respectively.
  2204. 1:10:28We have previously looked at local
  2205. 1:10:29variables defined within a function and
  2206. 1:10:31usable only within that function.
  2207. 1:10:32However, there is also a variable
  2208. 1:10:33called a "no-secret" variable that can
  2209. 1:10:34be used anywhere, regardless of whether
  2210. 1:10:35it is within or outside the function.
  2211. 1:10:37You have actually encountered no-secret
  2212. 1:10:38variables quite often. The method of
  2213. 1:10:40use is simple: if you create it outside
  2214. 1:10:41the function, that is a no-secret
  2215. 1:10:42variable. Then, it can be used anywhere
  2216. 1:10:44, whether inside or outside the
  2217. 1:10:46function. One thing to note is that if
  2218. 1:10:48you try to assign a value to a variable
  2219. 1:10:49named "Message"—which has the same
  2220. 1:10:51name as a no-secret variable—inside a
  2221. 1:10:52function, it is created as a new local
  2222. 1:10:53variable within the ` no-secret`
  2223. 1:10:55function rather than a no-secret
  2224. 1:10:56variable. Consequently, if you print it
  2225. 1:10:58, the output will differ. Therefore,
  2226. 1:11:01while it does not matter when you are
  2227. 1:11:02merely using the variable's value
  2228. 1:11:03within the function, if you intend to
  2229. 1:11:05directly modify the value, you should
  2230. 1:11:06declare it as "Global Message" using
  2231. 1:11:08the keyword "Global." By doing this,
  2232. 1:11:09you are using a variable from the
  2233. 1:11:10global scope. If the variable does not
  2234. 1:11:12exist in the global scope, it creates a
  2235. 1:11:14variable named "Message." Therefore, if
  2236. 1:11:17you declare a variable inside a
  2237. 1:11:18function in this way and change its
  2238. 1:11:20value, when you run the function and
  2239. 1:11:22print "Message," you can see that the
  2240. 1:11:24value declared above— "I am the
  2241. 1:11:26variable"—is printed first, and then
  2242. 1:11:28the value changed within the function
  2243. 1:11:30— "5 is the variable"—is printed
  2244. 1:11:32next. Please remember the difference
  2245. 1:11:34between a variable that can be used
  2246. 1:11:36anywhere and a variable that can only
  2247. 1:11:37be used within a function. The programs
  2248. 1:11:50we have created so far have been
  2249. 1:11:50one-way, like a broadcast flowing
  2250. 1:11:51through speakers. The input simply
  2251. 1:11:55flowed by. This time, let's make the
  2252. 1:11:57program communicate directly with us.
  2253. 1:12:01In Python, you can receive user input
  2254. 1:12:02using a built-in function called `
  2255. 1:12:02input`. The usage is very simple. A
  2256. 1:12:06hamburger suddenly comes to mind. Yes.
  2257. 1:12:07Shall we try making a program to
  2258. 1:12:08reserve a burger restaurant? If you
  2259. 1:12:10want to ask the hamburger staff, "What
  2260. 1:12:11is the name of the person who made the
  2261. 1:12:13reservation?", you simply write the
  2262. 1:12:15sentence inside the parentheses of the
  2263. 1:12:16` input` function like this. Then, it
  2264. 1:12:18waits until the user enters a value,
  2265. 1:12:20stores the entered value in the ` Name`
  2266. 1:12:22variable, and executes the next
  2267. 1:12:23statement. To verify, I will print `
  2268. 1:12:26Name`. When you run the code, a
  2269. 1:12:28statement is printed like this,
  2270. 1:12:29followed by user input. If you type
  2271. 1:12:31your name or "I am coding too" and
  2272. 1:12:33press Enter, you can see that the user
  2273. 1:12:35input value stored in ` Name` is
  2274. 1:12:36printed correctly. Now, shall we ask
  2275. 1:12:38how many people are making the
  2276. 1:12:39reservation? We will use ` Input` again
  2277. 1:12:41, but this time, let's receive the
  2278. 1:12:42input value into a variable called `
  2279. 1:12:43Num`. However, it says that this
  2280. 1:12:45restaurant only allows a maximum of
  2281. 1:12:46four people per group. Then, we can
  2282. 1:12:48compare the value received in ` Num`.
  2283. 1:12:50Using this part, if ` Num` is greater
  2284. 1:12:52than 4, let's write a statement like
  2285. 1:12:54this and run it. It waits for user
  2286. 1:12:55input while asking "How many people are
  2287. 1:12:57in total?", and I'll try entering 8.
  2288. 1:12:59Yes. I entered 8 and left it, but an
  2289. 1:13:01error occurred. I looked into why, and
  2290. 1:13:03it says that comparison operations are
  2291. 1:13:04impossible between a string and an
  2292. 1:13:05integer. This means that ` Sa` is an
  2293. 1:13:07integer, but the ` Num` we received as
  2294. 1:13:09input is not an integer but a string.
  2295. 1:13:11As seen earlier, the user input values
  2296. 1:13:13received are all in string format, so
  2297. 1:13:14the number 4 is also stored as a string
  2298. 1:13:16as shown in Table 4. In such cases, we
  2299. 1:13:18can use the type conversion we learned
  2300. 1:13:20earlier to wrap the input in integer
  2301. 1:13:21parentheses like this, allowing us to
  2302. 1:13:23directly convert the input value to a
  2303. 1:13:25numeric data type. After changing the
  2304. 1:13:26code and running it, I can see that it
  2305. 1:13:28works well. When I was in the military,
  2306. 1:13:40I used to write a journal called a "
  2307. 1:13:41Suyangnok." A Suyangnok is similar to a
  2308. 1:13:42journal entry. Since it served as a
  2309. 1:13:44kind of memory, I converted everything
  2310. 1:13:45into digital files after completing it
  2311. 1:13:46properly. One page in particular stands
  2312. 1:13:48out in my memory, so I will use it as
  2313. 1:13:49an example. I won't tell you what the
  2314. 1:13:51content is. In Python, you can write
  2315. 1:13:52text to a file or read from a file with
  2316. 1:13:54just a few lines of code. You simply
  2317. 1:13:56use the ` open` function. You just need
  2318. 1:13:58to open the parentheses, specify the
  2319. 1:14:00filename, the opening mode, and the
  2320. 1:14:01encoding if necessary. You just need to
  2321. 1:14:03remember these three main types of
  2322. 1:14:04opening modes. Let's try writing to a
  2323. 1:14:06file first. To open a file in writing
  2324. 1:14:08mode, F is the open parentheses, and
  2325. 1:14:10the list. I will open the file in write
  2326. 1:14:14mode by adding' W 'at the end of the
  2327. 1:14:15filename. To write Korean characters, I
  2328. 1:14:18will set the encoding to UTF-8. This
  2329. 1:14:20means that if a file exists in the
  2330. 1:14:22current path, it will be overwritten;
  2331. 1:14:24if not, a new file will be created.
  2332. 1:14:26Through this, we can now write content
  2333. 1:14:28to the file. After that, there is the'
  2334. 1:14:30f.write 'function. I will use' f.write
  2335. 1:14:33'to write about three sentences to the
  2336. 1:14:35file. To separate the sentences, I will
  2337. 1:14:37add a line break at the end. Finally, I
  2338. 1:14:40will call the' f.close 'function to
  2339. 1:14:42close the file. You must always close a
  2340. 1:14:44file once it has been opened. If you
  2341. 1:14:46run the application, you can see that a
  2342. 1:14:47file named' list.text 'has been created
  2343. 1:14:49and the content has been entered
  2344. 1:14:50correctly. Now, let's try reading the
  2345. 1:14:52file created in this way. To open the
  2346. 1:14:54file in read mode, I will add' f.open '
  2347. 1:14:56and write the filename exactly the same
  2348. 1:14:57, but this time I will set the open
  2349. 1:14:59mode to' R '. I will also set the
  2350. 1:15:01encoding to UTF-8. Then, by using the
  2351. 1:15:04F. read function to load the entire
  2352. 1:15:05file content, storing it in a variable
  2353. 1:15:07called' content ', and printing it out,
  2354. 1:15:09you can read the file contents very
  2355. 1:15:11easily like this. Oh, and remember to
  2356. 1:15:13close the file using F. close at this
  2357. 1:15:15point as well. If you want to read the
  2358. 1:15:17file line by line and process it, you
  2359. 1:15:19can do so very simply using method 4.
  2360. 1:15:21This retrieves one line at a time from
  2361. 1:15:23the file and inserts it into the line.
  2362. 1:15:25At this point, the print function has
  2363. 1:15:27an' end 'property; if you do not set
  2364. 1:15:29this to a blank space, unnecessary
  2365. 1:15:31lines may be inserted one by one like
  2366. 1:15:33this, so please use' end 'if necessary.
  2367. 1:15:46You must turn off the heat after
  2368. 1:15:47cooking, and turn off the faucet after
  2369. 1:15:49washing your hands. Similarly, if you
  2370. 1:15:51have opened a file, you must close it
  2371. 1:15:53after you are finished using it. When
  2372. 1:15:55you are working on a file for a while,
  2373. 1:15:56you might forget to touch it. Because
  2374. 1:15:57we are human. However, there is a tool
  2375. 1:15:59that does this automatically. It is
  2376. 1:16:00called' with '. When using ` with`, the
  2377. 1:16:02file is automatically closed when you
  2378. 1:16:04exit the ` with` block, so you do not
  2379. 1:16:05need to call the ` close` function
  2380. 1:16:07separately. It is also simple to use.
  2381. 1:16:09Do you remember how we did this earlier
  2382. 1:16:10to write to a file? When using ` with`,
  2383. 1:16:13the code looks like this. Start the
  2384. 1:16:15statement with ` with`, leave a space,
  2385. 1:16:16and write the ` open` syntax exactly as
  2386. 1:16:18it is. At the very end, specify a
  2387. 1:16:19variable name to store the file opened
  2388. 1:16:21via ` open`. This is exactly the same
  2389. 1:16:23as receiving the result of opening in
  2390. 1:16:24the statement above as ` F`. Here, you
  2391. 1:16:27can replace the name ` F` with ` file`,
  2392. 1:16:29` my underscore file`, or any other
  2393. 1:16:31format you prefer. After doing this,
  2394. 1:16:34write a colon, and then write the
  2395. 1:16:36statements starting from the next line.
  2396. 1:16:39Now, you do not need to call the `
  2397. 1:16:40close` function separately; the file
  2398. 1:16:42will close automatically once you exit
  2399. 1:16:43this ` with` statement. Convenient,
  2400. 1:16:45right? It works the same way when
  2401. 1:16:46reading a file. You simply use ` with`
  2402. 1:16:48like this for the statements you wrote
  2403. 1:16:49earlier, and then perform the
  2404. 1:16:51operations on the file. Again, you
  2405. 1:16:52won't need to call ` close` for the
  2406. 1:16:54file. It is a good habit to use the `
  2407. 1:16:56with` statement whenever possible when
  2408. 1:16:57outputting files. Nowadays, most cars
  2409. 1:17:12are equipped with dashcams. If you are
  2410. 1:17:14purchasing a new dashcam, you should
  2411. 1:17:16first examine its specifications. Let's
  2412. 1:17:18create variables for the dashcam
  2413. 1:17:19information. Yes. First, let's name the
  2414. 1:17:22model "Kkamang." Resolution is also
  2415. 1:17:25important for a dashcam, so we will
  2416. 1:17:26assign "Full HD" to the variable "
  2417. 1:17:27Resolution." Now, for the price, we
  2418. 1:17:29will assign 200,000 won to the variable
  2419. 1:17:31"Price." And since the color can vary
  2420. 1:17:33even within the same model, we will
  2421. 1:17:34assign "Black" to the variable "Color."
  2422. 1:17:37Although it is a very small amount of
  2423. 1:17:39data, we used four variables to store
  2424. 1:17:40the information for a single dashcam.
  2425. 1:17:42Now, what if we want to manage another
  2426. 1:17:45dashcam model using variables? We could
  2427. 1:17:48create four more variables and assign
  2428. 1:17:49values like this. If another dashcam is
  2429. 1:17:52added, we would simply create four more
  2430. 1:17:54variables. However, isn't there a way
  2431. 1:17:56to manage this more conveniently and
  2432. 1:17:57efficiently? Yes, that is correct.
  2433. 1:17:58Dictionaries managed as key-value pairs
  2434. 1:18:01can be very useful. Now, let's say a
  2435. 1:18:04dash cam has a feature that detects an
  2436. 1:18:06impact while driving and manages the
  2437. 1:18:09video footage in a separate event
  2438. 1:18:11folder. This falls into the realm of
  2439. 1:18:13functions, which is difficult to handle
  2440. 1:18:14using only variables. Yes, relying
  2441. 1:18:16solely on dictionaries seems
  2442. 1:18:17impractical. This is where we can
  2443. 1:18:19utilize classes. Classes allow you to
  2444. 1:18:21group multiple variables together for
  2445. 1:18:23management, and you can also create
  2446. 1:18:24functions within a class to execute
  2447. 1:18:25specific tasks. The concept of a class
  2448. 1:18:28can be quite difficult when you first
  2449. 1:18:30encounter it. Therefore, it is crucial
  2450. 1:18:32to clearly understand the concept first
  2451. 1:18:33. It is helpful to think of a class as
  2452. 1:18:35a combination of a blueprint and an
  2453. 1:18:36instruction manual. By following this
  2454. 1:18:39manual, you can verify the dash cam's
  2455. 1:18:41information, build the dash cam based
  2456. 1:18:43on the blueprint, and understand how
  2457. 1:18:44its features operate. You can create
  2458. 1:18:47multiple dash cam products with just a
  2459. 1:18:49single blueprint, much like how having
  2460. 1:18:50a book on how to write a paper crane
  2461. 1:18:52allows you to create multiple paper
  2462. 1:18:54cranes by following that book. As such,
  2463. 1:18:56once a class is defined, you can create
  2464. 1:18:59as many products as you want using it;
  2465. 1:19:01these products are called objects. In
  2466. 1:19:04this case, each object is referred to
  2467. 1:19:06as an instance of the class. Do you
  2468. 1:19:08have a better grasp of classes now?
  2469. 1:19:22Let's review them. A class is a concept
  2470. 1:19:24that combines a blueprint and an
  2471. 1:19:25instruction manual. A class can contain
  2472. 1:19:27various variables and even define
  2473. 1:19:29functions. The products created through
  2474. 1:19:31a class are called objects, and each
  2475. 1:19:33object is called an instance of that
  2476. 1:19:35class. You can define a class like this
  2477. 1:19:37: write "Class" in English, leave a
  2478. 1:19:39space, write the class name, and then a
  2479. 1:19:40colon. Then, indent the next line and
  2480. 1:19:43write the content defining the class.
  2481. 1:19:45You can define a class called "Black
  2482. 1:19:47Box" like this. For reference, class
  2483. 1:19:48names should be formed by combining
  2484. 1:19:50words that start with a capital letter.
  2485. 1:19:52Here, since we combined the two words "
  2486. 1:19:54Black" and "Box," we capitalized the "B
  2487. 1:19:56" part. Also, in the definition section
  2488. 1:19:58, we have written "Pass" for now. In
  2489. 1:19:59Python, you can understand "pass" as a
  2490. 1:20:01way to temporarily set aside the parts
  2491. 1:20:03you need to implement. Think of it like
  2492. 1:20:06making plans with a friend but not yet
  2493. 1:20:08deciding what to do when you meet. Just
  2494. 1:20:11as defining a function doesn't mean it
  2495. 1:20:12will execute, defining a class doesn't
  2496. 1:20:14mean the code will run. To use a class,
  2497. 1:20:17you must create a class object, which
  2498. 1:20:20you can do like this: define the object
  2499. 1:20:23as B1, place an equal sign, write the
  2500. 1:20:26class name, and then open parentheses.
  2501. 1:20:29This creates an object named B1. Shall
  2502. 1:20:32we create a variable for this Black Box
  2503. 1:20:33object? Using a dot, you can assign the
  2504. 1:20:35value "black" to B1.Name, just as you
  2505. 1:20:37would normally assign a string value.
  2506. 1:20:39This declares a variable named "Name"
  2507. 1:20:41within the B1 object. To verify that
  2508. 1:20:44the value is being assigned correctly,
  2509. 1:20:46you can print B1.Name. The result
  2510. 1:20:49should be "Black". Let's check if the
  2511. 1:20:51B1 object is indeed an instance of the
  2512. 1:20:53Black Box. In that case, you can use
  2513. 1:20:55the ` isInstance` function, passing the
  2514. 1:20:57object B1 in front and the class Black
  2515. 1:21:00Box behind. If the result is ` True`,
  2516. 1:21:02it means B1 is indeed an instance of
  2517. 1:21:04Black Box; if it is ` Pes`, it means it
  2518. 1:21:05is not. We defined the class like this,
  2519. 1:21:19created an object named B1, created a `
  2520. 1:21:21Name` variable using a dot, and
  2521. 1:21:23assigned the value "Kkamang" to it. Now
  2522. 1:21:25, let's create a new object named B2.
  2523. 1:21:27What happens if we try typing ` B2 dot
  2524. 1:21:29Name`? That's right. An error occurs.
  2525. 1:21:31We created a ` Name` variable for the
  2526. 1:21:33B1 object, but we haven't created one
  2527. 1:21:35for the B2 object yet. However, I
  2528. 1:21:37mentioned that a class is a concept
  2529. 1:21:38that combines a blueprint and a
  2530. 1:21:39description. Right now, our blueprint
  2531. 1:21:41is just an empty one with nothing in it
  2532. 1:21:42. So, let's fill in the contents of the
  2533. 1:21:44blueprint. To fill in the content, we
  2534. 1:21:46can use the ` insert` function. Now,
  2535. 1:21:48delete the path on the line next to the
  2536. 1:21:50class definition, and just like
  2537. 1:21:51creating a regular function, add a
  2538. 1:21:53space, an underscore, an underscore, an
  2539. 1:21:55underscore, and then write the
  2540. 1:21:56variables needed for the product we are
  2541. 1:21:58creating inside the parentheses. For
  2542. 1:22:01example, if our product has a' Name '
  2543. 1:22:03for the name and a' Price 'for the
  2544. 1:22:04price, you would write it as' self
  2545. 1:22:06comma Name comma Price '. (Just write'
  2546. 1:22:09self 'for now.) Then, write a colon.
  2547. 1:22:11Next, after indenting, write' self dot
  2548. 1:22:14Name 'as' Name 'and' self dot Price 'as
  2549. 1:22:16' Price '. You might be wondering what
  2550. 1:22:18on earth this is all about. Now, to
  2551. 1:22:19create an object using the blueprint
  2552. 1:22:21class created like this, you define B1
  2553. 1:22:23as a' Black Box 'and simply enter the
  2554. 1:22:24name and price in order inside the
  2555. 1:22:26parentheses. Right? It is the same as
  2556. 1:22:28calling a regular function. After doing
  2557. 1:22:29this, if you try' print B1.Name, B
  2558. 1:22:311.Price ', you can see that the values
  2559. 1:22:33we entered when creating the object are
  2560. 1:22:35printed correctly. Shall we try B2 as
  2561. 1:22:37well? We create an object for B2 called
  2562. 1:22:39a' Black Box ', set' White 'to' 100,000
  2563. 1:22:40won ', and when we print the value, the
  2564. 1:22:42value is printed correctly as well. In
  2565. 1:22:44other words, the class we created
  2566. 1:22:46called' Black Box 'has two variables
  2567. 1:22:48named' Name 'and' Price '. The
  2568. 1:22:49initialization function is what
  2569. 1:22:51performs the role of assigning' Name '
  2570. 1:22:53here and' Price 'here when creating
  2571. 1:22:55each object. Now, the' Black Box 'class
  2572. 1:22:57—our blueprint—has been created in
  2573. 1:22:59this form. The B1 and B2 objects
  2574. 1:23:01created through this will each take
  2575. 1:23:03this form. Please remember that the
  2576. 1:23:05initialization function runs
  2577. 1:23:06automatically when an object is created
  2578. 1:23:08. Earlier, we defined' Name 'and' Price
  2579. 1:23:22'through this function; in this case,'
  2580. 1:23:24Name 'and' Price 'are called member
  2581. 1:23:26variables. Member variables can have
  2582. 1:23:28different values for each class object.
  2583. 1:23:30Just as people use identical desks for
  2584. 1:23:32different purposes, the values of their
  2585. 1:23:35member variables can vary even if
  2586. 1:23:37objects are created from the same class
  2587. 1:23:39. Furthermore, the two objects are
  2588. 1:23:41completely independent of each other.
  2589. 1:23:42However, if you need an additional
  2590. 1:23:44member variable for a specific object
  2591. 1:23:46—for example, if you want to give a
  2592. 1:23:48special nickname to the B1 Black Box—
  2593. 1:23:49then B1. You can add a special member
  2594. 1:23:51variable specifically to the B1 object,
  2595. 1:23:53such as "Nickname is No. 1." In other
  2596. 1:23:55words, the form of the B1 object
  2597. 1:23:57changes like this; this is a new
  2598. 1:23:58variable that is not defined in the
  2599. 1:24:00documentation—that is, the class—so
  2600. 1:24:02B2 knows nothing about it. We mentioned
  2601. 1:24:15that a class can have multiple
  2602. 1:24:16variables, but it can also have
  2603. 1:24:18functions that perform tasks. We have
  2604. 1:24:20already used a function that is
  2605. 1:24:21automatically called when an object is
  2606. 1:24:24created. Functions declared within a
  2607. 1:24:26class like this are called methods. The
  2608. 1:24:28dash cams produced at our factory have
  2609. 1:24:29a special feature. If the user wants,
  2610. 1:24:31pressing the "Travel Mode" button
  2611. 1:24:33automatically manages the video footage
  2612. 1:24:35in a separate folder from that point on
  2613. 1:24:37. Normal recordings are general footage
  2614. 1:24:39taken to prepare for potential
  2615. 1:24:41accidents, so they tend to be
  2616. 1:24:42overwritten over time. However, if you
  2617. 1:24:45turn on Travel Mode, the recorded
  2618. 1:24:46footage is managed in a separate folder
  2619. 1:24:49. This allows you to save scenes like
  2620. 1:24:51beautiful mountains you encounter while
  2621. 1:24:52driving. Now, this is not a variable
  2622. 1:24:55but a function; since we said that
  2623. 1:24:57classes can have functions, let's
  2624. 1:24:58define this function of our dash cam.
  2625. 1:25:00The class is set like this, and all you
  2626. 1:25:02have to do is add one more instance
  2627. 1:25:04here using DF below, and that's it. DF,
  2628. 1:25:07yes. We are creating a method called `
  2629. 1:25:09Set Travel Mode`. If you don't receive
  2630. 1:25:11any values, you just need to write `
  2631. 1:25:13self`. I will explain ` self` later.
  2632. 1:25:15Then, inside the method, we will print
  2633. 1:25:18the value "Travel Mode On". After that,
  2634. 1:25:21create the B1 object and, just like
  2635. 1:25:23accessing member variables, use the dot
  2636. 1:25:25: B1 dot Set Travel Mode, and then
  2637. 1:25:27write the parentheses. If you run it,
  2638. 1:25:30you can see that the text "Travel Mode
  2639. 1:25:32On" is printed correctly. If the method
  2640. 1:25:34requires a value to be passed, you can
  2641. 1:25:36define it like this after ` self`, just
  2642. 1:25:38like a method with 1. Let's try
  2643. 1:25:40applying Travel Mode only for the
  2644. 1:25:41duration passed here. And if you change
  2645. 1:25:45the print mode to "Travel Mode On for a
  2646. 1:25:46few minutes" and want to record in
  2647. 1:25:48Travel Mode for only 20 minutes from
  2648. 1:25:49now, you can do it like this. Only 20
  2649. 1:25:51is displayed inside the parentheses
  2650. 1:25:52here. Now, if you run it, you can see
  2651. 1:25:55that "Travel Mode On for 20 minutes"
  2652. 1:25:57appears. We used' self 'when defining
  2653. 1:26:10methods within a class. When defining a
  2654. 1:26:13function, if there are two values to
  2655. 1:26:14pass, the function must also be passed
  2656. 1:26:15two values when called. However, class
  2657. 1:26:17methods, even if they pass two values
  2658. 1:26:19including' self ', exclude' self 'when
  2659. 1:26:21calling and only pass the remaining
  2660. 1:26:22value. In this context,' self 'refers
  2661. 1:26:24to the object itself. Let's modify our
  2662. 1:26:26method slightly. We changed the print
  2663. 1:26:28part of the ` setTravelMode` method
  2664. 1:26:30like this.' self.name 'has been added
  2665. 1:26:31at the beginning. Let's delete the line
  2666. 1:26:33above it. Now, in this code,' self.name
  2667. 1:26:35'prints the member variable' name 'of
  2668. 1:26:36this object, and as mentioned,' self '
  2669. 1:26:37refers to the object itself. Therefore,
  2670. 1:26:45if two objects named B1 and B2 are
  2671. 1:26:46created and you call the method on each
  2672. 1:26:47of them, you can see the output that
  2673. 1:26:48includes the' name 'member variable of
  2674. 1:26:49each object. In other words, when this
  2675. 1:26:51statement is executed, the B1 object is
  2676. 1:26:53placed in the' self 'part, and when the
  2677. 1:26:55statement below is executed, the B2
  2678. 1:26:56object itself is placed in the' self '
  2679. 1:26:58part. It's a bit confusing, isn't it?
  2680. 1:26:59Now, look closely at this statement.
  2681. 1:27:01Write the name of the Black Box class
  2682. 1:27:02directly and call the Set Wrap with the
  2683. 1:27:04purchase amount. Then, set B1 as the
  2684. 1:27:06first pass value and 20 as the second
  2685. 1:27:08pass value. By doing this, B1 is passed
  2686. 1:27:11as' Self 'and 20 as' Min ', just as if
  2687. 1:27:13calling a function. Whether you fold
  2688. 1:27:16the code like in example 1 or write it
  2689. 1:27:17like in example 2, the result is the
  2690. 1:27:18same. Looking at it this way, it makes
  2691. 1:27:20a little more sense, doesn't it?
  2692. 1:27:21Ultimately, you can understand it as
  2693. 1:27:22follows: while we could directly call
  2694. 1:27:24the Black Box class method and specify
  2695. 1:27:26the object and pass values, it is more
  2696. 1:27:27convenient to call the method through
  2697. 1:27:29the B1 object; since we already know it
  2698. 1:27:31is the B1 object, we can exclude' Self
  2699. 1:27:32'and just specify the remaining pass
  2700. 1:27:34values. If it still feels difficult,
  2701. 1:27:37please remember just two things. When
  2702. 1:27:39defining a class method, you must
  2703. 1:27:40always set the first value to' Self '.
  2704. 1:27:42Also, within the method, use member
  2705. 1:27:45variables using the' Self 'dot, as in'
  2706. 1:27:47Self Dot Name '. The Black Box we are
  2707. 1:28:00creating includes a feature that
  2708. 1:28:01manages videos separately for travel
  2709. 1:28:02purposes from the moment a button is
  2710. 1:28:04pressed. We added this feature and
  2711. 1:28:06raised the price slightly, but sales
  2712. 1:28:07actually dropped. It seems fewer people
  2713. 1:28:10used Travel Mode than expected. So, we
  2714. 1:28:12changed our strategy and decided to
  2715. 1:28:13sell a separate Black Box that performs
  2716. 1:28:15only basic functions and another that
  2717. 1:28:16supports Travel Mode. For the basic
  2718. 1:28:18Black Box, we will simply leave the `
  2719. 1:28:20set` method as we did before. For the
  2720. 1:28:22Black Box supporting Travel Mode, we
  2721. 1:28:23can create a new class called "Travel
  2722. 1:28:25Black Box" and have it with the ` set`
  2723. 1:28:26method added. However, upon closer
  2724. 1:28:28inspection, there is some overlap. The
  2725. 1:28:30members of the two classes are
  2726. 1:28:31completely identical. They are members
  2727. 1:28:33that have exactly the same name and
  2728. 1:28:35price. In fact, our Black Boxes are
  2729. 1:28:37very small classes, but as classes grow
  2730. 1:28:39even slightly in scale, there can be a
  2731. 1:28:41lot of duplicated code like this.
  2732. 1:28:43Rewriting all the duplicated content
  2733. 1:28:45every time a new class is created is
  2734. 1:28:47clearly inefficient. Fortunately,
  2735. 1:28:48classes have something called
  2736. 1:28:49inheritance. Inheritance usually refers
  2737. 1:28:51to a child receiving property left
  2738. 1:28:53behind by a parent. In the context of
  2739. 1:28:55classes, inheritance can be used to
  2740. 1:28:57inherit all the code used in a class
  2741. 1:28:59exactly as it is, and then extend it
  2742. 1:29:00with additional necessary content.
  2743. 1:29:02Returning to our code, both the Black
  2744. 1:29:04Box and the Travel Black Box have the
  2745. 1:29:06same method. Furthermore, the Travel
  2746. 1:29:08Mode Support Black Box is an extended
  2747. 1:29:10version of the ` Set Travel Mode`
  2748. 1:29:11method. In this case, if you write `
  2749. 1:29:13Black Box` in parentheses after the
  2750. 1:29:14class name ` Travel Black Box`, it
  2751. 1:29:16signifies that you intend to inherit
  2752. 1:29:17and use everything from the Black Box
  2753. 1:29:19class exactly as is. In other words,
  2754. 1:29:21the code that looked like this can be
  2755. 1:29:23changed to this by removing this part,
  2756. 1:29:24since it inherits everything from the
  2757. 1:29:26Black Box class and uses the method
  2758. 1:29:27from here. Here, the basic Black Box on
  2759. 1:29:30the left is called the parent class,
  2760. 1:29:31and the Travel Mode Support Black Box
  2761. 1:29:33inherited from it is called the child
  2762. 1:29:35class. For reference, the code usage is
  2763. 1:29:36the same. Just as you create a B1
  2764. 1:29:38object using the Black Box class, you
  2765. 1:29:39can create a B2 object using the Travel
  2766. 1:29:41Black Box. Then, when the object is
  2767. 1:29:43created, the inherited values from the
  2768. 1:29:45Black Box—specifically the "Hayang"
  2769. 1:29:47and "100,000 Won"—are set as member
  2770. 1:29:48variables named "Name" and "Price,"
  2771. 1:29:50respectively. And the B2 object becomes
  2772. 1:29:52able to use the three travel mode
  2773. 1:29:54methods that the B1 object cannot.
  2774. 1:30:07Fortunately, sales started to rise
  2775. 1:30:08again as we sold the products by
  2776. 1:30:09dividing them into a basic dash box and
  2777. 1:30:11a dash box that supports both travel
  2778. 1:30:12and dash boxes. However, one concern
  2779. 1:30:14arose. As videos recorded during travel
  2780. 1:30:16kept accumulating in the dash box
  2781. 1:30:17supporting travel mode, storage space
  2782. 1:30:19became insufficient. Therefore, we
  2783. 1:30:21decided to offer options: the 64GB
  2784. 1:30:23model for those who diligently transfer
  2785. 1:30:25and delete videos to a PC as needed,
  2786. 1:30:26and the 128GB model for those who do
  2787. 1:30:28not, allowing for more ample space. Now
  2788. 1:30:30, we need to change our code as well,
  2789. 1:30:32right? For the dash box supporting
  2790. 1:30:33travel mode, we added' SD 'as a member
  2791. 1:30:35to denote the storage space as an SD
  2792. 1:30:37card, and redefined the' Entry 'method
  2793. 1:30:38like this. Previously, we could have
  2794. 1:30:40omitted the' Entry 'method by
  2795. 1:30:42inheriting from the basic dash box, but
  2796. 1:30:43it has reappeared. Then, is it really
  2797. 1:30:45necessary to inherit from the parent
  2798. 1:30:46class? Yes. Of course, it is necessary,
  2799. 1:30:48which is why I am doing this. We
  2800. 1:30:49learned that through inheritance, we
  2801. 1:30:50can utilize everything from the parent
  2802. 1:30:52class and extend it for anything
  2803. 1:30:53additionally needed. Then, we would
  2804. 1:30:54need to use everything from the parent
  2805. 1:30:56class here as well. Now, if you look
  2806. 1:30:58here, the parent class defines member
  2807. 1:31:00variables by receiving Name and Price.
  2808. 1:31:02Additionally, the Travel Mode Supported
  2809. 1:31:04Black Box defines only the ST card data
  2810. 1:31:06here. In that case, it would be best to
  2811. 1:31:08use the default Black Box's values for
  2812. 1:31:09setting Name and Price, at the very
  2813. 1:31:11least. You can do it like this. You
  2814. 1:31:14directly call the method of the
  2815. 1:31:15inherited Black Box class, explicitly
  2816. 1:31:17specify ` self`, and pass Name and
  2817. 1:31:19Price as the values. So, at least Name
  2818. 1:31:21and Price are set as member variables
  2819. 1:31:23using the Black Box's method, and here,
  2820. 1:31:25you only need to define the ST card
  2821. 1:31:27part additionally. It works like this,
  2822. 1:31:29too. There is no problem. However,
  2823. 1:31:31usually, as a child, we call our
  2824. 1:31:33parents "Dad" or "Mom," not "Mr./Ms.
  2825. 1:31:35So-and-so." Inheritance is the same.
  2826. 1:31:37When you want to call a method of the
  2827. 1:31:38parent class, you can write the parent
  2828. 1:31:40class name exactly as it is and call it
  2829. 1:31:42as a method, but you can also call it
  2830. 1:31:43as a method using "Super." In this case
  2831. 1:31:45, "Super" refers to the parent class
  2832. 1:31:47itself. However, please note that you
  2833. 1:31:49must enclose the ` super` class in
  2834. 1:31:50parentheses like this, and that ` self`
  2835. 1:31:52is not needed as a method value.
  2836. 1:31:54Therefore, in the case of the Travel
  2837. 1:31:55Mode Supported Dash Box, you could do
  2838. 1:31:56it like this, but you can also do it
  2839. 1:31:58like this using ` super`. All parents
  2840. 1:31:59in this world are actually our Supermen
  2841. 1:32:02. We decided to add a new feature to
  2842. 1:32:14the Travel Mode Supported Dash Box. It
  2843. 1:32:16is to create nostalgic travel videos by
  2844. 1:32:18editing footage recorded by the dash
  2845. 1:32:20box with cool backgrounds. While
  2846. 1:32:22contemplating how to implement this
  2847. 1:32:23feature, we confirmed that it was
  2848. 1:32:25already implemented in another product
  2849. 1:32:26our factory manufactures, the 360-
  2850. 1:32:28degree camera. Furthermore, they
  2851. 1:32:29provided the class directly, so we were
  2852. 1:32:30told that we could use the feature
  2853. 1:32:31immediately just by inheriting it. The
  2854. 1:32:33class name is Video Maker, and it looks
  2855. 1:32:35like this. Video production is possible
  2856. 1:32:36through the Make tool. Now, let's try
  2857. 1:32:38inheriting this class for our Travel
  2858. 1:32:40Mode Supported Dash Box. But wait, we
  2859. 1:32:42are already inheriting the basic Dash
  2860. 1:32:44Box class. What should we do? That is
  2861. 1:32:46correct. Since there is another way, I
  2862. 1:32:47have laid the groundwork here. What is
  2863. 1:32:48needed in cases like this is multiple
  2864. 1:32:50inheritance. It means inheriting from
  2865. 1:32:51multiple classes. The usage is simple:
  2866. 1:32:53just list multiple class names
  2867. 1:32:55separated by commas. For instance, you
  2868. 1:32:56just need to put a comma after the
  2869. 1:32:58Black Box class and write the Video
  2870. 1:32:59Maker class. Then, we create a B1
  2871. 1:33:01object using the Travel Black Box (note
  2872. 1:33:03: this implies the ST card capacity is
  2873. 1:33:0664GB). After doing this, if we call the
  2874. 1:33:08B 1.Make method to use the memory video
  2875. 1:33:10creation feature, we can see that the
  2876. 1:33:12result is displayed correctly. This
  2877. 1:33:14method is inherited directly from the
  2878. 1:33:15Video Maker class. However, another
  2879. 1:33:17feature is said to be added here. This
  2880. 1:33:19feature allows sending the created
  2881. 1:33:20video directly via email. The class
  2882. 1:33:22name is MailSender, and it looks like
  2883. 1:33:24this. Calling the Send method sends the
  2884. 1:33:26video via email. We will need to use
  2885. 1:33:27this in our class as well. To enable
  2886. 1:33:28multiple inheritance, we enter a comma
  2887. 1:33:30after VideoMaker and add MailSender.
  2888. 1:33:32This allows us to easily create a B1
  2889. 1:33:34object, create a memory travel video
  2890. 1:33:35using the Make method, and then send it
  2891. 1:33:37via email by calling the Send method of
  2892. 1:33:39the MailSender class. So far, the
  2893. 1:33:41Travel Black Box has inherited
  2894. 1:33:43multiplely from three A classes. We
  2895. 1:33:56decided to create a computed version of
  2896. 1:33:58the Travel Mode Supported Black Box.
  2897. 1:33:59The part being improved is the behavior
  2898. 1:34:01of the Set Travel Mode method. Setting
  2899. 1:34:03the device to Travel Mode for a given
  2900. 1:34:05period and managing travel videos
  2901. 1:34:06separately is good, but we want it to
  2902. 1:34:08automatically produce a memory video
  2903. 1:34:10and send it via email once the Travel
  2904. 1:34:11Mode ends. Therefore, we decided to
  2905. 1:34:13create the Advanced Travel Black Box,
  2906. 1:34:15an improved version of the Travel Mode
  2907. 1:34:16Supported Black Box. Since this class
  2908. 1:34:18uses the functionality of the Travel
  2909. 1:34:20Black Box while extending the features
  2910. 1:34:21for when it is in Travel Mode, we can
  2911. 1:34:23simply inherit from the Travel Black
  2912. 1:34:24Box. However, when the Set Travel Mode
  2913. 1:34:27method is called, it needs to not only
  2914. 1:34:28set Travel Mode but also produce the
  2915. 1:34:30video and send the email. In this case,
  2916. 1:34:33the parent class's Set Travel Mode
  2917. 1:34:34method can be redefined within the
  2918. 1:34:36Advanced Travel Black Box class. In
  2919. 1:34:39other words, we define three Travel
  2920. 1:34:40Mode methods with the same name within
  2921. 1:34:42the child class, and then use the Make
  2922. 1:34:44and Send methods within them to handle
  2923. 1:34:46video production and email sending. By
  2924. 1:34:48doing this, the ` Set Travel Mode`
  2925. 1:34:50method of the parent class, ` Travel
  2926. 1:34:52Black Box`, is overridden by this
  2927. 1:34:54method defined in the child class. In
  2928. 1:34:56other words, if the child class does
  2929. 1:34:58not define the same method anew, it
  2930. 1:34:59uses the parent class's method; if it
  2931. 1:35:01does, it uses the child class's method.
  2932. 1:35:04This is called method overriding. It
  2933. 1:35:05involves redefining a method. So, if
  2934. 1:35:07you call the ` Set Travel Mode` method
  2935. 1:35:09with a B1 object created from the `
  2936. 1:35:11Travel Black Box` class, the execution
  2937. 1:35:13result is as shown. However, if you
  2938. 1:35:14call the same method with a B2 object
  2939. 1:35:16created through the ` Advanced Travel
  2940. 1:35:18Black Box` class, through method
  2941. 1:35:20overriding, it accommodates code that
  2942. 1:35:22includes the operations of video
  2943. 1:35:24production and email sending. What we
  2944. 1:35:37will learn this time is "Pass." We have
  2945. 1:35:38already used this when defining classes
  2946. 1:35:40earlier. "Pass" is used to indicate
  2947. 1:35:42that you should leave it as "Never" for
  2948. 1:35:43now, as we will cover it later. When
  2949. 1:35:45creating a class, we can list the
  2950. 1:35:47methods we have planned in advance like
  2951. 1:35:49this. As we go along, we might come up
  2952. 1:35:51with other things that need to be done.
  2953. 1:35:52However, you might not be able to write
  2954. 1:35:54the code for this part right now
  2955. 1:35:55because you are working on other tasks
  2956. 1:35:56first. In such cases, if you simply
  2957. 1:35:58write "Pass," the code is in a
  2958. 1:35:59completed form for now, so it will not
  2959. 1:36:01cause an error when called via a format
  2960. 1:36:02method. Therefore, you can use Passes
  2961. 1:36:04like this to create the basic structure
  2962. 1:36:06of a class first, and then implement
  2963. 1:36:07the actual behavior of the methods one
  2964. 1:36:09by one. Passes can be used not only for
  2965. 1:36:11class methods but also for loops like 4
  2966. 1:36:13, if statements, and functions. What
  2967. 1:36:26should you do if you tap your
  2968. 1:36:27transportation card on a bus and are
  2969. 1:36:29told that your balance is insufficient?
  2970. 1:36:30What if you set up a tent at a campsite
  2971. 1:36:32to cook but forgot to bring ingredients
  2972. 1:36:34? Or what if you are waiting for an
  2973. 1:36:37important call on your smartphone but
  2974. 1:36:38the battery is about to die? As we go
  2975. 1:36:40through life, we encounter various
  2976. 1:36:42kinds of unexpected situations like
  2977. 1:36:43this. The same applies to computers.
  2978. 1:36:45For instance, you should not attempt to
  2979. 1:36:46divide a number by zero, convert
  2980. 1:36:48something that cannot be converted to a
  2981. 1:36:50number into an integer, or access index
  2982. 1:36:51999 of a list that only holds two
  2983. 1:36:53values. In programming, this situation
  2984. 1:36:55is referred to as an error, and if this
  2985. 1:36:57error is not handled, the program will
  2986. 1:36:59stop working. Ensuring that the program
  2987. 1:37:01can operate correctly even when
  2988. 1:37:02encountering such situations is called
  2989. 1:37:04exception handling. You can use
  2990. 1:37:05exception handling as follows: Enter a
  2991. 1:37:07colon (*) for the trie, then write the
  2992. 1:37:09desired number of statements on the
  2993. 1:37:11next line. Below that, enter an
  2994. 1:37:13exception colon (*) to write the
  2995. 1:37:14statements to be executed only if an
  2996. 1:37:16error occurs while executing the
  2997. 1:37:17preceding statements. These statements
  2998. 1:37:19will not be executed unless an error
  2999. 1:37:20occurs. The statements written after
  3000. 1:37:22the extra colon (*) below that are
  3001. 1:37:23executed only when the statements at
  3002. 1:37:25the trie location have operated
  3003. 1:37:26successfully without any issues.
  3004. 1:37:27Finally, enter a final colon (*) to
  3005. 1:37:29write the last statement; this
  3006. 1:37:31statement is always executed last,
  3007. 1:37:32regardless of whether an error occurs
  3008. 1:37:34while the statements in the trie area
  3009. 1:37:36are being executed. Let's look at an
  3010. 1:37:38example. First, I will attempt to store
  3011. 1:37:40the value obtained by dividing a number
  3012. 1:37:42by 2 into the trie. Let's assume that
  3013. 1:37:44Num is 3 and Num 2 is 0. We will output
  3014. 1:37:46the result here, but since no number
  3015. 1:37:48can be divided by zero, an error will
  3016. 1:37:50occur for 3 or 0 based on this
  3017. 1:37:51assumption. Therefore, I will use an
  3018. 1:37:53exception to print a message indicating
  3019. 1:37:54that an error occurred. However, what
  3020. 1:37:56if Num 2 were 3 instead of 0? Yes, yes.
  3021. 1:37:59There would be no problem with the
  3022. 1:38:00operation for 3 or 3. To prepare for
  3023. 1:38:02such cases, I will add a statement
  3024. 1:38:03using L to indicate that the operation
  3025. 1:38:05was successful. We do this because we
  3026. 1:38:06don't know what the values of Num On
  3027. 1:38:08and Num To will be during actual
  3028. 1:38:09programming. Finally, I add the
  3029. 1:38:11execution type statement using Final.
  3030. 1:38:13This statement is executed
  3031. 1:38:15unconditionally, regardless of whether
  3032. 1:38:16an error occurred earlier. So, when you
  3033. 1:38:18run the completed program, if you
  3034. 1:38:20execute it for the case where Number 1
  3035. 1:38:21is 3 and Number 2 is 0, you can see
  3036. 1:38:23that the execution result comes out
  3037. 1:38:24like this. First, since an error occurs
  3038. 1:38:26while this statement is executing, it
  3039. 1:38:27jumps to the ` except () ` block to
  3040. 1:38:29execute the code there, and finally
  3041. 1:38:30executes ` finally () `. If Number 1 (1
  3042. 1:38:32) is 3 and Number 2 (2) is also 3, this
  3043. 1:38:33operation will not be a problem.
  3044. 1:38:35Therefore, the ` print` statement below
  3045. 1:38:37is executed, the statement processed
  3046. 1:38:38upon successful execution is printed,
  3047. 1:38:40and finally, the ` finally () ` block
  3048. 1:38:42is executed, so you can confirm that
  3049. 1:38:43the execution result is like this. For
  3050. 1:38:45reference, exception handling can be
  3051. 1:38:46written in four main types. A Trie `
  3052. 1:38:48Exception` handles the error when it
  3053. 1:38:49occurs, while a Trie ` finally () `
  3054. 1:38:51does not handle the error separately;
  3055. 1:38:53it simply executes the last statement
  3056. 1:38:54to be executed and moves on. Thus, a
  3057. 1:38:56Trie must always be paired with either
  3058. 1:38:58an ` except` or a ` finally () ` block.
  3059. 1:39:01The ` try` block handles errors if they
  3060. 1:39:03occur and proceeds with normal
  3061. 1:39:04operation if no errors occur, whereas
  3062. 1:39:06the ` finally` block adds to that by
  3063. 1:39:07executing a final statement regardless
  3064. 1:39:09of whether an error occurs or not,
  3065. 1:39:11thereby terminating the process.
  3066. 1:39:24Imagine going to borrow a book from the
  3067. 1:39:25library for the first time in 100 years
  3068. 1:39:26. You find the book you want and go to
  3069. 1:39:27the librarian, but are told it cannot
  3070. 1:39:29be borrowed. It's absurd, isn't it? So
  3071. 1:39:31you ask why. When you ask, the answer
  3072. 1:39:33you get is simply that it cannot be
  3073. 1:39:34borrowed. You get angry, right? Because
  3074. 1:39:36they don't tell you the reason why it
  3075. 1:39:37can't be borrowed; they just say it
  3076. 1:39:38can't be done. Errors can occur at any
  3077. 1:39:40time, whether in real life or in
  3078. 1:39:42programming. However, you need to know
  3079. 1:39:44at least what kind of error it is or
  3080. 1:39:45why the situation occurred in order to
  3081. 1:39:46deal with it. If the librarian explains
  3082. 1:39:49the reason why a loan cannot be made—
  3083. 1:39:51such as the maximum of five books are
  3084. 1:39:53already checked out, there are overdue
  3085. 1:39:55books, or membership registration is
  3086. 1:39:57required—you would be able to resolve
  3087. 1:39:59the issue or handle the situation
  3088. 1:40:01accordingly. I have slightly modified
  3089. 1:40:03the source code we covered earlier when
  3090. 1:40:04studying exception handling. This code
  3091. 1:40:06assigns the result of dividing 1 by 2
  3092. 1:40:08to ` err` and outputs the result using
  3093. 1:40:11a print statement. Assuming 1 is 3 and
  3094. 1:40:132 is 0, an error would occur at this
  3095. 1:40:15point. When I run the code as is, it
  3096. 1:40:17kindly informs me that an error has
  3097. 1:40:18occurred. But what kind of error
  3098. 1:40:20occurred? And why did it happen? We can
  3099. 1:40:23see here that the error occurred
  3100. 1:40:25because we divided by 0, but without
  3101. 1:40:26comments like this, it might have been
  3102. 1:40:28difficult to identify the cause of the
  3103. 1:40:30error. To find out what the error is
  3104. 1:40:33and why it occurred, you can write `
  3105. 1:40:35exception as err` between ` except` and
  3106. 1:40:38a colon, and then print ` err` to check
  3107. 1:40:40the error details. Since ` exception`
  3108. 1:40:43is the base class for all errors,
  3109. 1:40:44simply writing it like this allows you
  3110. 1:40:46to check for all types of errors.
  3111. 1:40:48However, there are cases where
  3112. 1:40:49different handling is required for
  3113. 1:40:50different situations, such as when all
  3114. 1:40:52five books are checked out at our
  3115. 1:40:53library, you might return one first; if
  3116. 1:40:55there are overdue books, you might
  3117. 1:40:56bring them back; or if membership
  3118. 1:40:58registration is required, you might
  3119. 1:40:59attempt to sign up immediately. Now,
  3120. 1:41:01let's return to the code. In our code,
  3121. 1:41:03we can try this: by adding an exception
  3122. 1:41:05type and specifying "Zero Division
  3123. 1:41:07Error" to handle the error that occurs
  3124. 1:41:09when dividing by zero, the statement "
  3125. 1:41:11Cannot divide by zero" will be executed
  3126. 1:41:13. Alternatively, if 2 is not the
  3127. 1:41:15numeric 0 but the character 0,
  3128. 1:41:16operations between an integer and a
  3129. 1:41:18character type are impossible. In that
  3130. 1:41:20case, by adding a new exception type
  3131. 1:41:21and setting the error type to "Type
  3132. 1:41:23Error," you can verify the output
  3133. 1:41:24indicating that the value's format is
  3134. 1:41:26abnormal. In other words, when you want
  3135. 1:41:27to handle different types of errors,
  3136. 1:41:29you can write multiple exception
  3137. 1:41:30statements like this to handle the
  3138. 1:41:31specific exceptions that correspond to
  3139. 1:41:32those errors. Alternatively, if none of
  3140. 1:41:34the previously listed errors apply, you
  3141. 1:41:36can handle the final case using an
  3142. 1:41:38exception. For reference, you can use
  3143. 1:41:40any name you like for this part. If you
  3144. 1:41:43type "Python errors" into Google and
  3145. 1:41:45scroll down the links that appear, you
  3146. 1:41:47can check the various types of errors
  3147. 1:41:50and their explanations. While coding,
  3148. 1:42:03there are times when you need code you
  3149. 1:42:05created last time again. If you are
  3150. 1:42:06working within the same file, you can
  3151. 1:42:08create a function to use it together,
  3152. 1:42:10but what if it is in a different file?
  3153. 1:42:12There is something called a module. In
  3154. 1:42:13Python, a module refers to a single
  3155. 1:42:15Python file where code is written. This
  3156. 1:42:17module may define variables, functions,
  3157. 1:42:19classes, and the things we have studied
  3158. 1:42:21so far. If you need a specific piece of
  3159. 1:42:23code or feature created previously
  3160. 1:42:24while working in a new environment, you
  3161. 1:42:26can bring it over and use it like this.
  3162. 1:42:27First, let's create a very simple
  3163. 1:42:29module. Good job. I will create a file
  3164. 1:42:31named ` file` and set up a simple
  3165. 1:42:33function called ` set` that prints a
  3166. 1:42:34sentence like this. There are two ways
  3167. 1:42:37to use this module in a new file. The
  3168. 1:42:39first method is to open the import
  3169. 1:42:41prompt and enter the module name. The
  3170. 1:42:43second method is to open the prompt,
  3171. 1:42:45enter the module name, open the import
  3172. 1:42:47prompt, and enter the variable or class
  3173. 1:42:50you want to use. Let's try the first
  3174. 1:42:52method first. Open the import prompt
  3175. 1:42:54and enter "Good Job"—that is, "Good
  3176. 1:42:55Job. File"(excluding the dot extension)
  3177. 1:42:59. Below, the module name "Good Job. Say
  3178. 1:43:03" allows this module to use the
  3179. 1:43:05function "Say." The execution result
  3180. 1:43:07looks like this. For the second method,
  3181. 1:43:09open the prompt, enter the module name
  3182. 1:43:11"Good Job," open the import prompt, and
  3183. 1:43:13then enter the name of the function "
  3184. 1:43:15Say" that we want to use. This time,
  3185. 1:43:17you can use the function "Say" without
  3186. 1:43:19the module name "Good Job," just as if
  3187. 1:43:21it were defined in this file. The
  3188. 1:43:23execution result is the same. The
  3189. 1:43:24difference between the two is that the
  3190. 1:43:26first method means importing the entire
  3191. 1:43:28module to use all the functions
  3192. 1:43:29included within it, while the second
  3193. 1:43:31method means using only the necessary
  3194. 1:43:33parts of the module—in our case, just
  3195. 1:43:35the ` say` function. Although we can
  3196. 1:43:37create modules like this, Python
  3197. 1:43:39already provides numerous modules. One
  3198. 1:43:41representative module is ` random`. For
  3199. 1:43:45instance, if you want to randomly
  3200. 1:43:46select one from ` guis`, ` by`, and `
  3201. 1:43:48bo`, you can import the ` random`
  3202. 1:43:49module, create a list with values named
  3203. 1:43:51` scissors`, ` by`, and ` bo`, and then
  3204. 1:43:53use the ` choice` function from the
  3205. 1:43:54imported ` random` module to input the
  3206. 1:43:56list and run the code. Since the result
  3207. 1:43:59varies each time, you can use it here.
  3208. 1:44:01This means we don't need to create a
  3209. 1:44:03separate function to randomly select a
  3210. 1:44:04value from this list; we can simply use
  3211. 1:44:06the existing module. If you search for
  3212. 1:44:09"list of Python modules" on Google, you
  3213. 1:44:11can check the modules automatically
  3214. 1:44:13provided when you install Python
  3215. 1:44:14through this link. If you click on `
  3216. 1:44:17random`, you can see it there. You can
  3217. 1:44:19also find detailed explanations and
  3218. 1:44:21examples of modules here. We previously
  3219. 1:44:34referred to a single Python file
  3220. 1:44:36containing code as a module. When
  3221. 1:44:37multiple modules like this are
  3222. 1:44:38displayed, it is called a package. A
  3223. 1:44:40package is essentially a single folder
  3224. 1:44:42containing multiple modules. Let's
  3225. 1:44:43continue from the .py file we created
  3226. 1:44:46when studying modules, create another
  3227. 1:44:48.py file, and define a function called'
  3228. 1:44:50by 'like this. Then, let's create a
  3229. 1:44:52folder called' I'm Coding 'within our
  3230. 1:44:54workspace and move these two files
  3231. 1:44:56inside it. Next, we will use the' I'm
  3232. 1:44:58Coding 'package in a new file located
  3233. 1:45:00in the same position as the' I'm Coding
  3234. 1:45:01'folder—for example, a .py file named
  3235. 1:45:03' pract s.py '. So, in Visual Studio
  3236. 1:45:05Code, the situation is as follows:
  3237. 1:45:07there is a' I'm Coding 'folder, inside
  3238. 1:45:09it are' goodbye.py 'and' good#job.py ',
  3239. 1:45:11and outside,' prtic s.py 'is in the
  3240. 1:45:13same location as the' I'm Coding '
  3241. 1:45:14package. Now, to use the' I'm Coding '
  3242. 1:45:17package, just like with modules, simply
  3243. 1:45:19import it. Write the package name you
  3244. 1:45:21want to bring in—that is,' I'm Coding
  3245. 1:45:23'—then a dot, and then write the name
  3246. 1:45:24of the module you want to bring in, for
  3247. 1:45:25example,' Good Job '. Then, on the next
  3248. 1:45:27line,' I'm Coding '. You can call the'
  3249. 1:45:29Say 'function through' Say '. If you
  3250. 1:45:31run it, you can see that "You did a
  3251. 1:45:33great job" is printed, just as defined
  3252. 1:45:34in the module. Another method is to
  3253. 1:45:36open the prompt, type the package name
  3254. 1:45:38"I am Coding," and then list the module
  3255. 1:45:40you want to import after the import.
  3256. 1:45:42For example, you would write "import
  3257. 1:45:43Goodbye." On the next line, you can
  3258. 1:45:44call the Goodbye. Bye bye Bye function
  3259. 1:45:46like this. The execution result shows
  3260. 1:45:48that "See you again" is printed, just
  3261. 1:45:49as defined in the Bye module's Bye
  3262. 1:45:51function. If you want to use both
  3263. 1:45:53modules, open "I am Coding," import
  3264. 1:45:55them, and then define the modules you
  3265. 1:45:57want to import separated by commas.
  3266. 1:46:00Then, you can freely use the functions
  3267. 1:46:01within each module like this: Good Job.
  3268. 1:46:03Say Goodbye. Bye and Bye. If you run
  3269. 1:46:06the code, you can see the output: "You
  3270. 1:46:08did a great job. See you again. This
  3271. 1:46:21concludes all evaluations. Thank you
  3272. 1:46:24for your hard work."

About this transcript

This page contains the full transcript of 파이썬 무료 강의 100분 완성 (1분 파이썬 모음) by 나도코딩, generated from the public captions YouTube serves with the video. The transcript has 22,142 words across 3,272 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.