Mostafa Talebi
Mostafa Talebi

Reputation: 9173

python shorthand method for a if-else based on the function's return

I have read examples of python shorthand-if in the stackoverflow but still can not figure out how to solve my own simple problem. I want to print("this is a prime number") else print(this is not a prime number) based on the [boolean] return value of the following function:

is_prime(2) 

How should I write a short hand method for?

Upvotes: 1

Views: 116

Answers (1)

falsetru
falsetru

Reputation: 368934

Do you mean conditional expression?

print("this is a prime number" if is_prime(2) else "this is not a prime number")

print('this {} a prime number'.format('is' if is_prime(2) else 'is not'))

Upvotes: 4

Related Questions