3

Why do the two code samples return the same result?

code sample A)

employees = ['Michael', 'Dwight', 'Jim', 'Pam', 'Ryan', 'Andy', 'Robert']
index4 = (employees[4]) # with brackets
print(index4)

code sample B)

employees = ['Michael', 'Dwight', 'Jim', 'Pam', 'Ryan', 'Andy', 'Robert']
index4 = employees[4] #without brackets 
print(index4)

Both result in 'Ryan'.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
moahiya
  • 37
  • 5

4 Answers4

3

Why does the two code samples return the same result?

Because you are using parentheses as a grouping operator. This has nothing to do with lists:

a = (4)
print(a)
b = 4
print(b)

Let's look at some other examples:

c = (4 + 3) * 5
print(d)
d = 4 + 3 * 5
print(d)

Here you get two different results because the parentheses force the addition first, just as in arithmetic. Without the parentheses, the multiplication is first.

Similarly to your example, you could do this:

e = (4 + 3)

but again, these parentheses are not needed because the addition is the only operator.

Is it just good practice?

Use parentheses in mathematical expressions when they give the result you want or when they make the calculation more clear. Otherwise, leave them out.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Thank you for your help. I am new to this, in the future I will try to format my question better – moahiya Jul 14 '20 at 00:48
2

In python, parenthesis are used for grouping and also for function calls. Python knows the difference because of context. foo(x +y) calls the function foo while foo * (x + y) does multiplication of the object in foo.

You should only use grouping when needed. Since

index4 = (employees[4])

is only grouping 1 thing, you shouldn't do it. In fact, if its obvious what the precedence is, don't use it. This is correct:

index4 = employees[4] + employees[2] + employees[3]

But you can use it when you want to keep your python statements from exceeding 80 characters per line

index4 = (employees[4] + employees[2] + employees[3]
    + employees[26])
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • there is a character limit for python statements? I have learned that today. What happens when you reach the limit? forced to create a new line? – moahiya Jul 14 '20 at 00:50
  • 1
    Python doesn't have a character limit for statements or line length that I know of. I just tested long lines of "1 + 1 + 1 + ..." and got over 3 million before hitting a segmentation fault. But the [PEP8 Style Guide](https://www.python.org/dev/peps/pep-0008/) recommends keeping lines under 80 characters, both for smaller screens and readability in general. Notice that newspapers / websites and etc... try to keep column width down for easy reading? That's a reason to do it. – tdelaney Jul 14 '20 at 01:26
1

Brackets are used in python in three main ways

  • to define tuples
  • to group code
  • to pass arguments into a method

In this specific case - there won't be any difference, as this is the second case. (a) is same as a. But, if there was a comma after a - then a tuple would've been created - (a,). One benefit of using ( for grouping code is that you can spread it into several lines for better formatting.

index4 = (
    employees[4]
)
avloss
  • 2,389
  • 2
  • 22
  • 26
1

This depends entirely on what you're trying to accomplish. If you're just assigning one particular value to index4, then without the parenthesis would be a better choice. Sure, both work well; but with the parenthesis, there is a doubt as to whether you want to create a tuple (especially it is easy to create a tuple but forgetting the extra comma).

index4 = employee[4] is concise. There is no ambiguity.

index4 = (employee[4]) isn't. Did you want to create a tuple but just forgot to add the comma? Sure, the variable itself does give evidence that you wish to set the 4th item in the list; but, after a few days, when you look back, are you sure?

ewokx
  • 2,204
  • 3
  • 14
  • 27