Python if...else Statement


In this article, you will learn how to make Python program decisions using different formats to explain if..else.

Table of Contents

  • What are if...else statement in Python?
    • Python if Statement Syntax
    • Python if Statement Flowchart
    • Example: Python if Statement
  • Python if...else Statement
    • Syntax of if...else
    • Python if..else Flowchart
    • Example of if...else
  • Python if...elif...else Statement
    • Syntax of if...elif...else
    • Flowchart of if...elif...else
    • Example of if...elif...else
  • Python Nested if statements

What are if ... else statement in Python?


Making important decisions when we want to perform a code only if certain conditions are met.
If ... Elif ... Python has used to reach decision makers.

if test expression:
    statement(s)

 Python if Statement Syntax:


Then the program evaluates the performance test by completing the text (s) only if the text is correct.
If the text is false, the text (s) is not enforced.
In Python, you know that it is referred to as the stream. The staff begins with an indication of ending without advance.
Python interprets the value of zero without real value. None of the 0 was interpreted as false.

Python if statement Flowchart:


                                                     Flowchart of if statement in Python programming

Example: Python if Statement

# If the number is positive, we print an appropriate message
 num = 3
if num > 0:
    print(num, "is a positive number.")
print("This is always printed.")
 num = -1
if num > 0:
    print(num, "is a positive number.")
print("This is also always printed.")

When you run the program, the output will be:

3 is a positive number
This is always printed
This is also always printed. 

Python if...else Statement:

Syntax of if...else

if test expression:

    Body of if

else:

   Body of else

If it does not, the value of the test scores will be carried out when the body is only if the test is accurate.
If the situation is fake, the body of another is carried out. Mediation is used in different parts.

Python if..else  flowchart:

                                                    
                                                        Flowchart of if...else statement in Python Programming

Example of if...else

# Program checks if the number is positive or negative
# And displays an appropriate message 
num = 3
 # Try these two variations as well.
# num = -5
# num = 0
if num >= 0:
    print("Positive or Zero")
else:
    print("Negative number")

Python if...elif...else Statement

Syntax of if...elif...else

if test expression:

    Body of if

elif test expression:

    Body of elif

else:

    Body of else

In the previous example, when NUM and 3, and experimental samples were performed in the other body, they were deleted.
NUM is equal to -5, the test score is a false passage that includes the body must be removed.
NUM corresponds to 0, the correct revelation, and if it is done it tastes the other body.

Flowchart of if...elif...else


                                      Flowchart of if...elif....else in python programming

Example of if...elif...else


Python if famous
We can afford if ... there is ... someone else inside if ... elif ... else. This is called a computer program.
# In this program,
# we check if the number is positive or
# negative or zero and
# display an appropriate message
 num = 3.4
 # Try these two variations as well:
# num = 0
# num = -4.5
 if num > 0:
    print("Positive number")
elif num == 0:
    print("Zero")
else:
    print("Negative number")

Python Nested if statements:

All these statements can be constructed among each other. Sangria is the only way to know the level of rubbish. This can be frustrating, so it should be avoided if we can.

# In this program, we input a number
# check if the number is positive or
# negative or zero and display
# an appropriate message
# This time we use nested if
num = float(input("Enter a number: "))
if num >= 0:
    if num == 0:
       print("Zero")
   else:
        print("Positive number")
else:
    print("Negative number")

Output 1:

Enter a number: 5
Positive number

Output 2:
Enter a number: -1
Negative number
Output 3
Enter a number: 0
Zero

To getting expect level training for Python training in your Location – Python training in Chennai | Python training institute in Chennai | Python Course institute in Chennai | Python training in Bangalore | Python training institute in Bangalore Python training in Electronic City Python training in Jaya Nagar | Python training in Pune | Python training institute in Pune | Python training in OMR | Python training in Velachery | Python training in Tambaram Python training in Annanagar | Python training in Marathahalli Python training in Btm | Python Online Training | Online Certification Course Python Course in Chennai Python interview questions and answers | Python tutorials | Python training in Indira Nagar | Python Course in Btm | Python Course in Marathahalli Python training institute in Marathahalli Python course institute in Btm Python course in Electronic City | Python online training in Chennai | Python online training in Bangalore | Python Interview Questions & Answers.

Comments

  1. Nice Blog. I hope really enjoyed while reading the article here. thank you for sharing the kind of information.
    Python Online Training
    Python Online Training in Chennai
    Python Online Course in Chennai
    Python Online Course

    ReplyDelete
  2. This blog is very useful it include very knowledgeable information. Thankyou for sharing this blog with us. If anyone want to experience certificate in bangalore can call at 9599119376 or can visit https://experiencecertificates.com/experience-certificate-provider-in-bangalore.html

    ReplyDelete
  3. Thanks for sharing this post , if anyone looking his/her career in sas so join with High technologies solutions Contact Here-+91-9311002620 Or Visit Website- https://www.htsindia.com/Courses/business-analytics/sas-training-institute-in-delhi

    ReplyDelete
  4. Your blog is very nice and interesting. Your way of writing this blog forced me to read the full blog. Being a new reader, your blog increased my interest in reading. If anyone is interested for Fake Experience Certificate in Hyderabad here we have the chance for you, Dreamsoft is providing is Fake experience certificate in Hyderabad. To get you experience certificate in Hyderabad you can contact at 9599119376. or can visit our website at https://experiencecertificates.com/experience-certificate-provider-in-Hyderabad.html

    ReplyDelete
  5. Thanks for this amazing blog , it is very useful content for us
    keep sharing this type of informtion
    if anyone is looking for the best python taining institute in Delhi
    I would like to talk about the High Technologies solutions .


    Python training institute in delhi
    Best training institute in delhi
    High Technologies Soutions Provide you the best training for the python , here you get the onlne and offline classes
    the faculty of this institiute is very experinced , they always give support to their students, you can get the job placements from here .
    For more details contact on 9311002620
    you can also visit the website for more information
    https://www.htsindia.com/Courses/python/python-training-institute-in-south-delhi

    ReplyDelete

Post a Comment

Popular posts from this blog

Python – Functions

What is Django in python?

What do you learn about Python?