Python provides following decision making statements (also known as conditional statements):
if..else statement is one of most commonly used conditional statement of Python programming. "if..else statement" is written by using the if and else keyword. if..else statement consists of a Boolean expression which is followed by one or more logical statements.
Example 1: Use of if..else statement to check greater number.
Example 2: Python program to display Good Morning message based in given time.
Example 3: Python program to check whether given number is positive or not.
- if statement
- if..else statement
- nested - if statement
if..else statement
if..else statement is one of most commonly used conditional statement of Python programming. "if..else statement" is written by using the if and else keyword. if..else statement consists of a Boolean expression which is followed by one or more logical statements.
Example 1: Use of if..else statement to check greater number.
number1 = 201
number2 = 101
if number1 > number2:
print ("number1 is greater than number2.")
else:
print ("number1 is not greater than number2.")
Output of Program
number1 is greater than number2.
number2 = 101
if number1 > number2:
print ("number1 is greater than number2.")
else:
print ("number1 is not greater than number2.")
Output of Program
number1 is greater than number2.
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...")
else:
print ("Have a nice time...")
Output of Program
Enter your time:5
Good Morning...
if time < 12:
print ("Good Morning...")
else:
print ("Have a nice time...")
Enter your time:5
Good Morning...
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")
else:
print ("It's not positive number")
Output of Program
Enter your time:5
It's positive number
#Note: If entered number is negative or zero then this program will print only "It's not positive number" message.
if number > 0:
print ("It's positive number")
else:
print ("It's not positive number")
Enter your time:5
It's positive number
#Note: If entered number is negative or zero then this program will print only "It's not positive number" message.
No comments:
Post a Comment