Order of Operations
In mathematics operation, we should solve it in the following order : parentheses, exponents, multiplication, division, addition, and subtraction; which known as PEMDAS. It is important to know the proper order as it crucial for getting the correct answers when coding programs. where such operations are performed.
In mathematics operation, we should solve it in the following order : parentheses, exponents, multiplication, division, addition, and subtraction; which known as PEMDAS. It is important to know the proper order as it crucial for getting the correct answers when coding programs. where such operations are performed.
Let's try using IDLE :
Enter the following command :
5+3*2
When you press ENTER, the result is 11.
It is obvious that multiplication part comes first (3*2) and then adding 5, which result is 11.
Let's try again with open another file from IDLE.
On above examples there are two kind, which are :
5-3*2 and (5-3)*2
The results of the two statements above will be :
Let's try other examples with exponent :
There are two additional examples using exponential, 5^3*2 and 5^(3*2).
The result between that two are :
Comparison Operation
We will try to practice about comparison operations, which compare two values in Python.
In the IDLE, please type the following :
a=8
b=8
Then, test for its equality using the statement :
a==b
(Note : equality symbolize by 2 equal signs (==)joined together)
When you press ENTER, Python will answer it with True, as the value of a and b are the same/equal.
Let's change the value of a to 5
a=5
If we type a==b again, Python will answer it with False, since a=5 and b=8.
If we want to try compare two numbers if they are non-equal, we can use statement !=
Enter the following in the IDLE shell :
a!=b
As a=7 and b=9, the answer will be True
Instead of !=, we can use < or > which are more familiar.
Let's try this :
23>21
Then press ENTER. Since 23 is greater than 21, the answer is True.
Other trial :
25>31
Then press ENTER. Since 25 is lesser than 31, the answer is False.
Let's try with combination between less than or greater than with equal sign.
Example : 15<=15
With that above example, the answer is True
Other example :
15<=17
This will show answer True, as 15 is lesser than 17.
There is also combination the assignment operation with addition and subtraction operations, using the following symbols :
+= and -=Let's try to get better understanding.
Type a =10 and b=15
As the value of b is 15, if we enter b+=5, it will add 5 to b, which mean that new value of b is 20.
Then, a+b, or 10+20, the result will be 30.
For the minus sign, as current value of b is 20, if we enter b-=8, the new value of b is 12.
If we subtract a from b (a-b), the result is -2.
Logical Operation
There are three logical operations in Python, which are : and, or, and not.
Run the IDLE Python and let's type some example :
(5==5 and 18==18)
When we press ENTER, the result is True, since both statements (5==5 and 18==18) are true. And operation need that both statements, to be true to return True.
(5==5 and 18<17)
In above example, 5==5 is true but 18<17 is false. The result is False since one of the statement is false.
Let's try with three statements :
(5==5 and 18==18 and 18>17)
Since all three statements are true, it will also result in True.
Other example :
(5==5 and 18==18 and 18<17)
In the case above, as one of the statements are false, it will result False.
Other logical operation is Or.
On the shell IDLE, enter the statement :
(5==5 or 18<17)
On above operation, 5==5 is true but 18<17 is false. Since only one statement is required to be true when using Or operation, this statement is true.
(5==5 or 18>19 or 18<17)
In that sample above, regardless of the number of statements, the or operator will always return True, as long as one of the statements is true.
Last logical operator, Not, which its will goes before the statement.
Example :
not(8==8)
This will result False, since not negates the value of statement. Since 8==8 is true, put not before the statement make it false.
not(7==8)That will result True, since 7==8 is false but negates by not.
Comments
Post a Comment