Finding a value in a range - python

def shazday(y):
if y >= 1200 or <= 1299:
x = 4
I keep getting syntax on the "=" just before 1299, any idea what I'm doing wrong? I'm trying to find any value between these two numbers to assign a variable. I've tried range itself as well but couldn't get it to work. If I could just find out how to properly check for a value in a range in Python, that would be great.

if y >= 1200 or y <= 1299:
But you probably mean:
if 1200 <= y <= 1299:

Look at it this way, in the if statement you want to evaluate several conditions. So we break it down
if y >= 1200 or <= 1299:
Is y >= 1200? Let's say yes.
if true or <= 1299
Next we ask is... wait a minute? There's nothing there to compare to! (blank) <= 1299 and the system has no idea what (blank) is supposed to be, so it yells at you until you give it something. In this case we have to tell it to check y
if y >= 1200 or y <= 1299:
^
Another way to form this is below, still satisfying that each comparison has something to compare to
if 1200 <= y <= 1299:
# this can be thought of as below
if 1200 <= y and
y <= 1299:

def shazday(y):
if y >= 1200 or y <= 1299:
x = 4
Note the y before the second <=. While y is greater equal or smaller equal is valid in the English language and everybody understands the meaning, this does not work for programming languages...
What the interpreter does is something like
(y >= 1200) is it true or false?
(y <= 1299) is it true or false?
...and then applies the logical operator or. If the second y is missing, the compiler does not know what actually should be smaller equal 1299.
Edit:
Besides the missing y you may also change or to and. Otherwise, the condition will always be true because every number is >= 1200 or >=1299.

Related

two numerical comparisons in one expression python

I thought I read somewhere that python (3.x at least) is smart enough to handle this:
x = 1.01
if 1 < x < 0:
print('out of range!')
However it is not working for me.
I know I can use this instead:
if ((x > 1) | (x < 0)):
print('out of range!')
... but is it possible to fix the version above?
It works well, it is your expression that is always False; try this one instead:
x = .99
if 1 > x > 0:
print('out of range!')
Python chained comparisons work like mathematical notation. In math, "0 < x < 1" means that x is greater than 0 and less than one, and "1 < x < 0" means that x is greater than 1 and less than 0.
And. Not or. Both conditions need to hold.
If you want an "or" , you can write one yourself. It's or in Python, not |; | is bitwise OR.
if x > 1 or x < 0:
whatever()
Alternatively, you can write your expression in terms of "and":
if not (0 <= x <= 1):
whatever()
You can do it in one compound expression, as you've already noted, and others have commented. You cannot do it in an expression with an implied conjunction (and / or), as you're trying to do with 1 < x < 0. Your expression requires an or conjunction, but Python's implied operation in this case is and.
Therefore, to get what you want, you have to reverse your conditional branches and apply deMorgan's laws:
if not(0 <= x <= 1):
print('out of range!')
Now you have the implied and operation, and you get the control flow you wanted.

Python: How to express less or equal, equal greater, environment?

I want to do the following example in python:
x = 10
y = 8
if x-5 <= y <= x+5:
print(y)
I see that this is working, but I would like to know if it's "ok" like this, if there is a better solution or something I have to consider doing it like this.
Chained expressions are acceptable in Python:
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent
to x < y and y <= z, except that y is evaluated only once (but in both
cases z is not evaluated at all when x < y is found to be False).
In fact, because and is lazy and the syntax is cleaner, they are preferable.
Just be aware that chained expressions take priority. For example, see Why does the expression 0 < 0 == 0 return False in Python?.

map values to a specific range

Let say we have some angular values from -inf to +inf as input.
I'd like to map them between only -180 and +180.
How could I properly achieve that?
Here's what I've found so far:
(i-180)%360-180
It works fine, but it maps +180 to -180 which I would like to be +180->+180.
Technically it's the same for my purpose so it's not a big deal, but it would be aesthetically better.
I'm working with Python but it doesn't really matter here.
If you don't value mathematical aestheticism as much, I think this could work
def f(i):
x = i % 360
if x > 180:
x -= 360
elif x == 180 and i < 0:
x = -x
return x
Suppose you have values in the range -inf to inf in a list sampleList[].
You need to map the values in sampleList[] in range -180 to 180. Lets name the new list of mapped elements as mappedList[]
Consider x as element in sampleList[]
Consider y as element in mappedList[]
Consider maxSL as maximum of sampleList[] and
minSL as minimum of sampleList[]
Therefore,
y = ((x - minSL)(((180-(-180))/(maxSL - minSL))) + (-180)

Powering x until reach y in while loop

Hi I want to make an app that will be raise X to a power until it reaches Y.
I have for now something like this
x = 10
y = 1000000
while x <= y:
x = x**x
print(x)
I don't want it in function.
I know that probably this is simple, but I just started learning Python :)
This might be what you are looking for. In python you want to use the operators for math as such += , -=, *=, /= for same variable operations.
counter = 10
while counter <= 1000000:
counter *= counter
print(counter)
101010… (x) will never be equal to 106 (y) because 1010 is four orders of magnitude larger. Your program will interpret x = 10 as less than 106, execute xx (1010), interpret this value as greater than 106, exit the loop, and print x (now 1010).
I don't think this is what you're trying to do; please consider the comments other users have left. I have a hunch you're looking for xn=y (10 * 10 * 10 …), for which you could simply use logarithms.

getting syntax error with <= sign

When using this code:
while str.find(target,key,n) != -1 and <= len(target):
I get a syntax error on 'less than or equal to sign'. Why?
In English we can say "if X is not equal to Y and also less than Z", but Python syntax doesn't work that way.
If you want to compare a number to two other numbers, you have to repeat the original number in the second comparison.
i.e., instead of this:
if x != y and < z:
you must do this:
if x != y and x < z:
If you want to use the double operand, you could rewrite it like:
if -1 < target.find(key, n) < len(target):
pass
But that said, I don’t think find can ever return a value larger than the length of target.

Categories