Disclaimer: New to stack overflow. In the Julia language the eval(parse()) ability comes in very useful. Example: eval(parse(&quot;x = 5&quot;)). I'm wondering if there is an equivalent in python, I get the consensus that this is a no. If it is a no why? Surely it would be a useful addition to the language.
50636
How do I execute a string containing Python code in Python?
16381
I would want to do something like:  &gt;&gt;&gt; lst = [1, 2, 3, 4, 5] &gt;&gt;&gt; lst.find(lambda x: x % 2 == 0) 2 &gt;&gt;&gt; lst.findall(lambda x: x % 2 == 0) [2, 4]   Is there anything nearing such behavior in Python's standard libraries?  I know it's very easy to roll-your-own here, but I'm looking for a more standard way.
9898
Code like this often happens: l = [] while foo:     # baz     l.append(bar)     # qux  This is really slow if you're about to append thousands of elements to your list, as the list will have to be constantly resized to fit the new elements. In Java, you can create an ArrayList with an initial capacity. If you have some idea how big your list will be, this will be a lot more efficient. I understand that code like this can often be refactored into a list comprehension. If the for/while loop is very complicated, though, this is unfeasible. Is there an equivalent for us Python programmers?
23996
I am a newbie to Python and have come across the following example in my book that is not explained very well. Here is my print out from the interpreter:  &gt;&gt;&gt; s = 'spam' &gt;&gt;&gt; s[:-1] 'spa'   Why does slicing with no beginning bound and a '-1' return every element except the last one? Is calling s[0:-1] logically the same as calling s[:-1]? They both return the same result. But I'm not sure what python is doing exactly. Any help would be greatly appreciated.
852
If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?
16336
Given the following code (that doesn't work):  while True:     #snip: print out current state     while True:         ok = get_input("Is this ok? (y/n)")         if ok.lower() == "y": break 2 #this doesn't work :(         if ok.lower() == "n": break     #do more processing with menus and stuff   Is there a way to make this work? Or do I have do one check to break out of the input loop, then another, more limited, check in the outside loop to break out all together if the user is satisfied?
15717
I'd like to do something like this:  &gt;&gt; foo = @() functionCall1() functionCall2()   So that when I said:  &gt;&gt; foo()   It would execute functionCall1() and then execute functionCall2().  (I feel that I need something like the )  EDIT:  functionCall1 and functionCall2 are not necessarily functions that return values.
63838
Let's say you have a few expressions like the following:  $$\begin{array}((x+17)^2 \\ x^2 + 34x + 289 \end{array} \\ 288 + \frac{x^2}{2} + \frac{x^2}{2} + 34x + 1 \\ [...] $$  You get the idea: there's an infinite number of ways of representing the same expression. The challenge is that my application is validating answers to math problems, input by math students practicing a certain concept. I'd like to make sure that whenever they submit an answer, the checker is completely insensitive to the issues of ordering, simplification, formatting etc. which would be plain frustrating. I'd also ideally like to support more than one variable such as x, y, z in the same expression.  What's a reliable way to check that two expressions are the same? The most brute force way I can think of would be to just plug a number into each variable of each expression and see what the result is.  I have a hunch however that there might be ways of getting false positives this way. That, and I might be either missing something really obvious about this, or there might be a better way of doing the comparison.  Would love to hear what you think.
25761
I need to evaluate a mathmatical expression that is presented to me as a string in C#.  Example noddy but gets the point across that the string as the expression.  I need the evaluate to then populate an int.  There is no Eval() in C# like in others langugaes...  String myString = "3*4";   Edit:  I am on VS2008   Tried the Microsoft.JScript. = Its deprecated method (but still complies - warning)  However the Microsoft.JScript dll that I have doens work on      public object InvokeMember(string   name, BindingFlags invokeAttr, Binder   binder, object target, object[] args);   Complains that there is a missing ";"  go figure...  EDIT 2  Solution - was the codeDom one - it worked for as there are no security issue - only me ever going to be running the code.  Many thanks for the replies ...  And the link to the new Dragon Book  awesome   EDIT 3  Matt dataTable.Compute() also works - even better for the security conscious.  (parameter checking noted)
17043
I have a Python script which takes as input a list of integers, which I need to work with four integers at a time.  Unfortunately, I don't have control of the input, or I'd have it passed in as a list of four-element tuples.  Currently, I'm iterating over it this way: for i in range(0, len(ints), 4):     # dummy op for example code     foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]  It looks a lot like &quot;C-think&quot;, though, which makes me suspect there's a more pythonic way of dealing with this situation.  The list is discarded after iterating, so it needn't be preserved.  Perhaps something like this would be better? while ints:     foo += ints[0] * ints[1] + ints[2] * ints[3]     ints[0:4] = []  Still doesn't quite &quot;feel&quot; right, though.  :-/ Related question:
24071
I'm trying to understand Python's approach to variable scope. In this example, why is f() able to alter the value of x, as perceived within main(), but not the value of n?  def f(n, x):     n = 2     x.append(4)     print('In f():', n, x)  def main():     n = 1     x = [0,1,2,3]     print('Before:', n, x)     f(n, x)     print('After: ', n, x)  main()   Output:  Before: 1 [0, 1, 2, 3] In f(): 2 [0, 1, 2, 3, 4] After:  1 [0, 1, 2, 3, 4]
12929
Could someone please tell me what I'm doing wrong with this code? It is just printing 'count' anyway.  I just want a very simple prime generator (nothing fancy).   import math  def main():     count = 3     one = 1     while one == 1:         for x in range(2, int(math.sqrt(count) + 1)):             if count % x == 0:                  continue             if count % x != 0:                 print count          count += 1
53471
Now that it's clear , there is an associated concept that I use all the time without knowing what it really means.   I suppose everybody made once a mistake with parenthesis, resulting  in an "object is not callable" exception. What's more, using __init__ and __new__ lead to wonder what this bloody __call__ can be used for.  Could you give me some explanations, including examples with the magic method ?
121693
To do the equivalent of Python list comprehensions, I'm doing the following:  some_array.select{|x| x % 2 == 0 }.collect{|x| x * 3}   Is there a better way to do this...perhaps with one method call?
2525
My  has failed me.  In Python, are the following two tests for equality equivalent?  n = 5 # Test one. if n == 5:     print 'Yay!'  # Test two. if n is 5:     print 'Yay!'   Does this hold true for objects where you would be comparing instances (a list say)?  Okay, so this kind of answers my question:  L = [] L.append(1) if L == [1]:     print 'Yay!' # Holds true, but...  if L is [1]:     print 'Yay!' # Doesn't.   So == tests value where is tests to see if they are the same object?
13332
I am trying to use a  as input and then creating a query with it - so I need a .    I was surprised to find that it couldn't do the conversion implicitly or explicitly - but I don't even know how I would do this, as the Java API is still fairly new to me.
57397
algpseudocode lets me write code such as:  \documentclass{article} \usepackage{algpseudocode} \begin{document}  \begin{algorithmic} \While{$n&gt;3$}    \If{$m&gt;n$}       \State ...       \State ...    \EndIf    \If{$m&gt;2$}       \State ...    \EndIf \EndWhile \end{algorithmic}  \end{document}   Yielding results similar to  while n&gt;3    if m&gt;n        ...        ...    end if    if m&gt;2        ...    end if end while   I'd like all of that "end if", "end while", "end procedure" text to disappear and everything to compress upwards such that the result is:  while n&gt;3    if m&gt;n        ...        ...    if m&gt;2        ...   That is, I'd like a Pythonic-style where indentations indicate blocks.  Can algpseudocode do this? Or is there another package with similar functionality?