Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis+ 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 Respostas
+ 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 :)