
#bootcamp
#python
Logical expressions operators
Every programming language is able to execute on conditions, in nutshell it means that programming languages take true and false as boolean values and will execute depending on the logic we implement. You may be familiar with conditional expressions from math class as they are:
- < less than
- > greater than
- == equal
- != not equal
- <= less or equal
- >= greater or equal
Conditional statements
Pure English words and you will see that once we use them that they are pretty much self-explanatory.
- if execute condition when true
- elif additional condition(else if)
- else execute when all other conditions fail
Logical operators
Once we use them it will be easy to make sense of them.
- and more conditions are true
- or one or more conditions are true
- not condition is not true
So, let us write some code using all we talked about. We will create a script with examples using most of the expressions and we will nest some conditions, code will be provided with comments so the best way for you to get the hang of this is to read the code, tweak it and play around with it.
In this code, we will use intended, in Python, it usually consists of four spaces and if this convention is not respected you will get an indention error. I will explain this in the script as well.
#for longer comments we can use three quotes comments, known as docstrings
"""
here we use a colon (:), the colon is used to indicate the start of a code block
as in conditional statements, functions, and classes
"""
if 1 > 0:
#if the code starts here it would cause an indention error
#make indention
print("1 is greater than 0")
elif(2 > 1):
print ("2 is greater than 1")
else:
print ("End of statement with 0 executions")
if 1 >= 1:
print("1 is greater or equal than 1")
if 1 != 1:
print("This will not execute because 1 is equal 1")
And what about operators? Do not worry, we will code an example for them too.
#using AND operator, It will execute if both conditions are true
if 1 < 2 and 2 < 3:
print("1 < than 2 and 2 < 3 --both are true")
#using OR operator, It will execute if both conditions are true
if 1 < 2 or 2 < 3:
print("1 < than 2 or 2 < 3 --atleast one is TRUE")
#using NOT ! operator
if not 1 > 2:
print("Will execute if condition is false-positive")
#using more operators combined, if first two fail OR condition still can execute
#here we should look at (1<2 and 2<3) as one segment
if 1 == 2 and 2 == 3 or 1==1:
print("OR made this possible to execute")
We can even nest statements into other statements.
if 1 < 2:
if 3 > 4:
print(":)")
else:
print(":(")
else:
print("Coffe time!")
It would be worth mentioning one more operator, the modulo operator %. This operator will produce true if the remainder of the number is 0.
#here we use brackets () to clarify the precedence
if (6 % 3) == 0:
print("6 divided by the 3 is 2, and the remainder is 0")
elif (7 % 3) == 0:
print("This will produce remainder 1, since 3*2=6")
Statements can also work with boolean values alone, without the need for expression operators.
#True and values greater or less than 0 will execute the code block
if(True or 1):
print("It is True")
if(-1):
print("Submerged")
#False and 0 will not execute the code block
if(False or 0):
print("It is False")
[root@techtoapes]$ Author Luka
Login to comment.