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 questions, please also use the [python-2.7] or [python-3.x] tags. When using a Python variant (Jython, Pypy, Iron-python, etc.) - please also tag the variant.

54 votes
29 answers
2.8K views
How to iterate over rows in a DataFrame in Pandas

I have a DataFrame from Pandas: import pandas as pd inp = [{'c1':10, 'c2':100}, {'c1

avatar
Frederick
0 1 0 9
asked 07 September, 2021
54 votes
30 answers
1.6K views
"Least Astonishment" and the Mutable Default Argument

Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a

avatar
Chaya
0 1 0 9
asked 07 September, 2021
59 votes
30 answers
1.4K views
How do I pass a variable by reference?

The Python documentation seems unclear about whether parameters are passed by reference or value, and the foll

avatar
Gigi
0 1 0 9
asked 07 September, 2021
60 votes
21 answers
495 views
Convert bytes to a string

I'm using this code to get standard output from an external program: >>> from subprocess import

avatar
Keith
0 1 0 9
asked 07 September, 2021
47 votes
19 answers
1.3K views
How do I copy a file in Python?

How do I copy a file in Python? I couldn't find anything under os.

avatar
Robt
0 1 0 9
asked 07 September, 2021
50 votes
29 answers
3K views
How do I concatenate two lists in Python?

How do I concatenate two lists in Python? Example: listone = [1, 2, 3] listtwo = [4, 5, 6] Expected outcom

avatar
Tana
0 1 0 9
asked 07 September, 2021
54 votes
20 answers
1.4K views
How can I add new keys to a dictionary?

Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an

avatar
Venice
0 1 0 9
asked 07 September, 2021
58 votes
25 answers
2.9K views
What is the difference between __str__ and __repr__?

What is the difference between __str__ and __repr__ in Python?

avatar
Nereida
0 1 0 9
asked 07 September, 2021
60 votes
27 answers
370 views
How do I check if a list is empty?

avatar
Lashawna
0 1 0 9
asked 07 September, 2021
61 votes
5 answers
2.4K views
Catch multiple exceptions in one line (except block)

I know that I can do: try: # do something that may fail except: # do this if ANYTHING goes wrong I

avatar
Chaya
0 1 0 9
asked 07 September, 2021
66 votes
30 answers
957 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
717 views
65 votes
21 answers
455 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
410 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
678 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
579 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
899 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