Ternary operator in python

80

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%400==0 or (year%4==0)  and  (year%100!=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)
Tags

إرسال تعليق

80تعليقات

POST Answer of Questions and ASK to Doubt

  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)


    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  10. deependra singh jadaun4 نوفمبر 2020 في 8:43 م

    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))

    ردحذف
  11. deependra singh jadaun6 نوفمبر 2020 في 11:08 ص


    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)

    ردحذف
  12. deependra singh jadaun6 نوفمبر 2020 في 11:11 ص

    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)

    ردحذف
  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)

    ردحذف
    الردود
    1. not correct because leap year is divisible by 400,4 and not divisible by 100

      حذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  17. Adarsh dixit
    year=int(input('enter year'))
    s = "leap year"if(year%4==0)else"not leap year"
    print(s)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  21. N=int(input("Enter the Number= "))
    a="divisible" if ((N%3==0) and (N%5==0)) else "not"
    print(a)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

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

    ردحذف
  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)

    ردحذف
  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")

    ردحذف
  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.....")

    ردحذف
  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.....")

    ردحذف
  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....")

    ردحذف
  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.....")

    ردحذف
  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) + "!")

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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")

    ردحذف
  42. I think there is some problem with that question of tax.Do check for the big amounts.

    ردحذف
  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) )

    ردحذف
  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")

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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")

    ردحذف
  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)

    ردحذف
  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)

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

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

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

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  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)

    ردحذف
  75. # ternary salary amount

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

    ردحذف
إرسال تعليق