Python convert a string in to its actual Boolean value? [duplicate] - python

This question already has answers here:
What does Python's eval() do?
(12 answers)
How to evaluate python string boolean expression using eval()?
(2 answers)
What's the difference between eval, exec, and compile?
(3 answers)
Closed 3 years ago.
I have a function that returns a string that can be interpreted as a Boolean
def foo():
return "not(True and False)"
How can I return the actual Boolean value, in this case True?

You could use eval:
def foo():
return eval("not(True and False)")
Even tho it is virtually the only solution, but it is still not safe.
Check this link for why it is not safe.

Related

Explanation for string behavior ["somestring" "somestring"] being evaluated to ["somestringsomestring"] [duplicate]

This question already has answers here:
Python string literal concatenation
(1 answer)
String concatenation without '+' operator
(6 answers)
Closed 4 months ago.
I'm using python 3.10.4 right now.
I mistakenly wrote ["somestring" "somestring"] without a , separating the strings. Which evaluated to ["somestringsomestring"] without raising an error (which an error raised was my expectation).
And it seems like it was like that for previous versions.
How should I interpret the process of "string" "string" being evaluated to "stringstring"?

How to return an operator from a function? [duplicate]

This question already has answers here:
How to pass an operator to a python function?
(5 answers)
Closed 2 years ago.
How can I return operators in python? I tried this but it didn't work obviously:
def plus():
return +
Is it even possible to write code like this without using the plus operator?
print(3plus()4)
Like this
import operator
print(operator.add(3,4))

Python : String value assigned via AND & OR [duplicate]

This question already has answers here:
How do "and" and "or" act with non-boolean values?
(8 answers)
Using "and" and "or" operator with Python strings
(6 answers)
Closed 2 years ago.
I am modifying some Python code that is used in an ODOO Addon.
How does the logic for the following statement work?
origin = (move.group_id and move.group_id.name or (move.origin or move.picking_id.name or "/"))
Values
move.group_id is an object
move.group_id.name is a string value
move.origin is a string value
move.picking_id.name is a string
the result is "origin" is assigned the string value of "move.group_id.name"
What is this kind of assignment called?
(I tried to google this first but its not inline if logic and I have a lack of words for what this is called.)

how do I distinguish in a simple way a number in an input? [duplicate]

This question already has answers here:
How do I check if a string represents a number (float or int)?
(39 answers)
How can I check if a string represents an int, without using try/except?
(23 answers)
Closed 4 years ago.
I am learning now how to make my program distinguish two types of input in one function... I know its possible but how can I actually do it ;-;
Heres abit of my code:
def alphanumeric_input_detection(base_input):
if (type(base_input)==str):
return (print("you've written it wrong you dumm dumm"))
else:
return(print("thats correct input"))
base_input=input("please write a number")
alphanumeric_input_detection(base_input)
You can use the isnumeric() function. It returns True if a string object is numeric.
s = '1'
print(s.isnumeric())
[Output] = True

Python Comprehensions troubleshooting [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 7 years ago.
I have problems to set up correctly my if statement.
This is my code:
def task_13():
Main_meal=['Meat','Cheese','Fish']
addons=['Potatoes','Rice','Salad']
my_meal=[(x+y) for x in Main_meal for y in addons if (x+y)!= 'FishRice' and 'CheeseRice']
print(my_meal)
My question is why Python filter out the 'CheeseRice' when is it stated there but only filter out the 'FishRice' option.
This is my output:
['MeatPotatoes', 'MeatRice', 'MeatSalad', 'CheesePotatoes', 'CheeseRice', 'CheeseSalad', 'FishPotatoes', 'FishSalad']
Thank you for your advice.
Here's the official reference on Python operator precedence, note that and is lower precedence than !=, so the != is evaluated first. Also and is a simple operator that takes the booleans on either side and returns a boolean representing their logical AND, it doesn't do what you tried to make it do.
Instead of
if (x+y)!= 'FishRice' and 'CheeseRice'
you need:
if (x+y)!= 'FishRice' and (x+y) != 'CheeseRice'
or alternatively
if (x+y) not in ('FishRice', 'CheeseRice')

Categories

Resources