Python literals are the fixed values assigned to variables or constants. They represent the raw data in a Python program and are used directly in the code. Python supports several types of literals, each serving different purposes.
Here’s an overview of the different types of Python literals:
1. Numeric Literals
Numeric literals are used to represent numbers. They can be of several types: integers, floating-point numbers, or complex numbers.
- Integer Literals (
int
):- Integers can be in decimal (base 10), binary (base 2), octal (base 8), or hexadecimal (base 16) forms.
- Examples:
decimal_literal = 10 # Decimal
binary_literal = 0b1010 # Binary (prefix '0b' or '0B')
octal_literal = 0o12 # Octal (prefix '0o' or '0O')
hex_literal = 0xA # Hexadecimal (prefix '0x' or '0X')
Floating-Point Literals (
float
):- Used to represent real numbers with a fractional part.
- They can also be written in scientific notation.
- Examples:
float_literal = 10.5 # Floating-point number
float_scientific = 1.5e2 # Scientific notation (1.5 * 10^2)
Complex Literals (
complex
):- Used to represent complex numbers with a real and an imaginary part.
- The imaginary part is denoted by
j
. - Examples:
complex_literal = 3 + 4j
2. String Literals
String literals are sequences of characters enclosed in quotes. Python supports single, double, triple single, and triple double quotes for strings.
- Single-Line Strings:
- Can be enclosed in single or double quotes.
- Examples:
single_quote_string = 'Hello'
double_quote_string = "Hello"
- Multi-Line Strings:
- Can be enclosed in triple quotes (either single or double).
- Used for strings that span multiple lines.
- Example:
multi_line_string = '''This is a
multi-line string'''
- String with Special Characters:
- Python allows the use of escape sequences to include special characters in strings.
- Examples:
string_with_newline = "Hello\nWorld"
string_with_tab = "Hello\tWorld"
3. Boolean Literals
Boolean literals represent one of two values:
True
orFalse
. These literals are used in conditional statements and logical operations.- Examples:
is_true = True
is_false = False
4. Special Literal (
None
)Python has a special literal called
None
, which represents the absence of a value or a null value. It is commonly used to signify that a variable has no value assigned to it.- Example:
result = None
5. Literal Collections
Python allows the use of literals for collections like lists, tuples, dictionaries, and sets.
- List Literals:
- A list is an ordered collection of items, defined using square brackets
[]
. - Examples:
list_literal = [1, 2, 3, 4, 5]
mixed_list = [1, "two", 3.0, True]
- A list is an ordered collection of items, defined using square brackets
- Tuple Literals:
- A tuple is an ordered, immutable collection of items, defined using parentheses
()
. - Examples:
tuple_literal = (1, 2, 3, 4, 5)
mixed_tuple = (1, "two", 3.0, True)
- A tuple is an ordered, immutable collection of items, defined using parentheses
- Dictionary Literals:
- A dictionary is an unordered collection of key-value pairs, defined using curly braces
{}
. - Examples:
dict_literal = {"name": "Alice", "age": 25}
- A dictionary is an unordered collection of key-value pairs, defined using curly braces
- Set Literals:
- A set is an unordered collection of unique items, defined using curly braces
{}
. - Example:
set_literal = {1, 2, 3, 4, 5}
6. Literal Constants
Python has some constant literals like
True
,False
, andNone
.True
andFalse
:-
- These are the Boolean literals in Python, representing truth values.
- Example:
is_valid = True
-
None
:-
- Represents the absence of a value or a null value.
- Example:
empty_value = None
7. Special Literals for Data Representation
Python also has special literals to represent different types of data:
- Ellipsis Literal (
...
):-
- The ellipsis literal (
...
) is used to represent an unfinished or incomplete code or as a placeholder. - It is often used in function definitions or when working with slices.
- Example:
- The ellipsis literal (
def incomplete_function():
...
Summary of Python Literals
Literal Type Description Example Integer Literals Whole numbers, in decimal, binary, octal, or hexadecimal. 10
,0b1010
,0o12
,0xA
Floating-Point Literals Numbers with a fractional part, or in scientific notation. 10.5
,1.5e2
Complex Literals Numbers with a real and imaginary part. 3 + 4j
String Literals Sequence of characters in single, double, or triple quotes. 'Hello'
,"World"
,'''Text'''
Boolean Literals Truth values represented by True
orFalse
.True
,False
Special Literal ( None
)Represents the absence of a value. None
List Literals Ordered, mutable collection of items in square brackets. [1, 2, 3]
,[1, "two", 3.0]
Tuple Literals Ordered, immutable collection of items in parentheses. (1, 2, 3)
,(1, "two", 3.0)
Dictionary Literals Unordered collection of key-value pairs in curly braces. {"name": "Alice", "age": 25}
Set Literals Unordered collection of unique items in curly braces. {1, 2, 3, 4, 5}
Ellipsis Literal ( ...
)Placeholder for incomplete code or slices. ...
Python literals are essential for creating and manipulating different types of data within a program. They allow you to define various types of values directly in your code, making it easier to perform operations, store data, and pass information between different parts of your program.
-
-
- A set is an unordered collection of unique items, defined using curly braces