After continuing my study of Python, I was able to create several games using conditional statements and loops, which was very enjoyable.
Even though the games were about the same topic, I found it particularly beneficial that I could now think about the algorithms, whereas before I was solely focused on writing the code.
I. Overview
To understand conditional statements in Python. Sequential structure, selection structure, iterative structureYou need to understand this. The sequential structure is "Like water flowing from top to bottom in a sequence"And the selection structure divides the decision points. "This refers to a situation where something deviates from its usual course when certain conditions are met."It involves repeating a specific action, whether from the beginning to the end or during an intermediate process. The structure of a conditional statement is: if, if...else, if...elif...else, nested ifThere are different types of loops, including the "for" loop, which is a construct used to repeat a specific section of code a certain number of times. for and while loops, nested for/while loops It utilizes the structure.

You can find detailed information about conditional statements and loops on the following website:
II. Python Conditional Statements
To understand conditional statements, it is necessary to understand sequential, selection, and iterative structures. Sequential structureis SequentialThe meaning of the English word "이라는" as defined in a Korean dictionary.
Following the established order.
Source: Standard Korean Dictionary
The structure is designed so that each stage, from the beginning to the end, is executed in a sequential order.It's the simplest yet most commonly used structure. Think of a waterfall flowing from top to bottom – that's an easy way to understand it! For practical examples, consider your own daily routine: for instance, the activities you do after arriving home late at night, such as showering, writing in a journal, doing laundry, or cleaning.
Selection structureThis refers to one of two possible scenarios. One is chosen, and the process proceeds in that direction.This means that it represents a choice. If you think about the decision we make at a fork in the road while hiking, it becomes easier to understand. Repetitive structureIt involves a specific stage Run the process multiple times.This is a repetitive pattern: watching YouTube before bed at home.
Regarding coding: The conditional statements are essential and must be understood and addressed.It is widely used and can be used to create various operating program structures based on conditional statements. Conditional statements were previously used when learning English. RecipesWhen considering this, it's helpful to think about the decision-making process at a point where there are multiple options. "If a certain condition arises, I will choose this path."This is because it operates based on a conditional statement. For example, when choosing a café with a friend, the condition might be that it should be a large café or that the coffee should be good.
The simplest conditional statement in Python is: ifThis is intended for individual use only.
score = 100
if score > 90 :
print("A학점입니다.")As shown in the example, the `if` statement is used independently, with the condition placed between `if` and the colon (`:`). The code within the `if` statement only executes when the condition matches (evaluates to true).
Secondly if... else There is a grammatical structure.
score = 80
if score > 90 :
print("A학점입니다.")
else :
print("F학점입니다.")At first glance, it appears to be simply adding an "else" statement, but its function is to execute the code within the "else" block when the "if" condition is not met (i.e., is false). If you've learned to code so far, I'd like to share one tip:
※ As will be described below, using "if...else" statements requires careful consideration. If the condition in an "if" statement is unclear, the "else" statement may execute unexpectedly, causing the program to behave in unintended ways.
Third if...elif...else There is a grammatical structure.
score = 60
if score > 90 :
print("A학점입니다.")
elif score > 80 :
print("B학점입니다.")
else :
print("F학점입니다.")Unlike other programming languages, Python... switch ~ case syntaxThere is no "switch ~ case" syntax. Instead, the "switch" statement uses a series of "case" statements, where a condition is checked, and the corresponding code block is executed. instanceFirst, define specific actions. Then, execute those actions. I recommend learning more about this through studying Java or C.
This structure includes an `elif` statement between the `if` and `else` statements. The `elif` condition checks whether the condition in the `if` statement is false. If the `elif` condition is met, the code within that `elif` block is executed. Otherwise, the code within the `else` block is executed. Multiple `elif` statements can be used, allowing for more granular control over the conditions.
Finally, Nested if statements This is a phrase.
price = 1000
if price > 100 :
if price > 500 :
print("가격이 500원 이상입니다.")This refers to a nested `if` statement, where one `if` statement is contained within another. This is not limited to the `if` statement alone; you can also include additional `if`, `if ~ else`, or `if ~ elif ~ else` statements within `if ~ else` and `if ~ elif ~ else` structures. As shown in the example, there is a data point with a price of 1000 won. This data meets the condition of being greater than 100 won, and also satisfies the additional condition of being greater than 500 won. Since it also meets this condition, the output statement "The price is 500 won or more" is executed.
※ And another tip!
When using conditional statements in Python IndentationProper indentation is crucial. It needs to be applied consistently in loops, functions, and classes, and the standard method is to use four spaces. In other languages, conditions are separated by parentheses '()', and the code to be executed within them is separated by curly braces '{}'.However, in Python, indentation is used to distinguish between conditional statements and the code to be executed. It's crucial to follow this rule, as failing to do so will result in errors.
I practiced conditional statements through practical exercises, receiving missions and directly writing code. People wrote code in various ways, and there isn't necessarily a single correct answer, so the code I submitted should be viewed as a reference. You can find different solutions to the same problem written by others, so it would be helpful to refer to them. If the code initially seems long or strange, but it works correctly, then you've successfully learned the conditional statements!
The mission is to implement a program that takes a score as input and outputs a grade based on the following criteria: 90 or above = A, 80 or above = B, 70 or above = C, 60 or above = D, and anything below = F. (Note: I have included the code I wrote below, so I recommend solving the problem first and then reviewing the code.)
score = int(input("점수를 입력해주세요 : "))
if score >= 90 :
print("A", end="")
else :
if score >= 80 :
print("B", end="")
else :
if score >= 70 :
print("C", end="")
else :
if score >= 60 :
print("D", end="")
else :
print("F", end="")
print("학점입니다.")Ⅲ. Python Loops
Loops are used to repeat a set of instructions. A process that is repeated multiple times.It's a technique where you explicitly define how many times a specific part of your code should be repeated and then execute it that many times. The main benefit is that it allows you to reduce repetitive, unnecessary code, potentially shrinking hundreds of lines of code down to just a few!
To use loops in Python, it is necessary to understand both the `for` loop and the `while` loop. The `for` loop 'for variable in range(start_value, end_value + 1, increment_value):'It is written in a specific format.
for i in range(1, 11, 1) :
print(i, "", end="")
출력 결과 => 1 2 3 4 5 6 7 8 9 10scopeThis refers to the `range()` function in Python, which returns a sequence of numbers within a specified range. When used in a `for` loop, the first argument of `range()` specifies the starting number. If it's set to 1, it will start counting from 1, and if omitted, it defaults to starting from 0. We'll start from zero. While I initially explained the second variable as "final value + 1," I'm simplifying it. This phrase is repeated several times.Deciding whether to use it is straightforward. If no starting value is specified, it defaults to starting from 0. If the ending value is 10, it will repeat 10 times. However, in this case, it's from 0 to 9, which is 10 repetitions. In the example above, it's from 1 to 10, which is 10 repetitions. The reason it's 11 instead of 10 is because it starts from 1, not the default value of 0. Therefore, it needs 11 to output up to 10. The last variable is the increment value, which is the starting value. How much has it increased?Determine whether to increment or not. If the value is 1, add 1. If the value is 2, increment and continue. In the example above, if the value changes to 2, the output will be '1 3 5 7 9'.
※ The "for" loop can also be nested within other "for" loops, similar to how the "if" statement can be nested. This also applies to the "while" loop; you can nest different types of loops, such as "for," "while," and "if," within each other.
※ In Python, the "for" loop is similar to the "foreach" loop in languages like Java and JavaScript. However, a key difference is that in Java, when using a "for" loop, setting the variable "i" to -1 or using "i--" allows you to go back to the beginning or the previous iteration. This is not possible in Python's "for" loop.
While the syntax allows for specifying a condition to determine when the loop should terminate, or using a True value to create an infinite loop. However, an infinite loop will cause the program to run indefinitely, so it needs to be stopped. pauseThe `break` statement can be used to terminate a loop when a specific condition is met. This can be achieved using the `if` statement.
count = 0
while True :
count += 1
if count == 10 :
break
while count < 10 :
count += 1I've written an example using the `while` loop that simply repeats 10 times. Since the `while True` loop is an infinite loop, I've used `if` statements and `break` to specify a separate time for loop termination. In the second example, the termination point is specified after the `while` loop. When a condition comes after the `while` loop, it's important to remember that the loop continues as long as the condition is true and terminates when it's false. It's crucial to carefully define the condition. Initially, I found this behavior confusing, and I often didn't terminate the loop. However, by repeatedly writing the code, I was able to understand it!
※ Regarding the phrases "break" and another phrase that can be used with the "while" keyword continue There is a `continue` statement. Unlike the `break` statement, which terminates a loop, `continue` skips the code block that follows it and returns to the beginning of the loop. For example, it can be used to skip odd numbers when printing even numbers from 1 to 100.
Before I share some helpful tips for completing missions, I want to start by discussing some things I've learned firsthand.
※ Code format: This refers to a code-like format that resembles a program code, but is written in either Korean or English. 'Pseudocode'It is called a "doctor code," and it doesn't function directly, but rather... Understand the flow of the program's algorithm.This can be helpful. I encourage you to look for other good examples and, to summarize, when working on a project, you should categorize the requirements and checklists, and then write in Korean how the corresponding code should be implemented. This is useful not only in Python but also in other programming languages, so I recommend giving it a try!
Mission: Let's print the multiplication table (99 times table) when practicing loops!
* There are various ways to output the information, so please try different methods to see what works best for you.
# 예시 1
for i in range(2, 10, 3) :
for j in range(1, 10) :
if (i+2) < 10 :
print(i, "X", j, "=", i*j, "\t", (i+1), "X", j, "=", (i+1)*j, "\t", (i+2), "X", j, "=", (i+2)*j)
else :
print(i, "X", j, "=", i*j, "\t", (i+1), "X", j, "=", (i+1)*j)
print()
# 예시 2
for i in range(2, 10, 1) :
for j in range(1, 10, 1) :
print(i, "X", j, "=", i*j)
print()Review
Today's lesson was on conditional statements and loops. While the initial part was somewhat dry, focusing on explaining the concepts, I found the practical exercises to be really enjoyable. 😊 It was great to be able to apply what I learned and work on creating my own programs and logic. I'm looking forward to learning about lists, methods, and classes next, and I hope to be able to tackle even more diverse tasks and create even more complex logic. 🥰
Log in