This is Part 2 of the Mastering Python for Blockchain series. It will explore the Python conditional statements, namely if, elif and else. We will also explore comparison operators and, or and not. But before you delve into this second part, visit Part 1 of this Mastering Python for Blockchain series.
I will suggest you to download the latest version of Python which will help you to practice coding and compiling programs. In case you do not have Python installed in your system you can use this link to online run your Python codes.
I also suggest you to visit the article here to learn the basic concepts of Blockchain.
Mastering Python for Blockchain—>> # for comments
- In Python, # is used for adding comments in a program. That means whatever you write after # till the end of a line is not executed by the Python interpreter.
For example, run the below program.
The result is 12.
- Now run the below code,
What do you think? I have added # I can write anything here without any effects. But when you run this you will get the same result 12. This is because Python interpreter doesn’t consider anything after the hash #.
In fact in many of the code examples given in this article, I will be using # to add comments for clarity.
- Another example,
It is a simple if and else statement in Python. We will explore these in detail in the upcoming sections. When you run this code the result is
- Now consider,
Again as you run the code the result is
Note: The operator == is used to test equality. So x==4 tells the Python interpreter to test whether x is equal to 4 or not. The operator = is used to assign a value. Hence x=4 means assigning a value 4 to the variable x.
Mastering Python for Blockchain—>Continue lines with \
- Codes with reasonably shorter lines are more readable. The recommended length (not required) is 80 characters per line.
- Now if after a suggested length, you still are not able to complete your message in a code you can use the \ (backlash).
- The Python interpreter simply treats all lines separated by \ as the same line.
The result is
Mastering Python for Blockchain–>if, elif and else statements
- Consider the below code
The result is
We used the conditional statements if and else.
The 1st line of the code assigns a value of 6 to the variable x.
The second line performed the conditional statement if. It then tests whether x is equal to 6 or not. It applies the condition that if x is equal to 6 then print “It is an even number”.
Note: We use colons : after every conditional statement if, elif (we will see) and else . An indent appears after every conditional statement.
The third line calls the print() function.
The fourth line performs the conditional statement and states if not or else (x is not equal to 6) print “It is odd”.
- We can have tests within tests by using multiple if and else statements.
The first and the second lines of the code assigns values of 4 and 5 to x and y respectively.
The 3rd line performs a conditional statement and tests whether x is equal to 6. If yes, then Python goes to the indented if x>2.
Since x is not equal to 6, Python goes to the else in line 8. Here in line 9 Python tests whether x is equal to y. If yes it goes to the indented print() function in line 10.
Again since x is not equal to y, Python goes to the else in line 11 and calls the print () function in line 12 to print the below
What do you think will be the output of the below code
- If we want to test more than two possibilities, then use if for the first, elif (else if) for all the middle ones and else for the last. Check the below code.
The output is
The output is
Note: Words or strings are assigned to a variable (x in the above example), by using " "
In all the above examples we have seen the == operator to test equality. Similarly != is used to test inequality.
Comparison Operators in Python
The below table presents the various comparison operators used in Python.
|less than or equal to||<=|
|greater than or equal to||>=|
Performing these operators will output the Boolean values of True or False.
Run the below code.
The outputs will be
Logical or Boolean operators
- We learnt about the standard operators in Python, i.e. +, -, *, **,//,%,/.
- Python also uses Logical Operators namely and, or, not.
- These Operators provide Booleans True or False as the output.
|and||x and y||True; if both the operands are true|
|or||x or y||True if one of the operands is true|
|not||x not y||True: if none of the operands is true|
- Note: Operators are used to perform operations on values or variables. These values or variables are know as Operands.
Let us consider the below
x<10 and y<10
x<10 and y<6
x<10 or y<6
x<10 or y<10
x<10 not y<10
x>10 not y>10
Check the below code performing the and operator
What do you think the output be?
Another one for the or operator.
In this program, the conditional statement if in line 4 states that if either of the two operations x<0 or y<0 is true then print “At least one of the numbers is negative”.
The else statement in line 6 states that for all other possible conditions, print “Both the numbers are positive”
Now the code for the not operator.
True & False in Python
The following are False in Python including the Boolean False.
All other values are True in Python.
In operator in Python
- In operator is used to replace multiple or operators.
- Consider the below program to find out whether a letter is a vowel or not.
Now rather than using multiple or operators, we can use in operator as seen in the below code example.
The code above can also be written as