757 Answered Questions for the topic Python

04/08/19

PYTHON LANGUAGE

Write a function that receives a disjoint set forest S and prints the number of sets encoded byS and the size of the largest set in S.
Python String

04/07/19

Is there a way to substring a string?

Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?Maybe like `myString[2:end]`?If leaving the second part means 'till the end', if... more
Python List Random

04/07/19

How to randomly select an item from a list?

Assume I have the following list: foo = ['a', 'b', 'c', 'd', 'e']What is the simplest way to retrieve an item at random from this list?
Python Attributes

04/07/19

How to know if an object has an attribute in Python?

Is there a way in Python to determine if an object has some attribute? For example: >>> a = SomeClass() >>> a.someProperty = value >>> a.property Traceback... more

04/07/19

Python simple if or logic statement?

How would you write, in python: if key < 1 or key > 34: I've tried every way I can think of, and am finding it very frustrating.
Python List Count

04/07/19

How can I count the occurrences of a list item?

Given an item, how can I count its occurrences in a list in Python?
Python Kwargs

04/07/19

What is the purpose and use of **kwargs?

What are the uses for `**kwargs` in Python? I know you can do an `objects.filter` on a table and pass in a `**kwargs` argument. Can I also do this for specifying time deltas i.e.... more
Python Logic

04/07/19

Reason for "all" and "any" result on empty lists?

In Python, the built-in functions [`all`](http://docs.python.org/library/functions.html#all) and [`any`](http://docs.python.org/library/functions.html#any) return `True` and `False` respectively... more
Python Dictionary Unset

04/05/19

How to remove a key from a Python dictionary?

When trying to delete a key from a dictionary, I write: if 'key' in myDict: del myDict['key']Is there a one line way of doing this?

04/04/19

NetBeans aficionados looking for a Python IDE?

I used to develop Java or HTML/PHP project on NetBeans. I appreciate a number of its functionality like : - Fast code libraries browsing - Code refactoring - Auto-completion - Highlight syntax... more

04/04/19

How to read a file line-by-line into a list?

How do I read every line of a file in Python and store each line as an element in a list? I want to read the file line by line and append each line to the end of the list.
Python

04/03/19

Converting integer to string in Python?

I want to convert an integer to a string in Python. I am typecasting it in vain: d = 15 d.str()When I try to convert it to string, it's showing an error like `int` doesn't have any attribute... more
Python Null

04/03/19

null object in Python?

How do I refer to the null object in Python?

04/03/19

Convert bytes to a string?

I'm using this code to get standard output from an external program: >>> from subprocess import * >>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]The... more

04/03/19

How do I check if a variable exists?

I want to check if a variable exists. Now I'm doing something like this: try: myVar except NameError: # Do something.Are there other ways without exceptions?

04/02/19

How to determine a Python variable's type?

How do I see the type of a variable whether it is unsigned 32 bit, signed 16 bit, etc.?How do I view it?
Python Module Pip

04/01/19

How can I get a list of locally installed Python modules?

I would like to get a list of Python modules, which are in my Python installation (UNIX server).How can you get a list of Python modules installed in your computer?

04/01/19

How do I trim whitespace?

Is there a Python function that will trim whitespace (spaces and tabs) from a string?Example: `\  example string\ ` → `example string`
Python Iterable

04/01/19

In Python, how do I determine if an object is iterable?

Is there a method like `isiterable`? The only solution I have found so far is to call hasattr(myObj, '__iter__')But I am not sure how fool-proof this is.
Python List

03/30/19

Finding the index of an item given a list containing it in Python?

For a list `["foo", "bar", "baz"]` and an item in the list `"bar"`, how do I get its index (1) in Python?
Python Class Oop Self

03/29/19

What is the purpose of self?

What is the purpose of the `self` word in Python? I understand it refers to the specific object created from that class, but I can't see why it explicitly needs to be added to every function as a... more

03/29/19

How to use threading in Python?

I am trying to understand threading in Python. I've looked at the documentation and examples, but quite frankly, many examples are overly sophisticated and I'm having trouble understanding them.How... more

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.