Python use OR operator [duplicate] - python

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 8 years ago.
I am new in Python and I try to sense the difference between two expression.
Is that:
a=1
if a==0 or 1:
print (a)
is same as:
a=1
if a==0 or a==1:
print(a)

Try using == instead of =. = is an assignment operator whereas == checks if the two are equal. Also, the first bit of code is not correct. You have to rewrite the entire condition.

Related

String with a minus sign passed to a function in python [duplicate]

This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 2 years ago.
Why is a string with a minus sign not recognised properly when passed to a function in python? The simple code
def f(s):
print(s)
if s is 'a-b':
print('I expect this to be printed.')
else:
print('Instead, this is printed.')
s = 'a-b'
if s is 'a-b':
print('Oustide everything is fine.')
f(s)
gives the output
Oustide everything is fine.
a-b
Instead, this is printed.
Can someone explain please why this happens? Without the minus sign everything is fine.
Python distinguishes between values that are equal and values that are identical. You should use the is operator rarely. In fact, you probably won't encounter too many cases where you use is other than is None.
Here, you want if s == 'a-b':.

Control flow in python with strings [duplicate]

This question already has answers here:
Python 2.7 Boolean Operators Logic
(1 answer)
How does boolean operator work on string in python
(6 answers)
Closed 2 years ago.
I have a very basic snippet of code.
print("a" or "b")
>> a
I'm not entirely sure what I expected to happen.
Why does this print "a", and in general how are strings handled with python control flow?
In python, empty strings evaluate to False in a boolean context, while non-empty strings are True.

Does Python Check ALL conditions in an multi-condition if statement? [duplicate]

This question already has answers here:
Does Python support short-circuiting?
(3 answers)
Closed 3 years ago.
I am curious if Python will continue checking conditions in an if statement if the first condition returns False. I'm wondering about this because I want to know if best practice is try to check conditions with low time-complexity before more complex checks.
Is there any difference between these two snippets?
if condition_1() and condition_2():
do_something()
and
if condition_1():
if condition_2():
do_something()
Yes, python boolean operators do short-circuit
Both code samples are semantically equivalent, but the first is more readable, as it has lower level of nesting.

Is there a better way to do booleans? [duplicate]

This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed 3 years ago.
if "a_string" in random_word or "b_string" in random_word:
...
Is there a cleaner, less dense way to write this boolean?
In case of if, you could use any to check for the occurrence of either of the two strings
if any(i in random_word for i in ["a_string", "b_string"]):
You can use a regular expression.
import re
if re.search(r'a_string|b_string', random_word):
...

Difference between != and <>? [duplicate]

This question already has answers here:
What does `<>` mean in Python?
(5 answers)
Closed 8 years ago.
I just saw some code using the <> operator (don't know what this is called) instead of the != operator in Python. Is there any difference between the two or do they mean the same thing? What's the <> operator called? Thanks.
The <> operator is considered obsolete:
https://docs.python.org/2/reference/expressions.html#not-in

Categories