Whatā€™s the point of writing each if, elif statements in parentheses? | Sololearn: Learn to code for FREE!
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")

24th Nov 2018, 9:23 PM
Shams Karimi
Shams Karimi - avatar
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
24th Nov 2018, 9:49 PM
Dlite
Dlite - avatar
+ 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
24th Nov 2018, 9:34 PM
D_Stark
D_Stark - avatar
+ 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.
24th Nov 2018, 10:38 PM
HonFu
HonFu - avatar
0
thanks for help :)
24th Nov 2018, 10:02 PM
Shams Karimi
Shams Karimi - avatar