1
x = {'a':1},
y = {'a':1,},

print(type(x), type(y), type({'a':1},), type({'a':1,},))

<class 'tuple'> <class 'tuple'> <class 'dict'> <class 'dict'>

why are the assigned type is tuple but the direct print outputs dict type?

Roll20
  • 29
  • 1
  • 3
  • Hint: what happens if you try `x = {'a':1}` instead? Do you see how this code is different from `x = {'a':1},`? – Karl Knechtel Aug 29 '22 at 04:30
  • 2
    Defining a variable with a comma at the end produces a tuple: `a = 1,` >>> `(1,)` However, when you pass an argument to a function with a trailing comma, the comma is ignored. eg `float(3,)` is the same as `float(3)`. This is why the trailing comma produces two different results in your two different contexts. The `type({'a':1},)` is indeed a dict. I'd also argue that this question is not a direct duplicate of the linked answer and was closed prematurely. – PeptideWitch Aug 29 '22 at 04:35
  • 2
    I added another duplicate link to highlight the differing behaviour for function calls. "Why does X behave differently from Y?" is usually not a well-posed question, but instead two questions: "Why does X behave as it does?" and "Why does Y behave as it does?" The provided title further confuses the matter. – Karl Knechtel Aug 29 '22 at 05:34

0 Answers0