#variable assignmentx = 5#ans: x is 5name = "Alice"#ans: name is "Alice"pi = 3.14#ans: pi is 3.14
#assign multiple variablesa, b, c = 1, 2, 3#ans: a=1, b=2, c=3# what are the values?x, y = 10, 20#ans: x=10, y=20
# traditional swap needs temp variablex, y = 5, 10temp = xx = yy = temp#ans: x=10, y=5#ans: python swapx, y = 5, 10x, y = y, x#ans: x=10, y=5
# assign same value to multiple variablesx = y = z = 0#ans: x=0, y=0, z=0a = b = c = "same"#ans: all are "same"
# what is the value of x?x = 10#ans: 10# what happens after this swap?a, b = 5, 15a, b = b, a#ans: a=15, b=5# what is z after this?x, y, z = 1, 2, 3z = x + y#ans: 3# can you assign like this?x = y = z = 0#ans: yes, all are 0
# what is the value of b?a = 5b = aa = 10#ans: 5# what does this produce?x, y = 1, 2, 3#ans: Error: too many values to unpack# what is y?x = y = 5x = x + 1#ans: 5
# multiple assignment with calculation?a, b = 10, 20a, b = a+b, a-b#ans: a=30, b=-10# what happens here?x = 1x = x#ans: x is still 1#ans: tricky swapx, y = 5, 10x, y = y, x+y#ans: x=10, y=15
# assignment from right to left?a = b = 3b = 5#ans: a=3, b=5# what is x?x = 10#ans: 10# unpacking with extra values?x, y = [1, 2, 3]#ans: Error: too many values
# what about this?a, b, c = "abc"#ans: a='a', b='b', c='c'# swap three variables?x, y, z = 1, 2, 3x, y, z = z, x, y#ans: x=3, y=1, z=2# empty assignment?x, y = [], []#ans: x=[], y=[]
Google tag (gtag.js)