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"?
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))
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.)
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
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')