if-else Statements

'''
if-else Statements:
Sometimes the programmer needs to check the evaluation of certain expression (s), whether the expression(s) evaluate True or False.

An if-else statement in Python is a way to make decisions in our code.

It allows our program to choose between two or more paths based on a certain condition.
'''

a = int(input("Enter your age: "))
print("Your age is:" ,a)

if(a>18):
    print("You can drive")
    print("Yes")
else:
    print("You cannot drive")
    print("No")
# #output = Enter your age: 23
#           Your age is : 23
#           You can drive
#           Yes

'''Conditional Operators:
< , > , <= , >= , == , != '''
print(a<18)    #Enter your age: 18   output = False
print(a>18)                         # output = False
print(a<=18)                        # output = True
print(a>=18)                        # output = True
print(a==18)                         #output = True
print(a!=18)                        # output = False

'''
if-else statement always takes conditional operators which evaluates with either True or False.

The space before print() in if-else statement is called as "indentation".

Indentation in Python refers to the spaces or tabs at the beginning of a line of code that indicates "which code blocks belong together" i.e. to show that we have entered into a certain block of codes.

Unlike many other programming languages that use curly brackets{} to define a block of codes, Python uses indentation to define the same.
'''

Apple_price = 200
Budget = 250
if (Apple_price ==200 <= Budget ==250):
    print("Alexa, Add 1 kg apple in the cart")
else:
    print("Alexa, do not add apple in the cart")
#output = Alexa, Add 1 kg apple to the cart

num = int(input("Enter the value of num: "))
if(num < 0):
    print("Number is negative.")
elif(num == 0):
    print("Number is zero.")
elif(num == 24):
    print("Number is special.")
else:
    print("Number is positive.")
    
print("I am happy now.")

#Enter the value of num:2002
#output = Number is positive.
#          I am happy now.

#Enter the value of num:24
#output = Number is special
#        I am happy now.

#Enter the value of num: -10
#output = Number is negative
#         I am happy now.

'''
After entering the expression that we want to evaluate, Python interpreter first checks the if-statement, if it comes out to be true, it direct jumps into the print() statement to execute which is not a part of if / elif / else.

If the if-statement comes out to be false, then it checks the elif-statement(1st).

if the if and 1st elif-statement are evaluated to be false, then the interpreter checks the 2nd elif-statement and the evaluation continues.
'''

num = 18
if(num < 0):
    print("Number is negative.")
elif(num > 0):
    if(num <=10):
        print("Number is between 1-10")
    elif(num > 10 and num <= 20):
        print("Number is between 11-20")
    else:
        print("Number is greater than 20")
else:
    print("Number is zero")
#output = Number is between 11-20




    
    
    
 

 

Comments

Popular posts from this blog

Print Function

Arithmetic Operators

Sets in Python