Python data type behaving some different - python

I am finding out difference between data types in python . I am little bit confuse why type of demo2 is giving list instead of tuple . Can anyone tell me why it is printing type list instead of tuple ?
Code
demo1 = ()
print ("demo1 {}".format(type(demo1)))
demo2 = ([])
print ("demo2 {}".format(type(demo2)))
demo3 = ([],[])
print ("demo3 {}".format(type(demo3)))
Output
demo1 <type 'tuple'>
demo2 <type 'list'>
demo3 <type 'tuple'>

To create an empty tuple, use tuple(). It will print as (), but that's not how you create an empty tuple. As minimum, a tuple must contain one item. So you need to create it as ([],).
Your code ([]) is the same as [], the parenthesis in this case act as continuation characters` and not an empty tuple.
Expressions in parentheses, square brackets or curly braces can be
split over more than one physical line without using backslashes.
See the documentation on lexical analysis for more details.
You can also verify this at the interpreter:
>>> ([]) == []
True

Basically () small brackets are block enclosing brackets but eventually this represents tuple too in python. whenever we declare a tuple into python we need to comma separate 2 values or assign () (small opening and closing brackets, this will create an empty tuple).
So in your demo3 you made a list and enclosed with brackets that means your demo3 have a list vector. If you provide comma after [] just like ([], ) it will make a tuple of lists.
Below are some enhanced examples: -

Related

FastApi pydantic returns Boolean as True but sqlalchemy throws error Not a boolean value: True [duplicate]

Why does adding a trailing comma after an expression create a tuple with the expression's value? E.g. in this code:
>>> abc = 'mystring',
>>> print(abc)
('mystring',)
Why is the printed output ('mystring',), and not just mystring?
It is the commas, not the parentheses, which are significant. The Python tutorial says:
A tuple consists of a number of values separated by commas
Parentheses are used for disambiguation in other places where commas are used, for example, enabling you to nest or enter a tuple as part of an argument list.
See the Python Tutorial section on Tuples and Sequences
Because this is the only way to write a tuple literal with one element. For list literals, the necessary brackets make the syntax unique, but because parantheses can also denote grouping, enclosing an expression in parentheses doesn't turn it into a tuple: you need a different syntactic element, in this case the comma.
Make sure to read this great answer by Ben James.
Tuples are not indicated by the parentheses. Any expression can be enclosed in parentheses, this is nothing special to tuples. It just happens that it is almost always necessary to use parentheses because it would otherwise be ambiguous, which is why the __str__ and __repr__ methods on a tuple will show them.
For instance:
abc = ('my', 'string')
abc = 'my', 'string'
What about single element tuples?
abc = ('mystring',)
abc = 'mystring',
So in effect what you were doing was to create a single element tuple as opposed to a string.
The documentation clearly says:
An expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right.
Unpacking multi-element tuple:
a, b = (12, 14)
print(type(a))
Output:
int
Unpacking single-element tuple:
a, = (12, )
print(type(a))
Output:
int
Otherwise:
a = (12,)
print(type(a))
Output:
tuple
In the question's example, you assigned the variable 'abc' to a Tuple with a length of 1.
You can do multiple assignments with this similar syntax:
x,y = 20,50
Also note that the print statement has a special understanding for ending a print statement with a comma; This tells print to omit the trailing newline.
print 'hello',
print 'world'
result:
hello world
I was somewhat confused about the application of the comma, as you also apply a comma to make a list instead of tuple, but with a different variable assignment.
Hereby, a simple example that I made of how to create a tuple or a list.
abc = 1,2,3 # prints a tuple: (1, 2, 3)
*abc, = 1,2,3 # prints a list: [1, 2, 3]

Python dictionary['key'] = .. results in a Tuple rather than a String in some cases [duplicate]

Why does adding a trailing comma after an expression create a tuple with the expression's value? E.g. in this code:
>>> abc = 'mystring',
>>> print(abc)
('mystring',)
Why is the printed output ('mystring',), and not just mystring?
It is the commas, not the parentheses, which are significant. The Python tutorial says:
A tuple consists of a number of values separated by commas
Parentheses are used for disambiguation in other places where commas are used, for example, enabling you to nest or enter a tuple as part of an argument list.
See the Python Tutorial section on Tuples and Sequences
Because this is the only way to write a tuple literal with one element. For list literals, the necessary brackets make the syntax unique, but because parantheses can also denote grouping, enclosing an expression in parentheses doesn't turn it into a tuple: you need a different syntactic element, in this case the comma.
Make sure to read this great answer by Ben James.
Tuples are not indicated by the parentheses. Any expression can be enclosed in parentheses, this is nothing special to tuples. It just happens that it is almost always necessary to use parentheses because it would otherwise be ambiguous, which is why the __str__ and __repr__ methods on a tuple will show them.
For instance:
abc = ('my', 'string')
abc = 'my', 'string'
What about single element tuples?
abc = ('mystring',)
abc = 'mystring',
So in effect what you were doing was to create a single element tuple as opposed to a string.
The documentation clearly says:
An expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right.
Unpacking multi-element tuple:
a, b = (12, 14)
print(type(a))
Output:
int
Unpacking single-element tuple:
a, = (12, )
print(type(a))
Output:
int
Otherwise:
a = (12,)
print(type(a))
Output:
tuple
In the question's example, you assigned the variable 'abc' to a Tuple with a length of 1.
You can do multiple assignments with this similar syntax:
x,y = 20,50
Also note that the print statement has a special understanding for ending a print statement with a comma; This tells print to omit the trailing newline.
print 'hello',
print 'world'
result:
hello world
I was somewhat confused about the application of the comma, as you also apply a comma to make a list instead of tuple, but with a different variable assignment.
Hereby, a simple example that I made of how to create a tuple or a list.
abc = 1,2,3 # prints a tuple: (1, 2, 3)
*abc, = 1,2,3 # prints a list: [1, 2, 3]

python tuple single element [duplicate]

Why does adding a trailing comma after an expression create a tuple with the expression's value? E.g. in this code:
>>> abc = 'mystring',
>>> print(abc)
('mystring',)
Why is the printed output ('mystring',), and not just mystring?
It is the commas, not the parentheses, which are significant. The Python tutorial says:
A tuple consists of a number of values separated by commas
Parentheses are used for disambiguation in other places where commas are used, for example, enabling you to nest or enter a tuple as part of an argument list.
See the Python Tutorial section on Tuples and Sequences
Because this is the only way to write a tuple literal with one element. For list literals, the necessary brackets make the syntax unique, but because parantheses can also denote grouping, enclosing an expression in parentheses doesn't turn it into a tuple: you need a different syntactic element, in this case the comma.
Make sure to read this great answer by Ben James.
Tuples are not indicated by the parentheses. Any expression can be enclosed in parentheses, this is nothing special to tuples. It just happens that it is almost always necessary to use parentheses because it would otherwise be ambiguous, which is why the __str__ and __repr__ methods on a tuple will show them.
For instance:
abc = ('my', 'string')
abc = 'my', 'string'
What about single element tuples?
abc = ('mystring',)
abc = 'mystring',
So in effect what you were doing was to create a single element tuple as opposed to a string.
The documentation clearly says:
An expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right.
Unpacking multi-element tuple:
a, b = (12, 14)
print(type(a))
Output:
int
Unpacking single-element tuple:
a, = (12, )
print(type(a))
Output:
int
Otherwise:
a = (12,)
print(type(a))
Output:
tuple
In the question's example, you assigned the variable 'abc' to a Tuple with a length of 1.
You can do multiple assignments with this similar syntax:
x,y = 20,50
Also note that the print statement has a special understanding for ending a print statement with a comma; This tells print to omit the trailing newline.
print 'hello',
print 'world'
result:
hello world
I was somewhat confused about the application of the comma, as you also apply a comma to make a list instead of tuple, but with a different variable assignment.
Hereby, a simple example that I made of how to create a tuple or a list.
abc = 1,2,3 # prints a tuple: (1, 2, 3)
*abc, = 1,2,3 # prints a list: [1, 2, 3]

Python string converts magically to tuple. Why?

I've got a dict in which I load some info, among others a name which is a plain string. But somehow, when I assign it to a key in the dict it gets converted to a tuple, and I have no idea why.
Here's some of my code:
sentTo = str(sentTo)
print type(sentTo), sentTo
ticketJson['sentTo'] = sentTo,
print type(ticketJson['sentTo']), ticketJson['sentTo']
which outputs the following on my terminal:
<type 'str'> Pete Chasin
<type 'tuple'> ('Pete Chasin',)
Why does assigning it to a dict convert it to a tuple?
You told Python to create a tuple containing a string:
ticketJson['sentTo'] = sentTo,
# ^
It is the comma that defines a tuple. Parentheses are only needed to disambiguate a tuple from other uses of a comma, such as in a function call.
From the Parenthesized forms section:
Note that tuples are not formed by the parentheses, but rather by use of the comma operator. The exception is the empty tuple, for which parentheses are required — allowing unparenthesized “nothing” in expressions would cause ambiguities and allow common typos to pass uncaught.
and from Expression lists:
An expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right.
ticketJson['sentTo'] = sentTo, is single element tuple

What is the difference between ('string') and ('string',)? [duplicate]

Why does adding a trailing comma after an expression create a tuple with the expression's value? E.g. in this code:
>>> abc = 'mystring',
>>> print(abc)
('mystring',)
Why is the printed output ('mystring',), and not just mystring?
It is the commas, not the parentheses, which are significant. The Python tutorial says:
A tuple consists of a number of values separated by commas
Parentheses are used for disambiguation in other places where commas are used, for example, enabling you to nest or enter a tuple as part of an argument list.
See the Python Tutorial section on Tuples and Sequences
Because this is the only way to write a tuple literal with one element. For list literals, the necessary brackets make the syntax unique, but because parantheses can also denote grouping, enclosing an expression in parentheses doesn't turn it into a tuple: you need a different syntactic element, in this case the comma.
Make sure to read this great answer by Ben James.
Tuples are not indicated by the parentheses. Any expression can be enclosed in parentheses, this is nothing special to tuples. It just happens that it is almost always necessary to use parentheses because it would otherwise be ambiguous, which is why the __str__ and __repr__ methods on a tuple will show them.
For instance:
abc = ('my', 'string')
abc = 'my', 'string'
What about single element tuples?
abc = ('mystring',)
abc = 'mystring',
So in effect what you were doing was to create a single element tuple as opposed to a string.
The documentation clearly says:
An expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right.
Unpacking multi-element tuple:
a, b = (12, 14)
print(type(a))
Output:
int
Unpacking single-element tuple:
a, = (12, )
print(type(a))
Output:
int
Otherwise:
a = (12,)
print(type(a))
Output:
tuple
In the question's example, you assigned the variable 'abc' to a Tuple with a length of 1.
You can do multiple assignments with this similar syntax:
x,y = 20,50
Also note that the print statement has a special understanding for ending a print statement with a comma; This tells print to omit the trailing newline.
print 'hello',
print 'world'
result:
hello world
I was somewhat confused about the application of the comma, as you also apply a comma to make a list instead of tuple, but with a different variable assignment.
Hereby, a simple example that I made of how to create a tuple or a list.
abc = 1,2,3 # prints a tuple: (1, 2, 3)
*abc, = 1,2,3 # prints a list: [1, 2, 3]

Categories

Resources