Quick question guys: How does the 'and' operation work in Python? Does it actually return false immediately when the first criteria is false or would it go through the whole comparison?
Oh right and another one, is it advisable to abuse the 'break' statement in a loop? I seem to be using it one time too many.
type in help('and') and you will get this: The expression ``x and y`` first evaluates *x*; if *x* is false, its value is returned; otherwise, *y* is evaluated and the resulting value is returned. The 'abusing break' thing... that depends. I don't use break that often myself, so I don't know the real consequences. If you've structured your code as well as you could and it requires a lot of breaks, or if your code works better/faster with breaks, then I don't know :-(
I think using break is totally fine. Look at this toy (stupid) example. You want to iteratively count from 1 to 100. You can do it in multiple ways: http://codepad.org/0xahgHge in the first version you run the operations in the for loop for 1000 times - but you really need to run it only 100 (because this is your target). Putting a break here (as in the second version) hence will speed up your code, since you avoid unneccessary computation. The third version finally uses a while loop: you can see that this takes the same amount of operations as version 2 (btw, i think that for loops interrupted by breaks can be equivalently written as while loops with an additional condition checking in the statement.)
fra- interesting comparison. I suppose if we listen to Tim Peters & the Zen of Python, "Flat is better than nested." So the third example in your set would be the most desirable.
i really think the second and the third are completely equivalent, really... i noticed i tend to prefer while against for, but i think you can always convert one into the other, anyways :) btw: for those of you already around lecture 8. the complexity of the three versions is the same, and is linear in N - meaning they'll run in the same time for the worst case (in the example, N=1000). However: if we suppose that we have the same probability for each possible N (which can be 1,2,...,1000) to be chosen, and we evaluate the average time of running, the second and the third versions win.
edit of the previous: the complexity of the first is constant (because it runs 1000 iterations anyway)! second and third are linear in N. sorry for the mistake...
Learned it the hard way, there are 2 ANDs, and and & The first will try the first value to see if it is true, if true goes to the next, if false it breaks. So, if you want to do something like a circuit simulator with something such as OR, you will get yourself in trouble if you use the first or instead of |, it would shortcircuit and don't do what is expected. Though it is a valid operand in most code.
the difference between & and and, and | and or is that the symbols are bitwise operators. They work on binary representations of their operands.
Join our real-time social learning platform and learn together with your friends!