Python Conditions & If, elif, Else With Suitable Example

Python Conditions

/ins>

The Python Conditions and the If Statements

This is the python conditional operator tutorial. And as a developer who is using this programming language, you should be aware of the fact that this programming language also uses a number of different usual logical conditions. These usual logical conditions actually originate from mathematics itself. And the examples of these conditions or of different python conditional are mentioned below.

  • Equals ( a == b )
  • Not Equals ( a != b )
  • Less than ( a < b )
  • Less than or equal to ( a <= b )
  • Greater than ( a > b )
  • Greater than or equal to ( a >= b )

These conditions are often used in a number of different if statements and various kinds of loops. However, you can also choose to use these conditions or the different python conditional in a number of other ways too. It is also important for you to know that the if statements are always written by using the ‘if’ keyword.

For example

As you must be clearly able to observe from the example that we have mentioned above that there are two different variables used. These two different variables are ‘a’ and ‘b’. These two variables are used in the python if statement so that an individual can check the truth behind the statement that b is actually greater than a. Here, the value of the variable a is 33 and the value of the variable b is 200. This shows that the variable b is greater than the variable a. This enables us to print on the screen that the variable ‘b is greater than a’.

 

The Indentations

While using this programming language if you wish to define any particular kind of scope in a code then it is important for you to use whitespace or indentations. However, when it comes to the other programming languages then in most of those programming languages curly brackets are often used. For example, if we try to run a python if statement without using any kind of indentations then we will get an error. This is depicted in the example that is mentioned below.

 

The Elif Conditions

While using this programming language, if you wish to convey the message that the condition mentioned previously was not true but you can also try this new condition then for that you will be required to use the elif keyword or the python if else.

For example

 

As you can see from the example that is mentioned above, that the first condition is not true for the case of this example since the variable a and the variable b are equal here. However, the elif condition or the python if else is actually true which will enable python to print ‘a and b are equal’ on the screen.

 

The Else

If you need to catch any particular thing that is not already caught by the conditions of for if python that was used earlier then, it is recommended that you should use the else keyword.

 

For example

As we can clearly observe from the example that is mentioned above, that a is actually greater than b. This means that the first condition which is mentioned here is not true. The elif condition which is used here is not true either. However, the else condition is true which will enable python to print ‘a is greater than b’ on the screen. It is also important for you to know that you can also choose to use the else keyword without first using the elif keyword or the for if python.

 

For Example

 

The Short Hand If

If you wish to execute a statement by putting it in a single line as a kind of an if statement or python if and operator. For example

 

The Short Hand If … Else

If you wish to execute a statement by using the two different keywords of if and else or python if and operator then you can also do that in a single line.

For example

If you wish to use a number of different else statements in a single line then you can do that in this programming language too. For example

 

The And

Before we move on to the examples of this concept, it is important for you to know that the and keyword is a logical operator. This particular keyword is often used by a number of different developers using this programming language to combine a number of different statements. For example, if you need to check if a is greater than b and if c is also greater than a then

 

The Or

If you need to combine a number of different conditional statements in this programming language then you can also do that by using the or keyword. This keyword of or is also a logical operator. For example, if you need to check if a is greater than b and if a is also greater than c then

 

 

With this, we finish the python conditional operator part of our Python tutorial.