python
  1. python-literals

Python Literals

In Python, literals are the data values that are represented in a fixed form in the source code. The literal values can be of different data types such as numeric (integer, float, complex), string, boolean, and none.

Syntax

# Numeric literals
12          # Integer literal
3.14        # Float literal
3+4j        # Complex literal

# String literals
'Hello'     # Single quoted string literal
"World"     # Double quoted string literal
"""This is a multiline 
string literal"""  # Multiline string literal

# Boolean literals
True        # Boolean literal for true
False       # Boolean literal for false

# None literal
None        # None literal

Example

# Numeric literals
x = 7       # Integer literal
y = 3.14    # Float literal
z = 3 + 4j  # Complex literal

# String literals
str1 = 'Hello'              # Single quoted string literal
str2 = "World"              # Double quoted string literal
str3 = """This is a multiline 
string literal"""           # Multiline string literal

# Boolean literals
bool_true = True            # Boolean literal for true
bool_false = False          # Boolean literal for false

# None literal
val = None                  # None literal

print(x)
print(y)
print(z)
print(str1)
print(str2)
print(str3)
print(bool_true)
print(bool_false)
print(val)

Output

7
3.14
(3+4j)
Hello
World
This is a multiline 
string literal
True
False
None

Explanation

  • Numeric literals are used to represent numeric data types such as integers, floating-point numbers, and complex numbers. Integer literals are represented by an integer value, float literals are represented by decimal numbers or exponential notations, and complex literals are represented by a real number followed by an imaginary component.

  • String literals represent a sequence of characters and are enclosed within single or double-quotes. Python also supports triple quotes for a multiline string literal.

  • Boolean literals represent a Boolean value of True or False.

  • None is a special literal in Python that represents no value or null.

Use

Literals are used to represent various types of data in Python. They can be used to assign values to variables or as input parameters for functions and methods.

Important Points

  • Numeric literals can be represented as integers, floats, and complex numbers.
  • String literals can be enclosed with single or double-quotes.
  • Python supports triple quotes for multiline string literals.
  • Boolean literals are True and False.
  • None is a special literal that represents null or no value.

Summary

In Python, literals are the values that are represented in a fixed form in the source code. They can be of different data types such as numeric, string, boolean, and none. Numeric literals can be represented as integers, floats, and complex numbers. String literals are enclosed with single or double-quotes, and Python supports triple quotes for multiline string literals. Boolean literals are True and False, while None is a special literal that represents null or no value.

Published on: