python logical operator with variable number of arguments [duplicate] - python

This question already has answers here:
How do Python's any and all functions work?
(10 answers)
Closed 5 years ago.
is there a way in python to check the logical operator AND with variable number of arguments. for example:
def and_function(a,b,c,d,e....):
if a and b and c and d and c and d and e and . and . is True:
return True
i see that this is possible using *args function and using a counter. But wish to know if there is a better way of doing it.

You basically want all() but to customize it you can use this:
def and_function(*args):
return all([a > 5 for a in args])

Related

How can I honor parentheses in evaluating arithmetic expressions? [duplicate]

This question already has answers here:
Math operations from string [duplicate]
(8 answers)
Evaluating a mathematical expression in a string
(14 answers)
Closed 3 years ago.
I am fairly new to python/ programming in general and i am trying to write a function that will convert an equation passed in as a string to its numeric representation and do some basic calculations. I am having some trouble with the parenthesis as i am not sure how to represent them for order of operations.
Any help of tips would be greatly appreciated. Thank you!
EquationAsString ="( 2 + 3 ) * 5"
def toEquation(EquationAsString):
Equation = EquationAsString.split(' ')
#store info in list and use it like a stack, check the type etc.
answer = 25
return answer
You can use the eval method to do such a thing.
Example:
print(eval('(2+3)*5'))
Output:
25
And if you really wanted to put it in a function:
def evaluation_string(input):
print(eval(input))
Example
def evaluation_string(input):
print(eval(input))
string_equation = '(2+3)*5'
evaluation_string(string_equation)
Output:
25

Is it possible to save plus and minus signs inside a variable in python [duplicate]

This question already has answers here:
Math operations from string [duplicate]
(8 answers)
Evaluating a mathematical expression in a string
(14 answers)
Simplifying code - perform mathematical operations based on operator
(2 answers)
Closed 5 years ago.
This is just a general question as I am curious. When I run the following code I get a syntax error which is understandable. However would there be a way to save the + sign in an variable so I get an answer of 4?
SIGN = +
UNIT = 1
UNIT2 = 3
print(UNIT SIGN UNIT2)
You can make a function for the + operator:
def add(a, b):
return a + b
and store it:
operation = add
unit = 1
unit2 = 3
print(operation(unit, unit2))
Functions for operators are even built into Python:
from operator import add

What does **dict mean in python formatting expression? [duplicate]

This question already has answers here:
What does ** (double star/asterisk) and * (star/asterisk) do for parameters?
(25 answers)
Closed 7 years ago.
I'm learning formatting expressions from a book and i have a question:
'{num} = {title}'.format(**dict(num=7, title='Strings'))
in the line above why are there two * before dict?? what do they represent?
the book didn't even mention this type of formatting expression and suddenly it was in the examples!!
The Principles
Python functions allow keyword arguments. For example this function:
def sub(a, b):
return a - b
can be called like this:
>>> sub(b=10, a=4)
-6
If you have the arguments in a dictionary:
>>> args = {'a': 4, 'b': 10}
you can use the ** syntax to achieve the same:
>>> sub(**args)
-6
This is called unpacking because the items in the dictionary are used individually as arguments.
Your Example
Your example is over complicated. This would work:
'{num} = {title}'.format(num=7, title='Strings')
No need for using ** and a dictionary. The format() methods works like normal function and can be used with keyword arguments directly. The values 7 and Strings are put into a dictionary just be taken out again immediately.

Dynamic arguments for Python functions [duplicate]

This question already has answers here:
Expanding tuples into arguments
(5 answers)
Closed 7 years ago.
I have a list of lists, and I would like to enter them into a function in such a way that each element of the main list is a different argument to the function.
squares = [[1,5,9,13], [2,6,10,14], [3,7,11,15], [4,8,12,16]]
print zip(squares[0], squares[1], squares[2], squares[3])
# displays [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
Is there a way to do something like print zip(arguments(squares)) so that the arguments can be entered dynamically?
This is what the * operator is for:
print zip(*squares)

Calling itertools.product with unkown number of args [duplicate]

This question already has answers here:
How to get the cartesian product of multiple lists
(17 answers)
Closed 9 years ago.
I hope this is a easy one.
Is there any possiblity to call itertools.product with a yet (not hardcoded) not knwon number of arguments?
Something like this:
itertools.product(x[0],x[1],x[2],....)
and the dimension of x can't be hardcoded
Thanks!
Try:
itertools.product(*x)
i.e. we unpack the argument list.
You can use
itertools.product(*x)
Lookup *args and **kwargs?
a = [1,2,3]
b = [2,3,4]
c= [a,b]
itertools.product(*c)
You can pass array of arguments using *

Categories