Tag: c

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
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
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
414 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
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
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
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
51 votes
16 answers
1.9K views
Wordpress keeps redirecting to install-php after migration

Here's my situation. I have followed the exact instructions on wordpress codex page about moving a site t

avatar
Daniel
0 1 0 9
asked 07 September, 2021
54 votes
4 answers
2.7K views
WooCommerce: Finding the products in database

I'm creating a website using WooCommerce and I want to restrict the available products to users depending

avatar
Scot
0 1 0 9
asked 07 September, 2021