What Is "For (X:Y)"

What does 'x, y =' mean in python syntax?

At least, as of python 2.7.x, the function will unpack a tuple of 2 arguments returned from a function. If it returns anything other than 2 arguments in the tuple, I believe it will throw an error if you try to unpack more than this. If it returns 3 arguments and you unpack 2, for example, you will get an exception.

For example:

def func(a):
return (a,a+1,a*2)

a,b,c = func(7)
print a,b

==> 7 8 # NOTE Values

a = func(3)
print a

==> (3, 4, 6) # NOTE: TUPLE

a,b = func(9)
print a,b

==> Exception - ValueError: too many values to unpack

This may be different in 3.0+.

Simplify Boolean Expression: X + X'Y'Z

Expression                            Justification
--------------------------------- -------------------------
X + X'Y'Z initial expression
(XY'Z + X(Y'Z)') + X'Y'Z r = rs + rs'
(XY'Z + XY'Z + X(Y'Z)') + X'Y'Z r = r + r
(XY'Z + X(Y'Z)' + XY'Z) + X'Y'Z r + s = s + r
(XY'Z + X(Y'Z)') + (XY'Z + X'Y'Z) (r + s) + t = r + (s + t)
X(Y'Z + (Y'Z)') + (Y'Z)(X + X') rs + rt = r(s + t)
X(1) + (Y'Z)(1) r + r' = 1
X + Y'Z r(1) = r

Difference between (X != Y) and (!(X == Y))?

!(x == y) and x != y are logically equivalent and I doubt any language, compiled or interpreted, would evaluate them differently internally. In other words, I'd be surprised if you found a measurable difference.

Some languages such as C++ and Scala lets you override these operators, in which case it's a different story of course.

What does the construct x = x || y mean?

It means the title argument is optional. So if you call the method with no arguments it will use a default value of "Error".

It's shorthand for writing:

if (!title) {
title = "Error";
}

This kind of shorthand trick with boolean expressions is common in Perl too. With the expression:

a OR b

it evaluates to true if either a or b is true. So if a is true you don't need to check b at all. This is called short-circuit boolean evaluation so:

var title = title || "Error";

basically checks if title evaluates to false. If it does, it "returns" "Error", otherwise it returns title.



Related Topics



Leave a reply



Submit