When Are Parentheses Required Around a Tuple

When are parentheses required around a tuple?

The combining of expressions to create a tuple using the comma token is termed an expression_list. The rules of operator precedence do not cover expression lists; this is because expression lists are not themselves expressions; they become expressions when enclosed in parentheses.

So, an unenclosed expression_list is allowed anywhere in Python that it is specifically allowed by the language grammar, but not where an expression as such is required.

For example, the grammar of the if statement is as follows:

if_stmt ::=  "if" expression ":" suite
( "elif" expression ":" suite )*
["else" ":" suite]

Because the production expression is referenced, unenclosed expression_lists are not allowed as the subject of the if statement. However, the for statement accepts an expression_list:

for_stmt ::=  "for" target_list "in" expression_list ":" suite
["else" ":" suite]

So the following is allowed:

for x in 1, 2, 3:
print(x)

Why parenthesis are needed in tuples?

Because sometimes without the parentheses the expression would be ambiguous: without them, you are making a function call with three arguments. Since some functions do need to take three arguments, a different representation is needed to express a single three-element tuple argument. As it happens, type() can be called with one or three arguments. In the latter case the first argument must be a classname, so it complains when it sees an integer.

Why are tuples enclosed in parentheses?

The parentheses are just parentheses - they work by changing precedence. The only exception is if nothing is enclosed (ie ()) in which case it will generate an empty tuple.

The reason one would use parentheses nevertheless is that it will result in a fairly consistent notation. You can write the empty tuple and any other tuple that way.

Another reason is that one normally want a literal to have higher precedence than other operations. For example adding two tuples would be written (1,2)+(3,4) (if you omit the parentheses here you get 1,2+3,4 which means to add 2 and 3 first then form the tuple - the result is 1,5,4). Similar situations is when you want to pass a tuple to a function f(1,2) means to send the arguments 1 and 2 while f((1,2)) means to send the tuple (1,2). Yet another is if you want to include a tuple inside a tuple ((1,2),(3,4) and (1,2,3,4) are two different things.

Why are double parentheses in tuples converted to single parentheses (Python)?

Tuples with a single element must be declared with the syntax (x,). Otherwise, parentheses are interpreted as a means of clarifying computations (or changing priorities with respect to operations).

Why do tuples in a list comprehension need parentheses?

Python's grammar is LL(1), meaning that it only looks ahead one symbol when parsing.

[(v1, v2) for v1 in myList1 for v2 in myList2]

Here, the parser sees something like this.

[ # An opening bracket; must be some kind of list
[( # Okay, so a list containing some value in parentheses
[(v1
[(v1,
[(v1, v2
[(v1, v2)
[(v1, v2) for # Alright, list comprehension

However, without the parentheses, it has to make a decision earlier on.

[v1, v2 for v1 in myList1 for v2 in myList2]

[ # List-ish thing
[v1 # List containing a value; alright
[v1, # List containing at least two values
[v1, v2 # Here's the second value
[v1, v2 for # Wait, what?

A parser which backtracks tends to be notoriously slow, so LL(1) parsers do not backtrack. Thus, the ambiguous syntax is forbidden.

How can I take off the parenthesis when I output a tuple in python?

print(f'The examination will start from {str(exam_st_date).strip("()"))}')

In python what type of thing is a list without square brackets?

You can always run type(variable) to check the variable type. In this case, a "list" without brackets is not a list, it's a tuple.

From the docs:

A tuple consists of a number of values separated by commas.

Parentheses are not required, though you will have to use them when building complex data structures, with nested tuples for example.

Reference: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences

Convert a tuple of tuples to list and remove extra bracket

use for loop to match data:

sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))

target = 'orange,apple,coconut'

found = False

for item in sql_data:
if target in item:
found = True

print(found)

Output:

True

Use list compression to find weather target is found or not:

sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))

target = 'orange,apple,coconut'

out = [True if target in item else False for item in sql_data]

print(True in out)

Output:

True

Use lambda to find weather target value exits or not:

from functools import reduce
sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))

target = 'orange,apple,coconut'

found = reduce(lambda bool_value1, bool_value2:bool_value1 or bool_value2,[True if target in item else False for item in sql_data])

print(found)

Output:

True


Related Topics



Leave a reply



Submit