Questions tagged [python]

Python is a multi-paradigm, dynamically typed, multi-purpose programming language. It is designed to be quick to learn, understand, and use, and enforces a clean and uniform syntax. Please note that Python 2 is officially out of support as of 2020-01-01. For version-specific Python questions, add the [python-2.7] or [python-3.x] tag. When using a Python variant (e.g. Jython, PyPy) or library (e.g. Pandas, NumPy), please include it in the tags.

Filter by
Sorted by
Tagged with
11912votes
47answers
2.9mviews

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._leftchild ...
user avatar
  • 135k
7380votes
39answers
4.0mviews

What does if __name__ == "__main__": do?

Given the following code, what does the if __name__ == "__main__": do? # Threading example import time, thread def myfunction(string, sleeptime, lock, *args): while True: lock.acquire() ...
user avatar
  • 96.3k
7205votes
31answers
2.4mviews

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?
6684votes
24answers
988kviews

What are metaclasses in Python?

In Python, what are metaclasses and what do we use them for?
user avatar
  • 546k
6565votes
39answers
4.8mviews

How do I check whether a file exists without exceptions?

How do I check whether a file exists or not, without using the try statement?
user avatar
  • 72.2k
6197votes
48answers
2.7mviews

How do I merge two dictionaries in a single expression (take union of dictionaries)?

I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged (i.e. taking the union). The update() method would be what I need, if it returned ...
user avatar
  • 113k
5636votes
63answers
4.0mviews

How to execute a program or call a system command?

How do you call an external command (as if I'd typed it at the Unix shell or Windows command prompt) from within a Python script?
user avatar
5129votes
30answers
3.2mviews

How can I safely create a nested directory?

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 os file_path = "/my/...
user avatar
  • 96.7k
4599votes
22answers
3.1mviews

Accessing the index in 'for' loops?

How do I access the index in a for loop like the following? ints = [8, 23, 45, 12, 78] for i in ints: print('item #{} = {}'.format(???, i)) I want to get this output: item #1 = 8 item #2 = 23 ...
user avatar
  • 289k
4554votes
27answers
3.3mviews

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

Is there a shortcut to make a simple list out of a list of lists in Python? I can do it in a for loop, but is there some cool "one-liner"? I tried it with functools.reduce(): from functools ...
user avatar
  • 46.2k
4239votes
33answers
951kviews

Difference between staticmethod and classmethod

What is the difference between a function decorated with @staticmethod and one decorated with @classmethod?
user avatar
4174votes
33answers
2.5mviews

Understanding slice notation

I need a good explanation (references are a plus) on Python's slice notation. To me, this notation needs a bit of picking up. It looks extremely powerful, but I haven't quite got my head around it....
user avatar
  • 74.4k
3972votes
39answers
5.3mviews

Finding the index of an item in a list

Given a list ["foo", "bar", "baz"] and an item in the list "bar", how do I get its index (1) in Python?
user avatar
  • 44k
3836votes
15answers
5.1mviews

Iterating over dictionaries using 'for' loops

I am a bit puzzled by the following code: d = {'x': 1, 'y': 2, 'z': 3} for key in d: print (key, 'corresponds to', d[key]) What I don't understand is the key portion. How does Python recognize ...
user avatar
  • 40k
3616votes
22answers
3.7mviews

Using global variables in a function

How can I create or use a global variable in a function? If I create a global variable in one function, how can I use that global variable in another function? Do I need to store the global variable ...
user avatar
  • 143k

15 30 50 per page
1
2 3 4 5
127721