+ 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 Antworten
+ 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