[Python] Expression (1,2) does not make tuples with parentheses

When I tried to check the tuple type, I got the error TypeError:'type' object is not subscriptable. The bottom line was that I was using subscript operators for things that weren't tuples. Example: my_tuple [0]

Check for errors

The cause can be expressed in one word, but when I investigated the tuple from here, I found it interesting. First, let's take a look at the error that occurred this time.

foo = (object)
type(foo)
# => type

type(foo[0])
# => TypeError: 'type' object is not subscriptable

#Make two elements
foo = (object, str)
foo[0]
# => object

type(foo)
# => tuple

I've tried it numerically and get the same result. I think this is because python is all about objects.

bar = (0)
type(bar)
# => int

bar[0]
# => TypeError: 'int' object is not subscriptable

#Make two elements
bar = (1,2)
bar[0]
# => 1

type(bar)
# =>tuple

Make sure that you can put any type in the tuple.

foo = (object, 1)
type(foo)
# => tuple

How do you define a tuple with one element?

How do you define a tuple with one element? The answer was in the official documentation.

Use commas for single-element tuples: a, or (a,) 4. Embedded — Python 3.4.3 documentation </ cite >

I tried it right away.

foo = (object,)
type(foo)
# => tuple

foo[0]
# => object

It is the comma that makes the tuples

If you add a comma, it will be recognized as a tuple. ** If you think "I see! Can tuples be made with a combination of parentheses () and commas!", It's a mistake. ** It's easy to get caught here. However, there was a polite explanation in the official documentation.

Note that tuples are created with commas, not with parentheses. The exception is an empty tuple, which requires parentheses — allowing the use of “nothing” without parentheses would obscure the grammar. Common typos are no longer detected. 6. Expression — Python 3.4.3 documentation < / cite>

** "Created by commas" **. Let's check this.

()
# => ()
type(())
# => tuple

1,2
# => (1, 2)
type(1,2)
# => TypeError: type() takes 1 or 3 arguments
type((1,2))
# => tuple

foo = 1,
foo
# => (1,)
type(foo)
# => tuple

I didn't need any parentheses except for an empty tuple. However, if you try to generate a tuple on the fly when passing it to a function argument, it will be recognized as multiple arguments unless you add parentheses. The parentheses in the above argument are used as an operator to raise the priority by first generating the tuple and then passing it as an argument.

Also, if you can define an empty tuple without parentheses, you can't create a blank (not a half-width space). Empty tuples may be mass-produced in blank lines, at the end, and everywhere.

What does the parentheses mean?

What was returned when there was no comma? Let's check it.

foo = (object)
foo
# => object

bar = (1)
bar
# => 1

(object)
# => object

(1)
# => 1

The argument was returned as it is. This was also mentioned in the official documentation.

The list of expressions in parentheses will be what each expression represents: if the list contains at least one comma, it will be a tuple; otherwise it will compose the list of expressions. It will be the ** value of the single expression itself **. 6. Expression — Python 3.4.3 documentation < / cite>

Speaking of which, when performing arithmetic operations, parentheses are used to raise the priority of + and-, but that also returns the evaluation value of the expression inside the parentheses.

1 + 3 * 2
# => 7

(1 + 3) * 2
# => 8

Isn't the parentheses used to call python functions an operator?

The List of lexical analysis delimiters in the official document had parentheses and did not include operators. Isn't the parentheses that call a function in python a "function call operator"?

[Wikipedia C](https://en.wikipedia.org/wiki/C%E3%81%A8C%2B%2B%E3%81%AE%E6%BC%94%E7%AE%97%E5 Looking at% AD% 90), the parentheses () are still the operator "function call". Please let me know if anyone knows.

Recommended Posts

[Python] Expression (1,2) does not make tuples with parentheses
Make one repeating string with a Python regular expression.
Make Puyo Puyo AI with Python
Python version does not switch
Make Echolalia LINEbot with Python + heroku
Make apache log csv with python
String replacement with Python regular expression
Make a recommender system with python
Let's make a graph with python! !!
Make the Python console covered with UNKO
Let's make a shiritori game with Python
Fractal to make and play with Python
[VScode] autopep8 format does not work [Python]
Virtualenv does not work on Python3.5 (Windows)
Let's make a voice slowly with Python
gqlgen command does not work with go gqlgen
Make pypy submission easier with atcoder-cli (python)
[Python] Let's make matplotlib compatible with Japanese
Python> Python does not include the last offset
Let's make a web framework with Python! (1)
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
Opencv4.1 + Windows10 + Python 3.7.5 cv2.VideoCapture does not display correctly
Make Python scripts into Windows-executable .exes with Pyinstaller
Make a Twitter trend bot with heroku + Python
[Python] Make a game with Pyxel-Use an editor-
Python log is not output with docker-compose up
I want to make a game with Python
Try to make a "cryptanalysis" cipher with Python
Make OpenCV3 available from python3 installed with pyenv
Make your own module quickly with setuptools (python)
[Python] Make a simple maze game with Pyxel
Jinja2 2.9.6 does not work on Lambda Python 3 series
Let's replace UWSC with Python (5) Let's make a Robot
Try to make a dihedral group with Python
Make JSON into CSV with Python from Splunk
Make Python built with jhbuild work on OSX
When wildcard specification does not work with pylint
Make your Python environment "easy" with VS Code
python Boolean operation does not return a Boolean value
[# 1] Make Minecraft with Python. ~ Preliminary research and design ~
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Play with 2016-Python
Tested with Python
with syntax (Python)
Bingo with python
Excel with Python
Microcomputer with Python
Cast with python
[Python] Boolean operator (or / and) does not return Boolean value
Python> assert not np.isnan (loss_value),'Model diverged with loss = NaN'
Try to make a command standby tool with python
Right click event acquisition does not work with opencv-python
Key input that does not wait for key input in Python
Explain in detail how to make sounds with python