View Single Post
shoehorn is offline
Apr16-10, 09:29 PM
P: 448
Quote Quote by Mark44
In other words, no matter what value x is, the expression 0.5 <= x <= 1 always comes out true - at least in C, and I would be very surprised if python behaves any differently.
Python is a very different language to C! Although Python (the standard version, at least) is written in C, the style, intent, and scope of the language is extremely unlike what you might be used to in C.

For what it's worth, the change in mindset that I needed to undergo when I first learned Python was quite painful; I had a solid C/C++ background and many Pythonic ways of doing things just seemed wrong. With the benefit of hindsight though, it's clear that Python gives rise to a much more natural style of expression than C/C++.

Quote Quote by Mark44 View Post
Also, I looked at the section on logical conditions and didn't see anything to contradict what I said in my previous reply about what 0.5 <= x(i) <= 2 would evaluate as. I still believe this always evaluates to 1 (or true).
You can have several arithmetic tests as conditions to an if statement in Python. For instance, consider the following situation where I define a Python list which contains the integers from one to ten, inclusive.

# Define a list containing the integers from 1 to 10.
li = range(1, 11)

# Print out whether the numbers in the list are between 6 and 9, inclusive.
for x in li:
    if 6 <= x <= 9:
        print x
        print 'FAIL!'
The output from this is as one would expect: