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)

#check leap year using a ternary operator in Python?

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

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

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

Greater number program uisng ternary operator

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

Lokesh Rathore

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

Lokesh Rathore

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

Lokesh Rathore

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

Lokesh Rathore

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

Lokesh Rathore

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

Lokesh Rathore

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

program for income tax ,salay ,taxable amount

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

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

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

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

Parag Jaiswal

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

not correct because leap year is divisible by 400,4 and not divisible by 100

DeleteParag Jaiswal

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

correct

DeleteParag Jaiswal

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

correct

DeleteParag Jaiswal

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

correct

DeleteAdarsh dixit

ReplyDeleteyear=int(input('enter year'))

s = "leap year"if(year%4==0)else"not leap year"

print(s)

not correct

Delete#Mohit Chouhan

ReplyDelete1.

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)

#1) WAP to check leap year using a ternary operator in Python?

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

y=int(input("enter the year = "))

ReplyDeletea="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)

N=int(input("Enter the Number= "))

ReplyDeletea="divisible" if ((N%3==0) and (N%5==0)) else "not"

print(a)

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

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

#NIKHIL SINGH CHOUHAN

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

#NIKHIL SINGH CHOUHAN

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

#Check Leap Year By Surendra

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

#Check Leap Year By Surendra

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

num= int(input("write a number to chack if no is divisible by 3 or 5"))

ReplyDeletex="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)

num= int(input("write a number to chack if no is divisible by 3 or 5"))

ReplyDeletex="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)

salary= int (input("write down your salary"))

ReplyDeletex="not taxable" if salary <=250000 else "taxable amount is"+str(salary-250000)

print(x)

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

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

#Q) WAP to check vowel and consonant using a ternary operator??

ReplyDeletech1=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")

#1) WAP to check leap year using a ternary operator in Python?

ReplyDeleteyear=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.....")

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

ReplyDeleteno=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.....")

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

ReplyDeleteno=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....")

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

ReplyDeleteslry= int(input("enter salary : "))

print("Tax amount is " + str(slry-250000) ) if(not(slry<=250000)) else print("No Noo, It's not taxable ammount.....")

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

ReplyDeleteunitp = 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) + "!")

num1=int(input("enter first number"))

ReplyDeletenum2=int(input("enter second number"))

greater="num1 is greater" if num1>num2 else "num2 is greater"

print(greater)

year=int(input("enter a year"))

ReplyDeletex="leap year"if(year%400==0 or (year%4==0) and (year%100!=0)) else "not leap year"

print(x)

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

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

number= int(input("enter number"))

ReplyDeletex= "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)

'''WAP to check leap year using a ternary operator in Python?'''

ReplyDeletey=int(input("enter year:-"))

print("leap year" if y%4==0 or y%400==0 and y%100!=0 else "not leap year")

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

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

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

#Leap Year

ReplyDelete#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")

#find leap year or not

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

#find a year is leap year or not

ReplyDeletea=int(input("leap year"))

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

print(year)

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

ReplyDeletea=int(input("enter divisible value"))

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

print(s)

#find Divisible value or not

ReplyDeletex=int(input("enter divisible value"))

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

print(s)

#WAP to check leap year using ternary operator

ReplyDeleteyear=int(input("enter year"))

s="leap year"if (year%400==0) or (year%4==0) and(year%100!=0) else "notleapyear"

print(s)

#WAP to check the positive or negative value

ReplyDeletea=int(input("enter the value"))

s="positive" if a>=0 or a<10==0 else "negative"

print(s)

#wap to check that salary is in income tax or not display the total amount and taxtable amount

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

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

print(s)

#wap to check divisble by 3 and 5 or not

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

#wap to calculate find the number is negative or positive

ReplyDeletex=int(input("enter value"))

x="positive" if (x>0==0) or (x<-9==0) else "negative"

print(x)

#WAP to calculate electricity bill

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

#wap greatest two number using ternary operator:

ReplyDeletea=int(input("enter first number"))

b=int(input("enter second number"))

num="a is greatest"if a>b else "b is gratest"

print(num)

#wap greatest third number using ternary operator

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

#find greatest number of ?

ReplyDeletea=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")

#wap vowels and consonant using ternary operator

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

#wap leap year

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

# wap salary .

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

s="not taxable"if salary<=3500000 else "tax amount"+str(salary-3500000)

print(s)

# wap divisible 3 and 5

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

x=int(input("enter any number"))

ReplyDeletey=int(input("enter any number"))

d="a is greater " if a>b else "b is greater"

print(d)

char=input("Enter Any char")

ReplyDeletevc="Vowel" if char=='a' or char=='e' or char=='i' or char=='o' or char=='u' else "consonent"

print(vc)

a=int(input("enter any number a"))

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

# wap to check the greater number using ternary operator?

ReplyDeletea=int(input("enter first number"))

b=int(input("enter second number"))

c="a is greater" if a>b else "b is greater"

print(c)

# wap to vowel and consonant using a ternary operator?

ReplyDeletevol=(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)

# wap to check leap year using a ternary operator in python?

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

# wap to ckeck divisibility of number that it is divisible by 3 and 5

ReplyDeletenum=int(input("enter no is divisibleor not"))

z="divisible" if num%3==0 or num%5==0 else "not divisible"

print(z)

# wap to check that salary is in income tax or not display the total amount and the taxable amount?

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

s="not taxable" if salary<=30000 else "taxable or taxable amount"+str(salary-30000)

print(s)

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

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

#wap to check even or odd number?

ReplyDeletenum=int(input("enter the value"))

s="even" if num%2==0 else "odd"

print(s)

# greatest number using ternary operator?

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

#wap to check vowel and consonant using a ternary operator?

ReplyDeletech=input ("enter vowel and consonant")

res="vowel" if ch in ['a','e','i','o',u'] else "consonant"

print(res)

a=int(input("enter number a ="))

ReplyDeleteb=int(input("enter number b ="))

ans="a is greater b" if a>b else "b is greater than a"

print(ans)

# ternary salary amount

ReplyDeletesal=float(input("enter the salary amount"))

tax=300000

ans ="taxable" if sal>=tax else "non taxable"

print(ans)