A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/literals-in-python/ below:

Literals in Python - GeeksforGeeks

Literals in Python

Last Updated : 15 Jul, 2025

Literals in Python are fixed values written directly in the code that represent constant data. They provide a way to store numbers, text, or other essential information that does not change during program execution. Python supports different types of literals, such as numeric literals, string literals, Boolean literals, and special values like None. For example:

Literals in Python

In this article, we'll explore various types of literals in Python with examples to demonstrate their usage:

Numeric Literals

Numeric literals represent numbers and are classified into three types:

Python
# Integer literals
a = 100
b = -50

# Floating-point literals
c = 3.14
d = -0.005

# Complex number literals
e = 4 + 7j
f = -3j

print(a, b, c, d, e, f)

Output
100 -50 3.14 -0.005 (4+7j) (-0-3j)
String Literals

String literals are sequences of characters enclosed in quotes. They are used to represent text in Python.

Types of String Literals: Python
# Different string literals
a = 'Hello'      # Single-quoted
b = "Python"     # Double-quoted
c = '''This is 
a multi-line string'''  # Triple-quoted
d = r"C:\Users\Python"  # Raw string

print(a)
print(b)
print(c)
print(d)

Output
Hello
Python
This is 
a multi-line string
C:\Users\Python
Boolean Literals

Boolean literals represent truth values in Python. They help in decision-making and logical operations. Boolean literals are useful for controlling program flow in conditional statements like if, while, and for loops.
Types of Boolean Literals:

Python
# Boolean literals
a = True
b = False

print(a, b)       # Output: True False
print(1 == True)  # Output: True
print(0 == False) # Output: True
print(True + 5)   # Output: 6 (1 + 5)
print(False + 7)  # Output: 7 (0 + 7)

Explanation:

Collection Literals

Python provides four different types of literal collections:

Python
Rank = ["First", "Second", "Third"]  # List
colors = ("Red", "Blue", "Green")  # Tuple
Class = { "Jai": 10, "Anaya": 12 }  # Dictionary
unique_num = {1, 2, 3}  # Set

print(Rank, colors, Class, unique_num)

Output
['First', 'Second', 'Third'] ('Red', 'Blue', 'Green') {'Jai': 10, 'Anaya': 12} {1, 2, 3}

Explanation:

Special Literal

Python contains one special literal (None). 'None' is used to define a null variable. If 'None' is compared with anything else other than a 'None', it will return false.

Python

Explanation: None represents "nothing" or "empty value."

Difference between Literals

Type of Literals

Description

Example

Mutable/Immutable

Integer literals

Whole numbers (without decimals).

a = 77

Immutable

Float literals

Numbers with a decimal point.

b=3.144

Immutable

Complex literals

Numbers with real and imaginary part.

c = 7 + 5j

Immutable

String literals

Stores text. Can use single, double, or triple quotes.

greeting = "Bonjour"
story = """Once upon a time..."""

Immutable

Boolean literals

Represents True (1) or False (0).

a = (1 == True) → True
b = (1 == False) → False

Immutable

Boolean as Number

Boolean values can act as numbers (1 or 0).

c = True + 3 → 4
d = False + 7 → 7

Immutable

List literal

Stores multiple values; can be changed.

Rank = ["First", "Second", "Third"]

Mutable

Tuple literal

Like a list but cannot be changed.

colors = ("Red", "Blue", "Green")

Immutable

Dictionary literal

Stores key-value pairs.

Class = { "Jai": 10, "Anaya": 12 }

Mutable

Set literal

Stores unique values, unordered.

unique_num = {1, 2, 3}

Mutable

Special literal

Represents "nothing" or "empty."

water_remain = None

Immutable



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4