+ 1
What’s the point of writing each if, elif statements in parentheses?
What’s the point of writing each if, elif statement in parentheses? Is there any difference between ; name = input() length = len(name) for x in range(0, length): p = name[x] if (p == "A"): print("something ") elif (p == "B"): print("somthingg") And name = input() length = len(name) for x in range(0, length): p = name[x] if p == "A": print("something ") elif p == "B": print("somthingg")
4 odpowiedzi
+ 4
There's no obvious difference in the way you used above.
However, if you want to code more compound decisions, there's a need for the parentheses.
Example
age = int(input());
if (age > 18) and (age < 40):
print("Welcome to the party")
Without using parentheses, the above code wouldn't be very readable
+ 6
i found this,
If you had an additional part in your conditional, such as an 'and', it would be advisable to use parenthesis to indicate which 'or' that 'and' paired with. ... In Python and many other programming languages, parentheses are not required for every expression with multiple operators
+ 4
Maybe it's just that people coming from other languages like C or Java are used to writing it this way.
I see no convincing reason to use parentheses in that sort of if conditions.
0
thanks for help :)