Skip to content
Home » Programming » Python program with Conditional Statements

Python program with Conditional Statements

conditional-statements-python

In this post, we will be investigating the process that how python program works with Conditional Statements checking whether a number given to the program by the user is positive or negative or zero in Python programming language.

Before letting into the program, let us make a rough sketch in order to differentiate the forms of number as if the number is greater than zero that is positive and similarly, if it is smaller than zero then it is negative.

The rough sketch or pseudo code for understanding the algorithm to check whether a number is positive or negative or zero is shown below.

Start:
      if num > 0 then
                 "Number is positive"
      elif num < 0 then
                  "Number is negative"
      else
                  "Number is zero"
Stop.

Program with conditional statements in Python

Using if

num = input("Enter a number : ")

if (int(num) > 0 ):
                   print("Positive Number")
if (int(num) < 0):
                   print("Negative Number")
if (int(num) == 0):
                   print("Number is Zero")

Output:

Enter a number: 23
Positive Number

Explanation:

In the first place, we have to take input in variable. Here we are using variable num of data type string (initially) as the input() takes input in the form of string data type.

num = input("Enter a number: ")

After that, we will check if the number is positive or not converting the string value of num variable into int data type as int(num) shown below.

if (int(num) > 0):
                 print("Positive Number")

If the number is greater than 0 then the number is positive. If it satisfies the condition we will print saying “Positive Number”.

Then, even if the entered number satisfies the first condition with if statement (i.e. even if the entered number is positive) we have to check for the remaining conditions also. This is the drawback of using multiple if statements which results slight decrement in program execution speed.

if (int(num) < 0):
                 print("Negative Number")

If the number is less than 0 then it is negative. And again we will have to check whether the number is zero or not even the conditions above are satisfied.

if (int(num) == 0):
                   print("Number is Zero")

If the number is equal to zero then it is zero.

Using if…else

Since we are comparing the same number multiple times even if we get our answer in the first condition satisfied the use of if statement is inefficient. In order to eliminate this problem we can use another conditional statement known as if…else statement.

num = input("Enter a number: ")

if (int(num) >= 0):
        if (int(num) == 0):
                print("Number is Zero")
        else:
            print("Positive Number")
else:  
    print("Negative Number")

Output:

Enter a number: -23
Negative Number

Explanation:

In the first place, we will check if the number is greater than or equals zero or not. If the number is less than zero the control flow jumps to the else statement and print saying “Negative Number”.

if (int(num) >= 0):
                  ...
else:
    print("Negative Number")

If the number is not negative or the control enters the first if block and encounters the second if block which checks if the number is equal to zero or not if it satisfies the condition we end printing “Number is Zero” and if does not satisfies the condition for zero then we end printing “Positive Number”.

if (int(num) >= 0):
        if (int(num) == 0):
                print("Number is Zero")
        else:
            print("Positive Number")
else:  
    ...

Also Read: Python Program with Arithmetic Operators

Using if…elif…else

However, using if…else provided us better efficiency but we are nesting the if…else statements which makes our code a bit complex to read. In order to eradicate this problem we can use if…elif…else. The conditional statement if…elif…else works similar as if…else if…else as in other programming languages.

num = input("Enter a number: ")
if (int(num) > 0):
    print("Positive Number")

elif (int(num) < 0):
    print("Negative Number")

else:
    print("Number is Zero")

Output:

Enter a number: 0
Number is Zero

Explanation:

As we have done previously, we will check whether the number is positive or not. This step is same as we have done before.

if (int(num) > 0):
    print("Positive Number")

If the number is positive, we will end the program printing “Positive Number” and if not, the control is transferred to elif.

elif works similar to the if blocks. We can check for any condition in this block. Here, we are checking if the number is negative or not.

elif (int(num) < 0):
    print("Negative Number")

If the number is negative we will end the program printing “Negative Number” if not, the control is transferred to the else block.

At last, in the else block we will print specifying number is zero since both the conditions for positive and negative number is already checked.

else:
    print("Number is Zero")

That’s it for this article, take this program and embellish it a bit with the Python supported IDE.

Leave a Reply

Your email address will not be published. Required fields are marked *