Python assign a variable use if condition [duplicate] - python

This question already has answers here:
Does Python have a ternary conditional operator?
(31 answers)
Closed 4 years ago.
I want to assign a variable when if statement is true. What's wrong with this assignment?
b = 1 if 3 > 2
File "<stdin>", line 1
b = 1 if 3 > 2
^
SyntaxError: invalid syntax

You need an else in case your condition fails.
b = 1 if 3>2 else 0

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)

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.

Generate a sequence using Recursion in Python [duplicate]

This question already has answers here:
Python, Make an iterative function into a recursive function
(4 answers)
Count to zero then Count up
(2 answers)
Closed 3 years ago.
I want to define a function as f(a,b) such that it generates a series as:
10,8,6,4,2,0,2,4,6,8,10 if a=10 and b=2 using Recursion.
def pattern(a,b):
if a-b < 0:
print(a)
return pattern(a+b,b)
print(a)
else:
print(a)
return pattern(a-b,b)
The result I get is
10
8
6
4
2
0
2
0
2
0
..... infinity
... but this is wrong.
You just need to use recursion
from __future__ import print_function
def psearch(a,b):
if a > 0:
print(a,end = ',')
psearch(a - b,b)
print(',',end="")
print(a,end = "")
else:
print(a,end="")
psearch(12,5)
print()
OUTPUT
12,7,2,-3,2,7,12

How can I compound a if and for loop inside a print statement in Python [duplicate]

This question already has answers here:
python 3 print generator
(2 answers)
Closed 4 years ago.
I am trying to combine this into one line -
for x in set(l_rooms):
if l_rooms.count(x) == 1:
print(x)
I tried:
print(x for x in set(l_rooms) if l_rooms.count(x)== 1)
but that only yields a generator object and doesn't actually print
print([x for x in set(l_rooms) if l_rooms.count(x)==1])

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

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.

Categories