conditional-operator

Multiple object returns from method not acting as expected

Multiple object returns from method not acting as expected Question: While playing around with pyautogui I decided to make a method to limit the position of my cursor depending on the size of my monitor, since I was moving my cursor randomy. This is what my first attempt looked like. max_x = 10 max_y = …

Total answers: 2

Too many values to unpack in ternary operator

Too many values to unpack in ternary operator Question: Why in the second line of below code snippet of ternary operator there are 3 values to unpack, I am expecting it to be 2: x,y=5,10 x,y=100,20 if x<5 else 30,40 print(x,y) Output: ValueError: too many values to unpack (expected 2) x,y=5,10 x,y,z=100,20 if x<5 else …

Total answers: 1

What is the correct syntax for Walrus operator with ternary operator?

What is the correct syntax for Walrus operator with ternary operator? Question: Looking at Python-Dev and StackOverflow, Python’s ternary operator equivalent is: a if condition else b Looking at PEP-572 and StackOverflow, I understand what Walrus operator is: := Now I’m trying to to combine the "walrus operator’s assignment" and "ternary operator’s conditional check" into …

Total answers: 2

Maths with Ternary Boolean in Python

Maths with Ternary Boolean in Python Question: I wrote this little script to calculate dog years. The first two dog years are 10.5 human years and all other years following are worth 4. human_age = int(input(“Enter the human age to convert it to doggy years: “)) valid = (human_age <=2) def convert_to_dog_years(human_age): if valid: dog_years …

Total answers: 2

"or" conditional in Python troubles

"or" conditional in Python troubles Question: I’m learning Python and I’m having a little bit of a problem. Came up with this short script after seeing something similar in a course I’m taking. I’ve used “or” with “if” before with success (it doesn’t show much here). For some reason I can’t seem to get this …

Total answers: 3

Jinja2 shorthand conditional

Jinja2 shorthand conditional Question: Say I have this: {% if files %} Update {% else %} Continue {% endif %} In PHP, say, I can write a shorthand conditional, like: <?php echo $foo ? ‘yes’ : ‘no’; ?> Is there then a way I can translate this to work in a jinja2 template: ‘yes’ if …

Total answers: 2

`elif` in list comprehension conditionals

`elif` in list comprehension conditionals Question: Can we use elif in list comprehension? Example : l = [1, 2, 3, 4, 5] for values in l: if values==1: print ‘yes’ elif values==2: print ‘no’ else: print ‘idle’ Can we include the elif in our list comprehension, in a similar fashion to the code above? For …

Total answers: 6

Does Python have a ternary conditional operator?

Does Python have a ternary conditional operator? Question: Is there a ternary conditional operator in Python? Asked By: Devoted || Source Answers: Yes, it was added in version 2.5. The expression syntax is: a if condition else b First condition is evaluated, then exactly one of either a or b is evaluated and returned based …

Total answers: 31