Use walrus operator when checking value - python-3.8

How can I check if a variable is equal to something, and set to a new variable in the child scope?
For example:
bar = 'foobar'
my_slice = bar[:3]
if my_slice == 'foo':
print(my_slice)
It seems like the new walrus operator here would be useful, but it's not immediately straightforward how you'd go about using it here

Walrus operators work here very well, we just need to understand exactly how they work.
if (my_slice := bar[3:]) == 'foo':
print(my_slice)
Walrus operators set a variable to the output of some expression. It's almost identical to the equal sign in function except it can be used inline.
So this expression:
(my_slice := bar[3:]) == 'foo'
Can be boiled down to (variable = expression) == value
So because the output of my_slice := bar[:3] is equal to bar[:3], the above is equivalent to
bar[3:] == 'foo'
Note: The parenthesis here are required, or else variable is going to equal the output of the comparison operation, i.e. True or False

Related

Why use the OR operator instead of the AND operator for a while loop?

So someone has briefly explained this to me, but unfortunately I still do not understand.
My thinking is, we use an AND because this means we need both conditions to be met in order to pass.
Whereas with an OR, it only requires one condition to pass.
So how come in my example we are using an OR operator for both conditions to be met?
#DOUBLE == MEANS EQUALITY
#SINGLE = MEANS ASSIGNMENT
#THIS WILL BE THE LEGIT USER CHOICE WHERE OUR CHOICE HAS TO BE
#A NUMBER THAT IS WITHIN RANGE, SO TWO VARIABLES TO MEET BIG BOY
def my_choice ():
#VARIABLES SECTION
#INITIALS
choice = 'wrong'
accepted_range = range(1,10)
within_range = False
#Just like our choice we have to give the false answer here to keep
#the while loop- why? I dont know yet, will update
#TWO CONDITIONS TO CHECK
#1-MAKE SURE ITS AN ACTUAL NUMBER
#2-MAKE SURE ITS WITHIN THE RANGE
#CODE TIME
while choice.isdigit()==False or within_range == False:
choice = input('Please enter a value bettwen 1-9, Thanks ')
#Digit check
if choice.isdigit() == False:
print('sorry mate {} is not a digit'.format(choice))
#Range Check
#If we have passed the digit check, we can use it in our range check
if choice.isdigit() == True:
#remember that input returns a string ya?
if int(choice) in accepted_range:
within_range = True
print('Well done, {} is defintely a number in range'.format(choice))
else:
within_range = False
print('Sorry, you have picked a number, just not in range')
If you use boolean algebra you can show that these are equivalent:
is_digit or within_range
not (is_digit and within_range)
not is_digit and not within_range
You can also make a truth table to confirm this.

In Kotlin, Why can the value of a val integer be reassigned by the inc() method?

Consider the following,
val x: Int = 0
val variables cannot be changed so doing x += 1 wouldn't work
The compiler says Val cannot be reassigned
why then does x.inc() work fine
doesn't x.inc() reassign the value from 0 to 1
x.inc() does not increment the variable x. Instead, it returns a value that is one more than the value of x. It does not change x.
val x = 0
x.inc() // 1 is retuned, but discarded here
print(x) // still 0!
As its documentation says:
Returns this value incremented by one.
That might seem like a very useless method. Well, this is what Kotlin uses to implement operator overloading for the postfix/prefix ++ operator.
When you do a++, for example, the following happens:
Store the initial value of a to a temporary storage a0.
Assign the result of a0.inc() to a.
Return a0 as the result of the expression.
Here you can see how inc's return value is used.

Kotlin: What is the difference between = vs ==?

I'm trying to set a certain value, monsterHealth, to display 0 instead of a negative number. If I'm trying to reassign the value to show 0 instead of, say, -2, would I want to use = or ==?
Those are different operators:
= - assignment operator,
== and != - equality operators,
=== and !== - referential equality operators.
also
val - read only variable/property (it cannot be reassigned/changed),
var - mutable variable/property.
Kotlin Documentation - Keywords and operators

operators precedence and associativity in C

i would be grateful if somebody could help me with this problem. The book I am currently reading has a question
Q What will be the output?
#include <stdio.h>
void main()
{
int a = 3, b = 2;
a = a ==b==0;
printf("%d, %d",a,b);
}
The answer is given as
1,2 ( even on codeblocks got the same answers)
Now i understand that equality operator has precedence over the assignment operator.
So it must be a== b or b == 0 first
Then as both the above have the same operator, The associativity rule causes
a == b to be evaluated first.
But from here on I am lost!
How does one get to 1 and 2 as the answer?
See https://en.cppreference.com/w/cpp/language/operator_precedence
Note the line that says, "Operators that have the same precedence are bound to their arguments in the direction of their associativity." You can see the associativity for each operator on the far right column.
Note that equality operator is on line 10, with associativity left-to-right.
Note that assignment is line 16, so it has lower precedence than equality.
// original
a = a == b == 0
// precedence rule
a = (a == b == 0)
// associativity rule
a = ((a == b) == 0)
I believe that the equality operator does does not have precedence over the assignment operator. C just works itself from left to right.
in
a = a == b == 0;
a is assigned to everything on the right of the equal sign.
On the right side of the equal sign, a == b is evaluated to false. Then we compare equality of the answer to a==b with 0. In c, 0 is equal to false, so we get true. This is our answer to "everything to the right of the equal sign". So then, we assign that value(true) to a, which is an integer. Because of automatic conversion, true is converted to 1 and assigned to a. Now a equals 1.
As an equation, the above process can be represented as: a = (( a == b ) == 0)
Hope this makes sense. Tell me if I need to clarify further.

Multiple Boolean statements VBA

I am having issues with some Boolean logic.
Essentially I want something to program in VBA a filter such that,
A = True AND (B = True OR B = False)
I just cannot seem to get the coding right to do this in VBA (I am using MS Access).
I have tried:
A = True AND B = True OR B = False
But this obviously fails (looks for either A,B = True OR B = False, essentially).
Am I missing something obvious here?
I chose to left out the actual code, but if requested I can post. My thinking is that I am missing some basic with the Boolean logic.
If A, B, and C are Boolean expressions, then comparing them to a Boolean literal (True, False) is redundant: the expression is already a Boolean expression.
That makes the original logic go like this:
If A And (B Or Not C) Then
The And operator has greater priority than the Or operator, so it gets evaluated first; if the (B Or Not C) part needs to be evaluated as a whole, then the parentheses are required, otherwise A And B takes precedence, making the expression (wrongly) evaluate as:
If (A And B) Or Not C ' redundant parentheses to illustrate operator precedence
Edit: I misread the OP, my brain put that C in there. If A And (B Or Not B) Then is redundant, as BigBen pointed out - the simplified logic would be If A Then. Still worth noting operator precedence and using parentheses when applicable and appropriate though.