Ad Code

✨🎆 Codex 1.0 PLACEMENT READY PROGRAM! 🎆✨

Get 75% Discount Early bird offer CLICK to JOIN CodeX 1.0 click

Ternary operator in python

Ternary Operator in Python

Python Ternary Operator – Complete Explanation

The ternary operator in Python is used to evaluate conditions in a single line. It is the compact form of an if–else statement and is useful when solving small condition-based expressions.


✨ Syntax of Ternary Operator

variable = true_statement if condition else false_statement

# Example
print(a if condition else b)

📌 Example 1: Greater Number Check

Solution 1 (Storing result in variable)

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 2 (Direct printing)

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

print(a if a > b else b)

📌 Example 2: Check Vowel or Consonant

s = input("Enter a character: ")

o = "Vowel" if s in ('a','e','i','o','u') else "Consonant"
print(o)

🔁 Nested Ternary Operator

Python allows multiple ternary expressions inside one another. This is called a nested ternary operator.

Syntax:

var = (true1 if condition1 else false1) if condition2 else (true2 if condition3 else false2)

📌 Example: Greatest Among Three Numbers

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)

✔ Advantages of Ternary Operator

  • Makes code shorter and cleaner
  • Provides better performance for small conditional checks

✘ Disadvantages

  • Not suitable for complex conditions
  • Nested ternary operators can reduce readability

📝 Assignments – Ternary Operator Programs

1️⃣ Check Leap Year

year = int(input("Enter year: "))

s = "Leap year" if (year % 400 == 0 or (year % 4 == 0 and year % 100 != 0)) else "NOT Leap year"
print(s)

2️⃣ Check if a Number is Divisible by 3 and 5

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

s = "Divisible" if num % 3 == 0 and num % 5 == 0 else "Not Divisible"
print(s)

3️⃣ Check Divisibility (3, 5, Both or None)

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 \
    "Divisible by 5" if num % 5 == 0 else \
    "Not divisible by 3 or 5"

print(s)

4️⃣ Check Taxable Salary

sal = int(input("Enter salary: "))

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

print(s)

5️⃣ Electricity Bill Using Ternary Operator

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) + " | Payable bill is " + str(total / 2)

print(s)

Post a Comment

