Wednesday, 22 February 2017

PYTHON IF...ELIF...ELSE

Q. What is if else?
 The if/else means a statement that executes a block of code if a specified condition is true and if the condition is false, another block of code can be executed.
Example Of IF...ELSE

x = int(raw_input("Please enter an integer: "))
Please enter an integer: 42
 if x < 0:
    x = 0
    print 'Negative changed to zero'
 elif x == 0:
     print 'Zero'
 elif x == 1:
     print 'Single'
 else:

     print 'More'

Python Program To Check If a Number Is Odd or Even(Using IF...ELSE)

Q. What is ELIF?
With elif statement you can check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.
It is similar to the else, the elif statement is optional. However, unlike else, for which there can be at most one statement, there can be an arbitrary number of elif statements following an if.


You May Like Also :-  Python Program To swap Two Variables

people = 30
cars = 40
trucks = 15


if cars > people:
    print "We should take the cars."
elif cars < people:
    print "We should not take the cars."
else:
    print "We can't decide."

if trucks > cars:
    print "That's too many trucks."
elif trucks < cars:
    print "Maybe we could take the trucks."
else:
    print "We still can't decide."

if people > trucks:
    print "Alright, let's just take the trucks."
else:
    print "Fine, let's stay home then."

Python program to check if a number is positive, negative or zero (Using IF...ELIF...ELSE )

num = float(input("Enter a number: "))
if num > 0:
   print("Positive number")
elif num == 0:
   print("Zero")
else:
   print("Negative number")



Python Program To Check Leap Year(Using Nested IF..ELSE)

 Python program to check if the input year is a leap year or not

year = 2000

# To get year (integer input) from the user
# year = int(input("Enter a year: "))

if (year % 4) == 0:
   if (year % 100) == 0:
       if (year % 400) == 0:
           print("{0} is a leap year".format(year))
       else:
           print("{0} is not a leap year".format(year))
   else:
       print("{0} is a leap year".format(year))
else:
   print("{0} is not a leap year".format(year))


0 comments:

Post a Comment