Join us in building a kind, collaborative learning community via our updatedCode of Conduct.

Questions tagged [python]

Python is a dynamic, strongly typed, object-oriented, multipurpose programming language, designed to be quick (to learn, to use, and to understand), and to enforce a clean and uniform syntax. Two similar but incompatible versions of Python are in use (Python 2.7 or 3.x). For version-specific Python ...

8354
votes
36answers
1.8m views

What does the “yield” keyword do?

What is the use of the yield keyword in Python? What does it do?For example, I'm trying to understand this code1:def _get_child_candidates(self, distance, min_dist, max_dist):if self....
4601
votes
15answers
619k views

What are metaclasses in Python?

What are metaclasses and what do we use them for?
4470
votes
23answers
1.4m views

Does Python have a ternary conditional operator?

If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?
4377
votes
39answers
2.9m views

How do I check whether a file exists?

How do I see if a file exists or not, without using the try statement?
4165
votes
27answers
1.8m views

What does if __name__==“__main__”: do?

What does the if __name__=="__main__": do?# Threading exampleimport time, threaddef myfunction(string, sleeptime, lock, *args):while True:lock.acquire()time.sleep(...
3650
votes
51answers
2.6m views

Calling an external command in Python

How can I call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?
3238
votes
48answers
1.2m views

How to merge two dictionaries in a single expression?

I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The update() method would be what I need, if it returned its result instead of ...
3006
votes
24answers
1.9m views

How can I safely create a nested directory in Python?

What is the most elegant way to check if the directory a file is going to be written to exists, and if not, create the directory using Python? Here is what I tried:import osfile_path="/my/...
2946
votes
40answers
1.8m views

How do I sort a dictionary by value?

I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.I can sort on the keys, but how ...
2901
votes
14answers
2.7m views

Does Python have a string 'contains' substring method?

I'm looking for a string.contains or string.indexof method in Python.I want to do:if not somestring.contains("blah"):continue
2839
votes
32answers
2.5m views

How do I list all files of a directory?

How can I list all files of a directory in Python and add them to a list?
2739
votes
27answers
1.8m views

How do I check if a list is empty?

For example, if passed the following:a=[]How do I check to see if a is empty?
2701
votes
23answers
2.2m views

Difference between append vs. extend list methods in Python

What's the difference between the list methods append() and extend()?
2683
votes
25answers
562k views

What is the difference between @staticmethod and @classmethod in Python?

What is the difference between a function decorated with @staticmethod and one decorated with @classmethod?
2571
votes
20answers
1.4m views

Accessing the index in 'for' loops?

How do I access the index itself for a list like the following?ints=[8, 23, 45, 12, 78]When I loop through it using a for loop, how do I access the loop index, from 1 to 5 in this case?

153050per page
angop.ao, elkhabar.com, noa.al, afghanpaper.com, bbc.com, time.com, cdc.gov, nih.gov, xnxx.com, github.com,