Access a value on an if condition

Asked

Viewed 158 times

0

Is there any way to access a value passed on the condition of a if? For example:

a = int(input('Digite um numero: '))
b = int(input('Digite outro numero: '))

if((a-b) > 0):
    print(f'O numero {(a-b)} é maior que Zero')

Instead of having to repeat the (a-b) within the print, it is possible to just call some magic method that pulls information from the created condition on the top row?

3 answers

7


In the Python 3.8 you can use the assignment Expression:

a = int(input('Digite um numero: '))
b = int(input('Digite outro numero: '))

if (result := (a-b)) > 0:
    print(f'O numero {result} é maior que Zero')

The variable result will receive the result of (a-b) and the value of this variable will be compared to 0. This way, you can use the variable inside the if normally.

Important to note the presence of parentheses, because otherwise you would not have the expected value:

if result := (a-b) > 0:
    print(f'O numero {result} é maior que Zero')

In this case result would receive the result of the comparison, a boolean, indicating if (a-b) is greater than zero.

  • Earned my +1 by remembering that this will exist... But every time I see this PEP I think the language will become Phpython

  • @fernandosavio this will depend only on the developers, in moderate use. This example is very forced and has no advantages over create a variable. I particularly prefer the variable here because it becomes more readable, but there are cases that the assignment Expression will help a lot.

  • I reread the PEP now and, in my opinion, will help at most in 5% of cases. Only that the code will be less readable... I even found it strange to approve a PEP that seems to break the Zen of Python...

  • I guess my biggest fear isn’t the Feature, and yes what people will do to her... hahahaha

  • @fernandosavio The idea is to supply the 5%... things like while chunk := file.read(8192) i think they will be much more readable and avoid having to start the variable with a "random" value, make a precondition of the while that initially will not make sense and only within the loop assign the real value to the variable.

  • It’s true, just confirms my last comment... I’m scared of the "creativity" that people will use to create absurd codes. uaheuaheuah

Show 1 more comment

6

Has, creating a variable, That’s exactly what this mechanism was created for. Some people create where they shouldn’t, that’s a case they should. Variables are used to store values for later use. Continuing to use bad names for the serial variables like this:

a = int(input('Digite um numero: '))
b = int(input('Digite outro numero: '))
c = a - b
if c > 0:
    print(f'O numero {c} é maior que Zero')

See working in the repl it.. Also put on the Github for future reference.

If you want something more magical than this, it doesn’t have and it doesn’t make sense to have.

0

Use a .format:

a = int(input('Digite um numero: '))
b = int(input('Digite outro numero: '))
c = a-b
if(c > 0):
    print('O numero {} é maior que Zero'.format(c))
  • In this case, it is more appropriate to maintain the f-string, as he did, than to use the format. I talked about it here and here, if you’re interested.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.