If else in Python


INDEX
  • Python program: Syntax of if-else and if-elif-else statement
  • Python program: Syntax of Comment/Comments in Python
  • Python program: Input integer and display it's Negative or not
  • Python program: Input Age and display Adult or Minor
  • Python program: Absolute Value Program
  • Python program: Greatest of two (2) Integers Program
  • Python program: Greatest of three (3) Numbers Program
  • Python program: Leap Year Program
  • Python program: Quadratic Equation Program
  • Python program: Accept marks of a student, and the maximum marks then display "PASS" or "FAIL"
    [If Percentage >= 35 then PASS otherwise FAIL]
    Ex:
    Enter Marks Scored: 46
    Enter Maximum Marks: 70
    Percentage = 65.71
    Result = PASS
  • Python program: Accept coefficients and constants of a quadratic equation ax^2 + bx + c = 0, then display whether it has real or imaginary roots.
    [if b^2-4ac>=0 then REAL ROOTS otherwise IMAGINARY ROOTS]
    Ex:
    Enter cooeficient of x^2 (a): 5
    Enter coefficient of x (b): 12
    Enter constant (c): 3
    Roots are REAL
  • Python program: Accept length of diagonals of a quadrialteral, then display the nature of quadrilateral thus formed.
    [equal diagonals => Rectangle, Square, or isosceles trapezium, otherwise => any other triangle]
    Ex:
    Enter length of diagonal1: 5
    Enter length of diagonal2: 7
    Result= Any quadrilateral otherthan Rectangle, Square and isosceles trapezium
  • Python program:Accept any integer and then display whether it is a positive, negative or zero
    Ex:
    Enter any integer: -23
    The number entered is NEGATIVE
  • Python program: Accept length of the three sides of a triangle and then display what kind of triangle it forms
    [All sides equal: Equilateral triangle,
    Two sides equal: Isosceles triangle
    No side equal: Scalene triangle]
  • Python program: Accept length of sides of a triangle, and display whether it forms a right angled triangle or not.
    [(longest side)^2 = (other side1)^2 + (other side2)^2 ]
    Ex:
    Enter Longest side(a): 13
    Enter Next side(b): 12
    Enter Remaining side(c): 5
    Result: They form RIGHT ANGLED TRIANGLE
  • Python program: Accept Total Marks scored, and Maximum Marks of a student in an Exam, Then display Percentage and also display Grade as per the following chart;
    [Percentage => 75 - 100, Grade = A
                   60 - less than 75, Grade = B
                   45 - less than 60, Grade = C
                   33 - less than 45, Grade = D
                   others, Grade = E or Fail ]
    Ex:
    Enter Marks Scored: 46
    Enter Maximum Marks: 70
    Percentage = 65.71
    Grade: B
  • Python program: Accept the coefficients and constants of two linear equations in 2 variables; a1x + b1y + c= 0, and a2x + b2y + c2 = 0. Then display whether they form intersecting lines, parallel lines or co-inciding lines.
    [a1/a2 not equal to b1/b2 => intersecting lines,
    a1/a2 = b1/b2 = c1/c2 => co-incident lines
    a1/a2 = b1/b2 not equal to c1/c2 => parallel lines ]
    Ex:
    Equation1 a1x + b1y + c1 = 0
    Enter a1: 2
    Enter b1: 3
    Enter c1: 7
    Equation2 a2x + b2y + c2 = 0
    Enter a2: 3
    Enter b2: 5
    Enter c2: 2
    Result: Lines are INTERSECTING
  • Python program: Accept monthly sales amount of a sales person, and then display his/her commission earned as per the following chart;
    Sales Amount (INR)      Commission Earned
    0 - less then 25,000        3% of SA
    25,000 - <50,000            5% of SA
    50,000 - <75,000            7% of SA
    75,000 - <1,00,000          9% of SA
    >= 1,00,000                 11% of SA
    Ex:
    Enter Monthly Sale Amount (in INR): 62000
    Commission earned = 4340
  • Python program: Accept no. of electrical energy consumed (in units), then display the Bill Amount as per the following chart;
    Units Consumed Rate (in INR/unit)
    0-75 3.25/unit
    next 75 units 3.75/unit
    next 100 units 4.25/unit
    next 150 units 4.75/unit
    next 200 units 5.25/unit
    above 600 units 5.75/unit
    Ex:
    Enter No. of units consumed: 235
    Your Bill Value is = 886.25
  • Python Program: A program to accept two numbers and an operator (+,-,*,/,%). Then based upon the operator entered, perform the necessary calculation and display the answer. The program should give appropriate message if any wrong operator is input.

    EX:
    Enter Number1: 7
    Enter Number2: 11
    Enter Operator (+,-,*,/, or %): +
    Result = 18
  • Python Program: A program to accept the month number and then display the number of days in that month. The program should give appropriate message if any wrong month number is input.
    [Taking Month1 as January and Month12 as December]

    Ex:
    Enter Month Number: 5
    No. of days in month-5 is = 31
  • Python Program: A program to display the following menu, and then do the necessary task as per user's choice. The program should give appropriate message if any wrong choice-number is input.
    MENU
    1. Lateral Surface Area of Cuboid (LSA)
    2. Total Surface Area of Cuboid (TSA)
    3. volume of Cuboid
    4. Length of Longest Diagonal of Cuboid
    Enter your choice:

    [IF L, B and H are length, breadth and height respectively, then
    LSA = 2(L+B)H, TSA = 2(LB+BH+HL), Vol=LBH,
    Longest Diagonal = square-root of (L^2 + B^2 + H^2)]
  • Python Program: A program to display the following menu and then do the necessary task as per user's choice. The program should give appropriate message if any wrong choice-number is input.
    MENU
    1. Area of triangle using base and altitude
    2. Area of triangle using three sides
    3. Area of triangle using coordinates of three vertices
    Enter your choice:

    [By method1, Area = 1/2*base*height;
    By method2, area = square-root of s(s-a)(s-b)(s-c) where s=(a+b+c)/2
    By method3, area = 1/2{x1(y2-y3) + x2(y3-y1) + x3(y1-y2)}]
  • Python Program: A program to display the following menu and then do the necessary task as per user's choice. The program should give appropriate message if any wrong choice-number is input.
    MENU
    1. Simple Interest
    2. Compound Interest
    Enter Your choice:

    [SI = PRT/100; CI = p{(1+R/100)^T - 1} ]
Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners

