Questions tagged [literals]

a notation for representing fixed values in source code

External links

1554 questions
795
votes
5 answers

Why is [] faster than list()?

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…
Augusta
  • 7,171
  • 5
  • 24
  • 39
594
votes
7 answers

Empty set literal?

[] = empty list () = empty tuple {} = empty dict Is there a similar notation for an empty set? Or do I have to write set()?
Johan Råde
  • 20,480
  • 21
  • 73
  • 110
583
votes
6 answers

Can I escape a double quote in a verbatim string literal?

In a verbatim string literal (@"foo") in C#, backslashes aren't treated as escapes, so doing \" to get a double quote doesn't work. Is there any way to get a double quote in a verbatim string literal? This understandably doesn't work: string foo =…
kdt
  • 27,905
  • 33
  • 92
  • 139
442
votes
5 answers

What does the M stand for in C# Decimal literal notation?

In order to work with decimal data types, I have to do this with variable initialization: decimal aValue = 50.0M; What does the M part stand for?
coson
  • 8,301
  • 15
  • 59
  • 84
397
votes
8 answers

How do you express binary literals in Python?

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 Python? Summary…
Justin Standard
  • 21,347
  • 22
  • 80
  • 89
332
votes
10 answers

Why does instanceof return false for some literals?

"foo" instanceof String //=> false "foo" instanceof Object //=> false true instanceof Boolean //=> false true instanceof Object //=> false false instanceof Boolean //=> false false instanceof Object //=> false 12.21 instanceof Number //=>…
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
296
votes
3 answers

What is the >>>= operator in C?

Given by a colleague as a puzzle, I cannot figure out how this C program actually compiles and runs. What is this >>>= operator and the strange 1P1 literal? I have tested in Clang and GCC. There are no warnings and the output is "???" #include…
CustomCalc
  • 2,182
  • 3
  • 12
  • 9
278
votes
6 answers

How do you specify a byte literal in Java?

If I have a method void f(byte b); how can I call it with a numeric argument without casting? f(0); gives an error.
Charbel
  • 14,187
  • 12
  • 44
  • 66
273
votes
5 answers

C# short/long/int literal format?

In C/C#/etc. you can tell the compiler that a literal number is not what it appears to be (ie., float instead of double, unsigned long instead of int): var d = 1.0; // double var f = 1.0f; // float var u = 1UL; // unsigned long etc. Could someone…
3Dave
  • 28,657
  • 18
  • 88
  • 151
262
votes
9 answers

Java: how do I get a class literal from a generic type?

Typically, I've seen people use the class literal like this: Class cls = Foo.class; But what if the type is generic, e.g. List? This works fine, but has a warning since List should be parameterized: Class cls = List.class So why not add…
Tom
  • 18,685
  • 15
  • 71
  • 81
258
votes
14 answers

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

Technically, any odd number of backslashes, as described in the documentation. >>> r'\' File "", line 1 r'\' ^ SyntaxError: EOL while scanning string literal >>> r'\\' '\\\\' >>> r'\\\' File "", line 1 r'\\\' …
cdleary
  • 69,512
  • 53
  • 163
  • 191
252
votes
4 answers

(-2147483648> 0) returns true in C++?

-2147483648 is the smallest integer for integer type with 32 bits, but it seems that it will overflow in the if(...) sentence: if (-2147483648 > 0) std::cout << "true"; else std::cout << "false"; This will print true in my testing. However,…
benyl
  • 2,097
  • 2
  • 13
  • 7
233
votes
4 answers

Literal notation for Dictionary in C#?

I currently have a WebSocket between JavaScript and a server programmed in C#. In JavaScript, I can pass data easily using an associative array: var data = {'test': 'val', 'test2': 'val2'}; To represent this data object on the server…
pimvdb
  • 151,816
  • 78
  • 307
  • 352
193
votes
4 answers

How do I do a literal *int64 in Go?

I have a struct type with a *int64 field. type SomeType struct { SomeField *int64 } At some point in my code, I want to declare a literal of this (say, when I know said value should be 0, or pointing to a 0, you know what I mean) instance :=…
ThisGuy
  • 2,661
  • 2
  • 18
  • 18
169
votes
2 answers

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

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…
Nikki Erwin Ramirez
  • 9,676
  • 6
  • 30
  • 32
1
2 3
99 100