**The ternary operator in python:-**

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)

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

## Post a Comment

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