Skip to content
Advertisement

Simple prime number generator in Python

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). Answer There are some problems: Why do you print out count when it didn’t divide by x? It doesn’t mean it’s prime, it means only that this particular x doesn’t divide it

Subtracting 2 lists in Python

Right now I have vector3 values represented as lists. is there a way to subtract 2 of these like vector3 values, like Should I use tuples? If none of them defines these operands on these types, can I define it instead? If not, should I create a new vector3 class? Answer If this is something you end up doing frequently,

Accessing the index in ‘for’ loops

How do I access the index while iterating over a sequence with a for loop? Desired output: Answer Use the built-in function enumerate(): It is non-pythonic to manually index via for i in range(len(xs)): x = xs[i] or manually manage an additional state variable. Check out PEP 279 for more.

How to clear the interpreter console?

Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc. Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same command several times. I’m wondering if, and how, to clear the

Multidimensional array in Python

I have a little Java problem I want to translate to Python. Therefor I need a multidimensional array. In Java it looks like: Further values will be created bei loops and written into the array. How do I instantiate the array? PS: There is no matrix multiplication involved… Answer You can create it using nested lists: If it has to

Why is it string.join(list) instead of list.join(string)?

This has always confused me. It seems like this would be nicer: Than this: Is there a specific reason it is like this? Answer It’s because any iterable can be joined (e.g, list, tuple, dict, set), but its contents and the “joiner” must be strings. For example: Using something other than strings will raise the following error: TypeError: sequence item

Timeout on a function call

I’m calling a function in Python which I know may stall and force me to restart the script. How do I call the function or what do I wrap it in so that if it takes longer than 5 seconds the script cancels it and does something else? Answer You may use the signal package if you are running on

Convert list of ints to one number?

I have a list of integers that I would like to convert to one number like: What is the best way to implement the magic function? EDIT I did find this, but it seems like there has to be a better way. Answer

Locking a file in Python

I need to lock a file for writing in Python. It will be accessed from multiple Python processes at once. I have found some solutions online, but most fail for my purposes as they are often only Unix based or Windows based. Answer Alright, so I ended up going with the code I wrote here, on my website link is

Advertisement