2

Im trying to check is the number passed is a valid perfect square by

return True if(math.sqrt(num)%2 == 0 or \
 (math.sqrt(num)+1) % 2 == 0 ) else False

I want to know if I could write this better by breaking the if statement into two parts like

return True if(math.sqrt(num)%2 == 0) 
 elif ((math.sqrt(num)+1) % 2 == 0 ) 
 else False

Could someone help me with how to use elif here or if there is a better approach.

Thank you.

Prune
78k14 gold badges63 silver badges83 bronze badges
asked Sep 14, 2018 at 0:07
10
  • 5
    Don't use inline if statements if they are going to be very complicated. Use full if statements with blocks... Commented Sep 14, 2018 at 0:09
  • 2
    Can't you just write return (math.sqrt(num)%2 == 0 or (math.sqrt(num)+1) % 2 == 0)? Commented Sep 14, 2018 at 0:09
  • Yes i can just write return (math.sqrt(num)%2 == 0 or (math.sqrt(num)+1) % 2 == 0) but i want to break this as I don't have to compute the or case if the first one is true. Commented Sep 14, 2018 at 0:14
  • That isn't a reliable test. It would call 1 + math.factorial(25) a perfect square even though it is manifestly not. Commented Sep 14, 2018 at 0:16
  • @Tarun Run def f(x): print(x); return x; f(True) or f(False);. False won't be printed. or short circuits. Commented Sep 14, 2018 at 0:18

6 Answers 6

5

No need to use if statements at all

return math.sqrt(num) % 2 == 0 or (math.sqrt(num)+1) % 2 == 0 
answered Sep 14, 2018 at 0:15
Sign up to request clarification or add additional context in comments.

4 Comments

These type of codes are hard to read and are prone to unintended consequences. Although python interpreter doesn't need if, humans do.
Sure for some stuff but this is a pretty straightforward and easy to read statement. All the extra syntax in other answers just makes it harder to read.
The ternary if statement adds an extra layer of indirection which in my opinion makes it harder to read. If the complexity of the boolean equation becomes too large for a single line (I don't think that's the case here), my first step would be to seperate that equation into variables e.g. a = math.sqrt(num) % 2 == 0 b = (math.sqrt(num)+1) % 2 == 0 return a or b It'd have to be a rather large boolean equation for me to resort to an if, and I would never use a ternary if.
even simpler: return math.sqrt(num) % 1 == 0
2

Simple one-liner with readability in mind.

def check(num): 
 return True if math.sqrt(num) % 2 == 0 or (math.sqrt(num)+1) % 2 == 0 else False

Result

>>> check(25)
True
>>> check(23)
False
answered Sep 14, 2018 at 0:16

2 Comments

You and I differ on "readability". I find this redundant, and thus distracting from readability. I'm not saying you're wrong, just that we differ. When I see something like this, I have to take time to see what the coder intended that could not be handled with the expression itself.
@Prune I think we are not that different. I too look for exceptions in the expression. The reason that I think this is more readable is it clearly states the return values for the values that match the expression, and value that does not match the expression.
2

First of all, there is no elif in the ternary operator. Second, your elif doesn't specify a return value (one reason it was left out of the ternary expression). Third, your check for the root being an integer is susceptible to round-off error. Instead, try rounding teh root to an integer, square that, and compare against the original value.

answered Sep 14, 2018 at 0:15

Comments

1

Just answering your question on how to use elif and do it in more than a single line by breaking the if statement. I assume you put these in a function. There could be better ways though as pointed out in other answers

def check_sqrt(num):
 if math.sqrt(num)%2 == 0:
 return True
 elif (math.sqrt(num)+1) % 2 == 0:
 return True
 else:
 return False
print (check_sqrt(23)) 
> False 
print (check_sqrt(25))
> True 
answered Sep 14, 2018 at 0:13

Comments

1

You should always write clearer code that everyone could understand. And you should make it a habit.

if math.sqrt(num)%2 == 0:
 return True
if (math.sqrt(num)+1) % 2 == 0:
 return True
return False
answered Sep 14, 2018 at 0:20

Comments

1

A cleaner version with Python 3.8+ would be:

import math
def is_square(number):
 return math.isqrt(number) ** 2 == number if number >= 0 else False

As an example:

is_square(0)
True
is_square(4)
True
is_square(1)
True
is_square(3)
False
is_square(-4)
False
is_square(3.5)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in is_square
TypeError: 'float' object cannot be interpreted as an integer
answered Mar 26, 2025 at 14:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.