WhatsApp and Telegram Group Card
WhatsApp Group Join Now
Telegram Group Join Now

IF and IF-Else statement in Python : Python Concepts

Welcome to EduPaat.com, In this article we will see a basic introduction about conditional statements i.e., IF and IF-Else statement in Python. We have tried to cover each and every point of aspects in this article. There are following types of Conditional statements in Python :-

  1. If statement.
  2. If-Else statement.
  3. Nested If statement.
  4. elif statements.
  5. elif-else statement

In this article, we have discussed about If and If-else Statement only, rest we will cover in the upcoming posts.

Python supports the usual logical conditions from mathematics like :

  • Equals: a == b // (a equals b)
  • Not Equals: a != b // (a not equal to b)
  • Less than: a < b // (a is less than b)
  • Less than or equal to: a <= b // (a is less than or equal to b)
  • Greater than: a > b // (a is greater than b)
  • Greater than or equal to: a >= b //(a is greater than or equal to b)

Above conditions are used in almost every part of Python, mostly in IF statement and in loops.

1. IF statements

An “IF statement” is used where certain conditions are given from which we have to select an appropriate options. “if” Keyword is use in this type of Statements.

Example

a = 30
b = 200
if b > a:
    print("b is greater than a")

Explanation

In this example we have used two variables, a and b, and assigned them values 30 and 200 Respectively. After that we have used if statement to compare whether b is greter than a. As a is 30, and b is 200, we know that 200 is greater than 30, and so we will get “b is greater than a” as output on Screen.

Note :- As we know Python will show output, if and only if the indentation done properly. otherwise it will show error as “Indentation Error”.

2. If – Else Statement

Next one is “If – Else statement“, It is used when multiple conditions are given and we have to choose one from them then, “If – Else” is use. If we want to use it we have use keyword like “if” and “else“.

Example

a = 200
b = 30
if b > a:
    print("b is greater than a")
else:
    print("a is greater than b")

In this example, we have taken two variables a & b and assigned them values 200 and 30 respectively. After that we have use if statement where we have compared b with a. but condition is false that’s why it printed a is greater than b”.

You can also have an else with the elif also.

Hope, after going through the whole article, you will came to know some basics of about Conditional Statements.

Also Read : Global Keyword in Python

Important Links

Join WhatsAppClick Here
Join TelegramClick Here
Join Twitter (X)Click Here
Join InstagramClick Here
Important Links