If...Else



Python - If Else


Decision making is required when we want to execute a code only if a certain condition is satisfied.

The if, elif, else statement is used in Python for decision making.

An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value. The else statement is an optional statement and there could be at most only one else statement following if.


Python Conditions and If statements :


Python supports the usual logical conditions from mathematics:


  • Equals: a == b

  • Not Equals: a != b

  • Less than: a < b

  • Less than or equal to: a <= b

  • Greater than: a > b

  • Greater than or equal to: 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.


a=33
b=44
if b>a:
 print ("b is greater than a")

====o/p====
b is greater than a

Indentation :


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


a=3
b=4
if(b>a):
print("b is greater than a")

====o/p====
#raises an error because of indentation(.i.e white space is not given before print)

Elif :


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


a=330
b=330
if a>b:
    print("a is greater than b")
elif  a==b:
     print ("a is equals to b")

====o/p====
a is equals to b


Else :


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


a=1128
b=28
if b>a:
    print("b is greater than a")
elif a==b:
    print("a is equals to b")
else:
    print("a is greater than b")

====o/p====
a is greater than b

we can also have an else without the elif :

a=1128
b=28
if b>a:
    print("b is greater than a")
else:
    print("a is greater than b")

====o/p====
a is greater than b

Short Hand If :


If we have only one statement to execute, we can put it on the same line as the if statement.


a=24
b=10
if a>b: print("a is greater than b")

====o/p====
a is greater than b

Short Hand If ... Else :

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


a=214
b=220
  print("a>b") if a>b else print("b>a")

====o/p====
b>a

We can also have multiple else statements on the same line :


a=330
b=330
  print("A") if a>b else print("=") if a==b else print("B")

====o/p====
=

And :


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




a=330
b=546
c=890
if a<b and c>b:
  print("both conditions are true")


====o/p====
both conditions are true


Or :


The or keyword is a logical operator, and is used to combine conditional statements:


a=14
b=5
c=65
if a<b or c>a:
  print("at least one of the conditions is true")

====o/p====
at least one of the conditions is true

Nested If :


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


x=41
if x>10:
    print("above 10")
    if x>20:
        print("above 20!")
else:
            print("not above 20")

=====o/p=====
above 10
above 20!

The pass Statement :


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


a=30
b=666
if b>a:
 pass

====o/p====
we gets nothing as an output but executes without an error


RegEx SETS

Python - RegEx SETS

posted on 2019-11-12 23:06:24 - Python Tutorials


RegEx_Functions

Python - RegEx_Functions

posted on 2019-11-09 06:07:29 - Python Tutorials


RegEx_Sets

Python - RegEx_Sets

posted on 2019-11-09 05:30:54 - Python Tutorials


Prompt Examples

ChatGPT Prompt Examples

posted on 2023-06-21 22:37:19 - ChatGPT Tutorials


Use Cases

Chat GPT Key Use Cases

posted on 2023-06-21 21:03:17 - ChatGPT Tutorials


Prompt Frameworks

Prompt Frameworks

posted on 2023-06-21 19:33:06 - ChatGPT Tutorials