literals

Why is [] faster than list()?

Why is [] faster than list()? Question: I recently compared the processing speeds of [] and list() and was surprised to discover that [] runs more than three times faster than list(). I ran the same test with {} and dict() and the results were practically identical: [] and {} both took around 0.128sec / …

Total answers: 5

What are constants and literal constants?

What are constants and literal constants? Question: I’m learning Python and I am confused with the constants and literal constants. What are they? For what kind of purpose do we use them? What is the difference from the normal variable? I’m a true beginner. As beginner, I can say I know nothing about the programming …

Total answers: 1

What's with the integer cache maintained by the interpreter?

What's with the integer cache maintained by the interpreter? Question: After dive into Python’s source code, I find out that it maintains an array of PyInt_Objects ranging from int(-5) to int(256) (@src/Objects/intobject.c) A little experiment proves it: >>> a = 1 >>> b = 1 >>> a is b True >>> a = 257 >>> …

Total answers: 1

String literal with triple quotes in function definitions

String literal with triple quotes in function definitions Question: I am following the Python tutorial and at some point they talk about how the 1st statement of a function can be a String Literal. As far as the example goes, this String Literal seems to be done with three “s, giving in the example “””Print …

Total answers: 6

Empty set literal?

Empty set literal? Question: [] = empty list () = empty tuple {} = empty dict Is there a similar notation for an empty set? Or do I have to write set()? Asked By: Johan Råde || Source Answers: No, there’s no literal syntax for the empty set. You have to write set(). Answered By: …

Total answers: 7

What does preceding a string literal with "r" mean?

What does preceding a string literal with "r" mean? Question: I first saw it used in building regular expressions across multiple lines as a method argument to re.compile(), so I assumed that r stands for RegEx. For example: regex = re.compile( r’^[A-Z]’ r'[A-Z0-9-]’ r'[A-Z]$’, re.IGNORECASE ) So what does r mean in this case? Why …

Total answers: 2

Why can't Python's raw string literals end with a single backslash?

Why can't Python's raw string literals end with a single backslash? Question: Technically, any odd number of backslashes, as described in the documentation. >>> r” File “<stdin>”, line 1 r” ^ SyntaxError: EOL while scanning string literal >>> r’\’ ‘\\’ >>> r’\’ File “<stdin>”, line 1 r’\’ ^ SyntaxError: EOL while scanning string literal It …

Total answers: 12

How do you express binary literals in Python?

How do you express binary literals in Python? Question: How do you express an integer as a binary number with Python literals? I was easily able to find the answer for hex: >>> 0x12AF 4783 >>> 0x100 256 and octal: >>> 01267 695 >>> 0100 64 How do you use literals to express binary in …

Total answers: 8