DEV Community

Cover image for Python Conditional statement
EzeanaMichael
EzeanaMichael

Posted on • Edited on

Python Conditional statement

Most of us are familiar with making real-life condition statements like "if I have enough money, I will buy a ball" which gives the condition that if you have enough money you will purchase the item but if not you won't.
Python, like all programming languages, also has a way of writing conditional statements like this, with the use of "if….." Statement
An example is a code
image description
When run

Image description
The program won't do anything when it's less than the amount but if it's greater:

Image description

If the condition is fulfilled the operation is carried out, but if not it would be carried out.
In addition, in the if statement it's possible you want the program to run when the condition is not fulfilled here you use the "else" statement.
The else is designed so if the condition under the if statement isn't fulfilled it will be
Example
Image description
When run

Image description
Or

Image description

So if the money is greater than 500 it will send a message saying bought ball but if not it will send a message saying not enough funds.

Hope you enjoyed this short lesson, more on this will be uploaded next week, read, like, comment

Top comments (0)