Subtitle

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

An operator is a

symbol that tells Python to perform a


certain operation. You can think of them like
road signs in real life. For example, suppose
you're driving on a dangerous road and you spot an alert sign
to reduce speed, then you encounter a stop sign, and finally, assign
instructing you to turn right. You may not have realized that you were on a
dangerous road. These symbols help
keep you safe by instructing you to perform
a specific operation. Similarly, when Python comes across an operator that
you place in your code, it will also perform
that specific operation. These operations can be mathematical, logical,
and comparison. In this video, you
will learn about Math and logical
operators in Python. Most of the time, operators
work on two values. Math operators are used for simple and complex calculations.
It's essentially all
the same options as the calculator would have. Let me explain
this with examples of Math and logical operators. The first operator
I want you to know about is the addition
or plus operator. The plus sign is
a symbol that you must use when adding
numbers together. For example, 2 plus 3. To subtract numbers
from each other, you use the subtraction
or minus operator. Use the minus sign
to subtract numbers. An example of this is 3 minus 2. The division operator is
next, and the symbol you use for
it is a forward slash. Division is an
operation in which one number is
divided by another. For example, 35 divided by 5. The last operator
you need to know about is the
multiplication operator. The symbol we use for that
is the star or asterisk key. Use this to multiply
numbers with each other. For example, 7 multiplied by 4. Now let's explore
logical operators. Logical operators are
used in Python on conditional statements
to determine a true or false outcome. Let's explore some of these now. The 1st
logical
operator is named and. This operator checks for
all conditions to be true. For example, a is greater
than 5 and a is less than 10. The 2nd logical
operator is named or. This operator checks for at least one of the
conditions to be true. For example, a is
greater than 5, or b is greater than 10. The final operator is named not. This
operator returns
a false value if the result is true. For example, a is
not greater than 5. Operators are usually combined with conditional statements to
control the flow of a program that meets specific criteria. For example, let's say
a restaurant gives discounts based on the
following two conditions. Is the customer part of
the loyalty program, and did they spend over $100? To determine this, you can write
code using logical operators to check if a customer is in the loyalty program and
if they spent over $100. You'll learn more
about conditional statements in a later lesson. Now, let me
demonstrate how to use Python Math logical operators. Math operators
basically give you the same functionality as what you have on a
standard calculator, so you can perform
operations like addition, subtraction,
division, multiplication. I start with a simple
addition example. I'm using the print statements so the output displays
on my console. I type Print and in the
parentheses I add 2 plus 2. The value I expect back is 4. When I run this
statement, the value of 4 displays
in the terminal. For subtraction, I change the
plus sign to a minus sign. I click on the Run button and
the value displayed is 0. If I subtract 2 minus 2, the answer is 0. For division, I
changed the minus sign to
a forward slash. I type 35 forward slash
5 in the parentheses, I click the Run button and the result is the value of 7.0.
Just to note on this, the value returned is a float
instead of an integer. Now let's cover multiplication. I changed the forward slash
to a star sign that
represents multiplication. I type 25 asterisk 5. I click on the Run button and
get back the value of 175. That was a short introduction
to the math operators. Next, you'll explore
logical operators. Logical operators are used to control the flow of
your application. The logical operators are; and, or, and not. Let's cover the
different
combinations of each. In this example, I
declare two variables, a equals true and b
also equals true. From these variables, I use an if statement. I type if a and b
colon,
and on the next line, I type print, and in
parentheses and double-quotes, I type, all true. You'll learn about the
if statement shortly. But for now, just know
that this print statement will only be executed if
both a and b are true. The print statement of all true is displayed
in the terminal. If I change the value of b to false and I run the
statement again, nothing gets printed out. The reason for it is that the and
statement as a condition is both a and b to be true so that it will
print out the statement. Now let's cover the OR operator. I'm changing and to or,
and I click on the Run button. The all true value has
been printed out again. The reason for it is that
with the OR operator, if either a or b is true, the if statement is true. If I set
the values
of both variables to false and click on
the Run button, nothing gets printed out. This is because a is
false and b is false so the condition in the if
statement has not been met. In this last example, I'm going to demonstrate
the NOT operator. I'll keep the OR operator. Before or, I type if
not a in parentheses, then or cannot be
in parentheses, followed by a colon. I click on the Run button and the value
returned is all true. What that's doing, is it's looking for a
negation against a. So not a, is not
false, which is true. The negation of B, which results in
true B or condition, checks to see if either is true. Now I change the a
and the b to be true. I click on Run and
nothing gets printed out. The reason for that is that it's checking again for if
not a, essentially, if a is not true. In this case, a is true
and its negation is false, so it's not going to
meet that condition. Or not b also results
in false and does not meet that condition
as well because both are the negation of true. This is still not going to print out
any value because again, none of the conditions
are being met. That's a brief
introduction to using both Math and Logical
Operators in Python. Congratulations. In this video, you learned about Math
and logical operators. Great job. If you'd like to learn more about Math
operators in Python, there's an additional reading
at the end of this lesson.

You might also like