TOP


'''A program to accept any integer and display whether it is negative integer or a non-negative integer.'''
n=int(input("Enter any integer: "))
if (n<0):
    print(n,' is negative integer.')
else:
    print(n,' is NOT a negative integer.')

Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners

TOP

'''A program to accept any persons age as integer and display whether the individual is an Adult or a Minor.'''

age=int(input('Enter any integer: '))
if (age>=18):
    print('The person is Adult')
else:
    print('The person is a MINOR')

Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners

TOP

'''A program to accept any number and display its absolute value.'''

x=float(input('Enter any integer: '))
if (x<0):
    x*=(-1)#alternatively use: x=x*(-1)
print('Required Absolute value is = ',x)

Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners

TOP

'''(IF... ELSE IF..... block statement)
Accept two integers and display the relation between them (i.e. >, < or =).'''

x=int(input('Enter 1st integer: '))
y=int(input('Enter 2nd integer: '))
if x>y:
    print(x,' is greater of the two.')
elif y>x:
    print(y,' is greater of the two.')
else:
    print('Both are equal.')

Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners

TOP

/'''(NESTED IF) Accept three numbers and display the greatest among the three.'''/

x=float(input('Enter 1st number: '))
y=float(input('Enter 2nd number: '))
z=float(input('Enter 3rd number: '))
if x>y:
    if x>z:
        print(x,' is greatest.')
    else:
        print(z,' is greatest.')
else:
    if y>z:
        print(y,' is greatest.')
    else:
        print(z,' is greatest.')

Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners

TOP

'''(NESTED IF) Accept any year in "yyyy" format and display weather it is a Leap-Year or not.'''

year=int(input('Enter any year in "yyyy" format: '))
if year%400==0:
    print(year,' is a Leap Year.')
else:
    if year%100==0:
        print(year,' is NOT a Leap Year.')
    else:
        if year%4==0:
            print(year,' is a Leap Year.')
        else:
            print(year,' is NOT a Leap Year.')

Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners

TOP

'''A program to accept Coefficients and constants (a,b,c) of a quadratic equation ax^2+bx+c=0, and then display its roots.
Author: Pradeep Das Gupta
Compiler: GNU GCC Compiler'''

import math
a=float(input('Enter Coefficient of x^2 (a): '))
b=float(input('Enter Coefficient of x (b): '))
c=float(input('Enter Constant-term (c): '))
d=b**2-4*a*c
if d<0:
    print('\nRoots are Imaginary.')
else:
    x1=(-b+math.sqrt(d))/(2*a)
    x2=(-b-math.sqrt(d))/(2*a)
    print('\nRoots are X1 = ',x1,', X2 = ',x2)

Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners | Python Programming | Computer Programming for Beginners

TOP

Comments in Python:
Single Line Comments: They are preceded by #(hash) character, they can appear either at a fresh line or at the end of any statement.
Syntax1:
# comment or remark
Ex:
#This is a program to display 'Pradeep Sir's Programming Classes'

Syntax2:
statement..... # comment or remark
Ex:
si=p*r*t/100 #calculating simple interest

Multiline comments: They generally appear at the middle of the program, and are genrally used to describe a code-fragment.
They are either enclosed in tripple single inverted commas or tripple double inverted commas.
Syntax1:
'''comment line1....
comment line2....
comment line3....
....
....
comment lineN...'''

Ex:
'''Python Programming
Pradeep Sir's programming classes
Commenting in Python3.7'''

Syntax1:
"""comment line1....
comment line2....
comment line3....
....
....
comment lineN..."""

Ex:
"""Python Programming
Pradeep Sir's programming classes
Commenting in Python3.7"""

TOP

if-else statements and if-elif-else statements in Python:
if - else block statement in Python: It checks for truth of an expression, if the expression/condition evaluates to TRUE, 
then it processes the statemnts written in if-block,
otherwise,
it processes the statements of the else block

Syntax:
if condition:
 statement1...
 statement2...
 ...
 ...
 statementN...
else:
 statementA...
 statementB...
 statementC...
 ...
 ...
 ...

if - elif - else block statement: It checks for truthness of the condition/expression in 'if' and 'elif' blocks. 
Whichever condition satisfies(returns true) first from top, the statements belonging to only that block is processed. 
When none of the condition satisfies (or returns true), then the statements in the 'else' block is processed.
synatx:
if condition1:
 statements for condition1=TRUE
 ...
 ...
elif condition2:
 statements for condition2=TRUE
 ...
 ...
elif condition3:
 statements for condition3=TRUE
 ...
 ...
....
....
....
elif conditionN:
 statements for conditionN=TRUE
 ...
 ...
else:
 statements to be processed when all above conditions are FALSE
 ...
 ...

Note: Unlike other OOP languages like C++, Java, etc. Python does not uses braces{} to demarkate a block, 
it rather uses INDENTATION. Thus indentation is a must in Python.

TOP