Simple function that simulates survey results based on sample size and probability What is this:

This is a simple function, part of a basic Monte Carlo simulation. It takes sample size and probability as parameters. It returns the simulation result (positive answers) plus the input parameters in a tuple.

What I'm asking:

I'm trying to avoid using temporary variables, I have two questions.


Do I really save memory by avoiding storing interim results?
How could I improve readability without adding variables?



def simulate_survey(sample_size, percent_subscribes):
    return (
        sample_size,
        percent_subscribes,
        round(
            (
                sum([
                    r.random() &lt; percent_subscribes 
                    for _ in range(sample_size)
                ]) / sample_size
            ),
            2
        )
    )
As I discovered recently, summing a lot of booleans, where the chance that the value is False is not negligible, can be surprisingly slow.

So I would change your survey result calculation to:

sum([1 for _ in range(sample_size) if r.random() &lt; percent_subscribes])


This allows sum to use its faster integer implementation and you do not sum a bunch of zeros.
Alternatively, you could look at this problem as an application of the binomial distribution. You have some chance that a certain result is obtained and you want to know how often that chance was true for some population. For this you can use numpy.random.binomial:

import numpy as np

def simulate_survey(sample_size, percent_subscribes):
    subscribers = np.random.binomial(sample_size, percent_subscribes)
    return sample_size, percent_subscribes, round(subscribers / sample_size, 2)


Using numpy here may also speed up your process in other places. If you need to run this function many times, you probably want to use the third argument to generate multiple values at once.

IMO, the readability is also greatly increased by using one temporary variable here, instead of your many levels of parenthesis.
I am not a fan of your function returning its inputs. The values of those should already be available in the scope calling this function, so this seems unnecessary. One exception would be that you have other, similar, functions which actually return different/modified values there.
You should add a docstring describing what your function does.