765 Answered Questions for the topic Python
05/28/19
How do I parse a string to a float or int in Python?
In Python, how can I parse a numeric string like `"545.2222"` to its corresponding float value, `545.2222`? Or parse the string `"31"` to an integer, `31`?I just want to know how to parse a *float*...
more
05/27/19
How do I sort a list of dictionaries by a value of the dictionary?
I have a list of dictionaries and want each item to be sorted by a specific property values.Take into consideration the array below, [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]When...
more
Large data work flows using pandas?
I have tried to puzzle out an answer to this question for many months while learning pandas. I use SAS for my day-to-day work and it is great for it's out-of-core support. However, SAS is...
more
Use of *args and **kwargs?
So I have difficulty with the concept of `*args` and `**kwargs`.So far I have learned that: * `*args` = list of arguments - as positional arguments * `**kwargs` = dictionary - whose keys become...
more
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
05/24/19
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?
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?
05/21/19
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
05/20/19
What is the Python 3 equivalent of "python -m SimpleHTTPServer"?
What is the Python 3 equivalent of `python -m SimpleHTTPServer`?
05/19/19
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
05/18/19
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
Python Switch Statement
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?
What's the difference between lists and tuples?
What's the difference?What are the advantages / disadvantages of tuples / lists?
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.