if else in Pythpn

if...else Statement in Pythpn


The if .. else statement provides control to check the true block as well as the false block. Following is the syntax of ‘if..else’ statement.

Syntax:

if <condition>:
statements-block 1
else:
statements-block 2

if..else statement thus provides two possibilities and the condition determines which BLOCK is to be executed.


Example : #Program to check if the accepted number odd or even

a = int(input("Enter any number :"))
if a%2==0:
print (a, " is an even number")
else:
print (a, " is an odd number")

Output 1:

Enter any number :56

56 is an even number

Output 2:

Enter any number :67

67 is an odd number


An alternate method to rewrite the above program is also available in Python. The complete if..else can also written as:

Syntax:

variable = variable1 if condition else variable 2


Example : #Program to check if the accepted number is odd or even (using alternate method of if...else)

a = int (input("Enter any number :"))
x="even" if a%2==0 else "odd"
print (a, " is ",x)

Output 1:

Enter any number :3

3 is odd

Output 2:

Enter any number :22

22 is even

Python : If else in Pythpn Program List




Program [1]

WAP to take two numbers and check that the first number is fully divisible by second number or not.


View Solution


Program [2]

WAP to check the given year is leap year or not.


View Solution


Program [3]

WAP to calculate the roots of a given quadratic equation.


View Solution


Program [4]

Program to find the simple interest based upon number of years. If number of years is more than 12 rate of interest is 10 otherwise 15.


View Solution


Program [5]

Write a python program to input cost price, selling price of product from user and check whether is profit or loss and also print the Profit/loss amount


View Solution


Program [6]

Write a python program to input a number form user and check whether number is buzz number or not.

A number is said to be Buzz Number if it ends with 7 or is divisible by 7.


View Solution


Program [7]

Write a program to input assets, liabilities and capital of a company and test if accounting equation holds true for the given value (i.e., balanced or not).

Hint : assets == liabilities + capital 



View Solution


Program [8]

Write a program to input total debt and total assets and calculate total-debt-to-total-assets ratio (TD/TA) as Total Debt/Total Assets. Then print if the major portion of the debt is funded by assets (when TD/TA > 1) or greater portion of assets is funded by equity (when TD/TA < 1).


View Solution


Program [9]

Write a program to calculate Debt-to-equity (D/E) ratio as [ Total Liabilities  / Total shareholders' ] equity after inputting total liabilities and total shareholders' equity. And then print if an investor should invest in the company or not. A D/E ratio greater than 2.0 indicates a risky scenario for an investor.


View Solution


CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad