Friday 29 June 2018

if statement

Python provides following decision making statements (also known as conditional statements):

  • if statement
  • if..else statement
  • nested - if statement

if statement


if statement is one of most commonly used conditional statement of Python. "if statement" is written by using the if keyword. if statement consists of a Boolean expression which is followed by one or more logical statements.

Example 1: Use of if statement to check greater number.

number1 = 101
number2 = 201

if number2 > number1:
    print ("number2 is greater than number1.")

if number1 > number2:
    print ("number1 is greater than number2.")


Output of Program

number2 is greater than number1.

Example 2: Python program to display Good Morning message based in given time.

time = int(input("Enter your time:"))

if time < 12:
    print ("Good Morning...")

print("Have a nice time...")

Output of Program

Enter your time:5
Good Morning...
Have a nice time...

Example 3: Python program to check whether given number  is positive or not.

number = int(input("Enter your number:"))

if number > 0:
    print ("It's positive number")
print("It's end of program")

Output of Program

Enter your time:5
It's end of program

#Note: If entered number is negative or zero then this program will print only "It's end of program" message.

No comments:

Post a Comment