What is a = a and b?

Asked 2 years ago, Updated 2 years ago, 12 views

I'm asking you a question because there's an unknown part while studying through the library.

def exemple_function(version):
    self.version = version and int(version)

It said that, but I don't understand why the and statement came out there, not even the conditional statement...

I experimented with the command line for a moment.

A = 1 and 'b'
A
>>> 'b'

B = 1 and 2
B
>>> 2

This is how it's assigned a value after... Then you can write self.version = int(version) but I don't know why you used and like that.

I want you to tell me why you use it like that. Thank you.

python

2022-09-21 20:21

2 Answers

I think it's simply because of the type.

If '1.1', int('1.1') is an error. Usually, the version has more float form than an integer, so it seems to prevent input as a string.


2022-09-21 20:21

I don't know Python, but it works like JavaScript.

JavaScript returns the right side if both sides are true during the and operations. Conversely, or operations return the left side if both sides are true.


2022-09-21 20:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.