Saturday, July 2, 2022

What is the IF statement in Python?

The conditional execution is achieved using conditional statements and they give us the ability to check conditions and change the behaviour of the program. The simplest conditional statement is if statement and the general form can be written as:
if condition:
indented statement
In the previous code block the general form of the if statement is given. In the first row after the if word there is a condition and after that the colon character (:). All lines after if statement is indented.

If the logical condition is True than the indented statement is executed. If the logical condition is False, the indented statement is skipped.

The if statement has a similar structure as function definitions of for loops. The statement consists of a header line, ending with a colon character (:) followed by an indented block. These types of statements are called compound statements since they stretch across multiple lines. There is no limit to the number of statements however, there must be at least one. If there are no statements you can put pass statement which does nothing but is neat to see it.
Example 1 Crete two if statemetns in which is checked if x1 is greater than x2. Put inside the frist if statement print function to print something, and in second put the pass statement (x1 = 100, x2 = 20).
Solution:
x1 = 100; x2 = 20
if x1 > x2:
print("x1 is greater than x2")
if x1 > x2:
pass
In the 1. code line the variables x1 and x2 are defined and values in the following order 100, and 20 are assigned to those variable names, respectively. In the 2. code line the condition (x1 > x2) is tested. If this condition is true the indented statements are executed, and if False the indented statement is skipped. The condition x1 > x2 is checking out if 100 > 20 which is true since 100 is greater than 20. Since the condition is True the indented statement is executed which is, in this case, the print function (print("x1 is greater than x2")) i.e. the string will be printed out. The condition of the second statement is also True so The result of the previous block of code is shown below.
x1 is greater than x2
As seen from previous output only the text inside the print function was shwon in the output. From this is example it can be concluded that pass statement does nothing.
Example 2 Using IF statements only create a program that will compare two numbers and give the output which number if greater than the other. Both numbers must be entered by the user.
Solution: The first step is to create two input variables named USER_INPUT_1 and USER_INPUT_2 that will ask user to type in first and second number. This will be done using input built-in function in Python. Then both nubmers will be converted to integers and assigned to USER_INPUT_1, and USER_INPUT_2, respectively.
USER_INPUT_1 = int(input("Enter first number:> ")
USER_INPUT_2 = int(input("Enter second number:> ")
The first if statement will be used to check if USER_INPUT_1 value is greater than USER_INPUT_2 value. If this is True the output will be "The {} is greater than {}." Curly brackets represent values of USER_INPUT_1 and USER_INPUT_2. The second if statement will be used to check if USER_INPUT_2 is greater than USER_INPUT_1. If this IF statement is True than the output will be "The {} is greater than {}." where first curly brackets represents the value of USER_INPUT_2 and the second pair of curly brackets represents the value of USER_INPUT_1.
if USER_INPUT_1 > USER_INPUT_2:
print("The {} is greater than {}.".format(USER_INPUT_1, USER_INPUT_2))
if USER_INPUT_2 > USER_INPUT_1:
print("The {} is greater than {}.".format(USER_INPUT_2, USER_INPUT_1))
The output in the Example 2 is given below.
Enter first number:> 150
Enter second number:> 400
The 400 is greater than 150.

No comments:

Post a Comment