Literals in Python

A literal is a way to express a fixed value code in Python. Depending on their kind, the computer handles these fixed values in different ways. Literals in python give you a simple way to include values of the many built-in data types in Python into your projects.
Here are a few examples of the literal types
Numeric Literals
The fundamentals of numbers, including integer and floating-point forms, and make reference to numeric literals.
- The representation of a whole number is an integer literal. Simply put, an integer literal is a string of numbers without a decimal point.
- The representation of a decimal number is a floating-point literal. Among them are 10.5 and 3.1416. A decimal point is used to indicate these.
- Demonstrate the use of numerical numbers as variables or as literals in calculations. One example of an expression that uses numeric literals is (10.5 + 2 * 3) / (45 – 3.5). When allocated to variables subtotal and tax, the values 25 and 3 are utilised as integer literals.
- These numeric types can be used for arithmetic operations in Python. Numbers, as opposed to letters or words, can be employed in mathematics and indicate amounts.
Code Example for Numeric Literals:
# Integer literals
age = 30
count = 100
# Floating-point literals
price = 19.95
pi = 3.14159
# Using numeric literals in an expression
result = (10 + 5 * 2) / 2.5
print(result)
Output:
8.0
String Literals
A string is defined as a collection of characters. A string literal is a literal representation of the text or words’ character sequence. You can use matching single quotes (‘…’) or double quotes (“…”) to contain string values.
- An example of a single quote is “Hello There (Single Quote).”
- An example of a double quote is “Hello There (Double Quote)!”
- When used for multiline strings or documentation strings (docstrings), single or double quotes enable the text to span many lines.
- Here are some instances of code that uses string literals: “Hello, World!” “Game Over,” “beauty brought butter, butter was bitter, so she brought more butter to make bitter butter better.” and text inside print() instructions.
- Formatted string literals, or f-strings, are supported by Python as well. They are prefixed with f or F and can include replacement fields to embed values in curly brackets {}.
- Strings can have special characters represented by utilising escape sequences, which start with a backslash \ and then contain a letter or number. For instance, \t for a tab is displayed.
Code Example for String Literals:
# Single-quoted string literal
message1 = 'This is a string with single quotes.'
# Double-quoted string literal
message2 = "This is a string with double quotes."
# Multiline string literal using triple double quotes
multiline_message = """This is a string
that spans multiple
lines."""
# Formatted string literal (f-string)
name = "Alice"
greeting = f"Hello, {name}!" # Embeds the value of the 'name' variable
print(greeting)
# String literal with an escape sequence
tabbed_output = "\tThis line is indented."
print(tabbed_output)
Output:
Hello, Alice!
This line is indented.
Aggregate Literal
Present aggregate data types or containers, including dictionaries, lists, and tuples. These are items that have the capacity to store an infinite amount of additional items. It include examples of how lists, tuples, and dictionary structures are defined directly in the code using particular syntax using literals for their members, even though they don’t use the term “literal” for the complete structure.
- List literals Commas are used to divide the members of list literals, which are specified by square brackets []. The items themselves may be variables or literals. Here’s one example.
- Tuple literals To define a tuple literal, use parenthesis () with commas between the components. An illustration of a tuple assignment or return is provided.
- Dictionary literals are defined with curly braces {} that contain key-value pairs, where the keys and values can be variables or literals, and are separated by commas. As an illustration, consider {“a”: 97, “b”: 98, “A”: 65, “B”: 66}. Additional instances of dictionary generation include {‘nile’: ‘egypt’}.
Code Example for Aggregate Literals:
# List literal
numbers_list = [55 - 58]
print(f"numbers_list: {numbers_list}")
mixed_list = [1, "hello", 3.14]
print(f"mixed_list: {mixed_list}")
# Tuple literal
coordinates = (100, 200)
print(f"coordinates: {coordinates}")
colors = ("red", "green", "blue")
print(f"colors: {colors}")
# Dictionary literal
student_info = {"name": "Bob", "age": 25, "is_student": True}
print(f"student_info: {student_info}")
encoding_map = {"a": 97, "b": 98}
print(f"encoding_map: {encoding_map}")
Output:
numbers_list: [-3]
mixed_list: [1, 'hello', 3.14]
coordinates: (100, 200)
colors: ('red', 'green', 'blue')
student_info: {'name': 'Bob', 'age': 25, 'is_student': True}
encoding_map: {'a': 97, 'b': 98}
Role in Programs
literals in Python are essential python program building pieces. Without previously assigning them to variables, they let you express particular values that are needed for calculations, text display, or constructing data structures directly. The correct data type for the value represented is determined by the Python interpreter, which comprehends these literal notations. For instance, the interpreter interprets print(“Hello world”) as a string literal and executes it appropriately. It interprets 25 as an integer literal when it reads subtotal=25.
To put it briefly, in Python are the straightforward way that fixed values are represented in your Literals in Python code. Programs work with concrete data, which is defined by them for a variety of data types, including as texts, numbers, and elements of aggregate kinds like lists, tuples, and dictionaries.