Bytes

Literals in Python - What are Python Literals?

Last Updated: 20th October, 2024

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.

What are Literals in Python?

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 in Python

Types of Literals

Types of Literals

Alie knew Python supports several types of literals in python, including:

  1. Numeric Literals
  2. String Literals
  3. Boolean Literals
  4. None Literal
  5. Container Literals
  6. Bytes and Bytearray Literals

Literals in Python (Examples with Output)

1. Numeric Literals in Python

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

2. String Literals in Python

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.

3. Boolean Literals 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

4. None Literal in Python

The None literal represents a null value in Python. Examples include:

Loading...

5. Container Literals In Python

Container literals represent data structures that can contain other literals in python.

  1. List Literals: List literals are used to represent a list of values. They are enclosed in square brackets and separated by commas.
  2. Tuple Literals: Tuple literals are used to represent a sequence of values. They are enclosed in parentheses and separated by commas.
  3. Dictionary Literals: Dictionary literals are used to represent key-value pairs. They are enclosed in curly braces and separated by commas.
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}

6. Bytes and Bytearray Literals

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')

Conclusion

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 📩.

Key Takeaways on Literals in Python

  • Literals in python are fixed values that represent constant data in Python code. They are utilized to relegate values to variables, define constants, and perform different operations within the code. đŸ’»
  • Python has a few types of literals, including numeric literals, string literals, boolean literals, None literal, and container literals. 📩
  • Numeric literals incorporate integer, floating-point, and complex literals, which are utilized in numerical operations, comparisons, and other numeric computations. 🔱
  • String literals in python represent strings of characters in Python and are encased in single quotes, double quotes, or triple quotes. đŸ”€
  • Boolean literals represent the truth values Genuine and Wrong in Python. ✔❌
  • The None literal represents a null value in Python. đŸ…Ÿïž
  • Container literals represent data structures that can contain other literals, counting list literals, tuple literals, and dictionary literals. 📩
  • Literals in python can be utilized to gather diverse literals and represent complex data structures.đŸ€

Quiz

  1. What do you mean by python literals? 
    1. Fixed values that represent constant data in Python code. 
    2. Variable values that can be changed during the execution of the program.  
    3. Values that are used to perform mathematical operations in Python. 
    4. Values that are used to perform logical operations in Python.

Answer: a) Fixed values that represent constant data in Python code.

  1. How many types of numeric literals are there in Python? 
    1. 2 
    2. 3 
    3. 4 
    4. 5

Answer: b) 3

  1. How are string literals enclosed in Python?
    1. In square brackets
    2. In parentheses
    3. In single quotes, double quotes, or triple quotes
    4. In curly braces

Answer: c) In single quotes, double quotes, or triple quotes

  1. What does the None literal represent in Python?
    1. A null value
    2. An empty value
    3. A false value
    4. A true value

Answer: a) A null value

  1. Which type of literal is used to represent a sequence of values in Python?
    1. List literals
    2. Tuple literals
    3. Dictionary literals
    4. Container literals

Answer: b) Tuple literals

  1. Which type of literal is used to represent a data structure that can contain other literals in Python? 
    1. List literals 
    2. Tuple literals 
    3. Dictionary literals 
    4. Container literals

Answer: d) Container literals

Module 2: Basics of Python ProgrammingLiterals in Python - What are Python Literals?

Top Tutorials

Related Articles

  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter