What are type codes in Python?

Arrays in python behave very similar to lists but they have the same data type of values stored in it. The data type is specified at the array creation time by using a single character is called the type code.

Below is the table for type codes, used for defining arrays in python, and their corresponding C++ data type.

Type Code C++ Type Python Type Minimum Sizes in Bytes
‘c’ char character 1
‘b’ signed char int 1
‘B’ unsigned char int 1
‘u’ Py_UNICODE unicode character 2
‘h’ signed short int 2
‘H’ unsigned short int 2
‘i’ signed int int 2
‘I’ unsigned int long 2
‘l’ signed long int 4
‘L’ unsigned long long 4
‘f’ float float 4
‘d’ double float 8

Below is an example of an array, of type char in C++ and character in python.

import array as arr #importing array module
a = arr.array('c',['a','e','i','o','u']) #defining array of type char and size 5
print(a) # printing value at 2nd index

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved