Certain syntax I dont understand

I was doing a problem that looked like this

first_match = i < len(text) and pattern[j] in {text[i], '.'}

and this

ans = i == len(text)

How can you use an and or a == after an assignment operator?

Thanks

Hello @jaccobtw! Essentially, ans = i == len(text) means i is now storing a boolean value. We’ll take it from the right hand side:

  1. i == len(text) will return a boolean value: either len(text) is equal to i(True) , or it is not (False).
  2. ans = bascially just assigns the above boolean to the ans variable.

A very smiliar thing is happening with this line of code

There is just a bit more going on, but overall way it happens and result is the same.

I hope this helps!

3 Likes

So if the condition are true, then it becomes true, otherwise false

1 Like

Exactly!

1 Like