# Shorthand if conditional expression

Hi Team,

I just wanted to ask can we use elif in shorthand if conditional expression.

I am unable to execute below code.

a_number = 17

x = ‘even’ if a_number % 2 == 0 elif ‘odd’ if a_number % 2 != 0
else a_number ‘is prime number’
print(‘This number is {} is {}’.format(a_number,x))

hi! 17 will still fall under odd since the result will yield a non 0 number. prime #s can only be divided with 1 and itself. you will have to go loop through every # or a combination of #s (ex. 9 is odd but not prime since it has factors 3,1 and 9) up to the number in question to see if it can be divided to see if it’s prime or composite. the code will not read the ‘else’ condition unless you add an additional condition. i hope i explained it properly. have a good day.

2 Likes

The logic given by @dajo09 is correct, I just want you to let you know that you cannot use `elif` in shorthand if conditions, If you want to extend a condition you have to use `if` and `else ` again.

2 Likes