Control Flow: An Introduction
This is the Control Flow of your program. In Python, your script will execute from the top down, until there is nothing left to run. It is your job to include gateways, known as conditional statements, to tell the computer when it should execute certain blocks of code. If these conditions are met, then run this function.
Over the course of this lesson, you will learn how to build conditional statements using boolean expressions, and manage the control flow in your code.
Boolean Expressions
A boolean expression is a statement that can either be True or False.
Relational Operators: Equals and Not Equals
We can create a boolean expression by using relational operators.
Relational operators compare two items and return either True or False. For this reason, you will sometimes hear them called comparators(비교 측정기).
The two boolean operators we’ll cover first are:
- Equals: ==
- Not equals: !=
>>> is the prompt when you run Python in your terminal, which you can then use to evaluate simple expressions, such as these.
Why is the last statement false? The '' marks in '7' make it a string, which is different from the integer value 7, so they are not equal. When using relational operators it is important to always be mindful of type.
Boolean Variables
Before we go any further, let’s talk a little bit about True and False. You may notice that when you type them in the code editor (with uppercase T and F), they appear in a different color than variables or strings. This is because True and False are their own special type: bool.
True and False are the only bool types, and any variable that is assigned one of these values is called a boolean variable. Boolean variables can be created in several ways. The easiest way is to simply assign True or False to a variable:
지금부터는 질문들 여기에다가 적어야겠당 - !
True and False always need to be capitalized and do not have quotation marks.
If Statements
Understanding boolean variables and expressions is essential because they are the building blocks of conditional statements.
If it is raining then bring an umbrella.
Right, "it is raining" is the boolean expression, and this conditional statement is checking to see if it is True.
If "it is raining" == True then the rest of the conditional statement will be executed and you will bring an umbrella.
This is the form of a conditional statement:
You’ll notice that instead of “then” we have a colon, :. That tells the computer that what’s coming next is what should be executed if the condition is met.
Relational Operators II
- Greater than: >
- Less than: <
- Greater than or equal to: >=
- Less than or equal to: <=
Let’s say we’re running a movie streaming platform and we want to write a function that checks if our users are over 13 when showing them a PG-13 movie. We could write something like:
If age is greater than or equal to 13 it will return True.
문제와 답
Boolean Operators: and
you can build larger boolean expressions using boolean operators. These operators (also known as logical operators) combine smaller boolean expressions into larger boolean expressions.
- and
- or
- not
even though part of the expression is True, the entire expression as a whole is False because the other statement is False.
Boolean Operators: or
In English, or implies that if one component is True, then the other component must be False. This is not true in Python. If an or statement has two True components, it is also True.
at least one True component is True,
-> 해결. 괄호 붙여야해!
Boolean Operators: not
This operator is straightforward: when applied to any boolean expression it reverses the boolean value. So if we have a True statement and apply a not operator we get a False statement.
Oranges are not a fruit.
Here, we took the True statement oranges are a fruit and added a not operator to make the False statement oranges are not a fruit
This example in English is slightly different from the way it would appear in Python because in Python we add the not operator to the very beginning of the statement.
Else Statements
once you start including lots of if statements in a function the code becomes a little cluttered and clunky. Luckily, there are other tools we can use to build control flow.
else statements allow us to elegantly describe what we want our code to do when certain conditions are not met.
else statements always appear in conjunction(접속사) with if statements. Consider our waking-up example to see how this works:
In this way, we can build if statements that execute different code if conditions are or are not met. This prevents us from needing to write if statements for each possible condition, we can instead write a blanket else statement for all the times the condition is not met.
all it did was check if the user’s age was over 13 and if so return True. We can use an else statement to return a message in the event the user is too young to watch the movie.
답
Else If Statements
We have if statements, we have else statements, we can also have elif statements.
“else if”. An elif statement checks another condition after the previous if statements conditions aren’t met.
We can use elif statements to control the order we want our program to check each of our conditional statements. First, the if statement is checked, then each elif statement is checked from top to bottom, then finally the else code is executed if none of the previous conditions have been met.
Let’s take a look at this in practice. The following function will display a “thank you” message after someone donates to a charity: It takes the donation amount and prints a message based on how much was donated.
Take a second to think about this function. What would happen if all of the elif statements were simply if statements?
If you donated $1000.00, then the first three messages would all print because each if condition had been met.
But because we used elif statements, it checks each condition sequentially and only prints one message. If I donate $600.00, the code first checks if that is over $1000.00, which it is not, then it checks if it’s over $500.00, which it is, so it prints that message, then because all of the other statements areelif and else, none of them get checked and no more messages get printed.
if는 한 번에 한 가지 조건만을 본다는 얘기인듯.
Try and Except Statements
if, elif, and else statements aren’t the only way to build a control flow into your program. You can use try and except statements to check for possible errors that a user might encounter.
First, the statement under try will be executed. If at some point an exception is raised during this execution, such as a NameError or a ValueError and that exception matches the keyword in the except statement, then the try statement will terminate and the except statement will execute.
Let’s take a look at this in an application. I want to write a function that takes two numbers, a and b as an input and then returns a divided by b. But, there is a possibility that b is zero, which will cause an error, so I want to include a try and except flow to catch this error.
def divides(a,b):
1시간 28분 걸림.... 13문제.... 이러면 안돼... 아침 6시가 아니라 낮 12시가 돼도 못끝냄.
모르는건 pass pass 해야겠다...
'2020 > python (codecademy)' 카테고리의 다른 글
Loop (1) | 2020.04.15 |
---|---|
Working with Lists in Python (0) | 2020.04.15 |
Creating and Modifying a list in Python. (0) | 2020.04.15 |
Introduction to Functions (1) | 2020.04.14 |
Learn Python : Syntax (0) | 2020.04.14 |