파이썬 무료 강의 100분 완성 (1분 파이썬 모음) — Transcript
Full transcript
- 0:00Hello, this is Rado Coding. Yes. I have
- 0:02prepared a new course for those who
- 0:03want to study Python but find the basic
- 0:05videos too long and overwhelming, or
- 0:06for those who are too busy to make time
- 0:08. The title of this content is "1-
- 0:10Minute Python." We proceed by investing
- 0:12just one minute a day to learn the
- 0:14Python language comprehensively and
- 0:16solve test questions. Python is a
- 0:18programming language. It is loved by
- 0:20many people because its syntax is very
- 0:22easy to learn, yet it can be applied in
- 0:24a wide variety of fields. It can be
- 0:26used in various fields even by those
- 0:27who are not majoring in
- 0:28computer-related subjects, and it is a
- 0:30language I strongly recommend to
- 0:31students who want to learn coding. Rado
- 0:33Coding will conduct the lectures in a
- 0:35very easy and fun way. If you just
- 0:37trust and follow along without any
- 0:38preparation, you will find yourself
- 0:39familiar with Python before you know it
- 0:40, just like many others have already
- 0:41done. In "1-Minute Python," each
- 0:43lecture will cover the core concepts of
- 0:45the topic in just one minute, and we
- 0:46will present test questions to help you
- 0:48check your understanding. Since the
- 0:50test questions are displayed for 5
- 0:52seconds before the answers and
- 0:53explanations are revealed, please pause
- 0:55the screen briefly when the questions
- 0:56appear, solve the problems yourself,
- 0:58and then continue. Okay. Shall we give
- 1:00it a try? We will first proceed with
- 1:12the environment setup necessary for
- 1:13studying Python. For Japanese Python,
- 1:15we will use Microsoft's Visual Studio
- 1:17Code. First, to install Python, search
- 1:20for "Python" on Google and visit the
- 1:21official website. Download the
- 1:23installation file and run it. Select "
- 1:26Add Python 3,""Customize Installation"
- 1:27under the "Pass to" section, click Next
- 1:29, click Install on the Python 3 version
- 1:31information located right under the C
- 1:33drive, and click Close once the
- 1:35installation is complete. Now, let's
- 1:37try installing Visual Studio Code.
- 1:40Search for "VS Code" on Google, go to
- 1:41the website, and download the
- 1:42installation file. Run it, select "
- 1:44Agree to the terms," and keep clicking
- 1:46the Next button. Okay. Click Install,
- 1:48and then click the Finish button.
- 1:49Create a folder named "Wonmin Python"
- 1:51and click "Open Folder." Select the
- 1:53Wonmin Python folder. Click the New
- 1:55File button. Yes. Fract. Spy. Yes.
- 1:58Install the Python extension from the
- 2:00popup that appears at the bottom right.
- 2:01Please type it exactly as it appears.
- 2:02Yes. Open the Print parentheses, type "
- 2:04Hello World", and click the triangle
- 2:06button to save it. Yes. If the text "
- 2:08Hello World" is displayed correctly
- 2:09like this, the environment setup is
- 2:11ready. Earlier, we saw that "Hello
- 2:24World" was displayed when we entered
- 2:25the "Print Hello World" problem and
- 2:27clicked this button to run it. The
- 2:29meaning of this code is to print, or
- 2:31output, the content inside the
- 2:32parentheses. Now, the part "Hello World
- 2:34" enclosed in parentheses is a
- 2:36character data type, which is one of
- 2:37several data types. Programming
- 2:39languages have various forms of data
- 2:40types. Let's assume that we are
- 2:42entering our name, address, phone
- 2:44number, and year of birth to sign up
- 2:45for a website. There would also be a
- 2:47checkbox to select the consent option.
- 2:49If you input information in this way,
- 2:50names and addresses consisting of
- 2:52letters are of the character data type,
- 2:53phone numbers and birth dates
- 2:54consisting of numbers are of the number
- 2:56data type, and consent statuses where
- 2:57you can choose between "yes" and "no"
- 2:58are of the disadvantageous data type.
- 3:00In Python, number data types can be
- 3:02written as integers or real numbers.
- 3:04Character data types refer to text such
- 3:06as Korean or English words like "Hello
- 3:07World" or "안녕하세요," and you
- 3:09simply enclose them in single or double
- 3:11down arrows. Please note that if a
- 3:13number like 10 is also enclosed in down
- 3:15arrows, it becomes a character data
- 3:16type. Disadvantage data types are "True
- 3:18" and "F." In this case, the T and F at
- 3:20the beginning of the word must be a
- 3:33space to store a variable value. If you
- 3:35are an adult and your nieces and
- 3:36nephews bow three times on Lunar New
- 3:38Year's Day, you would give them New
- 3:39Year's money. Usually, people prepare
- 3:40envelopes in advance and put an
- 3:42appropriate amount of money inside.
- 3:43Let's assume you have three children.
- 3:45You met them on Lunar New Year's Day.
- 3:47So, you prepare three envelopes in
- 3:49order, calculate the appropriate amount
- 3:51, put it inside, and label them
- 3:52Envelope 1, 2, and 3 respectively.
- 3:54Since Jikjang earns money, he just
- 3:55sends it by letter. Then, after
- 3:56receiving the New Year's bows, you
- 3:57simply give the prepared envelopes in
- 3:59that order. You don't need to remember
- 4:01how much is inside each envelope, as it
- 4:03is already there. A space that stores a
- 4:05value like this is called a variable.
- 4:06In other words, in the previous example
- 4:08, the envelopes are the variables, and
- 4:10"dawn" is the value. Creating a
- 4:12variable and assigning a value to it is
- 4:14called variable declaration; you write
- 4:15the variable name on the left first,
- 4:17write an equal sign, and then write the
- 4:19value you want to assign to that
- 4:20variable. To create three variables
- 4:22corresponding to "Gongtu" and put
- 4:2310,000 won, 20,000 won, and a letter
- 4:24with the word "Fighting" written on it
- 4:26into each one, you would do it like
- 4:27this. And to print the value in these
- 4:29variables, you can use the print
- 4:30function and put each variable inside
- 4:32parentheses like this. You can create
- 4:45as many variables as you want, but
- 4:47there are a few rules you must follow
- 4:48when naming variables. First, the name
- 4:51must start with a letter or an
- 4:53underscore. After starting like this,
- 4:55the variable name must consist of
- 4:56letters, numbers, or underscores.
- 4:57Spaces or special characters cannot be
- 4:59used in variable names. Also, since
- 5:01case is sensitive, names composed
- 5:02entirely of lowercase letters, names
- 5:04with only the first letter capitalized,
- 5:05and names composed entirely of
- 5:07uppercase letters are all recognized as
- 5:08different variables. There are also
- 5:10terms called keywords or reserved words
- 5:11. These refer to words that are
- 5:12reserved in advance to perform specific
- 5:14functions. Remember "True" and "Press"
- 5:16when we studied data analysis? These
- 5:18are keywords, and there are over 30
- 5:19keywords in total, such as "4,""Il,""if
- 5:21," and "P," which we will learn later.
- 5:23Since these keywords are already
- 5:25reserved, they cannot be used as
- 5:26variable names. You will naturally
- 5:28become familiar with keywords as you
- 5:29study, so there is no need to memorize
- 5:30them. Furthermore, while not strictly
- 5:32mandatory, there is a recommended
- 5:33practice to improve readability—that
- 5:35is, to make the code easy to read.
- 5:36Generally, it is best to create
- 5:38variables using a single lowercase word
- 5:40or a combination of words separated by
- 5:42underscores, such as "Ah-Under Name" or
- 5:43"Your-Under Name." Data types can be
- 5:56converted between each other as needed.
- 5:57This is called type casting. In Oxygen,
- 6:00when we perform 2 + 1, the result is
- 6:02naturally 3. However, what would the
- 6:04result be if we did 2 + Honey Bag?
- 6:07Since we added Honey Bag to it, it
- 6:08becomes Honey Bag Bag. In Oxygen, such
- 6:10a calculation is impossible. It is the
- 6:11same in Python. As mentioned earlier, 2
- 6:13is a numeric data type, and if you put
- 6:15down brackets around 2, it becomes a
- 6:16character data type. Therefore, because
- 6:18the numeric 2 and the character 2 are
- 6:20different, adding the two is impossible
- 6:22. In this case, you can use something
- 6:23called type casting. You only need to
- 6:25know these three main types of type
- 6:26casting, which convert to integers,
- 6:27floating-point numbers, and characters,
- 6:29respectively. For reference, numeric
- 6:30data types can be distinguished into
- 6:31integers and floating-point numbers; `
- 6:32int` refers to an integer, and ` float`
- 6:34refers to a floating-point number with
- 6:35a decimal point. After writing it like
- 6:36this, if you put the value or variable
- 6:38you want to convert inside the
- 6:39parentheses, the conversion is
- 6:40performed. For example, if you want to
- 6:42convert the character '2' into an
- 6:43integer, you would write it like this.
- 6:44If you want to convert the string "1.5"
- 6:46into a floating-point number, you
- 6:47simply use ` float 1.5`. Conversely, if
- 6:49you want to convert the number 2 into a
- 6:51string, you simply put 2 inside the
- 6:53parentheses. There is something to be
- 6:54careful about here: what goes inside
- 6:56the parentheses must be in a form that
- 6:57the data type you are converting to can
- 6:58recognize. For example, the English
- 7:00letter 2 cannot be converted because it
- 7:02cannot be recognized as an integer.
- 7:04Also, if a floating-point value like
- 7:052.5 is enclosed in a string, direct
- 7:07conversion to an integer is not
- 7:08possible; you must first convert the
- 7:09string 2.5 into a floating-point form
- 7:11using ` float`, and then enclose it in
- 7:12an integer to convert it. Note that
- 7:14when converting a floating-point number
- 7:16to an integer, the decimal part is
- 7:17truncated rather than rounded, so the
- 7:18result in this example is 2, not 3.
- 7:31Let's study the various operators
- 7:33available in Python. First, there are
- 7:35arithmetic operators, which allow us to
- 7:37calculate values through addition,
- 7:38subtraction, multiplication, and
- 7:39division. Please note that while the
- 7:41symbols for addition and subtraction
- 7:42are the same, multiplication uses an
- 7:44asterisk (*) instead of a square (*)
- 7:45symbol, and division uses a slash (*)
- 7:47instead of a division symbol. There is
- 7:48also an operator called the percentage
- 7:49operator, which calculates the
- 7:50remainder. If you perform 5%, the
- 7:52result is the remainder of dividing 5
- 7:53by 2, which is 1. There is also an
- 7:55operator to calculate the quotient. You
- 7:57simply write two slashes for the
- 7:59quotient. For example, 5/2 results in
- 8:01the quotient (*2*) when dividing 5 by 2
- 8:03, and the remainder 1 is simply
- 8:05discarded. Additionally, writing two
- 8:07asterisks (*) for multiplication
- 8:09performs a power. That is, 5*asterisk*1
- 8:11results in 5 raised to the power of 2,
- 8:13or 5*5, which equals 25. There is also
- 8:15a comparison function that allows you
- 8:17to compare values. You can compare the
- 8:19left and right sides to determine if
- 8:21the left side is greater than, greater
- 8:22than or equal to, or less than or equal
- 8:24to the right side. Comparison operators
- 8:26produce a result that is either True or
- 8:28False. For example, 5 being greater
- 8:30than 2 is True, so the result is True.
- 8:33You can also compare whether the left
- 8:35and right sides are equal; to say they
- 8:37are equal, you write two equal signs.
- 8:39This is a bit different from the
- 8:39mathematical operator. For example, to
- 8:41compare if 5 is equal to 2, you write 5
- 8:44and 2. When comparing them together,
- 8:46you write an exclamation mark; if you
- 8:49write 5 and 2, 5 is not equal to 2, so
- 8:52the result is True. There is also the
- 8:54mathematical operator. Using 'END'
- 8:55means True if both the left and right
- 8:57sides are True, while 'A' means True if
- 9:00at least one of them is True. 'NOT'
- 9:02inverts a value. For example, 3 being
- 9:05less than 5 is True. However, since 7
- 9:07is less than 5 is false, the result is
- 9:09false because neither condition is true
- 9:11. In the case of 5, 3 being less than 5
- 9:13is true, and 7 being less than 5 is
- 9:15false; since at least one of them is
- 9:17true, the result is true. And here, 3
- 9:19being less than 5 is true. However,
- 9:21since there is a 'day' before 'true',
- 9:23we invert 'true', so yes, it comes out
- 9:24as false. There is also a member
- 9:26operator that allows you to check if a
- 9:28value is included—that is, to check
- 9:29if it is a member. If you use 'in', you
- 9:32can check if a value is included in
- 9:34what follows it. Since the letter 'C'
- 9:37is currently included in the word '
- 9:39Cache', the result is true. If you use
- 9:41'not in', it means it is not included.
- 9:43So, 'c not in cas' would mean that 'c'
- 9:45is not included in the word 'Cache',
- 9:48but this is incorrect. Yes, because 'C'
- 9:50is here, the result is false. A
- 10:03delimited data type holds one of two
- 10:05values: true or false. We have
- 10:07previously practiced how outputting
- 10:08this in comparison operators results in
- 10:10either true or false. Additionally,
- 10:11while we practiced converting values to
- 10:13int, float, and string when learning
- 10:14type casting, there is actually also a
- 10:16type casting to delimited types. In
- 10:17this case, you only need to specify up
- 10:19to 'boa' within the delimited type. A
- 10:21boolean converts to true if a value
- 10:22exists and false if it does not. The
- 10:24criterion for determining whether a
- 10:25value exists or not is as follows: for
- 10:27character data types, if you assign the
- 10:28string "Hello" to variable 'a', this
- 10:30indicates that a value exists.
- 10:31Similarly, if there are multiple spaces
- 10:33within a bracket like 'b', it indicates
- 10:34that a space has a value. However, if
- 10:36nothing is placed between the brackets,
- 10:38as in C, it indicates that there is no
- 10:40value. If you write this and run it,
- 10:41you can see that in the case of C, only
- 10:43'False' is returned. For numbers, all
- 10:45non-zero numbers are true, while zero
- 10:47is false. This is because zero
- 10:48literally means nothing. In the example
- 10:50, a and b have values of 1 and -2
- 10:52respectively, so they are True, but c
- 10:54is 0, so it is False. Also, there is a
- 10:56keyword in Python called 'Nun' which
- 10:58signifies the absence of a value. As
- 11:00you may have noticed, if you use 'Bul
- 11:02Nunn', there is no value, so it becomes
- 11:03False. To summarize, separation is True
- 11:06if true and False if false; it is True
- 11:07if there is a value and False if there
- 11:09is no value. When hosting a wedding,
- 11:22the master of ceremonies often follows
- 11:24a pre-written script exactly rather
- 11:25than proceeding spontaneously to
- 11:26minimize mistakes. Additionally, the
- 11:28script contains supplementary
- 11:30explanations to regulate the flow,
- 11:31indicating where to pause and when to
- 11:33resume. For example, if the sequence is
- 11:35greeting both sets of parents, the
- 11:37script is prepared like this. Of this,
- 11:38the part where the sound is actually
- 11:40played is right here. The part written
- 11:42in the script that is not read but is
- 11:43only referenced is here. During the
- 11:45wedding ceremony, guests do not hear
- 11:46these supplementary explanations. If
- 11:48there is no need for further
- 11:49explanation, there would be no problem
- 11:51if the master of ceremonies is
- 11:52experienced and knowledgeable about
- 11:53weddings; however, if it is their first
- 11:55time hosting, they might make mistakes.
- 11:57Similarly, explanations or notes
- 11:58written for reference, which are not
- 11:59actually read but are included for
- 12:01context, are called "comments" in
- 12:02programming. This is referred to as "
- 12:04commenting out" the written source code
- 12:05. In Python, there are two ways to
- 12:07comment out code. First, to comment out
- 12:09a single statement, or line, simply
- 12:11place a hash symbol (#) at the
- 12:12beginning of the statement. For example
- 12:13, if the code is written like this, the
- 12:15second statement is a comment, so it
- 12:16will simply be ignored and the program
- 12:17will proceed. To comment out one or
- 12:19more statements, you can enclose the
- 12:21area with three down symbols (*) at the
- 12:22beginning and end. By doing this, when
- 12:25the program runs, these two lines in
- 12:26green will simply be ignored and
- 12:27skipped over. Comments are useful when
- 12:30you need to provide additional
- 12:31explanations for complex code, when
- 12:32leaving listings for other developers,
- 12:34or when you need to temporarily block
- 12:35execution for testing purposes. A
- 12:48parking lot at a villa is displayed
- 12:49like this. This parking lot is a space
- 12:51exclusively for villa residents, and
- 12:52parking spots are designated by
- 12:54category. If you live in an office, you
- 12:56should park in the third spot from the
- 12:57left. Once, a friend came to visit.
- 13:00From the friend's perspective, since
- 13:01all the spots were empty for the second
- 13:02reason, it seemed like they could park
- 13:04anywhere, but you told them to park in
- 13:05the third spot. The friend successfully
- 13:07parked where you instructed. Although
- 13:09the parking lot was actually empty, the
- 13:11friend was able to communicate through
- 13:12the information of which spot it was.
- 13:14Python has the same concept. It is
- 13:16called an index. There is a position in
- 13:18Python strings, and also in lists,
- 13:20which we will learn about later.
- 13:21However, please remember that here, the
- 13:24starting point is 0, not 1. For example
- 13:26, when declaring a variable named 'Ray'
- 13:28, the first character would be 'p'. To
- 13:30print 'p', you would write 'Ray' and
- 13:32then write the position you want to
- 13:33fill inside; since the index value here
- 13:36is 0, you would write '0'. To print the
- 13:38last problem, the last index value is 5
- 13:40, so you would write 'Ray 5'. While
- 13:42this works, in line, you can also use
- 13:44-1 for the last position. In other
- 13:46words, you can use index values that
- 13:48start from 0 and increase by 1, or you
- 13:50can use indices that start at -1 and
- 13:52decrease by 1, such as -2 and -3. There
- 13:54is also something called slicing. This
- 13:56serves to cut a range based on an index
- 13:58value. As you saw earlier, all the
- 14:00positions starting from the second one
- 14:02were empty. To apply this to Python and
- 14:04find the range from the second to the
- 14:06sixth position—that is, from 1 to 5
- 14:08based on the index—you simply insert
- 14:09a colon at the start index and a value
- 14:11equal to the end index plus 1 inside
- 14:13the lang. Doing this cuts out the range
- 14:15from index 1 up to 5, excluding 6. For
- 14:17reference, in Python, if you want to
- 14:19cut from a specific starting index to
- 14:21the end, you can omit the latter part
- 14:23like this. That is, writing only up to
- 14:251 and the colon cuts from 1 to the end,
- 14:27including the end. Conversely, if you
- 14:30want to cut from the very beginning to
- 14:31a specific index, you simply leave the
- 14:33beginning part empty. If you use a
- 14:35colon 4, it will cut the value up to 3
- 14:37(which is just before index 4) from the
- 14:39beginning up to 'pyth'. To work a bit
- 14:41more and include the entire range from
- 14:42start to finish, simply write a colon
- 14:44at both the beginning and end without
- 14:45any indices. Do you remember that I
- 14:58mentioned it is impossible to add '
- 15:00honey bagi' to the number 2? However,
- 15:02if you convert the number to a string,
- 15:04'honey bagi' becomes possible. If the
- 15:06variable 'snack' contains the text '
- 15:07honey bagi' and the variable '2'
- 15:09contains the text 'two', and you want
- 15:10to combine them to make 'honey bagi two
- 15:12', you can simply handle it by using
- 15:13the addition operator to combine them.
- 15:15Let's try adding the text 'please' here
- 15:17. The variable 'please' is 'please plus
- 15:20please', and while it works like this,
- 15:21the variable 'please' is used twice. In
- 15:24Python, to make it more convenient, you
- 15:26can also express it using the addition
- 15:27function like this. In other words, the
- 15:29value obtained by adding the text '
- 15:30please' to the variable 'please' is
- 15:32assigned to the 'please' variable like
- 15:33this. The final value assigned to the '
- 15:34please' variable becomes this. In fact,
- 15:36it can be used with data other than
- 15:38text. If the variable 'Num' contains 3,
- 15:41you can add 2 to 'Num' by doing this.
- 15:44However, if you use 'add' to express it
- 15:46as 'Num is 2', it performs exactly the
- 15:48same action as the sentence above. You
- 15:50can use other functions besides
- 15:51addition as well. Currently, 'Num' is 5
- 15:53after adding 1, and if you want to
- 15:54subtract 1 from this state, you would
- 15:56use 'Num is 1'. If you want to multiply
- 15:58by 2, you would use 'Num is 2', and if
- 16:00you want to divide by 4, you would use
- 16:02'Divide by 4'. Returning to text, there
- 16:04is a way to determine the length of a
- 16:06string. This is the 'Lan' function,
- 16:07which stands for length. Yes. We will
- 16:09learn about functions later, but for
- 16:10now, if you put the string whose length
- 16:12you want to know inside the 'Lan'
- 16:13parentheses, you can find out how many
- 16:14characters it is. 'Honey price' is four
- 16:15characters long. Sometimes, you need a
- 16:17string that spans multiple lines. In
- 16:19such cases, when declaring a variable,
- 16:21you simply wrap the string in three
- 16:22parentheses at the beginning and end.
- 16:23It is similar to commenting out
- 16:25multiple lines. There is also a method.
- 16:38A method refers to a group of code
- 16:40defined within a class that performs a
- 16:41specific action or function; you can
- 16:43simply understand it as a "function."
- 16:44There are various functions available
- 16:46for handling strings. To use them, you
- 16:47enter the string, a period, the method
- 16:49name, and open and close parentheses.
- 16:51Sometimes, content is placed inside the
- 16:52parentheses. For example, the variable
- 16:54` Letter` contains the word "Ha-ryu."
- 16:56Only the "Yu" part is capitalized.
- 16:57However, if you want to change the
- 16:58entire text to lowercase, there is a
- 17:00method called ` Lower`. By opening `
- 17:02Letter` and typing ` Da-go`, you can
- 17:04easily convert all characters to
- 17:05lowercase. Conversely, if you want to
- 17:07convert the entire content to uppercase
- 17:08, you can use the ` Upper` method. If
- 17:10you want to capitalize only the first
- 17:11letter and keep the rest lowercase, you
- 17:13can use the ` Capitalize` method. This
- 17:14results in the form of a standard
- 17:15English sentence. If you want to
- 17:17capitalize only the first letter of
- 17:18each word, you can use the ` Title`
- 17:19method. You can see that the first
- 17:21letter of each word has been
- 17:22capitalized, just like a newspaper
- 17:23headline. There is also a way to swap
- 17:25lowercase letters. You can use the `
- 17:27letter.dot.case` and ` case.case`
- 17:28methods. Then you can see that
- 17:29lowercase letters are converted to
- 17:30uppercase and uppercase letters to
- 17:31lowercase. If you want to split a
- 17:33string, you can use the ` split` method
- 17:35, which returns the result split by
- 17:37spaces as a list. We will learn about
- 17:39lists later, so let's just skip over it
- 17:41for now. To check how many times a
- 17:42character appears within a string—
- 17:44that is, how many times "hower" appears
- 17:45in the example—use the ` count`
- 17:47method. If you input the character "cha
- 17:49" like this, you can easily check the
- 17:50count. We learned these things this
- 17:51time, and since there are so many types
- 17:53of methods, we will continue next time.
- 17:59I mentioned that methods are functions,
- 18:06right? Yes. Let's continue learning the
- 18:08main methods. The variable ` s`
- 18:10contains the text "I am also a high
- 18:11school." To check if this string starts
- 18:14with "what problem" or "I am," there is
- 18:16` starts with`. You just need to put
- 18:17the value you want to check inside the
- 18:19parentheses here. The result will come
- 18:20out as either "true" or "false" in a
- 18:21split form. To check what value it ends
- 18:23with, there is ` ends with`. Yes. The
- 18:25usage is the same as Start. If there
- 18:27are unnecessary parts like dots at the
- 18:29beginning or end, you can use Strip to
- 18:31remove the unnecessary parts by placing
- 18:33the characters you want to remove
- 18:34inside the parentheses. Note that if
- 18:36you leave nothing inside the
- 18:37parentheses, unnecessary spaces at the
- 18:38beginning and end of the text will be
- 18:40removed. You can also intentionally
- 18:41change characters. You can easily do
- 18:43this by using Reply and inputting the
- 18:44problem to replace the character you
- 18:45are looking for. Now, we are finding "
- 18:47High School" and changing it to "High
- 18:48School." To check where a specific
- 18:50character is located, you can use the
- 18:51Find method and input a value; it will
- 18:53tell you its position based on the
- 18:54index. Since "School" is between 0, 1,
- 18:572, and 4, the value 4 is displayed. If
- 19:00you want to insert a string in the
- 19:01middle between other characters, you
- 19:03can use the Center method. Here, we
- 19:04defined the total length as "ten
- 19:06characters," and by using a hyphen, you
- 19:07can produce a result corresponding to
- 19:09the end of the string, such as wrapping
- 19:10"I too High School" with "my Hi." We
- 19:12have learned these things so far. There
- 19:14are still many more methods available;
- 19:16you can search for "Python built-in
- 19:17methods" on Google. If you visit the
- 19:19official website, press Ctrl + F, and
- 19:21search using Find, you will see results
- 19:23like this. If you scroll up a bit, you
- 19:25will find "String Methods." Below that,
- 19:28you will see various different methods
- 19:30with explanations of their functions,
- 19:31so it would be helpful to refer to this
- 19:33section. When the variable 'Python'
- 19:45contains 'Python' and the variable '
- 19:47Jabal' contains the value 'Jabal', to
- 19:48print both using ` print`, you would
- 19:50have done it like this using addition.
- 19:51We also added a space in the middle for
- 19:53separation. However, there is another
- 19:55way besides this: using commas. If you
- 19:57insert the variables or values you want
- 19:59to print, separated by commas, it
- 20:01automatically prints them with spaces.
- 20:03But what if you want to include
- 20:04variable values within another sentence
- 20:05, rather than just concatenating them
- 20:07as they are? For example, you can
- 20:08create a sentence like this. First, you
- 20:10can do it like this using addition.
- 20:12Here, the colored parts are the string
- 20:14values, which have been combined
- 20:15through addition. Alternatively, you
- 20:17can do it like this using the commas we
- 20:19learned earlier. However, since
- 20:20addition is output via commas, it looks
- 20:22complicated with many columns, doesn't
- 20:23it? In such cases, we can use the "
- 20:25string format." First, there is a
- 20:27method using curly braces and the
- 20:28format. When you have a statement like
- 20:30this, simply place parentheses inside
- 20:31the statement, put a dot after the
- 20:33string, write "format," and then put
- 20:34the value you want to output inside the
- 20:36parentheses. By doing so, the values
- 20:37inside the format will be assigned to
- 20:38the curly braces inside the string in
- 20:40order. The second method is similar to
- 20:41the first, but numbers are placed
- 20:43inside the curly braces. In this case,
- 20:44the numbers increase from 0 to 1, 2,
- 20:46and so on. If you write the statement
- 20:47this way, the values inside the format
- 20:48will be assigned to the positions of
- 20:49curly brace 0 and curly brace 1 in
- 20:51order. The difference from the first
- 20:52method is that you can determine the
- 20:54order using numbers rather than curly
- 20:55braces. For example, if you write curly
- 20:57brace 1 and curly brace 0 instead of
- 20:58curly brace 0 and curly brace 1, you
- 21:00can see that "Java" goes into the 1
- 21:01position and "Python" goes into the 0
- 21:03position, changing the order. Finally,
- 21:04there is something called an 'f-string'
- 21:06. You place an 'f' at the beginning of
- 21:07the string and write the characters
- 21:09within curly braces; you simply define
- 21:10the value you want to output directly
- 21:11inside these braces. In other words, if
- 21:13you write 'Python Java' like this, it
- 21:14directly outputs the value of the
- 21:15Python variable or the Java variable.
- 21:17It's incredibly convenient, isn't it?
- 21:29Consider this sentence: "My friend
- 21:31asked, 'Is learning Pi easy?'" If you
- 21:33look here, you see large down brackets.
- 21:35Yes. To print this sentence, using
- 21:36single down brackets at the beginning
- 21:38and end will ensure it prints correctly
- 21:39. What about this case? "I thought to
- 21:42myself, 'It's incredibly difficult.'"
- 21:44Here, there are single down brackets
- 21:45like this. In this instance, attempting
- 21:47to print using single down brackets
- 21:49will cause an error. Since a string is
- 21:50the part enclosed in single or large
- 21:52down brackets, the parts "I thought to
- 21:54myself" and "thought" are recognized as
- 21:57separate characters. In other words,
- 21:58the part "It's incredibly difficult" in
- 22:00the middle becomes an unreadable entity
- 22:01. In this case, wrapping the entire
- 22:03sentence in large down brackets instead
- 22:04of single down brackets solves the
- 22:05problem. But what about this case?
- 22:07However, thinking "I can't be the only
- 22:09one suffering," I said, "It's super
- 22:10easy." This sentence contains both
- 22:12small and large slashes. Since I
- 22:14started by writing the large slashes
- 22:15first, problems arose starting from the
- 22:17part that was "super easy" later on. In
- 22:18such cases, you can use escape
- 22:20characters. An escape character is used
- 22:21to indicate functions or problems that
- 22:23cannot be expressed by combining a
- 22:24backslash and a specific problem.
- 22:25Within the text, you simply write the
- 22:27backslash after the backslash like this
- 22:28. In other words, if you write this
- 22:30sentence like this, both the small and
- 22:31large slashes will be printed correctly
- 22:32without any issues. In this case, you
- 22:34can use either a small or large slash
- 22:36for the start and end of the print
- 22:37statement. You can also express a
- 22:38backslash using escape characters. In
- 22:40Windows, a folder path looks like this,
- 22:42but an error occurs if you try to print
- 22:43the value exactly as is. In this case,
- 22:45if you write backslashes twice, it will
- 22:46be printed correctly in this form. You
- 22:48can also create line breaks using the
- 22:49backslash N. When creating text that
- 22:51spans multiple lines, you could do this
- 22:52by using three slashes. If you change
- 22:54this using the escape character/N, you
- 22:56can see the result with a line break at
- 22:59the escape character. Python provides
- 23:11built-in data types for managing
- 23:13multiple pieces of data at once. One of
- 23:15them is the list, which we discussed a
- 23:16few times in previous lectures. Let's
- 23:18assume we go to a convenience store to
- 23:19buy an OS. We can easily carry just one
- 23:21OS. But then we see a Mon Cher next to
- 23:23it. We should buy a Mon Cher too, right
- 23:25? Well, since we have two hands,
- 23:27carrying the Mon Cher isn't a big
- 23:28problem so far. But then we see a Choco
- 23:31Pie next to it. When I was in the
- 23:32military, I used to make a cake out of
- 23:34Choco Pies to add to my birthday party.
- 23:36That memory came back to me after a
- 23:37long time, so I'll buy about three
- 23:38Choco Pies. However, trying to carry a
- 23:40total of five items, it looks like I
- 23:41don't have enough hands. At this point,
- 23:43the convenience store employee asks, "
- 23:44Is it good? It will definitely be
- 23:46easier to carry if you have an empty
- 23:47bag." So, we put everything into a
- 23:48plastic bag and carry it comfortably
- 23:50with one hand. The list we will learn
- 23:51about this time is exactly like this
- 23:52plastic bag. While variables allow us
- 23:54to store a single value, lists can
- 23:56store multiple values. They are
- 23:58generally used to manage related,
- 24:00consecutive data. You can declare a
- 24:02list by separating the values you want
- 24:04to store inside with commas, like this.
- 24:05You just need to put the chocolate
- 24:06breads into a list called "My List" in
- 24:08order. Oh, I mentioned there are three
- 24:09chocolate files, right? Since lists
- 24:11allow duplicate values, you just need
- 24:13to write "chocolate file" three times
- 24:14like this. You can also mix and match
- 24:16any values inside the list, such as
- 24:18numbers, separated strings, etc. If you
- 24:20want to create a list of four plastic
- 24:21bags with no values, you simply write "
- 24:23List". It becomes an empty plastic bag.
- 24:37I mentioned that a list is like a
- 24:38plastic bag that can hold multiple
- 24:40values. Now, let's look at how to use
- 24:42lists. When a list is declared like
- 24:44this, to check the value corresponding
- 24:45to a specific index, you simply insert
- 24:47the index into the list, just like with
- 24:49strings, and the value at that position
- 24:50is immediately displayed. This is
- 24:52possible because the order of values in
- 24:54a list is guaranteed. For example, the
- 24:56first is OS, the second is Mongsil, and
- 24:57so on. So, strictly speaking, a list is
- 24:59not a random plastic bag where you just
- 25:01throw things in randomly; rather, it is
- 25:02a special plastic bag with internal
- 25:04compartments that manages items neatly
- 25:05according to the order in which they
- 25:06are inserted. Because the order is
- 25:08guaranteed, slicing is also possible.
- 25:09To cut from a specific point to a
- 25:11specific point, you simply insert the
- 25:12starting value through the colon, just
- 25:14like with strings. The result will be
- 25:15in the form of a list. To check what
- 25:17values are contained in the list, you
- 25:19use ` in`. Yes. The result is in a
- 25:21separated form; it returns true if
- 25:22there are values, and false otherwise.
- 25:23To check how many values are in the
- 25:25list, you can use the ` ran` function.
- 25:27Here, there are three values. Lists can
- 25:29be freely modified. To modify a value,
- 25:32you specify an index like this and
- 25:33insert a new value, just as you would
- 25:35change a variable. You can also add a
- 25:37new value after the list main using the
- 25:39` ahand` method, or delete it by
- 25:40entering the value to be removed inside
- 25:42the parentheses using ` remove`. To
- 25:43merge values from different lists, you
- 25:45can use the ` extend` method. Please
- 25:47note that there are various other
- 25:49methods available in addition to those
- 25:50introduced. There is also a built-in
- 26:02data type called a tuple that can
- 26:04manage multiple data items, similar to
- 26:05a list. With lists, values were placed
- 26:07inside parentheses and separated by
- 26:08commas. Tuples, however, place values
- 26:09inside parentheses rather than
- 26:10parentheses and separate them by commas
- 26:12. The biggest difference between lists
- 26:13and tuples is that once a tuple is
- 26:15created, it cannot be modified. When
- 26:17studying lists, let's assume you first
- 26:18buy OS and Mongsil at a convenience
- 26:20store, put them in a plastic bag, and
- 26:22go home. If you suddenly want to eat a
- 26:24Choco Pie and return to the store to
- 26:26buy one, you would simply put the Choco
- 26:28Pie into the plastic bag. However, as
- 26:30mentioned, tuples cannot be modified.
- 26:32It is similar to attaching a "Do Not
- 26:34Open" sticker to a plastic bag after
- 26:35payment; once you buy OS and Mongsil
- 26:37and the sticker is attached, you cannot
- 26:39buy Choco Pies later, even if you want
- 26:41to. Furthermore, since OS and Mongsil
- 26:42cannot be removed from the bag, a
- 26:44refund is also impossible. Tuples
- 26:45cannot be modified like this, but aside
- 26:47from that, they are very similar to
- 26:48lists, so you can understand them as
- 26:49lists dedicated to periodicity. You can
- 26:51define a tuple like this using
- 26:52parentheses. Just like lists,
- 26:54duplicates are allowed. You can put any
- 26:56data type into it. Except for
- 26:58modification, it is also similar to a
- 26:59list in terms of usage. You can access
- 27:01values at any position, and the order
- 27:03of elements is guaranteed. It is also
- 27:05possible to print only the desired
- 27:06amount using slashing. You can check
- 27:08inclusion using the ` include` keyword,
- 27:09and you can also check the length.
- 27:11Tuples also have these methods. Let's
- 27:24continue studying tuples. You can
- 27:26create a tuple by writing values and
- 27:28inserting them separated by commas.
- 27:30This process is called packing. It
- 27:31means putting the values highlighted in
- 27:33orange on the right into the 'My Tuple'
- 27:35on the left, just like putting an empty
- 27:37envelope. However, Python also has a
- 27:38concept that is the opposite of this.
- 27:40This is called unpacking. The contents
- 27:43of the 'my tuple' are taken out one by
- 27:44one and stored in the variables Pi 1,
- 27:46Pi 2, and Pi 3, respectively. It
- 27:47becomes exactly the same as code that
- 27:49assigns values to individual variables
- 27:51in this way. You can use asterisks
- 27:52during unpacking. If a tuple called '
- 27:54Numbers' is defined with values from 1
- 27:55to 10, writing the code like this would
- 27:57result in a mismatch between the number
- 27:58of elements in 'Numbers' and the number
- 27:59of variables. However, by using an
- 28:01asterisk, it means to put the first
- 28:03value (1) from 'Numbers' into the
- 28:05original variable, 2 into the variable
- 28:072, and all the rest into 'Others'.
- 28:09Please note that 'Others' contains
- 28:10multiple values but is in the form of a
- 28:12list, not a tuple. You can change the
- 28:13position of the asterisk depending on
- 28:14the exam requirements. If you place the
- 28:16asterisk at the very beginning of the
- 28:17variable like this, 9 will be assigned
- 28:18to the value 9, 10 to the value 10, and
- 28:20the remaining values—from 1 to 8—
- 28:21will be assigned to the three 'Others'.
- 28:23If you place a star between other
- 28:24variables, the original variable will
- 28:26contain 1 and 10, while the variable 10
- 28:27will contain 10, and the values in
- 28:29between will be from 2 to 9. There is
- 28:34something called a Set. Like lists and
- 28:43tuples, it can store multiple pieces of
- 28:46data. Sets are characterized by the
- 28:47fact that the order of the values is
- 28:48not guaranteed and duplicates are not
- 28:49allowed. Let's say you have decided to
- 28:51meet your friends for a meal. You are
- 28:52to decide the menu, but first, let's
- 28:54list what foods your friends like.
- 28:56Friend A says they like pork cutlet,
- 28:58bossam, and Jeu tteokbap. Friend B says
- 29:00they like jjamppong, sushi, and Jeu
- 29:02tteokbap. Now, which menu would be a
- 29:04safe bet? That's right. If you choose
- 29:06Jeyuk Deopbap, both friends will
- 29:08probably enjoy it. You can declare a
- 29:09Set like this. You put the values
- 29:11inside the curly braces separated by
- 29:13commas. First, for convenience, I will
- 29:15name Friend A with a capital A and
- 29:16create a Set of their favorite foods. I
- 29:18will declare Friend B like this as well
- 29:19. We already know the answer to what
- 29:21food the two friends like in common,
- 29:23but let's give it a try. There is a
- 29:24method called Intersection, which
- 29:26allows us to easily utilize the "Jeu"
- 29:27bait. However, actually, you all like
- 29:29all the foods listed here. Today, your
- 29:32friends told you to choose the food you
- 29:33want to eat when you grow up as the
- 29:34main character. Then, all the foods
- 29:36that the two friends like could be
- 29:37candidates, right? Yes. Let's combine
- 29:39all the foods using the Union method.
- 29:41You can use the Union method like this.
- 29:43Since sets do not guarantee order, the
- 29:45output result might be slightly
- 29:46different from this. Also, since
- 29:47duplicates are not allowed, we can see
- 29:49that Jeyuk Deopbap appears only once.
- 29:50However, suddenly, Friend A says they
- 29:52can't really eat other foods except for
- 29:54the ones they like. Also, they suggest
- 29:56that they want to eat Jeyuk without the
- 29:57rice because they have been eating too
- 29:59much rice with it recently. So, we have
- 30:00no choice but to deduce the food that
- 30:02only A likes. Since they said they
- 30:03don't want to eat Jeyuk, which they
- 30:05like in common with B, we can use the
- 30:06difference of sets—that is,
- 30:07subtracting Set B from Set A. In this
- 30:09case, you can use the ` difference`
- 30:10method. This extracts the values found
- 30:12only in Set A, subtracting the values
- 30:14from Set B. I mentioned that the order
- 30:28of values in a set is not guaranteed.
- 30:29This means that in a list, we could
- 30:31access the first value using an index
- 30:33or directly modify the value at index
- 30:350. Even with tuples, while value
- 30:36modification was impossible, accessing
- 30:40values via an index was possible.
- 30:43However, since the order of a set is
- 30:44not guaranteed, you cannot access
- 30:45values via an index. This is a
- 30:48significant difference from lists and
- 30:48tuples. However, that does not mean it
- 30:51is read-only like a tuple. For example,
- 30:53let's say a friend originally thought
- 30:54they would only eat three dishes, but
- 30:55after trying Dakgalbi (spicy stir-fried
- 30:56chicken) once, they found it delicious.
- 30:58This means the number of dishes they
- 30:59can eat has increased by one. In that
- 31:01case, they can add a value using the `
- 31:02add` method. Or, if they have eaten too
- 31:04much Jeyuk Deopbap (spicy pork rice
- 31:06bowl) and are sick of it and can't eat
- 31:07any more, they can remove a value using
- 31:09the ` remove` method. Alternatively, if
- 31:11you decide to go on a diet and stop
- 31:12eating all food in the future, you can
- 31:14use the ` clear` method to completely
- 31:15erase all values. Note that doing just
- 31:18this makes ` myset` an empty set.
- 31:20However, if you type ` set` followed by
- 31:22a space (D), which signifies delete, it
- 31:24completely erases the ` myset` set
- 31:26itself. Consequently, you will not be
- 31:27able to use ` myset` at all in the
- 31:28future. This applies equally to lists
- 31:30and tuples. Sets also have these kinds
- 31:32of methods. The last data type we will
- 31:46learn is the dictionary. The word means
- 31:47"statement" or "arbitrary." When we
- 31:49look up a word in a dictionary, it is
- 31:50usually structured like this: there is
- 31:52the word, and its meaning is always
- 31:53included. It is similar in Python. If
- 31:56you are curious about the meaning of
- 31:57the word "study," you would look up "
- 31:58study" in the image. In this case, "
- 32:00study" is called the key, and the part
- 32:01explaining the meaning is called the
- 32:03value. To summarize, a dictionary
- 32:05always consists of key-value pairs.
- 32:07However, duplicate keys are not allowed
- 32:09. Dictionaries are created using curly
- 32:11braces like this, separating keys and
- 32:12values with colons. This constitutes a
- 32:14single piece of data. Then, you
- 32:15separate the next data with commas,
- 32:17writing "Key 2, Value 2," and so on,
- 32:19continuing to write colon-separated
- 32:20data. For example, let's say there is a
- 32:23person. Name, age, height, and weight
- 32:24correspond to height, and the actual
- 32:26values are the values. You can express
- 32:29this in code like this. However,
- 32:30writing it out makes it a bit long and
- 32:32difficult to read. In that case, it is
- 32:34fine to write it using line breaks like
- 32:35this. In the example, the problem is
- 32:37structured as a table containing all
- 32:39key values, but keys can also be
- 32:40defined in other forms, such as numbers
- 32:41. You just need to avoid duplicates.
- 32:44Also, regarding the value part, while
- 32:45keys are characters and other values
- 32:47are integers, dictionaries can use
- 32:49numbers, characters, delimiters, lists,
- 32:51tuples, and anything else as values. To
- 32:53access data defined in this way, you
- 32:55simply insert the key inside. In other
- 32:58words, to print the value corresponding
- 32:59to the key 'Name', you do it like this.
- 33:01To print what corresponds to the key '
- 33:03...', you can do it like this. Let's
- 33:15look at how to use dictionaries step by
- 33:17step. First, when we declared a
- 33:18dictionary named 'Person', we tried
- 33:20checking the value corresponding to a
- 33:21key by inserting the key through the
- 33:23reference like this to retrieve the
- 33:24corresponding value. However, an error
- 33:26occurs if you try to access the key '
- 33:28Alias'. This is because the 'Alias' key
- 33:29does not exist in the dictionary. In
- 33:31this case, if you use the 'get' method,
- 33:33it attempts to access the 'Alias' key
- 33:34in the same way, but even if the key
- 33:36value does not exist, no error occurs
- 33:37and 'None' is printed. Of course, if
- 33:39the key value exists, the corresponding
- 33:40value will be displayed. To add new
- 33:42data, you simply insert a new key into
- 33:43the reference and define the value. If
- 33:46you want to change the value
- 33:47corresponding to a specific key, you do
- 33:48it in the same way. In other words, if
- 33:50it is a new key, the value is modified;
- 33:51if it is an existing key, the value is
- 33:53modified. To change the values
- 33:54corresponding to multiple keys rather
- 33:55than just one, use the update method.
- 33:57Inside the parentheses, define the keys
- 33:59to be changed and the values to be
- 34:00updated in a dictionary format. Here,
- 34:02we are updating the height to 130 and
- 34:04the weight to 26. To delete specific
- 34:07key-value data, use the pop method and
- 34:09pass the key value you want to delete.
- 34:11To delete all data, use clear, just
- 34:13like with other data types. To check
- 34:16which keys are in the dictionary, use
- 34:18the keys method. To check which values
- 34:21are present, use the values method.
- 34:24There is also a way to print all
- 34:25key-value pairs; by using the items
- 34:27method, you can view all data where the
- 34:29key and value are separated by commas.
- 34:31Dictionaries also have these features.
- 34:44So far, we have learned about four data
- 34:46types for managing multiple data at
- 34:48once. Let's compare the characteristics
- 34:50of each data type. First, for lists,
- 34:52you convert them to a list and insert
- 34:53values separated by commas. If you
- 34:56insert them exactly as they are, you
- 34:57can add as many duplicate values as you
- 34:58want. Since order is guaranteed, you
- 35:00can access them by inserting an index
- 35:02inside the list. Slashing is also
- 35:04possible. You can directly modify
- 35:05values through indexing, add values
- 35:07using methods like these, and delete
- 35:09them. Tuples can be declared using
- 35:12parentheses. Like lists, order is
- 35:14maintained and duplicate values are
- 35:16allowed. Accessing them works in the
- 35:17same way. However, tuples are read-only
- 35:19, so modification, addition, and
- 35:20deletion are impossible. This is a very
- 35:22important characteristic of tuples.
- 35:23Sets are declared using curly braces.
- 35:25Unlike lists and tuples, the order of
- 35:27values is not guaranteed. Also, since
- 35:29duplicates are not allowed, even if the
- 35:30same value is inserted multiple times,
- 35:32that value exists only once within the
- 35:33set. Because order is not guaranteed,
- 35:35access via index is impossible. Since
- 35:37direct access is impossible, you cannot
- 35:39directly modify any value within the
- 35:40set either. However, it is possible to
- 35:42add or delete new values within the set
- 35:43itself. Finally, dictionaries are
- 35:45declared using curly braces, and
- 35:46key-value pairs always follow.
- 35:48Additionally, the order of the input
- 35:50values is guaranteed. For reference,
- 35:51order guarantees began in Python
- 35:53version 3.7 and above. Key values do
- 35:55not allow duplicates. Access is done by
- 35:57key, not index, and values can be
- 35:59modified after access. New key-value
- 36:01pairs can be easily added and deleted
- 36:03in this way. While there are various
- 36:05scenarios during development, here is a
- 36:06brief summary of when to use each data
- 36:08type: If you need to manage multiple
- 36:10values in order, simply use a list. Use
- 36:12a tuple if the data will not change
- 36:14once created, or if you want to prevent
- 36:16accidental changes during program
- 36:17execution. Use a set if verifying the
- 36:19existence of a specific value is
- 36:21important and the data does not allow
- 36:23duplicates. If you want to manage data
- 36:25efficiently through keys, use a
- 36:26dictionary consisting of key-value
- 36:28pairs. I advised using tuples when
- 36:41values do not need to change or should
- 36:43not change. This is because tuples are
- 36:45a dedicated data type that cannot be
- 36:46modified, added, or deleted. However,
- 36:48there is actually a way to modify
- 36:50tuples. If you have declared a tuple
- 36:52like this and want to add "Choco Pie,"
- 36:54you can write a list and wrap it in
- 36:55parentheses to convert it into a list.
- 36:57First, define this list as "Mile List."
- 37:00Add a value using the "Ad" values
- 37:01provided by the list, and then wrap "
- 37:03Mile List" again in tuple parentheses
- 37:04to change the data type back to a tuple
- 37:06. You then simply need to put this into
- 37:08"My Tuple." To summarize, to convert a
- 37:10tuple into a list or a list into a
- 37:12tuple, you simply wrap each data type
- 37:13in tuple parentheses or list
- 37:15parentheses like this. Lists allow
- 37:17duplicate values, but sometimes you
- 37:18need to remove duplicate values. You
- 37:21can do this like this: if you have a
- 37:22list that contains "Choco Pie" three
- 37:24times, you can convert it into a set by
- 37:25folding the set and placing the list
- 37:27inside the parentheses. Since sets do
- 37:29not allow duplicates, duplicate values
- 37:30like "Choco Pie" will disappear during
- 37:32this process, leaving only one out of
- 37:33the three. Then, you can convert it
- 37:35back to a list. However, while sets do
- 37:37not allow duplicates, they do not
- 37:39guarantee order either. Therefore, if
- 37:40you print the values, the order might
- 37:42be jumbled like this. If the order is
- 37:44not important for the list, you can
- 37:45just use it as is, but if order is
- 37:47critical, you should not convert it to
- 37:48a set. In that case, there is another
- 37:50method: using a dictionary. A
- 37:52dictionary guarantees order while not
- 37:54allowing duplicate keys. So, if you
- 37:56have a list like this, entering `
- 37:58dict.fromkeys` and placing ` mylist`
- 38:00inside the parentheses creates a new
- 38:01dictionary with keys containing "OS
- 38:03Mongsil Choco" files, with duplicates
- 38:05removed from the list. Note that the
- 38:07values in this case are all null.
- 38:08Furthermore, if you wrap this
- 38:10dictionary in a list, you can extract
- 38:11only the key values and convert them
- 38:13back into a list. This way, the order
- 38:14from when the list was initially
- 38:16declared is guaranteed. In this way, we
- 38:18have studied how to swap data types
- 38:20with different characteristics. Up
- 38:34until now, the code we have written has
- 38:35always executed statements one by one
- 38:37in order from top to bottom. Of course,
- 38:39if we commented them out, those
- 38:40statements were ignored. However, when
- 38:42creating programs, it is not always
- 38:44necessary for every statement to be
- 38:45executed. You might want one piece of
- 38:47code to run in one case and another in
- 38:49another. That is where ` if` statements
- 38:51come in handy. ` if` statements allow
- 38:53you to branch based on a condition like
- 38:55"if something is true." You can ensure
- 38:57that a statement is executed only when
- 38:59the condition is true. If the condition
- 39:01is false, the ` if` statement will not
- 39:03be executed. In code, you can write it
- 39:06in this form: ` if`, write the
- 39:07condition, and add a colon at the end.
- 39:10Then, on the next line, write the
- 39:11statement to be executed when the
- 39:12condition is true. However, please note
- 39:14that you must indent the line before
- 39:16the condition. Indentation is usually
- 39:18done by pressing the spacebar four
- 39:19times or the Tab key once. In Visual
- 39:21Studio Code, if you type a colon after
- 39:23a condition and press Enter, it
- 39:24automatically indents. I wrote the code
- 39:26like this. If you were a student, you
- 39:28would need to study, but what do you do
- 39:29if it is Sunday? You would play a game
- 39:31first and then start studying. So, if
- 39:33the variable 'Today' is Sunday, the
- 39:34condition is true, so you play a game
- 39:36first and then start studying. However,
- 39:39if today is not Sunday—if it is
- 39:40Tuesday—the 'if' condition is false,
- 39:42so you should start studying
- 39:43immediately without playing a game.
- 39:45That is, this statement part—yes,
- 39:46right here. —is not executed, and the
- 39:48program moves directly to the next
- 39:49statement. There is also this form: if
- 39:51the condition is true, this statement
- 39:53is executed, and if the condition is
- 39:55false, that statement is executed. You
- 39:57choose one of the two based on the
- 39:58condition. In code, you simply write '
- 40:00if' below the conditional statement and
- 40:02then a colon. For the statement below
- 40:05that, you should also indent it before
- 40:07writing it. By doing this, if the 'if'
- 40:08condition is satisfied, this statement
- 40:10is executed; otherwise, that statement
- 40:11is executed, and the program moves on
- 40:12to the next statement. For example, if
- 40:14the code looks like this, if today is
- 40:16Sunday, you will play a game and then
- 40:18start studying. In this case, the ` ss`
- 40:20part will not execute at all. If today
- 40:22is not Sunday, the ` if` part will be
- 40:23ignored, and the ` else` part will
- 40:25execute, so you will use your
- 40:26smartphone for exactly 5 minutes before
- 40:27starting to study. Earlier, I explained
- 40:30that ` if` means "if something happens.
- 40:32" You can understand ` if else` like
- 40:33this: "If something happens, the ` if`
- 40:35statements execute," and "If it doesn't
- 40:38happen, the ` if else` statements
- 40:39execute." Let's review briefly. ` if`
- 40:52means "if something happens," so the A
- 40:53statements are executed. ` if s` means
- 40:55"if something happens, if the four `
- 40:57if` statements don't happen, then the s
- 40:59statements execute." However, there is
- 41:01one more thing here: ` l if`. ` l if`
- 41:04is used to check another condition
- 41:05again if the preceding condition is not
- 41:08true. You can insert as many ` l if`
- 41:09statements as you want between ` if`
- 41:11and ` l`. If the initial ` if`
- 41:12condition is false, the orange ` LF`
- 41:14condition is checked; if the orange is
- 41:15false, the green condition is checked;
- 41:16if the green is false, the blue
- 41:17condition is checked; and if the blue
- 41:19condition is false, the ` L` condition
- 41:20is executed. Note that while ` LF` is
- 41:22optional, the ` L` condition does not
- 41:23always have to be present when using
- 41:25this part. Therefore, you can omit the
- 41:27` L` condition if it is not needed. I
- 41:28have set up a flow using ` if L if L`.
- 41:30If the ` if` condition is true, this
- 41:32statement is executed, followed by the
- 41:34next statement; if it is false, the `
- 41:36LF` condition is checked; if it is true
- 41:37, that statement is executed, followed
- 41:39by the next statement; and if even that
- 41:41is false, it executes that statement
- 41:43and moves on to the next statement.
- 41:45Looking at the code, the first ` if`
- 41:46contains the first condition. Below
- 41:48that, you write ` L if flow` and write
- 41:50the second condition in the same format
- 41:51. After that, you can add the ` L`
- 41:53condition to handle the action when
- 41:54none of the conditions listed above are
- 41:56satisfied. In other words, with the
- 41:58code like this, if today is Sunday, it
- 42:00works like this: you play just one game
- 42:02, exit this entire section, and start
- 42:03studying. If today is Saturday, the
- 42:06first condition is not satisfied, so it
- 42:07moves to the next condition. Since the
- 42:09next condition is satisfied, the LFNA A
- 42:11code is executed, and the program exits
- 42:13. In other words, this allows you to
- 42:14take out your phone for just 5 minutes
- 42:16and start studying. If today is neither
- 42:18Sunday nor Saturday, neither the if nor
- 42:19the Lif conditions are satisfied, so
- 42:21the ELS part is executed, meaning you
- 42:23will have to drink a glass of water and
- 42:24start studying immediately. Remember
- 42:26that Lif can be added as many times as
- 42:28you want between L and S. Now, let's
- 42:41learn about nested if statements. Yes.
- 42:42Nested statements, as the name suggests
- 42:43, are cases where one statement is used
- 42:45within another statement. As you can
- 42:46see in the diagram, if the first if
- 42:48statement is true, this statement is
- 42:49executed first, and then the second if
- 42:51statement highlighted in yellow is
- 42:52checked. If the second if statement is
- 42:54true, that statement is executed, but
- 42:56if the second if statement is false,
- 42:57that statement is not executed, and the
- 42:59program moves directly to the next
- 43:00statement. In code, you write this
- 43:02statement after the line following the
- 43:04if statement. The nested statement on
- 43:07the next line should be written after
- 43:09being indented by the same size as this
- 43:11statement. Please note that the
- 43:13statement to be executed when the
- 43:15nested if is true must be indented by
- 43:17this amount relative to the nested if.
- 43:20When committing a foul during a soccer
- 43:21match, depending on the situation, a
- 43:23warning may be given, but if the
- 43:24offense is severe, a yellow or red card
- 43:26may be issued. Receiving a red card
- 43:28results in immediate expulsion, but
- 43:29receiving two yellow cards results in
- 43:30expulsion due to accumulated warnings,
- 43:32just like a red card. Let's try to find
- 43:34a method to handle a player differently
- 43:35when they receive a yellow card,
- 43:37depending on the number of yellow cards
- 43:38accumulated so far. First, there is a
- 43:40player. This player's yellow card count
- 43:42is initially 0, since they have never
- 43:43received a warning. However, they
- 43:44committed a foul while playing the game
- 43:46. It says "Troll" here, right? Yes. For
- 43:48convenience, let's call it a foul
- 43:49equivalent to a yellow card. On the
- 43:51next line, we check for foul status
- 43:52through this part. If it is confirmed
- 43:54to be a foul, you would receive one
- 43:55yellow card. If we represent the
- 43:56current situation graphically, it looks
- 43:58like this. Now, let's add code to check
- 44:00the total number of yellow cards; if
- 44:01there is one, the game continues, and
- 44:03if there are two, the player is ejected
- 44:04. Nesting is required here. After
- 44:07writing the same amount of text, I will
- 44:09write another statement. If it is
- 44:11confirmed that there are two yellow
- 44:13cards, I will indent the next line
- 44:14again and print the phrase "Ejected due
- 44:16to accumulated warnings." Visually, it
- 44:18would look like this. If there are two
- 44:20yellow cards, it prints "Ejected due to
- 44:22accumulated warnings" when the
- 44:23condition is true. If this is the first
- 44:25time receiving a yellow card, it is not
- 44:26an ejection, but the player should be
- 44:27careful going forward. So, I write "
- 44:29Else" and handle the case where there
- 44:30are not two yellow cards like this.
- 44:32Visually, it would look like this.
- 44:33However, if it wasn't a foul—yes, if
- 44:35the foul variable was "Foul"—the code
- 44:37inside here would not execute at all.
- 44:39In other words, if it is a foul that
- 44:41doesn't warrant a yellow card but
- 44:42merely gives a warning, I will use an
- 44:44Else statement at the same position as
- 44:46the first If statement to simply print
- 44:47"Yes, warning." It looks like this in
- 44:49the diagram. Yes. If it is not a foul,
- 44:51it just gives a warning. After that,
- 44:53the subsequent statements outside the
- 44:55second If statement will be executed.
- 44:57In this way, we have learned about
- 44:58nested If statements, which allow us to
- 45:00check another condition when a certain
- 45:01condition is met. Imagine a teacher
- 45:14having the students do floats to warm
- 45:15up during after-school class. However,
- 45:17the teacher doesn't keep saying "Float
- 45:20floats, fly floats, fly floats" every
- 45:22time they do them. Usually, the teacher
- 45:24just says, "Start with 10 floats." Then
- 45:26, the students repeat the same float
- 45:28motion ten times. Programs work the
- 45:30same way. Let's try printing a
- 45:32statement using a Print statement. To
- 45:34print the same statement ten times, you
- 45:35would simply copy and paste it ten
- 45:36times, right? However, if you want to
- 45:38print 1,000 times instead of ten, it
- 45:40seems that a simple loop won't suffice.
- 45:41This is where the ` print` statement
- 45:43comes in. There are various types of
- 45:45loops, but let's learn the most basic
- 45:46one first. The syntax is as follows:
- 45:48You write the loop variable (the range
- 45:50or target), add a colon, indent the
- 45:52next line, and then write the
- 45:54statements to be executed repeatedly.
- 45:56Let's look at an example. Yes. We use
- 45:58the range function 4x, input 10, and
- 45:59then write a colon. On the next line,
- 46:02we write ` print 8 times`. Here, range
- 46:0410 is used to mean repeating a total of
- 46:06ten times, so this statement is 100%
- 46:08identical to the statement on the right
- 46:10. We have produced the same output
- 46:12result in just two lines using the `
- 46:13print` statement. Let's modify the
- 46:14statement slightly. Here, I will use
- 46:16the part I wrote as 'x' inside the `
- 46:18print` statement. The value of 'x'
- 46:19changes every time the loop runs
- 46:21through range 10. If you run the code,
- 46:23you can see that the value of x changes
- 46:24from 0 to 9. A range serves the
- 46:26function of generating numbers within a
- 46:28specific range. So, if you use ` range
- 46:3010`, you can consider it to generate
- 46:32numbers from 0 to 9 (inclusive of 0 and
- 46:34exclusive of 10). If you use ` range
- 46:3610` (4x), you can understand it as
- 46:38assigning the numbers from 0 to 9 to x
- 46:40one by one in order. For reference, X
- 46:42is just a name I gave it; you can
- 46:44freely use any name you want, just like
- 46:46a regular variable. We used ranges when
- 46:59learning about loops. I mentioned that
- 47:01writing ` range 10` generates numbers
- 47:03from 0 to 10 (inclusive of 10). Here,
- 47:05the value 10 represents the stop. In
- 47:07other words, it generates numbers from
- 47:090 to less than the stop. If you write
- 47:11it like this in a loop and run it, it
- 47:12will output numbers from 0 to 9.
- 47:14However, unlike computers, in our daily
- 47:15lives, we start counting from 1. In
- 47:17such cases, you use the range function
- 47:18slightly differently. By entering two
- 47:20values separated by a comma, the first
- 47:22value becomes the starting value and
- 47:24the second becomes the ending value,
- 47:25allowing you to generate numbers from '
- 47:27start' up to 'stop'(exclusive). For
- 47:29example, if you input 1 and 10 into the
- 47:31range function sequentially, it creates
- 47:32numbers from 1 (inclusive) to '10'(
- 47:34exclusive) starting from 1 instead of
- 47:350. However, there is actually one more
- 47:37value that can be included when using
- 47:38range: 'step'. Specifying a step
- 47:40increment increases the interval
- 47:41between numbers by that amount. There
- 47:45are times when we need numbers with
- 47:46regular intervals, such as odd, even,
- 47:48or multiples of 3. If you use range 1
- 47:5010 2, it creates numbers from 1 up to '
- 47:5210'(exclusive), meaning that each digit
- 47:55increases by that amount. This allows
- 47:58you to generate odd numbers like 1 3 5
- 48:007 9. If you change the start value to 2
- 48:02, you can create numbers consisting of
- 48:04even numbers, such as 2, 4, 6, and 8,
- 48:06starting from 1 and increasing by that
- 48:08amount. If you use the range 3, 10, and
- 48:113, you can create numbers such as 3, 6,
- 48:13and 9, which are multiples of 3,
- 48:15starting from 3 and increasing numbers
- 48:21less than 10 by 3. The for loop can be
- 48:24used in this form, iterating through a
- 48:28loop range or target to repeatedly
- 48:31execute the statements within the loop.
- 48:36Previously, we specified the loop range
- 48:37using the range, but this time, let's
- 48:39utilize the loop by using the loop
- 48:41target. Lists, tuples, and dictionaries
- 48:42, which we learned about when studying
- 48:44data types, can be used for the loop
- 48:45target. If you have a list called '
- 48:48myList', you can use the loop target
- 48:49like this: 4 x myList colon. Now, the
- 48:52values 1, 2, and 3 from 'myList' are
- 48:54taken out one by one in order and put
- 48:56into x. If you perform the calculation,
- 48:59the result is 1 2 3. Tuples can be used
- 49:01just like lists, and the result of the
- 49:02example is the same as 'My List'.
- 49:04Dictionaries are a bit different. When
- 49:06a dictionary named 'Person' is declared
- 49:08like this, since dictionaries consist
- 49:10of key-value pairs, if you iterate over
- 49:12'PersonValues', the parts corresponding
- 49:14to the values in the dictionary 'Person
- 49:16' will be printed. If you input 'Key'
- 49:18instead of 'Values', the parts
- 49:19corresponding to the keys will be
- 49:20printed. Alternatively, if you use '
- 49:22Items', the key and value are returned
- 49:24together, allowing you to print
- 49:26everything. Please note that in this
- 49:28case, to receive both 'q' and the
- 49:30values, the variable part after '4d'
- 49:32contains 'k' for the 'q' and 'v' for
- 49:34the values, separated by a comma. In
- 49:36addition to these data types, general
- 49:37strings can also be used as targets for
- 49:39loops. If you declare the string 'Apple
- 49:41' in a variable named 'Pro' and run it,
- 49:43you can see that the characters within
- 49:45the string are printed one by one.
- 49:58Python loops also include something
- 49:59called ` while`. By definition, it
- 50:01means "while doing something." However,
- 50:03` while` is slightly different in
- 50:04nature. While ` while` iterates through
- 50:06a defined range or data, ` while`
- 50:08continues to repeat as long as the
- 50:10condition is true. For example, let's
- 50:11say you have decided to travel by plane
- 50:13. You would need to pack your luggage.
- 50:15Looking at your flight ticket, it says
- 50:17that checked baggage is allowed up to a
- 50:18maximum of 25kg. In this case, we
- 50:20usually check the weight while packing
- 50:22at home. We would pack items up to the
- 50:24limit of 25kg and then lock the
- 50:25suitcase. For convenience, let's assume
- 50:28your total luggage weighs 3kg. Then,
- 50:30you could fit up to eight pieces of
- 50:32luggage within the 25kg limit. Let's
- 50:34implement this in code. First, to use `
- 50:37Y`, enter the condition after ` Y` and
- 50:39write a colon. Below that, indent the
- 50:41text and write the statement to be
- 50:43executed repeatedly. Let's declare
- 50:44three variables first. I set 'Max',
- 50:46which represents the maximum allowable
- 50:47weight, to 25. For 'Weight', which
- 50:48represents the current carrier weight,
- 50:49I will start with 0. I will also fix
- 50:50the weight of each item in the 'Item'
- 50:52variable to 3. Next, I will use a while
- 50:56loop. You simply need to put a
- 50:58condition after 'while'; here, I will
- 51:00check if the sum of the current carrier
- 51:01weight ('Weight') and the weight of the
- 51:03items is less than or equal to the
- 51:04maximum allowable weight. By writing it
- 51:06this way, I am checking whether it is
- 51:07safe to add more items to the carrier.
- 51:09If this condition is true, I will add
- 51:10the items to 'Weight' and then print
- 51:12that items have been added using a
- 51:14print statement. Then, the while loop
- 51:15will continue to execute until the
- 51:17moment it can no longer add items—
- 51:19that is, when the condition becomes
- 51:20false—the loop will exit. Finally, I
- 51:22will print the total weight and
- 51:24terminate the program. Running the code
- 51:26, I can see that a maximum of eight
- 51:28items can be added, and the total
- 51:29weight at that point is 24kg. When you
- 51:42go to the gym, people usually do cardio
- 51:44exercises like using a treadmill or
- 51:45stationary bike first, followed by
- 51:46strength training. I find that I start
- 51:47sweating after using the treadmill for
- 51:49about 20 minutes. However, treadmills
- 51:50have a red emergency stop button. You
- 51:53can press this button if you need to
- 51:54stop the machine suddenly due to an
- 51:56emergency, even if 20 minutes haven't
- 51:58passed yet. Similarly, loops have
- 51:59something like this emergency stop
- 52:00button. It is called a "Break." A Break
- 52:02serves to immediately halt the action
- 52:04currently being executed within a loop
- 52:06and exit the loop. It is usually used
- 52:08in conjunction with an "If" conditional
- 52:09statement. For example, let's assume
- 52:10there is a drama series with seasons 1
- 52:12through 5. Since this drama is in a
- 52:14list format, you can use a loop to
- 52:15watch it in order starting from season
- 52:171. However, a friend who watched the
- 52:19drama first says that seasons 1 and 2
- 52:20were really fun, but seasons 3 onwards
- 52:22are too boring and tells you not to
- 52:24watch them. In this case, you can use
- 52:26an "If" conditional statement and a
- 52:27Break to ensure that if the current
- 52:29season is 3, the loop exits. In other
- 52:31words, the meaning of these statements
- 52:33is: if a certain condition is true,
- 52:35execute the print statement, and then
- 52:36execute the Break command to
- 52:38immediately exit the current loop. If
- 52:40you use a loop, the items within the
- 52:41list are iterated through in order. So,
- 52:43if it is not yet Season 3—that is, if
- 52:45it is Season 1 or 2—I will add a
- 52:46print statement to ensure it is watched
- 52:48. If you run the code, you can see that
- 52:50it only watches Seasons 1 and 2, and
- 52:52does not watch anything afterward. A `
- 52:55break` is used in this way to
- 52:57immediately exit a loop when a certain
- 52:59condition is true. Do you remember the
- 53:12drama with seasons 1 through 5? A
- 53:14friend who watched the drama first told
- 53:16you not to watch from Season 3 onwards
- 53:17because it wasn't interesting, so you
- 53:19only watched up to Season 2. However,
- 53:20you later found out that what your
- 53:21friend meant was not that all seasons
- 53:23from 3 onwards were boring, but that
- 53:24only Season 3 was boring. In other
- 53:26words, they were telling you to skip
- 53:28Season 3 and continue watching Seasons
- 53:294 and 5. Fortunately, they say that the
- 53:31storyline is not a major issue even if
- 53:32you don't watch Season 3. In Python
- 53:33visits, ` continue` is what you can use
- 53:35when you want to skip an action in
- 53:36certain cases. Just like with the `
- 53:38break`, there is a drama like this. We
- 53:40will use a loop to watch them one by
- 53:41one, but this is where it changes. The
- 53:43` if` condition is the same since it
- 53:45checks if it is Season 3, but since
- 53:46Season 3 isn't interesting, we need to
- 53:48skip it and add ` Continue` to continue
- 53:49watching from Season 4. In other words,
- 53:53even though there is a print statement
- 53:55like "Watch [something]," if `
- 53:56Continue` is encountered, it
- 53:58immediately moves to the next iteration
- 53:59. Therefore, the print statement below
- 54:02is not executed at all when it is
- 54:03Season 3, and it jumps to the next
- 54:05iteration element, Season 4. If you run
- 54:07the code, you can see that it watches
- 54:08Seasons 1 and 2, skips Season 3, and
- 54:10then watches Seasons 4 and 5—yes, all
- 54:12the way to the end. When we write, if a
- 54:25new paragraph begins, we leave a space
- 54:28at the beginning of the first sentence;
- 54:30we call this indentation. Unlike other
- 54:32programming languages, indentation
- 54:34plays a very significant role in Python
- 54:36. We have already used indentation when
- 54:37learning about ` if` statements, ` if`
- 54:39statements, and ` if` loops. When you
- 54:41indent, you can consider code lines
- 54:43that are indented by the same amount as
- 54:45a single paragraph. In other words,
- 54:47these three indented statements are all
- 54:49executed together when the subcondition
- 54:50is true. If you remove the indentation
- 54:52before a statement, it will no longer
- 54:54be part of the same paragraph as the
- 54:56statements above. The execution
- 54:57behavior will also change completely.
- 55:00For conditional statements, loops, and
- 55:01the functions, exception handling, and
- 55:03classes we will learn about later, the
- 55:05line following a section ending with a
- 55:06colon must be indented. If you need to
- 55:09separate a paragraph while writing
- 55:11indented statements, such as with
- 55:12nested statements, you must indent by
- 55:14the same amount one more time. Although
- 55:17there is no specific number of spaces
- 55:18required for indentation, Python
- 55:20generally applies four spaces. Since it
- 55:23involves the characters 'Siro', a
- 55:31company decided to recall all of its
- 55:34products because of a problem with
- 55:37certain items. This company makes two
- 55:40types of products, and let's assume
- 55:42that the model names of the problematic
- 55:44products start with 'Siro'. If we
- 55:46organize the names of all products
- 55:47released so far into a list, it looks
- 55:49like this. From this list, let's create
- 55:50a new list by extracting only the
- 55:52products starting with 'Siro' that are
- 55:53subject to recall. First, we create an
- 55:55empty list called 'Recall' to manage
- 55:57the list of recalled products. We then
- 56:00iterate through the 'Product' list
- 56:02using the method, and if a conditional
- 56:04statement checks if the product name
- 56:05starts with 'Siro', we add it to the
- 56:07recall list. After doing this, if we
- 56:10check the result using a print
- 56:11statement, we can see a list consisting
- 56:12of products that start with 'Siro'.
- 56:14However, there is a way to solve this
- 56:16part in just one line. It is called
- 56:19list compression. It can be used to
- 56:21extract only data that meets a specific
- 56:23condition from a list or to create a
- 56:24new list by changing values. The
- 56:26general syntax is like this, but it
- 56:27looks a bit complicated, doesn't it?
- 56:28Let me explain with an example. We have
- 56:30a list called 'My List'. It contains
- 56:32the values 1, 2, 3, 4, and 5. Now, we
- 56:33want to use list compression to extract
- 56:35only the numbers greater than 3 and
- 56:37create a new list. To do this, we first
- 56:39need to iterate through the My List and
- 56:40check if a value is greater than 3. To
- 56:42do this, just like we did in the 4th
- 56:44section, we first write the iteration
- 56:45target, or My List, which is 4x. The
- 56:48List values will be entered into x in
- 56:50order, but we can change x arbitrarily.
- 56:52You just need to write an expression
- 56:54using x right in front of 4 like this.
- 56:55If you simply write x, it means you are
- 56:57using the list values as they are
- 56:59without modification, but if you do x +
- 57:001, it adds 1 to each list value, and if
- 57:02you do x*3, it multiplies each list
- 57:04value by 3. Also, if you convert x into
- 57:05a character set and append the word 'th
- 57:07', a new list is created with the
- 57:08values converted into character sets,
- 57:10such as '1st', '2nd', and so on.
- 57:12Finally, there is an if conditional
- 57:13statement at the very end. Doing this
- 57:14means that when creating a new list,
- 57:15the value should only be used if it is
- 57:16greater than 3. In other words, when we
- 57:17utilized x in various ways earlier, the
- 57:18result appeared as shown in light green
- 57:19; this is actually the result when
- 57:24there is no if condition. However, with
- 57:26an if condition, new list elements are
- 57:27used only when x is greater than 3—
- 57:29that is, for values 4 and 5. Therefore,
- 57:30when utilizing x in various ways, the
- 57:31actual value becomes like this.
- 57:36Breaking this code down into sentences,
- 57:39it means to create a vertical list by
- 57:41using only the values greater than 3
- 57:43from the list. Let's return to the
- 57:45example explained earlier. If we use
- 57:47list compression on the code written
- 57:49using the body and conditions of Method
- 57:514, it becomes like this. If we
- 57:53interpret the code, it creates a
- 57:55vertical list by taking only the
- 57:56product names from the "Product" list
- 57:59if they start with "Silho." If you
- 58:01print the list created in this way, you
- 58:03can confirm that the exact same result
- 58:05is output as follows. In addition to
- 58:07what I have explained, list compression
- 58:08can be applied in various ways like
- 58:10this. You are the owner of a hair salon
- 58:23. This salon only offers haircuts, and
- 58:25the price is fixed at 10,000 won. To
- 58:27reduce labor costs, we use a payment
- 58:28kiosk, and you will write the program
- 58:30for it yourself. The first customer has
- 58:32arrived. We are writing down the code
- 58:33in advance so that we can provide price
- 58:35information when this customer pays
- 58:36later. We will store the name in a
- 58:37variable called "Customer One" and
- 58:39print the price. A little later, a
- 58:41second customer has arrived. We will
- 58:43create a new variable called "Customer
- 58:442," store the customer's name, and
- 58:45write the same code to print the price.
- 58:47Shortly after, a third customer arrives
- 58:49. We just need to write the same code
- 58:50as for the previous customers. However,
- 58:52while cutting hair, Kang-eon realizes
- 58:53that it will be difficult to run the
- 58:55salon for 10,000 won. So, he decides to
- 58:56raise the haircut price to 15,000 won.
- 58:58Consequently, he will need to modify
- 59:00the program he has created so far. He
- 59:01asks the customers for their
- 59:02understanding for a moment, finds all
- 59:03the sections where prices are listed,
- 59:04and edits them one by one. However, I
- 59:06was worried that simply raising prices
- 59:07might burden customers, so I decided to
- 59:09give the haircuts special names. I then
- 59:10went back to the code to find all the
- 59:12sections related to haircuts and
- 59:13modified them one by one. But having to
- 59:14manually change the same code scattered
- 59:16across various places every time a
- 59:18change occurs feels incredibly
- 59:19cumbersome. This is where functions
- 59:21come in handy. A function is a group of
- 59:23code that performs a specific action.
- 59:24It is especially convenient to
- 59:25consolidate code that is used
- 59:26repeatedly in multiple places into a
- 59:28single function. You define a function
- 59:29like this: write "define"(shortened to
- 59:31"def"), leave a space, write the
- 59:32function name, open and close
- 59:33parentheses, and then write a colon.
- 59:35From the next line onwards, you indent
- 59:37the code and write the statements to be
- 59:39executed. If you were to create a
- 59:40function for the price guide code in a
- 59:41hair salon, you could do it like this.
- 59:42This function is named "Shop Ice," and
- 59:44when the function is called, the
- 59:46printouts defined here will be executed
- 59:48. I used the term "call" because a
- 59:50function does nothing when it is simply
- 59:52defined. To execute the statements
- 59:54within the function, a "call" is
- 59:55required. By writing the function name
- 59:57and parentheses somewhere in the code
- 59:58like this, you can call the function.
- 1:00:00Only when this statement is executed do
- 1:00:02the corresponding statements within the
- 1:00:03function begin to run. The hair salon
- 1:00:05program I initially wrote was in this
- 1:00:06form. To save space, I will only leave
- 1:00:08the code for two customers. Applying
- 1:00:10the functions in this code results in
- 1:00:11this. We create a function called "Show
- 1:00:13Price" to display prices, and other
- 1:00:14places call and use this function.
- 1:00:16Furthermore, if you need to modify the
- 1:00:17code in this state, you don't need to
- 1:00:19go around looking for specific parts to
- 1:00:20edit; you just need to modify the
- 1:00:21section inside the function exactly
- 1:00:22once, and that's it. It's difficult,
- 1:00:24right? Yes. Yes. When using a function,
- 1:00:26the execution order of the code is as
- 1:00:27follows. First, it arrives sequentially
- 1:00:28from top to bottom. When the "Show
- 1:00:29Price" function is called, it briefly
- 1:00:31jumps to the function, and the code
- 1:00:32inside it is executed in order. Once
- 1:00:34the function finishes executing, it
- 1:00:35returns to the original code, and the
- 1:00:36code for the second customer is
- 1:00:37executed sequentially. When it
- 1:00:39encounters the function call section,
- 1:00:40the code within the new function is
- 1:00:41executed, after which it returns to the
- 1:00:43original code to continue. This is how
- 1:00:56we created functions earlier. And this
- 1:00:57is how the call was made. Since the
- 1:00:59part that displays haircut prices was
- 1:01:00created as a function, if modifications
- 1:01:01are needed, you only need to edit the
- 1:01:02inside of the function. However,
- 1:01:03looking at the code again, the part
- 1:01:05that prints "Customer [Name]" is
- 1:01:06similar but is still being used in
- 1:01:08multiple places. If you want to add the
- 1:01:10word "beloved" before "Customer [Name],
- 1:01:11" there is the inconvenience of having
- 1:01:13to go around and modify it in several
- 1:01:14places. Let's try to handle this
- 1:01:16together within the function. We
- 1:01:17learned that functions are written like
- 1:01:19this. The parentheses after the
- 1:01:20function name can be left empty like
- 1:01:21this, but if necessary, you can pass a
- 1:01:23value inside them. This is called a
- 1:01:25passed value or a parameter. In the
- 1:01:27hair salon program, when printing "
- 1:01:28Customer [Name]," variable values like
- 1:01:30"Customer" and "Customer 2" are used
- 1:01:32directly, but let's handle this part
- 1:01:33inside the function. As the first step,
- 1:01:35add a print statement within the
- 1:01:36function definition. Here, for the
- 1:01:38variable holding the customer's name, I
- 1:01:39will only write "Customer." As the
- 1:01:41second step, write "Customer" inside
- 1:01:42the parentheses after the function name
- 1:01:44. "Customer" is the value passed from
- 1:01:45the place calling the ` Show Price`
- 1:01:47function, and it allows you to use a
- 1:01:48variable named "Customer" within the
- 1:01:49function. Actually, you can use any
- 1:01:51name you want here, just like with "
- 1:01:53Name." As the third step, since the
- 1:01:55part in the original code that printed
- 1:01:57"Customer [Name]" is no longer needed,
- 1:01:59delete it like this. As the final step,
- 1:02:00to pass the customer name when calling
- 1:02:02the function, simply write "Customer"
- 1:02:03and "Customer 2" inside the parentheses
- 1:02:05of ` Show Price`, and you're done. Once
- 1:02:07you've done this, if you want to add
- 1:02:08the text "Sarang Haneun" before the
- 1:02:10customer name, you just need to change
- 1:02:11it once inside the function. You can
- 1:02:13pass as many values as you want to a
- 1:02:15function. One important feature is that
- 1:02:17passed values can only be used inside
- 1:02:19the function. Please remember that you
- 1:02:21cannot use the passed values outside
- 1:02:22the function. When you pass a value
- 1:02:35when calling a function, you can use
- 1:02:37that value within the function.
- 1:02:39Functions also have "return values."
- 1:02:41After performing an action or operation
- 1:02:42within a function, you can return the
- 1:02:44result to the place that called the
- 1:02:45function. You simply write the keyword
- 1:02:47'return', leave a space, and then write
- 1:02:49the value you want to return. This
- 1:02:50allows the place calling the function
- 1:02:52to receive and use this value. Let's
- 1:02:54modify the hair salon program slightly
- 1:02:56to create a function that returns price
- 1:02:58information. I will name the function '
- 1:03:00GetPrice' and write 'Return 15,000'
- 1:03:01directly without any other statements
- 1:03:03to execute. Then, the place calling
- 1:03:04this function needs to receive the
- 1:03:06value 15,000. Below, I will set the
- 1:03:09variable 'Price' to 'GetPrice'. Since
- 1:03:12the value 15,000 returned via 'Return'
- 1:03:13will be stored in the 'Price' variable,
- 1:03:15this statement will perform the exact
- 1:03:17same action as the statement written
- 1:03:18below. And you can output the value
- 1:03:20like this using a print statement.
- 1:03:22However, while running the hair salon,
- 1:03:24I encountered a concern. It is
- 1:03:25regarding regular customers. Since they
- 1:03:27visit the salon so often, I feel like I
- 1:03:29should offer them a discount for some
- 1:03:30reason. So, I’m going to change the
- 1:03:32pricing policy so that regular
- 1:03:33customers are charged only 10,000 won
- 1:03:34for a haircut, while regular customers
- 1:03:36are charged 15,000 won. First, to check
- 1:03:37for regular status, I will define a
- 1:03:39parameter called 'Easy VIP'. This
- 1:03:40parameter is in a variable form,
- 1:03:42meaning 'True' indicates a regular
- 1:03:43customer and 'False' indicates a
- 1:03:44regular customer. Using this
- 1:03:46conditional variable, I will return
- 1:03:4810,000 won if 'Easy VIP' is true, and
- 1:03:5115,000 won otherwise. The price will
- 1:03:53always be one of these two values. Also
- 1:03:55, to pass the regular status when
- 1:03:56calling this function, I write 'True'
- 1:03:57or 'False' inside the parentheses. If
- 1:03:59it is true, it means the customer is a
- 1:04:00regular customer and receives 10,000
- 1:04:01won; if it is false, it means the
- 1:04:04customer is a regular customer and
- 1:04:05receives 15,000 won, which is then
- 1:04:06stored in the 'Price' variable.
- 1:04:07Functions usually return a single value
- 1:04:09, but if necessary, you can return
- 1:04:10multiple values in a voting format.
- 1:04:12When a value is returned using the '
- 1:04:14return' keyword, the function exits
- 1:04:15immediately. In other words, even if
- 1:04:17there are other statements under the
- 1:04:18return statement, they will not be
- 1:04:19executed. Let's say you decided to
- 1:04:32charge 15,000 won for a haircut to
- 1:04:33regular customers and 10,000 won to
- 1:04:35regular customers at the hair salon you
- 1:04:36run. Usually, there are far more
- 1:04:38regular customers than regular
- 1:04:39customers. Looking at the function we
- 1:04:40previously created to return price
- 1:04:42information, it can be cumbersome to
- 1:04:43always pass a return value to find the
- 1:04:45price for regular customers, who are
- 1:04:46relatively more numerous compared to
- 1:04:48regular customers. In such cases, using
- 1:04:50a default value can make using the
- 1:04:52function very convenient. A default
- 1:04:53value is a value used as the standard
- 1:04:55for a monthly value. For example, there
- 1:04:57is a very famous Jajangmyeon restaurant
- 1:04:58. Although they sell other menu items
- 1:05:01like Jjamppong or Udon, most customers
- 1:05:03order Jajangmyeon. At this restaurant,
- 1:05:04if you order "Two servings of
- 1:05:06Jajangmyeon, please," the waiter simply
- 1:05:08tells the kitchen "Two servings."
- 1:05:09Although it wasn't explicitly stated
- 1:05:11what "two" refers to, the kitchen
- 1:05:12implicitly ends up making two servings
- 1:05:14of the best-selling Jajangmyeon. In
- 1:05:16other words, the default menu item at
- 1:05:17this Jajangmyeon restaurant is
- 1:05:18Jajangmyeon. In functions, you simply
- 1:05:20attach an equal sign after the value
- 1:05:22passed and set the value you want as
- 1:05:24the default. Since customers visiting a
- 1:05:25hair salon are often regular customers,
- 1:05:27if you set the Easy VIP value to false
- 1:05:28as the default like this, the place
- 1:05:29calling the function doesn't need to
- 1:05:30set false every time for a regular
- 1:05:31customer; simply calling the function
- 1:05:36with the parentheses empty will
- 1:05:38automatically set the Easy VIP value to
- 1:05:39false. Now, you only need to pass the
- 1:05:41value to regular customers. When
- 1:05:57creating functions, there are often
- 1:05:58cases where multiple values are
- 1:05:59required. For example, a price function
- 1:06:01used to obtain price information from a
- 1:06:03hair salon might check for regular
- 1:06:04customers to offer discounts, or accept
- 1:06:06discounts for birthdays or mobile
- 1:06:07carrier memberships. Additionally,
- 1:06:09prices can vary depending on various
- 1:06:10conditions such as card discounts,
- 1:06:11review discounts, and new visit
- 1:06:13discounts. However, if there are so
- 1:06:14many different pass values, you would
- 1:06:16have to check them all every time you
- 1:06:18call the function, which would make
- 1:06:19using the function quite difficult. How
- 1:06:21can we use the function a little more
- 1:06:23conveniently? That’s right. If you
- 1:06:24set default values, you won’t need to
- 1:06:26set the pass values individually. In
- 1:06:28other words, you set it so that for all
- 1:06:30cases, it is initially set to "not
- 1:06:31applicable." Then, if a situation
- 1:06:33arises—for example, if a customer
- 1:06:34mentions leaving a review—we can then
- 1:06:36set the "Review" pass value using the
- 1:06:38tool. But what if the code were written
- 1:06:41like this? What we need right now is to
- 1:06:44set only the "Review" pass value to
- 1:06:45True and leave everything else at the
- 1:06:46default value of False. In this case,
- 1:06:48you can use the keyword value. It
- 1:06:50precisely defines the target for the
- 1:06:51pass value. If the code is like this
- 1:06:53and you want to set only the "Review"
- 1:06:55pass value to True and leave the rest
- 1:06:56as is, you can do it exactly like this.
- 1:06:58When using the function, you simply
- 1:06:59write the target "Review" and the pass
- 1:07:01value "True" inside the parentheses
- 1:07:02like this. If you want to set the '
- 1:07:04Birthday' value to True because it
- 1:07:05happens to be a birthday and you are
- 1:07:07offering a review discount, you can add
- 1:07:09keyword values separated by commas like
- 1:07:10this. Please note that the order of the
- 1:07:12keyword values does not matter as it
- 1:07:14differs from what is defined in the
- 1:07:15function. Right now, you are the owner
- 1:07:27of a hair salon, aren't you? Yes. You
- 1:07:29decide to manage the history of which
- 1:07:31customers visit the salon every day.
- 1:07:33Based on this visit history, you plan
- 1:07:34to encourage future visits by
- 1:07:35contacting customers in advance to
- 1:07:36offer discounts when it is time for
- 1:07:38them to get their hair done again after
- 1:07:39a certain amount of time has passed.
- 1:07:41First, let's create a function to
- 1:07:42manage today's date and the visiting
- 1:07:44customers. Let's name the function '
- 1:07:46Business' and receive today's date as
- 1:07:47input. However, I realized there is one
- 1:07:49problem. Customers can change every day
- 1:07:52. While you could create a large number
- 1:07:54of input values in advance to be safe,
- 1:07:56an issue might arise if more customers
- 1:07:59visit than the total number of input
- 1:08:01values. In such cases, a very simple
- 1:08:03method is to use variable arguments. As
- 1:08:04the name suggests, variable arguments
- 1:08:06refer to arguments whose quantity can
- 1:08:07change. In other words, it allows you
- 1:08:08to use a function without worrying
- 1:08:10about the number of values to be passed
- 1:08:11when you call it, even if you don't
- 1:08:12know exactly how many will be passed.
- 1:08:14Using variable arguments is very simple
- 1:08:15. You just need to place an asterisk (*
- 1:08:17) in front of the value. You can neatly
- 1:08:19reduce the function you were previously
- 1:08:21worrying about regarding the number of
- 1:08:22values to pass into a variable called `
- 1:08:24customers`. For reference, ` customers`
- 1:08:26receives values in the form of a tuple.
- 1:08:28Inside the function, we will first
- 1:08:30write today's date. For now, we will
- 1:08:32set it up to print all customers 'names
- 1:08:33in half a won. By using variable
- 1:08:35arguments like this, you can use this
- 1:08:37function exactly the same way
- 1:08:38regardless of whether there is one
- 1:08:40customer or three. If you run these
- 1:08:42statements individually, it looks like
- 1:08:43this for one customer, like this for
- 1:08:45two, and like this for three. The
- 1:08:46number of customers keeps changing.
- 1:08:48There is one thing to keep in mind when
- 1:08:49using variable arguments: if a function
- 1:08:51has multiple values to pass, the
- 1:08:52variable argument can only be used
- 1:08:54exactly once at the end. For example,
- 1:08:55if the values passed to a function
- 1:08:57called' My Function 'are both' star's,
- 1:08:59'the first value, 1, will go into' a '.
- 1:09:01However, when passing the values in
- 1:09:03this way, it is impossible to know how
- 1:09:05many of the remaining values should be
- 1:09:07split and assigned to' b 'and' c '.
- 1:09:19There is a function called' Secret '.
- 1:09:21This function creates a variable called
- 1:09:23' Message 'to secretly store a secret
- 1:09:25message. Can you print the value of
- 1:09:27this' Message 'variable from outside
- 1:09:28the function like this? Or not? The
- 1:09:33answer is no. If you run this code
- 1:09:35directly, you will get an error stating
- 1:09:37that the variable' Message 'is not
- 1:09:39defined. The reason is that the'
- 1:09:40Message 'variable is a local variable.
- 1:09:43A local variable is a variable defined
- 1:09:44within a function, and it can only be
- 1:09:46used within that specific function.
- 1:09:48Therefore, the' Message 'defined within
- 1:09:50the' Secret 'function can be printed
- 1:09:51like this, and you can also modify the
- 1:09:53value as you wish. However, it can
- 1:09:55never be accessed or modified from
- 1:09:56outside the function or from another
- 1:09:57function. If you assign a value to a
- 1:09:59variable with the same name within
- 1:10:01different functions, are they actually
- 1:10:03the same variable? The answer is no.
- 1:10:09They share only the same name; they are
- 1:10:11local variables that can never interact
- 1:10:12with each other, as they are usable
- 1:10:14only within the ` secret` function or
- 1:10:15the ` please` function, respectively.
- 1:10:28We have previously looked at local
- 1:10:29variables defined within a function and
- 1:10:31usable only within that function.
- 1:10:32However, there is also a variable
- 1:10:33called a "no-secret" variable that can
- 1:10:34be used anywhere, regardless of whether
- 1:10:35it is within or outside the function.
- 1:10:37You have actually encountered no-secret
- 1:10:38variables quite often. The method of
- 1:10:40use is simple: if you create it outside
- 1:10:41the function, that is a no-secret
- 1:10:42variable. Then, it can be used anywhere
- 1:10:44, whether inside or outside the
- 1:10:46function. One thing to note is that if
- 1:10:48you try to assign a value to a variable
- 1:10:49named "Message"—which has the same
- 1:10:51name as a no-secret variable—inside a
- 1:10:52function, it is created as a new local
- 1:10:53variable within the ` no-secret`
- 1:10:55function rather than a no-secret
- 1:10:56variable. Consequently, if you print it
- 1:10:58, the output will differ. Therefore,
- 1:11:01while it does not matter when you are
- 1:11:02merely using the variable's value
- 1:11:03within the function, if you intend to
- 1:11:05directly modify the value, you should
- 1:11:06declare it as "Global Message" using
- 1:11:08the keyword "Global." By doing this,
- 1:11:09you are using a variable from the
- 1:11:10global scope. If the variable does not
- 1:11:12exist in the global scope, it creates a
- 1:11:14variable named "Message." Therefore, if
- 1:11:17you declare a variable inside a
- 1:11:18function in this way and change its
- 1:11:20value, when you run the function and
- 1:11:22print "Message," you can see that the
- 1:11:24value declared above— "I am the
- 1:11:26variable"—is printed first, and then
- 1:11:28the value changed within the function
- 1:11:30— "5 is the variable"—is printed
- 1:11:32next. Please remember the difference
- 1:11:34between a variable that can be used
- 1:11:36anywhere and a variable that can only
- 1:11:37be used within a function. The programs
- 1:11:50we have created so far have been
- 1:11:50one-way, like a broadcast flowing
- 1:11:51through speakers. The input simply
- 1:11:55flowed by. This time, let's make the
- 1:11:57program communicate directly with us.
- 1:12:01In Python, you can receive user input
- 1:12:02using a built-in function called `
- 1:12:02input`. The usage is very simple. A
- 1:12:06hamburger suddenly comes to mind. Yes.
- 1:12:07Shall we try making a program to
- 1:12:08reserve a burger restaurant? If you
- 1:12:10want to ask the hamburger staff, "What
- 1:12:11is the name of the person who made the
- 1:12:13reservation?", you simply write the
- 1:12:15sentence inside the parentheses of the
- 1:12:16` input` function like this. Then, it
- 1:12:18waits until the user enters a value,
- 1:12:20stores the entered value in the ` Name`
- 1:12:22variable, and executes the next
- 1:12:23statement. To verify, I will print `
- 1:12:26Name`. When you run the code, a
- 1:12:28statement is printed like this,
- 1:12:29followed by user input. If you type
- 1:12:31your name or "I am coding too" and
- 1:12:33press Enter, you can see that the user
- 1:12:35input value stored in ` Name` is
- 1:12:36printed correctly. Now, shall we ask
- 1:12:38how many people are making the
- 1:12:39reservation? We will use ` Input` again
- 1:12:41, but this time, let's receive the
- 1:12:42input value into a variable called `
- 1:12:43Num`. However, it says that this
- 1:12:45restaurant only allows a maximum of
- 1:12:46four people per group. Then, we can
- 1:12:48compare the value received in ` Num`.
- 1:12:50Using this part, if ` Num` is greater
- 1:12:52than 4, let's write a statement like
- 1:12:54this and run it. It waits for user
- 1:12:55input while asking "How many people are
- 1:12:57in total?", and I'll try entering 8.
- 1:12:59Yes. I entered 8 and left it, but an
- 1:13:01error occurred. I looked into why, and
- 1:13:03it says that comparison operations are
- 1:13:04impossible between a string and an
- 1:13:05integer. This means that ` Sa` is an
- 1:13:07integer, but the ` Num` we received as
- 1:13:09input is not an integer but a string.
- 1:13:11As seen earlier, the user input values
- 1:13:13received are all in string format, so
- 1:13:14the number 4 is also stored as a string
- 1:13:16as shown in Table 4. In such cases, we
- 1:13:18can use the type conversion we learned
- 1:13:20earlier to wrap the input in integer
- 1:13:21parentheses like this, allowing us to
- 1:13:23directly convert the input value to a
- 1:13:25numeric data type. After changing the
- 1:13:26code and running it, I can see that it
- 1:13:28works well. When I was in the military,
- 1:13:40I used to write a journal called a "
- 1:13:41Suyangnok." A Suyangnok is similar to a
- 1:13:42journal entry. Since it served as a
- 1:13:44kind of memory, I converted everything
- 1:13:45into digital files after completing it
- 1:13:46properly. One page in particular stands
- 1:13:48out in my memory, so I will use it as
- 1:13:49an example. I won't tell you what the
- 1:13:51content is. In Python, you can write
- 1:13:52text to a file or read from a file with
- 1:13:54just a few lines of code. You simply
- 1:13:56use the ` open` function. You just need
- 1:13:58to open the parentheses, specify the
- 1:14:00filename, the opening mode, and the
- 1:14:01encoding if necessary. You just need to
- 1:14:03remember these three main types of
- 1:14:04opening modes. Let's try writing to a
- 1:14:06file first. To open a file in writing
- 1:14:08mode, F is the open parentheses, and
- 1:14:10the list. I will open the file in write
- 1:14:14mode by adding' W 'at the end of the
- 1:14:15filename. To write Korean characters, I
- 1:14:18will set the encoding to UTF-8. This
- 1:14:20means that if a file exists in the
- 1:14:22current path, it will be overwritten;
- 1:14:24if not, a new file will be created.
- 1:14:26Through this, we can now write content
- 1:14:28to the file. After that, there is the'
- 1:14:30f.write 'function. I will use' f.write
- 1:14:33'to write about three sentences to the
- 1:14:35file. To separate the sentences, I will
- 1:14:37add a line break at the end. Finally, I
- 1:14:40will call the' f.close 'function to
- 1:14:42close the file. You must always close a
- 1:14:44file once it has been opened. If you
- 1:14:46run the application, you can see that a
- 1:14:47file named' list.text 'has been created
- 1:14:49and the content has been entered
- 1:14:50correctly. Now, let's try reading the
- 1:14:52file created in this way. To open the
- 1:14:54file in read mode, I will add' f.open '
- 1:14:56and write the filename exactly the same
- 1:14:57, but this time I will set the open
- 1:14:59mode to' R '. I will also set the
- 1:15:01encoding to UTF-8. Then, by using the
- 1:15:04F. read function to load the entire
- 1:15:05file content, storing it in a variable
- 1:15:07called' content ', and printing it out,
- 1:15:09you can read the file contents very
- 1:15:11easily like this. Oh, and remember to
- 1:15:13close the file using F. close at this
- 1:15:15point as well. If you want to read the
- 1:15:17file line by line and process it, you
- 1:15:19can do so very simply using method 4.
- 1:15:21This retrieves one line at a time from
- 1:15:23the file and inserts it into the line.
- 1:15:25At this point, the print function has
- 1:15:27an' end 'property; if you do not set
- 1:15:29this to a blank space, unnecessary
- 1:15:31lines may be inserted one by one like
- 1:15:33this, so please use' end 'if necessary.
- 1:15:46You must turn off the heat after
- 1:15:47cooking, and turn off the faucet after
- 1:15:49washing your hands. Similarly, if you
- 1:15:51have opened a file, you must close it
- 1:15:53after you are finished using it. When
- 1:15:55you are working on a file for a while,
- 1:15:56you might forget to touch it. Because
- 1:15:57we are human. However, there is a tool
- 1:15:59that does this automatically. It is
- 1:16:00called' with '. When using ` with`, the
- 1:16:02file is automatically closed when you
- 1:16:04exit the ` with` block, so you do not
- 1:16:05need to call the ` close` function
- 1:16:07separately. It is also simple to use.
- 1:16:09Do you remember how we did this earlier
- 1:16:10to write to a file? When using ` with`,
- 1:16:13the code looks like this. Start the
- 1:16:15statement with ` with`, leave a space,
- 1:16:16and write the ` open` syntax exactly as
- 1:16:18it is. At the very end, specify a
- 1:16:19variable name to store the file opened
- 1:16:21via ` open`. This is exactly the same
- 1:16:23as receiving the result of opening in
- 1:16:24the statement above as ` F`. Here, you
- 1:16:27can replace the name ` F` with ` file`,
- 1:16:29` my underscore file`, or any other
- 1:16:31format you prefer. After doing this,
- 1:16:34write a colon, and then write the
- 1:16:36statements starting from the next line.
- 1:16:39Now, you do not need to call the `
- 1:16:40close` function separately; the file
- 1:16:42will close automatically once you exit
- 1:16:43this ` with` statement. Convenient,
- 1:16:45right? It works the same way when
- 1:16:46reading a file. You simply use ` with`
- 1:16:48like this for the statements you wrote
- 1:16:49earlier, and then perform the
- 1:16:51operations on the file. Again, you
- 1:16:52won't need to call ` close` for the
- 1:16:54file. It is a good habit to use the `
- 1:16:56with` statement whenever possible when
- 1:16:57outputting files. Nowadays, most cars
- 1:17:12are equipped with dashcams. If you are
- 1:17:14purchasing a new dashcam, you should
- 1:17:16first examine its specifications. Let's
- 1:17:18create variables for the dashcam
- 1:17:19information. Yes. First, let's name the
- 1:17:22model "Kkamang." Resolution is also
- 1:17:25important for a dashcam, so we will
- 1:17:26assign "Full HD" to the variable "
- 1:17:27Resolution." Now, for the price, we
- 1:17:29will assign 200,000 won to the variable
- 1:17:31"Price." And since the color can vary
- 1:17:33even within the same model, we will
- 1:17:34assign "Black" to the variable "Color."
- 1:17:37Although it is a very small amount of
- 1:17:39data, we used four variables to store
- 1:17:40the information for a single dashcam.
- 1:17:42Now, what if we want to manage another
- 1:17:45dashcam model using variables? We could
- 1:17:48create four more variables and assign
- 1:17:49values like this. If another dashcam is
- 1:17:52added, we would simply create four more
- 1:17:54variables. However, isn't there a way
- 1:17:56to manage this more conveniently and
- 1:17:57efficiently? Yes, that is correct.
- 1:17:58Dictionaries managed as key-value pairs
- 1:18:01can be very useful. Now, let's say a
- 1:18:04dash cam has a feature that detects an
- 1:18:06impact while driving and manages the
- 1:18:09video footage in a separate event
- 1:18:11folder. This falls into the realm of
- 1:18:13functions, which is difficult to handle
- 1:18:14using only variables. Yes, relying
- 1:18:16solely on dictionaries seems
- 1:18:17impractical. This is where we can
- 1:18:19utilize classes. Classes allow you to
- 1:18:21group multiple variables together for
- 1:18:23management, and you can also create
- 1:18:24functions within a class to execute
- 1:18:25specific tasks. The concept of a class
- 1:18:28can be quite difficult when you first
- 1:18:30encounter it. Therefore, it is crucial
- 1:18:32to clearly understand the concept first
- 1:18:33. It is helpful to think of a class as
- 1:18:35a combination of a blueprint and an
- 1:18:36instruction manual. By following this
- 1:18:39manual, you can verify the dash cam's
- 1:18:41information, build the dash cam based
- 1:18:43on the blueprint, and understand how
- 1:18:44its features operate. You can create
- 1:18:47multiple dash cam products with just a
- 1:18:49single blueprint, much like how having
- 1:18:50a book on how to write a paper crane
- 1:18:52allows you to create multiple paper
- 1:18:54cranes by following that book. As such,
- 1:18:56once a class is defined, you can create
- 1:18:59as many products as you want using it;
- 1:19:01these products are called objects. In
- 1:19:04this case, each object is referred to
- 1:19:06as an instance of the class. Do you
- 1:19:08have a better grasp of classes now?
- 1:19:22Let's review them. A class is a concept
- 1:19:24that combines a blueprint and an
- 1:19:25instruction manual. A class can contain
- 1:19:27various variables and even define
- 1:19:29functions. The products created through
- 1:19:31a class are called objects, and each
- 1:19:33object is called an instance of that
- 1:19:35class. You can define a class like this
- 1:19:37: write "Class" in English, leave a
- 1:19:39space, write the class name, and then a
- 1:19:40colon. Then, indent the next line and
- 1:19:43write the content defining the class.
- 1:19:45You can define a class called "Black
- 1:19:47Box" like this. For reference, class
- 1:19:48names should be formed by combining
- 1:19:50words that start with a capital letter.
- 1:19:52Here, since we combined the two words "
- 1:19:54Black" and "Box," we capitalized the "B
- 1:19:56" part. Also, in the definition section
- 1:19:58, we have written "Pass" for now. In
- 1:19:59Python, you can understand "pass" as a
- 1:20:01way to temporarily set aside the parts
- 1:20:03you need to implement. Think of it like
- 1:20:06making plans with a friend but not yet
- 1:20:08deciding what to do when you meet. Just
- 1:20:11as defining a function doesn't mean it
- 1:20:12will execute, defining a class doesn't
- 1:20:14mean the code will run. To use a class,
- 1:20:17you must create a class object, which
- 1:20:20you can do like this: define the object
- 1:20:23as B1, place an equal sign, write the
- 1:20:26class name, and then open parentheses.
- 1:20:29This creates an object named B1. Shall
- 1:20:32we create a variable for this Black Box
- 1:20:33object? Using a dot, you can assign the
- 1:20:35value "black" to B1.Name, just as you
- 1:20:37would normally assign a string value.
- 1:20:39This declares a variable named "Name"
- 1:20:41within the B1 object. To verify that
- 1:20:44the value is being assigned correctly,
- 1:20:46you can print B1.Name. The result
- 1:20:49should be "Black". Let's check if the
- 1:20:51B1 object is indeed an instance of the
- 1:20:53Black Box. In that case, you can use
- 1:20:55the ` isInstance` function, passing the
- 1:20:57object B1 in front and the class Black
- 1:21:00Box behind. If the result is ` True`,
- 1:21:02it means B1 is indeed an instance of
- 1:21:04Black Box; if it is ` Pes`, it means it
- 1:21:05is not. We defined the class like this,
- 1:21:19created an object named B1, created a `
- 1:21:21Name` variable using a dot, and
- 1:21:23assigned the value "Kkamang" to it. Now
- 1:21:25, let's create a new object named B2.
- 1:21:27What happens if we try typing ` B2 dot
- 1:21:29Name`? That's right. An error occurs.
- 1:21:31We created a ` Name` variable for the
- 1:21:33B1 object, but we haven't created one
- 1:21:35for the B2 object yet. However, I
- 1:21:37mentioned that a class is a concept
- 1:21:38that combines a blueprint and a
- 1:21:39description. Right now, our blueprint
- 1:21:41is just an empty one with nothing in it
- 1:21:42. So, let's fill in the contents of the
- 1:21:44blueprint. To fill in the content, we
- 1:21:46can use the ` insert` function. Now,
- 1:21:48delete the path on the line next to the
- 1:21:50class definition, and just like
- 1:21:51creating a regular function, add a
- 1:21:53space, an underscore, an underscore, an
- 1:21:55underscore, and then write the
- 1:21:56variables needed for the product we are
- 1:21:58creating inside the parentheses. For
- 1:22:01example, if our product has a' Name '
- 1:22:03for the name and a' Price 'for the
- 1:22:04price, you would write it as' self
- 1:22:06comma Name comma Price '. (Just write'
- 1:22:09self 'for now.) Then, write a colon.
- 1:22:11Next, after indenting, write' self dot
- 1:22:14Name 'as' Name 'and' self dot Price 'as
- 1:22:16' Price '. You might be wondering what
- 1:22:18on earth this is all about. Now, to
- 1:22:19create an object using the blueprint
- 1:22:21class created like this, you define B1
- 1:22:23as a' Black Box 'and simply enter the
- 1:22:24name and price in order inside the
- 1:22:26parentheses. Right? It is the same as
- 1:22:28calling a regular function. After doing
- 1:22:29this, if you try' print B1.Name, B
- 1:22:311.Price ', you can see that the values
- 1:22:33we entered when creating the object are
- 1:22:35printed correctly. Shall we try B2 as
- 1:22:37well? We create an object for B2 called
- 1:22:39a' Black Box ', set' White 'to' 100,000
- 1:22:40won ', and when we print the value, the
- 1:22:42value is printed correctly as well. In
- 1:22:44other words, the class we created
- 1:22:46called' Black Box 'has two variables
- 1:22:48named' Name 'and' Price '. The
- 1:22:49initialization function is what
- 1:22:51performs the role of assigning' Name '
- 1:22:53here and' Price 'here when creating
- 1:22:55each object. Now, the' Black Box 'class
- 1:22:57—our blueprint—has been created in
- 1:22:59this form. The B1 and B2 objects
- 1:23:01created through this will each take
- 1:23:03this form. Please remember that the
- 1:23:05initialization function runs
- 1:23:06automatically when an object is created
- 1:23:08. Earlier, we defined' Name 'and' Price
- 1:23:22'through this function; in this case,'
- 1:23:24Name 'and' Price 'are called member
- 1:23:26variables. Member variables can have
- 1:23:28different values for each class object.
- 1:23:30Just as people use identical desks for
- 1:23:32different purposes, the values of their
- 1:23:35member variables can vary even if
- 1:23:37objects are created from the same class
- 1:23:39. Furthermore, the two objects are
- 1:23:41completely independent of each other.
- 1:23:42However, if you need an additional
- 1:23:44member variable for a specific object
- 1:23:46—for example, if you want to give a
- 1:23:48special nickname to the B1 Black Box—
- 1:23:49then B1. You can add a special member
- 1:23:51variable specifically to the B1 object,
- 1:23:53such as "Nickname is No. 1." In other
- 1:23:55words, the form of the B1 object
- 1:23:57changes like this; this is a new
- 1:23:58variable that is not defined in the
- 1:24:00documentation—that is, the class—so
- 1:24:02B2 knows nothing about it. We mentioned
- 1:24:15that a class can have multiple
- 1:24:16variables, but it can also have
- 1:24:18functions that perform tasks. We have
- 1:24:20already used a function that is
- 1:24:21automatically called when an object is
- 1:24:24created. Functions declared within a
- 1:24:26class like this are called methods. The
- 1:24:28dash cams produced at our factory have
- 1:24:29a special feature. If the user wants,
- 1:24:31pressing the "Travel Mode" button
- 1:24:33automatically manages the video footage
- 1:24:35in a separate folder from that point on
- 1:24:37. Normal recordings are general footage
- 1:24:39taken to prepare for potential
- 1:24:41accidents, so they tend to be
- 1:24:42overwritten over time. However, if you
- 1:24:45turn on Travel Mode, the recorded
- 1:24:46footage is managed in a separate folder
- 1:24:49. This allows you to save scenes like
- 1:24:51beautiful mountains you encounter while
- 1:24:52driving. Now, this is not a variable
- 1:24:55but a function; since we said that
- 1:24:57classes can have functions, let's
- 1:24:58define this function of our dash cam.
- 1:25:00The class is set like this, and all you
- 1:25:02have to do is add one more instance
- 1:25:04here using DF below, and that's it. DF,
- 1:25:07yes. We are creating a method called `
- 1:25:09Set Travel Mode`. If you don't receive
- 1:25:11any values, you just need to write `
- 1:25:13self`. I will explain ` self` later.
- 1:25:15Then, inside the method, we will print
- 1:25:18the value "Travel Mode On". After that,
- 1:25:21create the B1 object and, just like
- 1:25:23accessing member variables, use the dot
- 1:25:25: B1 dot Set Travel Mode, and then
- 1:25:27write the parentheses. If you run it,
- 1:25:30you can see that the text "Travel Mode
- 1:25:32On" is printed correctly. If the method
- 1:25:34requires a value to be passed, you can
- 1:25:36define it like this after ` self`, just
- 1:25:38like a method with 1. Let's try
- 1:25:40applying Travel Mode only for the
- 1:25:41duration passed here. And if you change
- 1:25:45the print mode to "Travel Mode On for a
- 1:25:46few minutes" and want to record in
- 1:25:48Travel Mode for only 20 minutes from
- 1:25:49now, you can do it like this. Only 20
- 1:25:51is displayed inside the parentheses
- 1:25:52here. Now, if you run it, you can see
- 1:25:55that "Travel Mode On for 20 minutes"
- 1:25:57appears. We used' self 'when defining
- 1:26:10methods within a class. When defining a
- 1:26:13function, if there are two values to
- 1:26:14pass, the function must also be passed
- 1:26:15two values when called. However, class
- 1:26:17methods, even if they pass two values
- 1:26:19including' self ', exclude' self 'when
- 1:26:21calling and only pass the remaining
- 1:26:22value. In this context,' self 'refers
- 1:26:24to the object itself. Let's modify our
- 1:26:26method slightly. We changed the print
- 1:26:28part of the ` setTravelMode` method
- 1:26:30like this.' self.name 'has been added
- 1:26:31at the beginning. Let's delete the line
- 1:26:33above it. Now, in this code,' self.name
- 1:26:35'prints the member variable' name 'of
- 1:26:36this object, and as mentioned,' self '
- 1:26:37refers to the object itself. Therefore,
- 1:26:45if two objects named B1 and B2 are
- 1:26:46created and you call the method on each
- 1:26:47of them, you can see the output that
- 1:26:48includes the' name 'member variable of
- 1:26:49each object. In other words, when this
- 1:26:51statement is executed, the B1 object is
- 1:26:53placed in the' self 'part, and when the
- 1:26:55statement below is executed, the B2
- 1:26:56object itself is placed in the' self '
- 1:26:58part. It's a bit confusing, isn't it?
- 1:26:59Now, look closely at this statement.
- 1:27:01Write the name of the Black Box class
- 1:27:02directly and call the Set Wrap with the
- 1:27:04purchase amount. Then, set B1 as the
- 1:27:06first pass value and 20 as the second
- 1:27:08pass value. By doing this, B1 is passed
- 1:27:11as' Self 'and 20 as' Min ', just as if
- 1:27:13calling a function. Whether you fold
- 1:27:16the code like in example 1 or write it
- 1:27:17like in example 2, the result is the
- 1:27:18same. Looking at it this way, it makes
- 1:27:20a little more sense, doesn't it?
- 1:27:21Ultimately, you can understand it as
- 1:27:22follows: while we could directly call
- 1:27:24the Black Box class method and specify
- 1:27:26the object and pass values, it is more
- 1:27:27convenient to call the method through
- 1:27:29the B1 object; since we already know it
- 1:27:31is the B1 object, we can exclude' Self
- 1:27:32'and just specify the remaining pass
- 1:27:34values. If it still feels difficult,
- 1:27:37please remember just two things. When
- 1:27:39defining a class method, you must
- 1:27:40always set the first value to' Self '.
- 1:27:42Also, within the method, use member
- 1:27:45variables using the' Self 'dot, as in'
- 1:27:47Self Dot Name '. The Black Box we are
- 1:28:00creating includes a feature that
- 1:28:01manages videos separately for travel
- 1:28:02purposes from the moment a button is
- 1:28:04pressed. We added this feature and
- 1:28:06raised the price slightly, but sales
- 1:28:07actually dropped. It seems fewer people
- 1:28:10used Travel Mode than expected. So, we
- 1:28:12changed our strategy and decided to
- 1:28:13sell a separate Black Box that performs
- 1:28:15only basic functions and another that
- 1:28:16supports Travel Mode. For the basic
- 1:28:18Black Box, we will simply leave the `
- 1:28:20set` method as we did before. For the
- 1:28:22Black Box supporting Travel Mode, we
- 1:28:23can create a new class called "Travel
- 1:28:25Black Box" and have it with the ` set`
- 1:28:26method added. However, upon closer
- 1:28:28inspection, there is some overlap. The
- 1:28:30members of the two classes are
- 1:28:31completely identical. They are members
- 1:28:33that have exactly the same name and
- 1:28:35price. In fact, our Black Boxes are
- 1:28:37very small classes, but as classes grow
- 1:28:39even slightly in scale, there can be a
- 1:28:41lot of duplicated code like this.
- 1:28:43Rewriting all the duplicated content
- 1:28:45every time a new class is created is
- 1:28:47clearly inefficient. Fortunately,
- 1:28:48classes have something called
- 1:28:49inheritance. Inheritance usually refers
- 1:28:51to a child receiving property left
- 1:28:53behind by a parent. In the context of
- 1:28:55classes, inheritance can be used to
- 1:28:57inherit all the code used in a class
- 1:28:59exactly as it is, and then extend it
- 1:29:00with additional necessary content.
- 1:29:02Returning to our code, both the Black
- 1:29:04Box and the Travel Black Box have the
- 1:29:06same method. Furthermore, the Travel
- 1:29:08Mode Support Black Box is an extended
- 1:29:10version of the ` Set Travel Mode`
- 1:29:11method. In this case, if you write `
- 1:29:13Black Box` in parentheses after the
- 1:29:14class name ` Travel Black Box`, it
- 1:29:16signifies that you intend to inherit
- 1:29:17and use everything from the Black Box
- 1:29:19class exactly as is. In other words,
- 1:29:21the code that looked like this can be
- 1:29:23changed to this by removing this part,
- 1:29:24since it inherits everything from the
- 1:29:26Black Box class and uses the method
- 1:29:27from here. Here, the basic Black Box on
- 1:29:30the left is called the parent class,
- 1:29:31and the Travel Mode Support Black Box
- 1:29:33inherited from it is called the child
- 1:29:35class. For reference, the code usage is
- 1:29:36the same. Just as you create a B1
- 1:29:38object using the Black Box class, you
- 1:29:39can create a B2 object using the Travel
- 1:29:41Black Box. Then, when the object is
- 1:29:43created, the inherited values from the
- 1:29:45Black Box—specifically the "Hayang"
- 1:29:47and "100,000 Won"—are set as member
- 1:29:48variables named "Name" and "Price,"
- 1:29:50respectively. And the B2 object becomes
- 1:29:52able to use the three travel mode
- 1:29:54methods that the B1 object cannot.
- 1:30:07Fortunately, sales started to rise
- 1:30:08again as we sold the products by
- 1:30:09dividing them into a basic dash box and
- 1:30:11a dash box that supports both travel
- 1:30:12and dash boxes. However, one concern
- 1:30:14arose. As videos recorded during travel
- 1:30:16kept accumulating in the dash box
- 1:30:17supporting travel mode, storage space
- 1:30:19became insufficient. Therefore, we
- 1:30:21decided to offer options: the 64GB
- 1:30:23model for those who diligently transfer
- 1:30:25and delete videos to a PC as needed,
- 1:30:26and the 128GB model for those who do
- 1:30:28not, allowing for more ample space. Now
- 1:30:30, we need to change our code as well,
- 1:30:32right? For the dash box supporting
- 1:30:33travel mode, we added' SD 'as a member
- 1:30:35to denote the storage space as an SD
- 1:30:37card, and redefined the' Entry 'method
- 1:30:38like this. Previously, we could have
- 1:30:40omitted the' Entry 'method by
- 1:30:42inheriting from the basic dash box, but
- 1:30:43it has reappeared. Then, is it really
- 1:30:45necessary to inherit from the parent
- 1:30:46class? Yes. Of course, it is necessary,
- 1:30:48which is why I am doing this. We
- 1:30:49learned that through inheritance, we
- 1:30:50can utilize everything from the parent
- 1:30:52class and extend it for anything
- 1:30:53additionally needed. Then, we would
- 1:30:54need to use everything from the parent
- 1:30:56class here as well. Now, if you look
- 1:30:58here, the parent class defines member
- 1:31:00variables by receiving Name and Price.
- 1:31:02Additionally, the Travel Mode Supported
- 1:31:04Black Box defines only the ST card data
- 1:31:06here. In that case, it would be best to
- 1:31:08use the default Black Box's values for
- 1:31:09setting Name and Price, at the very
- 1:31:11least. You can do it like this. You
- 1:31:14directly call the method of the
- 1:31:15inherited Black Box class, explicitly
- 1:31:17specify ` self`, and pass Name and
- 1:31:19Price as the values. So, at least Name
- 1:31:21and Price are set as member variables
- 1:31:23using the Black Box's method, and here,
- 1:31:25you only need to define the ST card
- 1:31:27part additionally. It works like this,
- 1:31:29too. There is no problem. However,
- 1:31:31usually, as a child, we call our
- 1:31:33parents "Dad" or "Mom," not "Mr./Ms.
- 1:31:35So-and-so." Inheritance is the same.
- 1:31:37When you want to call a method of the
- 1:31:38parent class, you can write the parent
- 1:31:40class name exactly as it is and call it
- 1:31:42as a method, but you can also call it
- 1:31:43as a method using "Super." In this case
- 1:31:45, "Super" refers to the parent class
- 1:31:47itself. However, please note that you
- 1:31:49must enclose the ` super` class in
- 1:31:50parentheses like this, and that ` self`
- 1:31:52is not needed as a method value.
- 1:31:54Therefore, in the case of the Travel
- 1:31:55Mode Supported Dash Box, you could do
- 1:31:56it like this, but you can also do it
- 1:31:58like this using ` super`. All parents
- 1:31:59in this world are actually our Supermen
- 1:32:02. We decided to add a new feature to
- 1:32:14the Travel Mode Supported Dash Box. It
- 1:32:16is to create nostalgic travel videos by
- 1:32:18editing footage recorded by the dash
- 1:32:20box with cool backgrounds. While
- 1:32:22contemplating how to implement this
- 1:32:23feature, we confirmed that it was
- 1:32:25already implemented in another product
- 1:32:26our factory manufactures, the 360-
- 1:32:28degree camera. Furthermore, they
- 1:32:29provided the class directly, so we were
- 1:32:30told that we could use the feature
- 1:32:31immediately just by inheriting it. The
- 1:32:33class name is Video Maker, and it looks
- 1:32:35like this. Video production is possible
- 1:32:36through the Make tool. Now, let's try
- 1:32:38inheriting this class for our Travel
- 1:32:40Mode Supported Dash Box. But wait, we
- 1:32:42are already inheriting the basic Dash
- 1:32:44Box class. What should we do? That is
- 1:32:46correct. Since there is another way, I
- 1:32:47have laid the groundwork here. What is
- 1:32:48needed in cases like this is multiple
- 1:32:50inheritance. It means inheriting from
- 1:32:51multiple classes. The usage is simple:
- 1:32:53just list multiple class names
- 1:32:55separated by commas. For instance, you
- 1:32:56just need to put a comma after the
- 1:32:58Black Box class and write the Video
- 1:32:59Maker class. Then, we create a B1
- 1:33:01object using the Travel Black Box (note
- 1:33:03: this implies the ST card capacity is
- 1:33:0664GB). After doing this, if we call the
- 1:33:08B 1.Make method to use the memory video
- 1:33:10creation feature, we can see that the
- 1:33:12result is displayed correctly. This
- 1:33:14method is inherited directly from the
- 1:33:15Video Maker class. However, another
- 1:33:17feature is said to be added here. This
- 1:33:19feature allows sending the created
- 1:33:20video directly via email. The class
- 1:33:22name is MailSender, and it looks like
- 1:33:24this. Calling the Send method sends the
- 1:33:26video via email. We will need to use
- 1:33:27this in our class as well. To enable
- 1:33:28multiple inheritance, we enter a comma
- 1:33:30after VideoMaker and add MailSender.
- 1:33:32This allows us to easily create a B1
- 1:33:34object, create a memory travel video
- 1:33:35using the Make method, and then send it
- 1:33:37via email by calling the Send method of
- 1:33:39the MailSender class. So far, the
- 1:33:41Travel Black Box has inherited
- 1:33:43multiplely from three A classes. We
- 1:33:56decided to create a computed version of
- 1:33:58the Travel Mode Supported Black Box.
- 1:33:59The part being improved is the behavior
- 1:34:01of the Set Travel Mode method. Setting
- 1:34:03the device to Travel Mode for a given
- 1:34:05period and managing travel videos
- 1:34:06separately is good, but we want it to
- 1:34:08automatically produce a memory video
- 1:34:10and send it via email once the Travel
- 1:34:11Mode ends. Therefore, we decided to
- 1:34:13create the Advanced Travel Black Box,
- 1:34:15an improved version of the Travel Mode
- 1:34:16Supported Black Box. Since this class
- 1:34:18uses the functionality of the Travel
- 1:34:20Black Box while extending the features
- 1:34:21for when it is in Travel Mode, we can
- 1:34:23simply inherit from the Travel Black
- 1:34:24Box. However, when the Set Travel Mode
- 1:34:27method is called, it needs to not only
- 1:34:28set Travel Mode but also produce the
- 1:34:30video and send the email. In this case,
- 1:34:33the parent class's Set Travel Mode
- 1:34:34method can be redefined within the
- 1:34:36Advanced Travel Black Box class. In
- 1:34:39other words, we define three Travel
- 1:34:40Mode methods with the same name within
- 1:34:42the child class, and then use the Make
- 1:34:44and Send methods within them to handle
- 1:34:46video production and email sending. By
- 1:34:48doing this, the ` Set Travel Mode`
- 1:34:50method of the parent class, ` Travel
- 1:34:52Black Box`, is overridden by this
- 1:34:54method defined in the child class. In
- 1:34:56other words, if the child class does
- 1:34:58not define the same method anew, it
- 1:34:59uses the parent class's method; if it
- 1:35:01does, it uses the child class's method.
- 1:35:04This is called method overriding. It
- 1:35:05involves redefining a method. So, if
- 1:35:07you call the ` Set Travel Mode` method
- 1:35:09with a B1 object created from the `
- 1:35:11Travel Black Box` class, the execution
- 1:35:13result is as shown. However, if you
- 1:35:14call the same method with a B2 object
- 1:35:16created through the ` Advanced Travel
- 1:35:18Black Box` class, through method
- 1:35:20overriding, it accommodates code that
- 1:35:22includes the operations of video
- 1:35:24production and email sending. What we
- 1:35:37will learn this time is "Pass." We have
- 1:35:38already used this when defining classes
- 1:35:40earlier. "Pass" is used to indicate
- 1:35:42that you should leave it as "Never" for
- 1:35:43now, as we will cover it later. When
- 1:35:45creating a class, we can list the
- 1:35:47methods we have planned in advance like
- 1:35:49this. As we go along, we might come up
- 1:35:51with other things that need to be done.
- 1:35:52However, you might not be able to write
- 1:35:54the code for this part right now
- 1:35:55because you are working on other tasks
- 1:35:56first. In such cases, if you simply
- 1:35:58write "Pass," the code is in a
- 1:35:59completed form for now, so it will not
- 1:36:01cause an error when called via a format
- 1:36:02method. Therefore, you can use Passes
- 1:36:04like this to create the basic structure
- 1:36:06of a class first, and then implement
- 1:36:07the actual behavior of the methods one
- 1:36:09by one. Passes can be used not only for
- 1:36:11class methods but also for loops like 4
- 1:36:13, if statements, and functions. What
- 1:36:26should you do if you tap your
- 1:36:27transportation card on a bus and are
- 1:36:29told that your balance is insufficient?
- 1:36:30What if you set up a tent at a campsite
- 1:36:32to cook but forgot to bring ingredients
- 1:36:34? Or what if you are waiting for an
- 1:36:37important call on your smartphone but
- 1:36:38the battery is about to die? As we go
- 1:36:40through life, we encounter various
- 1:36:42kinds of unexpected situations like
- 1:36:43this. The same applies to computers.
- 1:36:45For instance, you should not attempt to
- 1:36:46divide a number by zero, convert
- 1:36:48something that cannot be converted to a
- 1:36:50number into an integer, or access index
- 1:36:51999 of a list that only holds two
- 1:36:53values. In programming, this situation
- 1:36:55is referred to as an error, and if this
- 1:36:57error is not handled, the program will
- 1:36:59stop working. Ensuring that the program
- 1:37:01can operate correctly even when
- 1:37:02encountering such situations is called
- 1:37:04exception handling. You can use
- 1:37:05exception handling as follows: Enter a
- 1:37:07colon (*) for the trie, then write the
- 1:37:09desired number of statements on the
- 1:37:11next line. Below that, enter an
- 1:37:13exception colon (*) to write the
- 1:37:14statements to be executed only if an
- 1:37:16error occurs while executing the
- 1:37:17preceding statements. These statements
- 1:37:19will not be executed unless an error
- 1:37:20occurs. The statements written after
- 1:37:22the extra colon (*) below that are
- 1:37:23executed only when the statements at
- 1:37:25the trie location have operated
- 1:37:26successfully without any issues.
- 1:37:27Finally, enter a final colon (*) to
- 1:37:29write the last statement; this
- 1:37:31statement is always executed last,
- 1:37:32regardless of whether an error occurs
- 1:37:34while the statements in the trie area
- 1:37:36are being executed. Let's look at an
- 1:37:38example. First, I will attempt to store
- 1:37:40the value obtained by dividing a number
- 1:37:42by 2 into the trie. Let's assume that
- 1:37:44Num is 3 and Num 2 is 0. We will output
- 1:37:46the result here, but since no number
- 1:37:48can be divided by zero, an error will
- 1:37:50occur for 3 or 0 based on this
- 1:37:51assumption. Therefore, I will use an
- 1:37:53exception to print a message indicating
- 1:37:54that an error occurred. However, what
- 1:37:56if Num 2 were 3 instead of 0? Yes, yes.
- 1:37:59There would be no problem with the
- 1:38:00operation for 3 or 3. To prepare for
- 1:38:02such cases, I will add a statement
- 1:38:03using L to indicate that the operation
- 1:38:05was successful. We do this because we
- 1:38:06don't know what the values of Num On
- 1:38:08and Num To will be during actual
- 1:38:09programming. Finally, I add the
- 1:38:11execution type statement using Final.
- 1:38:13This statement is executed
- 1:38:15unconditionally, regardless of whether
- 1:38:16an error occurred earlier. So, when you
- 1:38:18run the completed program, if you
- 1:38:20execute it for the case where Number 1
- 1:38:21is 3 and Number 2 is 0, you can see
- 1:38:23that the execution result comes out
- 1:38:24like this. First, since an error occurs
- 1:38:26while this statement is executing, it
- 1:38:27jumps to the ` except () ` block to
- 1:38:29execute the code there, and finally
- 1:38:30executes ` finally () `. If Number 1 (1
- 1:38:32) is 3 and Number 2 (2) is also 3, this
- 1:38:33operation will not be a problem.
- 1:38:35Therefore, the ` print` statement below
- 1:38:37is executed, the statement processed
- 1:38:38upon successful execution is printed,
- 1:38:40and finally, the ` finally () ` block
- 1:38:42is executed, so you can confirm that
- 1:38:43the execution result is like this. For
- 1:38:45reference, exception handling can be
- 1:38:46written in four main types. A Trie `
- 1:38:48Exception` handles the error when it
- 1:38:49occurs, while a Trie ` finally () `
- 1:38:51does not handle the error separately;
- 1:38:53it simply executes the last statement
- 1:38:54to be executed and moves on. Thus, a
- 1:38:56Trie must always be paired with either
- 1:38:58an ` except` or a ` finally () ` block.
- 1:39:01The ` try` block handles errors if they
- 1:39:03occur and proceeds with normal
- 1:39:04operation if no errors occur, whereas
- 1:39:06the ` finally` block adds to that by
- 1:39:07executing a final statement regardless
- 1:39:09of whether an error occurs or not,
- 1:39:11thereby terminating the process.
- 1:39:24Imagine going to borrow a book from the
- 1:39:25library for the first time in 100 years
- 1:39:26. You find the book you want and go to
- 1:39:27the librarian, but are told it cannot
- 1:39:29be borrowed. It's absurd, isn't it? So
- 1:39:31you ask why. When you ask, the answer
- 1:39:33you get is simply that it cannot be
- 1:39:34borrowed. You get angry, right? Because
- 1:39:36they don't tell you the reason why it
- 1:39:37can't be borrowed; they just say it
- 1:39:38can't be done. Errors can occur at any
- 1:39:40time, whether in real life or in
- 1:39:42programming. However, you need to know
- 1:39:44at least what kind of error it is or
- 1:39:45why the situation occurred in order to
- 1:39:46deal with it. If the librarian explains
- 1:39:49the reason why a loan cannot be made—
- 1:39:51such as the maximum of five books are
- 1:39:53already checked out, there are overdue
- 1:39:55books, or membership registration is
- 1:39:57required—you would be able to resolve
- 1:39:59the issue or handle the situation
- 1:40:01accordingly. I have slightly modified
- 1:40:03the source code we covered earlier when
- 1:40:04studying exception handling. This code
- 1:40:06assigns the result of dividing 1 by 2
- 1:40:08to ` err` and outputs the result using
- 1:40:11a print statement. Assuming 1 is 3 and
- 1:40:132 is 0, an error would occur at this
- 1:40:15point. When I run the code as is, it
- 1:40:17kindly informs me that an error has
- 1:40:18occurred. But what kind of error
- 1:40:20occurred? And why did it happen? We can
- 1:40:23see here that the error occurred
- 1:40:25because we divided by 0, but without
- 1:40:26comments like this, it might have been
- 1:40:28difficult to identify the cause of the
- 1:40:30error. To find out what the error is
- 1:40:33and why it occurred, you can write `
- 1:40:35exception as err` between ` except` and
- 1:40:38a colon, and then print ` err` to check
- 1:40:40the error details. Since ` exception`
- 1:40:43is the base class for all errors,
- 1:40:44simply writing it like this allows you
- 1:40:46to check for all types of errors.
- 1:40:48However, there are cases where
- 1:40:49different handling is required for
- 1:40:50different situations, such as when all
- 1:40:52five books are checked out at our
- 1:40:53library, you might return one first; if
- 1:40:55there are overdue books, you might
- 1:40:56bring them back; or if membership
- 1:40:58registration is required, you might
- 1:40:59attempt to sign up immediately. Now,
- 1:41:01let's return to the code. In our code,
- 1:41:03we can try this: by adding an exception
- 1:41:05type and specifying "Zero Division
- 1:41:07Error" to handle the error that occurs
- 1:41:09when dividing by zero, the statement "
- 1:41:11Cannot divide by zero" will be executed
- 1:41:13. Alternatively, if 2 is not the
- 1:41:15numeric 0 but the character 0,
- 1:41:16operations between an integer and a
- 1:41:18character type are impossible. In that
- 1:41:20case, by adding a new exception type
- 1:41:21and setting the error type to "Type
- 1:41:23Error," you can verify the output
- 1:41:24indicating that the value's format is
- 1:41:26abnormal. In other words, when you want
- 1:41:27to handle different types of errors,
- 1:41:29you can write multiple exception
- 1:41:30statements like this to handle the
- 1:41:31specific exceptions that correspond to
- 1:41:32those errors. Alternatively, if none of
- 1:41:34the previously listed errors apply, you
- 1:41:36can handle the final case using an
- 1:41:38exception. For reference, you can use
- 1:41:40any name you like for this part. If you
- 1:41:43type "Python errors" into Google and
- 1:41:45scroll down the links that appear, you
- 1:41:47can check the various types of errors
- 1:41:50and their explanations. While coding,
- 1:42:03there are times when you need code you
- 1:42:05created last time again. If you are
- 1:42:06working within the same file, you can
- 1:42:08create a function to use it together,
- 1:42:10but what if it is in a different file?
- 1:42:12There is something called a module. In
- 1:42:13Python, a module refers to a single
- 1:42:15Python file where code is written. This
- 1:42:17module may define variables, functions,
- 1:42:19classes, and the things we have studied
- 1:42:21so far. If you need a specific piece of
- 1:42:23code or feature created previously
- 1:42:24while working in a new environment, you
- 1:42:26can bring it over and use it like this.
- 1:42:27First, let's create a very simple
- 1:42:29module. Good job. I will create a file
- 1:42:31named ` file` and set up a simple
- 1:42:33function called ` set` that prints a
- 1:42:34sentence like this. There are two ways
- 1:42:37to use this module in a new file. The
- 1:42:39first method is to open the import
- 1:42:41prompt and enter the module name. The
- 1:42:43second method is to open the prompt,
- 1:42:45enter the module name, open the import
- 1:42:47prompt, and enter the variable or class
- 1:42:50you want to use. Let's try the first
- 1:42:52method first. Open the import prompt
- 1:42:54and enter "Good Job"—that is, "Good
- 1:42:55Job. File"(excluding the dot extension)
- 1:42:59. Below, the module name "Good Job. Say
- 1:43:03" allows this module to use the
- 1:43:05function "Say." The execution result
- 1:43:07looks like this. For the second method,
- 1:43:09open the prompt, enter the module name
- 1:43:11"Good Job," open the import prompt, and
- 1:43:13then enter the name of the function "
- 1:43:15Say" that we want to use. This time,
- 1:43:17you can use the function "Say" without
- 1:43:19the module name "Good Job," just as if
- 1:43:21it were defined in this file. The
- 1:43:23execution result is the same. The
- 1:43:24difference between the two is that the
- 1:43:26first method means importing the entire
- 1:43:28module to use all the functions
- 1:43:29included within it, while the second
- 1:43:31method means using only the necessary
- 1:43:33parts of the module—in our case, just
- 1:43:35the ` say` function. Although we can
- 1:43:37create modules like this, Python
- 1:43:39already provides numerous modules. One
- 1:43:41representative module is ` random`. For
- 1:43:45instance, if you want to randomly
- 1:43:46select one from ` guis`, ` by`, and `
- 1:43:48bo`, you can import the ` random`
- 1:43:49module, create a list with values named
- 1:43:51` scissors`, ` by`, and ` bo`, and then
- 1:43:53use the ` choice` function from the
- 1:43:54imported ` random` module to input the
- 1:43:56list and run the code. Since the result
- 1:43:59varies each time, you can use it here.
- 1:44:01This means we don't need to create a
- 1:44:03separate function to randomly select a
- 1:44:04value from this list; we can simply use
- 1:44:06the existing module. If you search for
- 1:44:09"list of Python modules" on Google, you
- 1:44:11can check the modules automatically
- 1:44:13provided when you install Python
- 1:44:14through this link. If you click on `
- 1:44:17random`, you can see it there. You can
- 1:44:19also find detailed explanations and
- 1:44:21examples of modules here. We previously
- 1:44:34referred to a single Python file
- 1:44:36containing code as a module. When
- 1:44:37multiple modules like this are
- 1:44:38displayed, it is called a package. A
- 1:44:40package is essentially a single folder
- 1:44:42containing multiple modules. Let's
- 1:44:43continue from the .py file we created
- 1:44:46when studying modules, create another
- 1:44:48.py file, and define a function called'
- 1:44:50by 'like this. Then, let's create a
- 1:44:52folder called' I'm Coding 'within our
- 1:44:54workspace and move these two files
- 1:44:56inside it. Next, we will use the' I'm
- 1:44:58Coding 'package in a new file located
- 1:45:00in the same position as the' I'm Coding
- 1:45:01'folder—for example, a .py file named
- 1:45:03' pract s.py '. So, in Visual Studio
- 1:45:05Code, the situation is as follows:
- 1:45:07there is a' I'm Coding 'folder, inside
- 1:45:09it are' goodbye.py 'and' good#job.py ',
- 1:45:11and outside,' prtic s.py 'is in the
- 1:45:13same location as the' I'm Coding '
- 1:45:14package. Now, to use the' I'm Coding '
- 1:45:17package, just like with modules, simply
- 1:45:19import it. Write the package name you
- 1:45:21want to bring in—that is,' I'm Coding
- 1:45:23'—then a dot, and then write the name
- 1:45:24of the module you want to bring in, for
- 1:45:25example,' Good Job '. Then, on the next
- 1:45:27line,' I'm Coding '. You can call the'
- 1:45:29Say 'function through' Say '. If you
- 1:45:31run it, you can see that "You did a
- 1:45:33great job" is printed, just as defined
- 1:45:34in the module. Another method is to
- 1:45:36open the prompt, type the package name
- 1:45:38"I am Coding," and then list the module
- 1:45:40you want to import after the import.
- 1:45:42For example, you would write "import
- 1:45:43Goodbye." On the next line, you can
- 1:45:44call the Goodbye. Bye bye Bye function
- 1:45:46like this. The execution result shows
- 1:45:48that "See you again" is printed, just
- 1:45:49as defined in the Bye module's Bye
- 1:45:51function. If you want to use both
- 1:45:53modules, open "I am Coding," import
- 1:45:55them, and then define the modules you
- 1:45:57want to import separated by commas.
- 1:46:00Then, you can freely use the functions
- 1:46:01within each module like this: Good Job.
- 1:46:03Say Goodbye. Bye and Bye. If you run
- 1:46:06the code, you can see the output: "You
- 1:46:08did a great job. See you again. This
- 1:46:21concludes all evaluations. Thank you
- 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.