ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 30 Jan 2022 20:24:49 +0100Checking multiple things with an if else statementhttps://ask.sagemath.org/question/60890/checking-multiple-things-with-an-if-else-statement/ Is there a way to check multiple things in an if statement? I have a program in which I want to check if the degree of the lowest term of a polynomial is equal the degree of the lowest term of any polynomial in another list of polynomials.
Essentially, what I have so far is:
def W(i):
d=set()
for w in range(0,d[i]):
if ListBasis[i][w].polynomial().ord() notequal ListBasis[i-1][y].polynomial.ord():
d.add(ListBasis[i][w])
else:
return d
And what I would like to have is
def W(i):
F=set()
for w in range(0,d[i]):
if for y in range(0,d[i]) ListBasis[i][w].polynomial().ord() notequal ListBasis[i-1][y].polynomial.ord():
F.add(ListBasis[i][w])
else:
return F
Clearly this is not the correct syntax for this, but I do not know what is.Sun, 30 Jan 2022 20:21:14 +0100https://ask.sagemath.org/question/60890/checking-multiple-things-with-an-if-else-statement/Answer by Max Alekseyev for <p>Is there a way to check multiple things in an if statement? I have a program in which I want to check if the degree of the lowest term of a polynomial is equal the degree of the lowest term of any polynomial in another list of polynomials.
Essentially, what I have so far is:</p>
<pre><code>def W(i):
d=set()
for w in range(0,d[i]):
if ListBasis[i][w].polynomial().ord() notequal ListBasis[i-1][y].polynomial.ord():
d.add(ListBasis[i][w])
else:
return d
</code></pre>
<p>And what I would like to have is</p>
<pre><code>def W(i):
F=set()
for w in range(0,d[i]):
if for y in range(0,d[i]) ListBasis[i][w].polynomial().ord() notequal ListBasis[i-1][y].polynomial.ord():
F.add(ListBasis[i][w])
else:
return F
</code></pre>
<p>Clearly this is not the correct syntax for this, but I do not know what is.</p>
https://ask.sagemath.org/question/60890/checking-multiple-things-with-an-if-else-statement/?answer=60892#post-id-60892Python's `all()` and `any()` functions are your friends here:
if all(ListBasis[i][w].polynomial().ord() != ListBasis[i-1][y].polynomial.ord() for w in range(d[i])):
d.add(ListBasis[i][w])Sun, 30 Jan 2022 20:24:49 +0100https://ask.sagemath.org/question/60890/checking-multiple-things-with-an-if-else-statement/?answer=60892#post-id-60892