Python | Conditional Statements | If Statement

The if statement executes a block of code if the condition is met, otherwise it ignores it. The syntax for an if statement in Python is as follows:

if [condition is true]:
	do something

As a short and simple example, I have created a .py file (the same file named in my Documents folder.

option_one = input('Type number 5 here: ')
if option_one == 5:
	print('number 5 is your option')

Now, go to terminal, switch directory to Documents and run the file.

Python conditional statements

The user is prompted to enter number 5. If the user enters number 5 then the message “ number 5 is your option” is printed on the screen. Number 5 is assigned as value of the option_one variable. The if statement checks if the value of the variable option_one is 5 and if it is then it prints the message on the screen.

We didn’t add an else statement for the case when the user enters another number, not 5.

Leave a Reply