In Python, a literal đą is a notation representing a fixed value in the code. It is a constant value that doesn't change đ« during the execution đ» of the program. đ This lesson will provide đ a clear understanding đ§ of Python literals.
Python Literals
There was a software engineer named Alie who needed to learn Python. She chose to begin with the basics; one of the primary things she learned about was literal. đĄ Alie knew that in Python, literals are fixed values that represent constant data in Python code. They are utilized to assign values to variables, define constants, and perform different operations within the code. đ» She was presented to a few types of literals, such as numeric literals, string literals in python, boolean literals, None literal, and container literals đŠ. To better understand these literals in python, Alie decided to utilize them in a story. đ This lesson will examine diverse types of literals in python and their utilization.
Types of Literals
Alie knew Python supports several types of literals in python, including:
In Python, a numeric literal is a notation representing a fixed numeric value in the code. Python has three types of numeric literals: integer literals, floating-point literals, and complex literals.
1. Integer literals: Integer literals are used to represent whole numbers in Python. They can be positive, negative, or zero. Examples include:
Loading...
Example: Integer Literals
decimal_integer =Â 10
binary_integer =Â 0b1010
octal_integer =Â 0o12
hexadecimal_integer =Â 0xA
print(decimal_integer, binary_integer, octal_integer, hexadecimal_integer)
Output:
10Â 10Â 10Â 10
2. Floating-point literals: Floating-point literals are used to represent decimal numbers in Python. They can be positive, negative, or zero and written using scientific notation. Examples include:
Loading...
Example: Float Literals
float_number =Â 10.5
exponential_number =Â 1.5e2Â # Equivalent to 150.0
print(float_number, exponential_number)
Output:
10.5Â 150.0
3. Complex literals: Complex literals are used to represent complex numbers in Python. They consist of a real part and an imaginary part, which are both floating-point numbers. The imaginary part is denoted by the letter "j." Examples include:
Loading...
Numeric literals in python are used in mathematical operations, comparisons, and other numeric computations. She started using numeric literal to represent the ages of different characters in her story. For example, she wrote:
Loading...
Example: Complex Literals
complex_number =Â 3 +Â 4j
print(complex_number)
print("Real part:", complex_number.real)
print("Imaginary part:", complex_number.imag)
Output:
(3+4j)
Real part:Â 3.0
Imaginary part:Â 4.0
String literals in python represent strings of characters in Python. They are enclosed in single quotes, double quotes, or triple quotes. Examples include:
Loading...
Next, Alie used string literals to represent the characters' names in her story. She wrote:
Loading...
Example 1: Single-line String
single_line_string =Â 'Hello, Python!'
print(single_line_string)
Output:
Hello, Python!
Example 2: Multi-line String
multi_line_string =Â '''This is a
multi-line string in Python.'''
print(multi_line_string)
Output:
This is a
multi-line string in Python.
Boolean literals represent the truth values True and False in Python. Examples include:
Loading...
Alie also used boolean literals to represent whether the characters were friends. She wrote:
Loading...
Examples with Output:
is_python_fun =Â True
is_raining =Â False
print(is_python_fun, is_raining)
Output:
True False
The None literal represents a null value in Python. Examples include:
Loading...
Container literals represent data structures that can contain other literals in python.
Loading...
Finally, Alie used container literals to different group types of literals. For example, she used a literal list to represent the different items in Alie's backpack:
Loading...
These literals in python are used to represent constant values in Python programs, and they cannot be modified once they are defined.
Examples with Output:
Example 1: List Literal
my_list = [1, 2, 3, 4]
print(my_list)
Output:
[1, 2, 3, 4]
Example 2: Tuple Literal
my_tuple = (1, 2, 3, 4)
print(my_tuple)
Output:
(1, 2, 3, 4)
Example 3: Dictionary Literal
my_dict = {'name': 'Alice', 'age': 25}
print(my_dict)
Output:
{'name': 'Alice', 'age': 25}
Bytes literals: Represent immutable sequences of bytes, prefixed by b or B.
Example:
byte_literal =Â b'Hello'
print(byte_literal)
Output:
b'Hello'
Bytearray literals: Similar to bytes but mutable, prefixed by bytearray()****.
Example:
byte_array_literal = bytearray(b'Hello')
byte_array_literal[0] =Â 72Â # Replacing 'H' with ASCII value of 'H' (72)
print(byte_array_literal)
Output:
bytearray(b'Hello')
In conclusion, Python literals đąđ€đ represent constant values in Python code. They cannot be modified during the execution of the program. Understanding literals in python is crucial for writing efficient and readable code. Python supports different types of literals, such as numeric literals đą, string literals đĄ, boolean literals, None literal, and container literals đŠ.
Quiz
Answer: a) Fixed values that represent constant data in Python code.
Answer: b) 3
Answer: c) In single quotes, double quotes, or triple quotes
Answer: a) A null value
Answer: b) Tuple literals
Answer: d) Container literals
Top Tutorials
Related Articles