The ternary operator in python:-

It is used to solve conditional expression using a single-line statement.

Syntax:-

Syntax:-

var=True statement if condition else false statement

print(var if condition else var)

Example of a ternary operator in Python?

**WAP to check the greater number using Ternary Operator?**

Solution:-

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=100

b=63

c=45

o = "a " if a>c else "c" if a>b else "b" if b>c else "c"

print(o)

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

3)

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

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

## Post a Comment

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