python 无符号整数
An array can be declared by using "array" module in Python.
可以通过在Python中使用“数组”模块来声明数组 。
Syntax to import "array" module:
导入“数组”模块的语法:
import array as array_alias_name
Here, import is the command to import Module, "array" is the name of the module and "array_alias_name" is an alias to "array" that can be used in the program instead of module name "array".
在这里, import是导入Module的命令, “ array”是模块的名称, “ array_alias_name”是“ array”的别名,可以在程序中使用它而不是模块名称“ array” 。
Array declaration:
数组声明:
To declare an "array" in Python, we can follow following syntax:
要在Python中声明“数组” ,我们可以遵循以下语法:
array_name = array_alias_name.array(type_code, elements)
Here,
这里,
array_name is the name of the array.
array_name是数组的名称。
array_alias_name is the name of an alias - which we define importing the "array module".
array_alias_name是别名的名称-我们定义了导入“ array module”的名称 。
type_code is the single character value – which defines the type of the array elements is the list of the elements of given type_code.
type_code是单个字符值–定义数组元素的类型是给定type_code的元素列表。
声明有符号和无符号整数数组 (Declaring Signed and Unsigned Integer Array)
Signed Integer is defined by using type_code "i" (small alphabet "i") and it contains negative and posited integers.
通过使用type_code “ i” (小写字母“ i” )定义带符号整数 ,它包含负整数和正整数。
Unsigned Integer is defined by using type_code "I" (Capital alphabet "I") and it contains only positive integers.
通过使用type_code “ I” (大写字母“ I” )定义无符号整数,并且它仅包含正整数。
Examples to declare and initialize "unsigned" and "signed" integer array in Python:
在Python中声明和初始化“ unsigned”和“ signed”整数数组的示例:
# unsigned integer array
a = arr.array ("I", [10, 20, 30, 40, 50])
# signed integer array
b= arr.array ("i", [10, -20, 30, -40, 50])
Program:
程序:
# importing array class to use array
import array as arr
# an unsigned int type of array
# declare and assign elements
a = arr.array ("I", [10, 20, 30, 40, 50] )
# print type of a
print ("Type of a: ", type (a))
# print array
print ("Array a is: ")
print (a)
# a signed int type of array
# declare and assign elements
b = arr.array ("i", [10, -20, 30, -40, 50] )
# print type of a
print ("Type of b: ", type (a))
# print array
print ("Array b is: ")
print (b)
Output
输出量
Type of a: <class 'array.array'>
Array a is:
array('I', [10, 20, 30, 40, 50])
Type of b: <class 'array.array'>
Array b is:
array('i', [10, -20, 30, -40, 50])
翻译自: https://www.includehelp.com/python/signed-and-unsigned-integer-arrays-in-python.aspx
python 无符号整数