w3resource

Hexadecimal values of list elements using memory views in Python

Python Memory Views Data Type: Exercise-10 with Solution

Write a Python program that creates a memory view from a list of integers and print the hex values of each element.

Sample Solution:

Code:

def test(memory_view):
    for element in memory_view:
        print(hex(element))

def main():
    nums = [8, 16, 42, 92, 128]
    print("Original list values:",nums)

    # Create a memory view from the list of integers
    memory_view = memoryview(bytearray(nums))

    print("Hex Values of said list elements:")
    test(memory_view)

if __name__ == "__main__":
    main()

Output:

Original list values: [8, 16, 42, 92, 128]
Hex Values of said list elements:
0x8
0x10
0x2a
0x5c
0x80

Flowchart:

Flowchart: Hexadecimal values of list elements using memory views in Python.

Previous: Slicing memory views in Python: Indexing syntax and example.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/extended-data-types/python-extended-data-types-index-memory-views-exercise-10.php