756 Answered Questions for the topic Python

05/24/19

How to print to stderr in Python?

There are several ways to write to stderr: # Note: this first one does not work in Python 3 print >> sys.stderr, "spam" sys.stderr.write("spam\ ") os.write(2, b"spam\ ") ... more

05/24/19

How to leave/exit/deactivate a Python virtualenv?

I'm using virtualenv and the virtualenvwrapper. I can switch between virtualenv's just fine using the workon command. me@mymachine:~$ workon env1 (env1)me@mymachine:~$ workon env2 ... more

Create a dictionary with list comprehension in Python?

I like the Python list comprehension syntax.Can it be used to create dictionaries too? For example, by iterating over pairs of keys and values: mydict = {(k,v) for (k,v) in blah blah blah} #... more

05/24/19

Can someone explain __all__ in Python?

I have been using Python more and more, and I keep seeing the variable `__all__` set in different `__init__.py` files. Can someone explain what this does?
Python List Copy Clone

05/23/19

How to clone or copy a list?

What are the options to clone or copy a list in Python?While using `new_list = my_list`, any modifications to `new_list` changes `my_list` everytime.Why is this?

05/22/19

Most elegant way to check if the string is empty in Python?

Does Python have something like an empty string variable where you can do: if myString == string.empty:Regardless, what's the most elegant way to check for empty string values? I find hard... more

05/21/19

Print in terminal with colors?

How can I output colored text to the terminal, in Python?What is the best Unicode symbol to represent a solid block?

How to import a module given the full path?

How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.
Python Datetime

05/20/19

Converting string into datetime?

Short and simple. I've got a huge list of date-times like this as strings: Jun 1 2005 1:33PM Aug 28 1999 12:00AMI'm going to be shoving these back into proper datetime fields in a database... more

05/20/19

Catch multiple exceptions in one line (except block)?

I know that I can do: try: # do something that may fail except: # do this if ANYTHING goes wrongI can also do this: try: # do something that may fail except... more

What is the Python 3 equivalent of "python -m SimpleHTTPServer"?

What is the Python 3 equivalent of `python -m SimpleHTTPServer`?

Adding new column to existing DataFrame in Python pandas?

I have the following indexed DataFrame with named columns and rows not- continuous numbers: a b c d 2 0.671399 0.101208 -0.181532 0.241273 3 0.446172... more

How can you profile a Python script?

Project Euler and other coding contests often have a maximum time to run or people boast of how fast their particular solution runs. With python, sometimes the approaches are somewhat kludgey -... more
Python List

05/17/19

How do I remove an element from a list by index in Python?

How do I remove an element from a list by index in Python?I found the `list.remove` method, but say I want to remove the last element, how do I do this? It seems like the default remove searches... more

05/16/19

Proper way to declare custom exceptions in modern Python?

What's the proper way to declare custom exception classes in modern Python? My primary goal is to follow whatever standard other exception classes have, so that (for instance) any extra string I... more

05/14/19

What are metaclasses in Python?

What are metaclasses and what do we use them for?

05/14/19

Replacements for switch statement in Python?

I want to write a function in Python that returns different fixed values based on the value of an input index. In other languages I would use a `switch` or `case` statement, but Python does not... more

05/14/19

Measure time elapsed in Python?

What I want is to start counting time somewhere in my code and then get the passed time, to measure the time it took to execute few function. I think I'm using the timeit module wrong, but the docs... more
Python

05/14/19

RESTful API Web Development

Is there anyone experienced with front-end development or web development who could tutor me tomorrow evening on developing simple RESTful API's in Python (I can use other languages as well, but... more
Python Version

05/14/19

How do I check what version of Python is running my script?

How can I check what version of the Python Interpreter is interpreting my script?
Python List Tuples

05/13/19

What's the difference between lists and tuples?

What's the difference?What are the advantages / disadvantages of tuples / lists?

How to make a flat list out of list of lists?

I wonder whether there is a shortcut to make a simple list out of list of lists in Python.I can do that in a for loop, but maybe there is some cool "one-liner"? I tried it with *reduce*, but I get... more
Python String Count

05/12/19

Count the number occurrences of a character in a string?

What's the simplest way to count the number of occurrences of a character in a string?e.g. count the number of times `'a'` appears in `'Mary had a little lamb'`

Write a program to implement the DBSCAN algorithm using python. Graphically visualise the cluster outputs with each cluster points given in different colours.

The input will be a chameleon data set. And also check the quality measure using Homogenity and seperation

05/11/19

Difference between append vs. extend list methods in Python?

What's the difference between the list methods `append()` and `extend()`?

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.