Saturday, September 4, 2021

Python - Conditional statements ,if condition in python

 Python - if - Conditional statements


What is the if condition in python?

Decision-making is the anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions.

And if-else Python statement evaluates whether an expression is true or false. If a condition is true, the " if " statement executes. Python if-else statements help coders control the flow of their programs. When you're writing a program, you may want a block of code to run only when a certain condition is met.


Python programming language assumes any non-zero and non-null values as TRUE, and if it is either zero or null, then it is assumed as FALSE value.

if-condition functioning in the below tree chart.





python





S.No.Statement & Description for if condition
1if statements:

An if statement consists of a boolean expression followed by one or more statements.

2if-else statements:

An if statement can be followed by an optional else statement, which executes when the boolean expression is FALSE.

3nested - if statements:

You can use one if or else if statement inside another if or else if statement(s).


If the code of an if clause consists only of a single line, it may go on the same line as the header statement.


Example:

num = 15
if ( num == 15 ) : print("Your given number is 15")

Result:

Your given number is 15



Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b 
  • Less than: a < b
  • Less than or equals: a <= b
  • Greater than: a > b
  • Greater than or equals: a >= b

These conditions can be used in several ways, most commonly in "if statements" and loops.

An "if statement" is written by using the if keyword.

Indentation:

Python relies on indentation (whitespace at the beginning of a line) to define the scope in the code. Other programming languages often use curly brackets for this purpose.


Elif condition:

The elif keyword is pythons way of saying, if the previous conditions were not true, then try this condition below.


Example:

num_1 = 25
num_2 = 12
if num_1 > num_2:
  print("num_1 is greater than num_2")
elif num_1 == num_2:
  print("num_1 and num_2 are equal")

Result:

num_1 is greater than num_2


Else statement:

The else keyword catches anything which isn't caught by the preceding conditions.


Example:

num_1 = 15
num_2 = 20
if num_1 == num_2:
  print("num_1 and num_2 are equal")
elif num_1 > num_2:
  print("num_1 is greater than num_2")
else:
  print("num_1 is smaller than num_2")

Result:

num_1 is smaller than num_2


Single line if statement:

If you have only one statement to execute, one for if, and one for else, you can put it all on the same line.


Example:

if num_1 > num_2: print("num_1 is greater than num_2")

Result:

num_1 is greater than num_2


Single line if-else statement:

If you have only one statement to execute, one for if, and one for else, you can put it all on the same line.


Example:

num_1 = 222
num_2 = 333
print("Python"if num_1 > num_2 else print("learning")

Result:

learning


if condition using " and "operator:

The and keyword is a logical operator and is used to combine conditional statements.


Example:

num_1 = 100
num_2 = 99
num_3 = 101
if num_1 > num_2 and num_3 > num_2:
  print("Both conditions are True")
else:
    print("Not fallowing condition")

Result:
Both conditions are True


if condition using " or " operator:

The or the keyword is a logical operator and is used to combine conditional statements.


Example:

num_1 = 100
num_2 = 101
num_3 = 102
if num_1 > num_2 or num_3 > num_2:
  print("one of the conditions is True from both conditions")
else:
    print("Not fallowing condition")

Result:
one of the conditions is True from both conditions


Nested If conditional statements:

You can have if statements inside if statements, this is called nested if statements.


Example:

num_1 = 333
if num_1 >= 111:
  print("Given number is greater than 100")
  if num_1 >= 222:
    print("also, the number is above 200")
else:
    print("conditions are not fallowing.")

Result:
Given number is greater than 100 also, the number is above 200


if condition using " pass " Statement:

if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.


Example:

num_1 = 100
num_2 = 111

if num_1 > num_2:
  pass
Result:

No output and no error it just passes the statement.


This is all about if condition, if required any notes comment below.

Follow us on Facebook at @EduTech.trainings













No comments:

Post a Comment

Python - Conditional statements ,if condition in python

 Python - if - Conditional statements What is the if condition in python? Decision-making is the anticipation of conditions occurring while ...