Tag: python

66 votes
30 answers
962 views
How to get the current time in Python

What is the module/method used to get the current time?

avatar
Dario
0 1 0 9
asked 07 September, 2021
64 votes
30 answers
721 views
65 votes
21 answers
460 views
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 c

avatar
Leonardo
0 1 0 9
asked 07 September, 2021
57 votes
21 answers
415 views
57 votes
14 answers
1.9K views
Iterating over dictionaries using 'for' loops

I am a bit puzzled by the following code: d = {'x': 1, 'y': 2, 'z': 3} for key

avatar
Hilario
0 1 0 9
asked 07 September, 2021
56 votes
30 answers
684 views
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?

avatar
Chelsea
0 1 0 9
asked 07 September, 2021
52 votes
30 answers
581 views
Understanding slice notation

I need a good explanation (references are a plus) on Python's slice notation. To me, this notation need

avatar
Arline
0 1 0 9
asked 07 September, 2021
60 votes
30 answers
2.3K views
Difference between staticmethod and classmethod

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

avatar
Chaya
0 1 0 9
asked 07 September, 2021
62 votes
20 answers
903 views
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

avatar
Karolyn
0 1 0 9
asked 07 September, 2021
52 votes
23 answers
2.7K views
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: pri

avatar
Hildegard
0 1 0 9
asked 07 September, 2021
64 votes
28 answers
2.7K 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, cr

avatar
Dorian
0 1 0 9
asked 07 September, 2021
55 votes
30 answers
1.5K views
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

avatar
Dusty
0 1 0 9
asked 07 September, 2021
59 votes
30 answers
2.5K views
How do I merge two dictionaries in a single expression (taking union of dictionaries)?

I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, m

avatar
Thurman
0 1 0 9
asked 07 September, 2021
48 votes
30 answers
2.2K views
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?

avatar
Patsy
0 1 0 9
asked 07 September, 2021
60 votes
23 answers
3.1K views
What are metaclasses in Python?

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

avatar
Boyd
0 1 0 9
asked 07 September, 2021
54 votes
27 answers
1.6K 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 co

avatar
Mikki
0 1 0 9
asked 07 September, 2021
68 votes
30 answers
1.2K views
What does if __name__ == "__main__": do?

Given the following code, what does the if __name__ == "__main__": do? # Threading example import time, threa

avatar
Trudy
0 1 0 9
asked 07 September, 2021
46 votes
30 answers
1.5K views
57 votes
16 answers
371 views
"Large data" workflows using pandas

I have tried to puzzle out an answer to this question for many months while learning pandas. I use SAS for my

avatar
Robyn
0 1 0 9
asked 07 September, 2021