Can we put 2 conditions in a single if statement? [duplicate] - python

This question already has answers here:
Can you make multiple "if" conditions in Python? [duplicate]
(6 answers)
Closed 4 years ago.
I wanna ask if we can put 2 or more conditions in single 'if'
For example:
if a == 1 b == 2:
print 'can we do this? If yes, how? '

For both conditions to be true
if condition1 and condition2:
For one of the conditions to be true
if condition1 or condition2:

If you want to check that both are true, use and:
if (a == 1) and (b == 2):
"If a equals 1 and b equals 2 ...".
Look up logical operators for other similar operations.

Related

Ternary operator with returns of different length [duplicate]

This question already has answers here:
python function conditional return
(2 answers)
Closed 1 year ago.
Under python3.9, (1,0) if False else 1 resolves to 1 as one would expect. However, 1,0 if False else 1 (i.e., the above without parentheses) resolves to (1,1). Why is this happening?
Note that the same does not occur if one rewrites the expression as an if: [...] else, i.e.
if False:
1,0
else:
1
resolves to 1 as expected.
Operator precedence
1,0 if False else 1 is equivalent to 1, (0 if False else 1)

Using list comprehension to generate various lists of numbers: why comparisons work? [duplicate]

This question already has answers here:
What is Truthy and Falsy? How is it different from True and False?
(8 answers)
Closed 2 years ago.
I'm still new to python. Some of the exercises I'm working on are things like 'return a list of integers up to but less than n' or 'return a list of odd (or even) integers up to but less than n', etc.
Seems reasonably straight-forward, and I was able to understand how to make a short function that does these things. For example, for generating a list of odd numbers...
def odds(n)
return [i for i in range(n) if (i % 2 == 1)]
... works just fine.
But the suggested solution was:
def odds(n)
return [i for i in range(n) if i % 2]
This returns the same thing. But I don't understand the 'if i % 2' at the end. How can this work when we haven't clarified what the 'i % 2' test needs to evaluate to???
because in python booleans are just integers, 0 is False and any other number is True
So when you say if i % 2 the same as if you say if i % 2 != 0, if it equals 0 then it's false and it won't do anything, but if it's anything other than false 0 it will push i to the list
EDIT:
try to do something like this:
if 1:
print("True")
else:
print("False")
Then try it with 0 and you'll see the difference.

evaluate a list in one line code in python [duplicate]

This question already has answers here:
How do I check if there are duplicates in a flat list?
(15 answers)
Closed 2 years ago.
I created a function to evaluate list if they have duplicates or not:
def duplica(list_to_check):
if len(set(list_to_check)) != len(list_to_check):
print('there are duplicates inside the list')
result = 0
else:
result = 1
return result
print(duplica([1, 1, 2]))
##test it:
there are duplicates inside the list
0
I want to know if there's any alternative way to evaluate the list using a code of only one line (for example lambda or map)
If you only need the value:
0 if len(set(list_to_check)) != len(list_to_check) else 1
or even better (): (provided by: Olvin Roght in the comment)
int(len(set(list_to_check)) == len(list_to_check))
With print:
(0,print('there are duplicates inside the list'))[0] if len(set(list_to_check)) != len(list_to_check) else 1

Why is this else statement not returning False? [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 2 years ago.
I'm doing python bats and the question 'has23' says the following:
Given an int array length 2, return True if it contains a 2 or a 3.
I have written this code so far expecting it to work as intended with the else statement but I cannot trigger the False if 2 or 3 is not found within the array.
def has23(nums):
if 2 or 3 in nums:
return True
else:
return False
You need to completely write conditions that are separated by and or or. You code was actually checking if 2 or if 3 in nums. if 2 actually checks if 2 is nonzero, which is always true.
def has23(nums):
if 2 in nums or 3 in nums:
return True
else:
return False
Writing 2 or 3 in nums actually means, in Python, (2) or (3 in nums). Which will always evaluate to True because 2 is a truthy value. What you should write instead to perform your intent here is 2 in nums or 3 in nums.

How does list comparison work? [duplicate]

This question already has answers here:
Comparing two lists using the greater than or less than operator
(3 answers)
Closed 5 years ago.
I want to compare two lists. For example:
a = [8,9,9,11]
b = [8,7,20,10]
if a >= b :
print "true"
Why does this print "true"? I want to compare the values vertically like this:
8 >= 8 is true
9 >= 7 is true
9 >= 20 is false but the program return true
11 >= 10 is true
You can use list comprehension and all function as follows:
code:
a = 8,9,9,11
b = 8,7,20,10
print all([(a > b) for a, b in zip(a,b)])
output:
False
You can use a list comprehension for comparing two lists element wise then use all function to check all of comparison are True:
a = [8,9,9,11]
b = [8,7,20,10]
all(a[i]>=b[i] for i in range(len(a))) # False

Categories