Public Questions for tag [python]

Ask Question

Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation. Its language constructs as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.

15 questions


0

Vote

4

Answers

26

View

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 in

0

Vote

4

Answers

23

View

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

Jan 25

0

Vote

4

Answers

18

View

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 th

Jan 25

0

Vote

4

Answers

40

View

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

Jan 20

0

Vote

4

Answers

47

View

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.

Jan 20

0

Vote

4

Answers

27

View

What is the difference between a function decorated with @staticmethod and one decorated with @classmethod?

Jan 20

0

Vote

4

Answers

21

View

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 import reduce

Jan 20

0

Vote

5

Answers

20

View

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 ite

Jan 18

0

Vote

4

Answers

20

View

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/direct

Jan 18

0

Vote

5

Answers

22

View

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?

Jan 18

0

Vote

4

Answers

37

View

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() the method would be what I need if it returned

Jan 10

0

Vote

5

Answers

51

View

In Python, what are metaclasses and what do we use them for?

Jan 10

0

Vote

5

Answers

25

View

If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?

Jan 10

0

Vote

3

Answers

25

View

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

Jan 10

0

Vote

5

Answers

26

View

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