80 Comments

  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
  13. Parag Jaiswal
    1) WAP to check leap year using a ternary operator in Python?

    year = int(input("Enter year"))
    res ="A leap year" if(year%4==0) else "Not a leap year"

    print(year, res)

    ReplyDelete
    Replies
    1. not correct because leap year is divisible by 400,4 and not divisible by 100

      Delete
  14. Parag Jaiswal
    2) WAP to check divisibility of number that it is divisible by 3 and 5 or not?
    n = int(input("Enter number = "))

    res= " Divisible by 3 & 5" if(n%3 == 0 and n%5==0) else "Divisible by 3" if(n%3==0) else "Divisible by 5" if(n%5==0) else "Not divisible by 3 & 5"

    print(n, res)

    ReplyDelete
  15. Parag Jaiswal
    #WAP to check that salary is in income tax or not display the total amount and the taxable amount
    s = int(input("Enter your salary = "))

    r = "salary is not taxable" if s<= 500000 else "Salary is taxable and tax amount is " +str(s-500000)
    print(r)

    ReplyDelete
  16. Parag Jaiswal
    #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.
    price = int(input("Enter unit price = "))
    con = int(input("Enter consumption = "))

    total = price * con

    z= "Bill is 100 " if total<=400 else "Total bill is " +str(total) +"Payable bill is "+str(total/2)
    print(z)

    ReplyDelete
  17. Adarsh dixit
    year=int(input('enter year'))
    s = "leap year"if(year%4==0)else"not leap year"
    print(s)

    ReplyDelete
  18. #Mohit Chouhan
    1.
    s=int(input("Enter the Year : "))
    w="leap year" if s/4==0 else "not a leap year"
    print(w)

    # 2.
    s=int(input("Enter the number to check : "))
    r="Yes it's divisable " if s%3==0 and s%5==0 else"Not divisable"
    print(r)

    3.
    f=int(input("Enter the number : "))
    h="Yes its divisible by 3 & 5 both " if f%3==0 and f%5==0 else "divisible by 3 "if f%3==0 else "divible by 5 " if f%5==0 else"not divisible by 3 & 5 both "
    print(h)

    4.
    sal=int(input("Enter the salary to check : "))
    t="YOu need to pay Amount"+str (sal-35000) if sal>=350000 else "Tax free "
    print(t)

    5.
    up=int(input("Enter unit price: "))
    com=int(input("Enter unit comm: "))
    r=up*com
    e = "Pay only 100 rs " if r<400 else "bill " +str(r)+" pay bill "+str(r/2)
    print(e)

    ReplyDelete
  19. #1) WAP to check leap year using a ternary operator in Python?
    year= int(input("give an year"))
    num= "leap year" if(year%400==0 or(year%4==0) and (year%100!=0)) else "not a leap year"
    print(num)

    ReplyDelete
  20. y=int(input("enter the year = "))
    a="leap year" if (y%4==0) and (y%100!=0) else "not leap year" if (y%100==0) else "leap year" if(y%400==0) else "not leap year"
    print(a)

    ReplyDelete
  21. N=int(input("Enter the Number= "))
    a="divisible" if ((N%3==0) and (N%5==0)) else "not"
    print(a)

    ReplyDelete
  22. to check that salary is in income tax or not display the total amount and the taxable amount ?

    s=int(input("Enter the salary = "))
    A="Tax free" if s<=250000 else "Taxable tax amount is = " + str(s-250000)
    print(A)
    print("total salary = ",s)

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

    N=int(input("Enter the Number= "))
    a="divisible by 3 & 5" if ((N%3==0) and (N%5==0)) else "divisible by 3" if (N%3==0) else "divisible by 5" if (N%5==0) else "not divisible by both"
    print(a)

    #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.

    UnitPrice=float(input("enter the unit price = "))
    consumption=float(input("enter the total consumption= "))
    TotalBill=UnitPrice*consumption
    Q="pay only 100 rs" if (TotalBill<400) else "actual bill is = "+str(TotalBill)+"Payble bill is = "+str(TotalBill/2)
    print(Q)

    ReplyDelete
  23. #NIKHIL SINGH CHOUHAN

    #WAP to check divisibility of number that it is divisible by 3 and 5 or not?
    i=int(input("enter number"))
    v= "it is divisible by 3 and 5"if(i%3==0)and(i%5==0)else"ths number is divisible by 3 "if(i%3==0)else"number is divisible by 5"if(i%5==0)else"number is not divisible by 3 or 5"
    print(v)

    ReplyDelete
  24. #NIKHIL SINGH CHOUHAN

    # WAP to check leap year using a ternary operator in Python?
    i=int(input("enter year"))
    a="this year is leap year"if(i%4==0)and(i%100!=0)or(i%400!=0)else"this year is not leap year"
    print(a)

    ReplyDelete
  25. #Check Leap Year By Surendra
    year = int(input("Enter Year"))
    var = "This is Leap Year" if (year%400==0) or (year%4==0) and (year%100!=0)else "This is Not leap year"
    print(var)

    ReplyDelete
  26. #Check Leap Year By Surendra
    year = int(input("Enter Year"))
    var = "This is Leap Year" if (year%400==0) or (year%4==0) and (year%100!=0)else "This is Not leap year"
    print(var)

    ReplyDelete
  27. num= int(input("write a number to chack if no is divisible by 3 or 5"))
    x="number is divisible by 3 and 5" if( num%3==0 and num%5==0) else "number is not divisible by 3 and 5"
    print(x)

    ReplyDelete
  28. num= int(input("write a number to chack if no is divisible by 3 or 5"))
    x="number is divisible by 3 and 5" if( num%3==0 and num%5==0) else "number is divisible by 3"if num%3==0 else "number is divisible by 5" if num%5==0 else "number is not divisible by 3 or 5 and both"
    print(x)

    ReplyDelete
  29. salary= int (input("write down your salary"))
    x="not taxable" if salary <=250000 else "taxable amount is"+str(salary-250000)
    print(x)

    ReplyDelete
  30. #Q) WAP to check the greater number using Ternary Operator?
    x=int(input("Enter a value of x : "))
    y=int(input("Enter a value of y : "))
    z=int(input("Enter a value of z : "))
    print(x) if(x>y and x>z) else print(y) if(y>z) else print(z)

    ReplyDelete
  31. #Q) WAP to check vowel and consonant using a ternary operator??
    ch1=input("Enter Char : ")
    k= 1 if((ord(ch1)>=65 and ord(ch1)<=90) or (ord(ch1)>=97 and ord(ch1)<=122 )) else 0
    exit(0) if(k==0) else print()
    ch = chr(ord(ch1)+32) if ord(ch1)>=65 and ord(ch1)<=90 else ch1
    print("vowel") if ch=='a' or ch=='e' or ch=='i' or ch=='o' or ch=='u' else print("consonent")

    ReplyDelete
  32. #1) WAP to check leap year using a ternary operator in Python?
    year=int(input("enter year : "))
    print("yaa, this is a leap year.....") if(year%4==0 or year%400==0 and year%100==0) else print("No Noo, this is not a leap year.....")

    ReplyDelete
  33. #2) WAP to check divisibility of number that it is divisible by 3 and 5 or not?
    no=int(input("Enter number : "))
    print("yaaa! this value is divisible.....") if(no%3==0 or no%5==0) else print("No Noo! this value is not divisible.....")

    ReplyDelete
  34. #3) WAP to check divisibility of a number by 3 and 5, individually and both?
    no=int(input("enter a number "))
    print("this number is not divisible by both.....") if(not(no%3==0 or no%5==0)) else print("this number is divisible by both 3 nd 5.....") if(no%3==0 and no%5==0) else print("this number is not divisible by both, only with 3....") if(no%3==0) else print("this number is not divisible by both, only with 5....")

    ReplyDelete
  35. #4) WAP to check that salary is in income tax or not display the total amount and the taxable amount.
    slry= int(input("enter salary : "))
    print("Tax amount is " + str(slry-250000) ) if(not(slry<=250000)) else print("No Noo, It's not taxable ammount.....")

    ReplyDelete
  36. #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.
    unitp = float(input("enter unit price : "))
    tcons = float(input("enter consumption : "))
    total = unitp*tcons
    print("you pay only 100'rs.....") if total<400 else print(" there is actual bill ammount is " + str(total) + " but don't worry, payble bill ammount is " + str(total/2) + "!")

    ReplyDelete
  37. num1=int(input("enter first number"))
    num2=int(input("enter second number"))
    greater="num1 is greater" if num1>num2 else "num2 is greater"
    print(greater)

    ReplyDelete
  38. year=int(input("enter a year"))
    x="leap year"if(year%400==0 or (year%4==0) and (year%100!=0)) else "not leap year"
    print(x)

    ReplyDelete
  39. 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

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

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

    print(s)

    ReplyDelete
  40. number= int(input("enter number"))
    x= "divisible by 3 and 5" if number%3==0 and number%5==0 else "divisible by 3" if number%3==0 else "divislle by 5 " if number%5==0 else "not divisble by 3 and 5"

    print(x)

    ReplyDelete
  41. '''WAP to check leap year using a ternary operator in Python?'''
    y=int(input("enter year:-"))
    print("leap year" if y%4==0 or y%400==0 and y%100!=0 else "not leap year")

    ReplyDelete
  42. I think there is some problem with that question of tax.Do check for the big amounts.

    ReplyDelete
  43. # 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.
    # Mayank Sonwani


    unitprice=float(input("Enter the rate of an unit "))
    consumption=float(input("Consumption of the month "))
    bill=float(unitprice*consumption)
    print("Pay only Rs.100 to clear your bill " if bill<400 else "Bill amount is " +str(bill)+" and you need to pay only 0" +str(bill/2) )

    ReplyDelete
  44. #Leap Year
    #Mayank Sonwani

    year=int(input("Enter the year to check if it is leap or not: "))
    print("Entered year is a leap year" if year%400==0 or year%4==0 and year%100!=0 else "Entered year is not a leap year")

    ReplyDelete
  45. #find leap year or not

    a=int(input("find leap year"))
    year="leap year" if (a%4==0) or (a%400==0) and (a%100!=0) else "normal year"
    print(year)

    ReplyDelete
  46. #find a year is leap year or not

    a=int(input("leap year"))
    year="leap year" if (a%4==0) or (a%400==0) and (a%100!=0) else "normal year"
    print(year)

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

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

    ReplyDelete
  48. #find Divisible value or not

    x=int(input("enter divisible value"))
    s="divisible value" if (x%2==0) or (x%4==0) else "not divisible"
    print(s)

    ReplyDelete
  49. #WAP to check leap year using ternary operator
    year=int(input("enter year"))
    s="leap year"if (year%400==0) or (year%4==0) and(year%100!=0) else "notleapyear"
    print(s)

    ReplyDelete
  50. #WAP to check the positive or negative value
    a=int(input("enter the value"))
    s="positive" if a>=0 or a<10==0 else "negative"
    print(s)

    ReplyDelete
  51. #wap to check that salary is in income tax or not display the total amount and taxtable amount
    sal = int(input("enter salary"))

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

    print(s)

    ReplyDelete
  52. #wap to check divisble by 3 and 5 or not
    a=int(input("enter the value"))
    res="divisble by 3 or 5" if a%3==0and a%5==0 else "not divisble by 3 or5"
    print(res)

    ReplyDelete
  53. #wap to calculate find the number is negative or positive

    x=int(input("enter value"))
    x="positive" if (x>0==0) or (x<-9==0) else "negative"
    print(x)

    ReplyDelete
  54. #WAP to calculate electricity bill
    up=float(input("enter unit price"))
    cons=float(input("enter consumption"))
    total=up*cons
    x="pay only 100rs" if total<400 else"actual bill is"+str(total)+"payable billis"+str(total/2)
    print(x)

    ReplyDelete
  55. #wap greatest two number using ternary operator:
    a=int(input("enter first number"))
    b=int(input("enter second number"))
    num="a is greatest"if a>b else "b is gratest"
    print(num)

    ReplyDelete
  56. #wap greatest third number using ternary operator
    a=int(input("enter first number"))
    b=int(input("enter second number"))
    c=int(input("enter thired number"))
    num="a is greatest"if a>c else "c is gratest"if c>b else "b is grearest"if a>b else "b is greatest"
    print(num)

    ReplyDelete
  57. #find greatest number of ?


    a=int(input("enter first number"))
    b=int(input("enter second number"))
    c=int(input("enter third number"))
    d=int(input("enter fourth number"))
    if a>b and b>c and c>d and dc and c>d and dd and d<a:
    print("c is greatest")
    else:
    print("d is greatest number")

    ReplyDelete
  58. #wap vowels and consonant using ternary operator
    a=input("enter vowels and consonant")
    vc="vowels"if a=='a' or a=='e'or a=='i' or a=='o'or a=='u' else "consonant"
    print(vc)

    ReplyDelete
  59. #wap leap year
    year=int(input("enter year"))
    s="leap year"if (year%400==0)or(year%4==0)and (year%100!=0)else"not leap year"
    print(year)

    ReplyDelete
  60. # wap salary .
    salary=int(input("enter salary"))
    s="not taxable"if salary<=3500000 else "tax amount"+str(salary-3500000)
    print(s)

    ReplyDelete
  61. # wap divisible 3 and 5
    num=int(input("enter number"))
    s="divisible by 3and5"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(s)

    ReplyDelete
  62. x=int(input("enter any number"))
    y=int(input("enter any number"))
    d="a is greater " if a>b else "b is greater"
    print(d)

    ReplyDelete
  63. char=input("Enter Any char")
    vc="Vowel" if char=='a' or char=='e' or char=='i' or char=='o' or char=='u' else "consonent"
    print(vc)

    ReplyDelete
  64. a=int(input("enter any number a"))
    b=int(input("enter any number b"))
    c=int(input("enter any number c"))
    d="a is greater" if a>b else "b is greater" if a>c else "c is greater" if b>c else "c is greater" if a<c else "c is greater"
    print(d)

    ReplyDelete
  65. # wap to check the greater number using ternary operator?
    a=int(input("enter first number"))
    b=int(input("enter second number"))
    c="a is greater" if a>b else "b is greater"
    print(c)

    ReplyDelete
  66. # wap to vowel and consonant using a ternary operator?
    vol=(input("enter the vowel and consonant="))
    x="vowel" if vol=='a' or vol=='i' or vol=='o'or vol=='e' or vol=='u' else "consonant"
    print(x)

    ReplyDelete
  67. # wap to check leap year using a ternary operator in python?
    year=int(input("enter the year"))
    y="leap year" if (year%400==0) or (year%4==0) and (year%100!=0) else "not leap year"
    print(y)

    ReplyDelete
  68. # wap to ckeck divisibility of number that it is divisible by 3 and 5
    num=int(input("enter no is divisibleor not"))
    z="divisible" if num%3==0 or num%5==0 else "not divisible"
    print(z)

    ReplyDelete
  69. # wap to check that salary is in income tax or not display the total amount and the taxable amount?
    salary=int(input("enter salary"))
    s="not taxable" if salary<=30000 else "taxable or taxable amount"+str(salary-30000)
    print(s)

    ReplyDelete
  70. # 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?
    up=float(input("enter price"))
    con=float(input("enter consuption"))
    totalbill=up*con
    result="pay only 100rs" if totalbill<400 else "actual bill="+str(totalbill) + "above bill="+str(totalbill//2)
    pr

    print(result)

    ReplyDelete
  71. #wap to check even or odd number?
    num=int(input("enter the value"))
    s="even" if num%2==0 else "odd"
    print(s)

    ReplyDelete
  72. # greatest number using ternary operator?
    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)

    ReplyDelete
  73. #wap to check vowel and consonant using a ternary operator?
    ch=input ("enter vowel and consonant")
    res="vowel" if ch in ['a','e','i','o',u'] else "consonant"
    print(res)

    ReplyDelete
  74. a=int(input("enter number a ="))
    b=int(input("enter number b ="))
    ans="a is greater b" if a>b else "b is greater than a"
    print(ans)

    ReplyDelete
  75. # ternary salary amount

    sal=float(input("enter the salary amount"))
    tax=300000
    ans ="taxable" if sal>=tax else "non taxable"
    print(ans)

    ReplyDelete

POST Answer of Questions and ASK to Doubt