• Coding
  • [Exercise] Python quizz!

Today's exercise is about studying some edge cases in Python. Very simple actually:
Without running the code, what will be the code of the following:

Printing tuples

In Python 2:
a = (0)

print a
print (a)
print ((a))
print (((a)))
print (a, a)
print ((a, a))
In Python 3:
from __future__ import print_function # if you are running on Py2
a = (0)

print a
print (a)
print ((a))
print (((a)))
print (a, a)
print ((a, a))
Accessing globals
a = 5

def f(): print(a)
def g():
    a += 1
    print(a)

f()
g()
Write down your answers on a paper, then run the code and compare your results. Play with it for a while until you figure out why it behaves that way.

You can use this topic to ask questions if anything's unclear.
3 months later
I started learning python just today, and frankly, nothing is clear! haha.
One thing i realized is that with

a = 0

print ((a, a))

looks funny! (0, 0)
If you're just starting to learn Python, you won't get this exercise. The problem deals with a special type of variables called tuples. Once you learn how tuples work, come back to this exercise and try to solve it.

If you have specific questions (about this exercise) you can ask them here. If you have more general questions about Python programming open a new topic :)
rahmu wroteIf you're just starting to learn Python, you won't get this exercise. The problem deals with a special type of variables called tuples. Once you learn how tuples work, come back to this exercise and try to solve it.

If you have specific questions (about this exercise) you can ask them here. If you have more general questions about Python programming open a new topic :)
I do understand, but how come no feedback has been returned to this thread even though it's been there for a month?

Anyway i appreciate your support and guiding, but i'd rather look for solutions myself.
I'm not sure why nobody answered, although I do have an idea:

Unlike our other exercises, this one deals with a very specific language. A lot of programmers here don't know Python. The ones who do were probably able to find the solution by themselves (it takes about 20 mins of Googling to find the answer).

This exercise is exceptional. The goal is not to look for the answer, but to put forward some peculiar behavior in the Python parser. Once you get it (as mentioned, you need to know tuples for this), you won't feel like sharing your solution here. It'd be like giving away the answer to a riddle.

If you want to check more traditional exercises, you could start by practicing with these:
I believe you'd find them more interesting than this one.
2 months later
Here's one:

What's the output in both cases
a = [1]
b = a
a += [2]
print a == b
a = [1]
b = a
a = a + [2]
print a == b
xterm wroteHere's one:

What's the output in both cases
a = [1]
b = a
a += [2]
print a == b
a = [1]
b = a
a = a + [2]
print a == b
Case 1: true Because b should be holding a reference to a through the = operator and not a copy of what is in a. And in this case we are appending to the original "a" list value.

a += [2] => [1,2]

Case 2: false because when we are doing "a = some statement" we are creating a new variable "a" different than the original "a" create before(with a different memory address) which b doesn't point to. so a != b

a = a+[1,2] => [1,2] but b is no more pointing to that a so its value stays as [1] which is equal to the first a.