samedi 5 janvier 2019

What is the proper syntax for grouping conditions in a if-statement in Python?

I'm having trouble fully understanding the syntax of if-statement in python. Is it possible to group up conditions as demonstrated below?

if my_age and neighborhood_age > 20:

Will python understand the above code exactly as: if my_age> 20 and neighborhood_age > 20: ?

Provided it does understand exactly the same thing, how can I group conditions? e.g.:

Suppose I have three conditions:

my_age and neighborhood_age > 20 father_age < 60 cousin_age < my age

What is the correct way to write the if-statement? if (my_age and neighborhood_age > 20) and (father_age < 60) and (cousin_age < my age): ?

And what happens if I start mixing 'and' and 'or' operators? What is the best way to write the following code:

if ((my_age and neighborhood_age > 20) and (father_age < 60) and (cousin_age < my age)) or girlfriend_age > 18:

Aucun commentaire:

Enregistrer un commentaire