Skip to content

Types of literals in Python

  • by

In Python, literals are used to represent fixed values that are assigned to variables or used directly in the code. There are several types of literals in Python. Here are the common ones:

  1. Numeric Literals:
    • Integer literals: Whole numbers, e.g., 123, -45, 0.
    • Float literals: Numbers with decimal points, e.g., 3.14, -0.75, 1.0.
    • Complex literals: Numbers in the form a + bj, where a and b are numeric literals, e.g., 2+3j, -4-6j.
  2. String Literals:
    • Enclosed in single quotes (‘…’) or double quotes (“…”), e.g., ‘hello’, “world”.
  3. Boolean Literals:
    • Two possible values: True and False (Note that they should be capitalized).
  4. None Literal:
    • The special value None represents the absence of a value or a null value.
  5. Sequence Literals:
    • List literals: Enclosed in square brackets […], e.g., [1, 2, 3].
    • Tuple literals: Enclosed in parentheses (1, 2, 3).
    • Range literals: Generated using the range() function, e.g., range(5) creates [0, 1, 2, 3, 4].
    • String literals (already mentioned above).
  6. Mapping Literals:
    • Dictionary literals: Enclosed in curly braces {…}, e.g., {‘key’: ‘value’, ‘name’: ‘John’}.
  7. Set Literals:
    • Set literals: Enclosed in curly braces {…} but without key-value pairs, e.g., {1, 2, 3}.
  8. Bytes and Bytearray Literals:
    • Bytes literals: Enclosed in b’…’ format, e.g., b’hello’.
    • Bytearray literals: Enclosed in bytearray(…) format, e.g., bytearray(b’hello’).

Types of literals in Python example

Let’s provide some examples for each type of literal in Python:

Numeric Literals:

# Integer literals
my_integer = 42
negative_integer = -10

# Float literals
my_float = 3.14
negative_float = -0.75

# Complex literals
my_complex = 2 + 3j
another_complex = -4 - 6j

String Literals:

my_string = "Hello, World!"
another_string = 'Python is awesome!'

Boolean Literals:

is_true = True
is_false = False

None Literal:

empty_value = None

Sequence Literals:

# List literals
my_list = [1, 2, 3]
mixed_list = [1, "hello", 3.14, True]

# Tuple literals
my_tuple = (4, 5, 6)
mixed_tuple = (7, "world", 2.71, False)

Mapping Literals:

# Dictionary literals
my_dict = {'name': 'Alice', 'age': 30}
another_dict = {1: 'one', 2: 'two', 3: 'three'}

Set Literals:

my_set = {7, 8, 9}

Bytes and Bytearray Literals:

# Bytes literals
my_bytes = b'hello'
another_bytes = b'\x00\x01\x02'

# Bytearray literals
my_bytearray = bytearray(b'hello')
another_bytearray = bytearray([65, 66, 67])

Here’s an example code that includes different types of literals in Python:

# Numeric Literals
my_integer = 42
negative_integer = -10
my_float = 3.14
negative_float = -0.75
my_complex = 2 + 3j
another_complex = -4 - 6j

# String Literals
my_string = "Hello, World!"
another_string = 'Python is awesome!'

# Boolean Literals
is_true = True
is_false = False

# None Literal
empty_value = None

# List Literal
my_list = [1, 2, 3]
mixed_list = [1, "hello", 3.14, True]

# Tuple Literal
my_tuple = (4, 5, 6)
mixed_tuple = (7, "world", 2.71, False)

# Range Literal
my_range = range(5)  # Creates [0, 1, 2, 3, 4]

# Dictionary Literal
my_dict = {'name': 'Alice', 'age': 30}
another_dict = {1: 'one', 2: 'two', 3: 'three'}

# Set Literal
my_set = {7, 8, 9}

# Bytes and Bytearray Literals
my_bytes = b'hello'
another_bytes = b'\x00\x01\x02'
my_bytearray = bytearray(b'hello')
another_bytearray = bytearray([65, 66, 67])

# Printing the literals
print(my_integer)
print(negative_integer)
print(my_float)
print(negative_float)
print(my_complex)
print(another_complex)
print(my_string)
print(another_string)
print(is_true)
print(is_false)
print(empty_value)
print(my_list)
print(mixed_list)
print(my_tuple)
print(mixed_tuple)
print(list(my_range))
print(my_dict)
print(another_dict)
print(my_set)
print(my_bytes)
print(another_bytes)
print(my_bytearray)
print(another_bytearray)

Output:

Types of literals in Python

These examples demonstrate different types of literals in Python and how they are represented. Each type of literal serves specific purposes and allows Python programmers to work with various data types and structures effectively.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *