Literal Examples:
Here are examples of literals in different programming languages:
1. Integer Literal:
* Python: `10`, `-5`, `0`
* JavaScript: `10`, `-5`, `0`
* C++: `10`, `-5`, `0`
2. Floating-Point Literal:
* Python: `3.14`, `-2.718`, `1.0e-6`
* JavaScript: `3.14`, `-2.718`, `1.0e-6`
* C++: `3.14`, `-2.718`, `1.0e-6`
3. String Literal:
* Python: `"Hello, world!"`, `'This is a string'`
* JavaScript: `"Hello, world!"`, `'This is a string'`
* C++: `"Hello, world!"`, `'This is a string'`
4. Boolean Literal:
* Python: `True`, `False`
* JavaScript: `true`, `false`
* C++: `true`, `false`
5. Character Literal:
* C++: `'A'`, `'#'`, `'\n'`
6. Null Literal:
* Python: `None`
* JavaScript: `null`
* C++: `nullptr`
7. Array Literal:
* JavaScript: `[1, 2, 3]`, `["apple", "banana", "orange"]`
* C++: `{1, 2, 3}`, `{"apple", "banana", "orange"}`
8. Object Literal:
* JavaScript: `{ name: "John", age: 30 }`
Explanation:
* Literals represent fixed values directly within a program.
* They are not variables, meaning they cannot be changed once defined.
* They are often used to represent basic data types like numbers, text, and logical values.
Example Usage:
```python
Integer literal
age = 25
String literal
message = "Hello, world!"
Boolean literal
is_active = True
Calculate using integer literals
total_price = 10 + 20
Print string literal
print(message)
```
In this example, `25`, `"Hello, world!"`, and `True` are all literals. They represent fixed values that are used in the program.