Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

The ternary operator in python:-


It is used to solve a condition-based program using a single-line statement. ternary operator contains an if-else statement using a single line.

It is used to solve a condition-based program using a single-line statement.


Syntax:-

var =  True statement if condition else false statement

print(var if condition else var)


Example of a ternary operator in Python?

Q) WAP to check the greater number using Ternary Operator?


Solution First:-

a = int(input("enter first number"))
b = int(input("enter second number"))
res =  "a is greater" if a>b else "b is greater"
print(res)

Solution Second:-

a = int(input("enter first number"))
b = int(input("enter second number"))
print(a if a>b else b)



WAP to check vowel and consonant using a ternary operator?

s = input("enter char to check vowel and consonent")

o = "Vowel" if s=='a' or s=='e' or s=='i' or s=='o' or s=='u' else "Consonent"
print(o)



Nested Ternary:-

We can create more than one Ternary Statement using a nested sequence.

var =  (True if condition else false) if condition else (True if condition else false)
 
Example of Nested ternary:-

WAP to calculate the greatest number using a ternary operator in Python?

a = int(input("enter first number"))
b = int(input("enter second number"))
c = int(input("enter third number"))
res =   "a is greatest" if a>c else "c is greatest" if a>b  else "b is greatest" if b>c else "c is greatest"
print(res)


Advantage:-

Its performance is better if we can solve expression using a single line


Disadvantage:-

It is not used to solve complex condition-based or multiple lines based program logic.


Assignment of Ternary Operator:-

1) WAP to check leap year using a ternary operator in Python?
   
Solution:-

year  = int(input("enter year"))
s = "Leap year" if(year%4==0) else "NOT Leap year"
print(s)


2)  WAP to check divisibility of number that it is divisible by 3 and 5 or not?
Solution:-

num = int(input("enter number"))
s = "divisible" if num%3==0 and num%5==0 else "not divisible"
print(s)

3)  WAP to check divisibility of a number by 3 and 5, individually and both?

num = int(input("enter number"))
s = "divisible by 3 and 5" if num%3==0 and num%5==0 else "divisible by 3" if num%3==0 else "diviblle by 5 " if num%5==0 else "not divisble by 3 and 5"

print(s)


4)  WAP to check that salary is in income tax or not display the total amount and the taxable amount 


sal = int(input("enter salary"))

s = "not taxable" if sal<=250000 else "Tax amount is " + str(sal-250000)

print(s)


5)  WAP to calculate electricity bill where unit price, total consumption will be entered by the user, if the bill is <400 then pay only 100 RS and above this 50% amount will be paid.

Solution:-

up = float(input("enter unit price"))
con = float(input("enter consumption"))
total = up*con

s = "pay only 100 rs " if total<400 else  "Actual bill is " + str(total) + "payble bill is " + str(total/2)

print(s)









12 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. #check leap year using a ternary operator in Python?

    year = int(input("Please Enter the Year : "))

    if (( year%400 == 0)or (( year%4 == 0 ) and ( year%100 != 0))):
    print("%d is a Leap Year" %year)
    else:
    print("%d is Not the Leap Year" %year)

    ReplyDelete
  2. # check divisibility of number that it is divisible by 3 and 5 or not?

    number = int(input("Please Enter the Number : "))

    if (( number%3 == 0)or (( number%5 == 0 ))):
    print("%d is a divisible number" %number)
    else:
    print("%d is not a divisible number" %number)

    ReplyDelete
  3. Greater number program uisng ternary operator
    a=int(input("Enter first number"))
    b= int(input("Enter first number"))
    s = str(a)+" is greater" if a>b else str(b)+" is greater"
    print(s)

    ReplyDelete
  4. Lokesh Rathore

    Check divisibility of number that it is divisible by 3 and 5 or not ?
    Solution:-
    num = int(input("Enter the Number :- "))
    a="Divisible by 3 and 5" if num%3==0 and num%5==0 else "Divisible by 3" if num%3==0 else "Divisible by 5" if num%5==0 else "Not divisible by 3 and 5"
    print(a)

    ReplyDelete
  5. Lokesh Rathore

    Check leap year using a ternary operator in Python ?
    Solution :-
    year = int(input("Enter Year :-"))
    a = "This is Leap Year." if (year%4==0) else "This is Not Leap Year."
    print(a)

    ReplyDelete
  6. Lokesh Rathore

    check that salary is in income tax or not ?
    Solution :-
    sal = int(input("Enter the Salary :- "))
    a="Salary is Not Taxable." if (sal<=250000) else "Salary is Taxable."
    print(a)


    ReplyDelete
  7. Lokesh Rathore

    Check the greater number using Ternary Operator ?
    Solution :-
    a = int(input("Enter first number :- "))
    b = int(input("Enter second number :- "))
    s = "A is greater number." if a>b else "B is greater number."
    print(s)

    ReplyDelete
  8. Lokesh Rathore

    Check vowel and consonant using a ternary operator ?
    Solution :-
    char = input("Enter Character to check Vowel and Consonent : - ")
    r = "It is Vowel." if char=="a" or char=="e" or char=="i" or char=="o" or char=="u" else " It is Consonent."
    print(r)

    ReplyDelete
  9. Lokesh Rathore

    Calculate the greatest number using a ternary operator in Python ?
    Solution :-
    a = int(input("Enter first number :- "))
    b = int(input("Enter second number :- "))
    c = int(input("Enter third number :- "))
    s = "A is Greatest Number." if a>c else "C is Greatest Number." if a>b else "B is Greatest Number." if b>c else "C is Greatest Number."
    print(s)

    ReplyDelete
  10. deependra singh jadaunNovember 4, 2020 at 8:43 PM

    program for income tax ,salay ,taxable amount
    s=int(input("enter the salary"))
    i=s-250000
    r="salary is in income tax "if s>250000 else"salary is not in income tax"
    print(r)
    p=(0.1*i)# this much of tax is payable
    print("'{0}' salary amount is taxable and '{1}' is payable as tax amount " .format(i,p))

    ReplyDelete
  11. deependra singh jadaunNovember 6, 2020 at 11:08 AM


    WAP to calculate the greatest number using a ternary operator in Python?

    WAP to calculate the greatest number using a ternary operator in Python?

    a=10
    b=15
    c=20
    s="'a is greatest' if a>c else 'c is greatest' "if a>b else "b is greatest"if b>c else "c is greatest"
    print(s)

    ReplyDelete
  12. deependra singh jadaunNovember 6, 2020 at 11:11 AM

    WAP to check divisibility of number that it is divisible by 3 and 5 or not?
    a=int(input(" enter any no. "))
    s="number is divisible by 3 or 5" if a%3==0 or a%5==0 else "no. is not divisible"
    print(s)

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post