python __base___Python numpy.base_repr()用法及代码示例

numpy.base_repr(number,base = 2,padding = 0)函数用于返回给定基本系统中数字的字符串表示形式。

例如,十进制数字10以二进制形式表示为1010,而以八进制形式表示为12。

用法: numpy.base_repr(number, base=2, padding=0)

参数:

number :输入号码。只能使用整数十进制数作为输入。

base :[int,可选]将数字转换为基数系统。有效范围是2-36,默认值是2。

padding :[int,可选]在左侧添加零个数。默认值为0。

Return :基本系统中输入数字的字符串表示形式。

代码1:工作

# Python program explaining

# base_repr() function

import numpy as geek

in_num = 10

print ("Input  number:", in_num)

out_num = geek.base_repr(in_num, base = 2, padding = 0)

print ("binary representation of 10:", out_num)

输出:

Input number: 10

binary representation of 10: 1010

代码2:

# Python program explaining

# base_repr() function

import numpy as geek

in_arr = [5, -8, 21 ]

print ("Input array:", in_arr)

print()

# binary representation of first array

# element without using padding parameter

out_num = geek.base_repr(in_arr[0], base = 2)

print("binary representation of 5")

print ("Without using padding parameter:", out_num)

# binary representation of first array

# element using padding parameter

out_num = geek.base_repr(in_arr[0], base = 2, padding = 3)

print ("Using padding parameter:", out_num)

print()

# octal representation of 2nd array

# element without using width parameter

out_num = geek.base_repr(in_arr[1], base = 8, padding = 0)

print("octal representation of -8")

print ("Without using padding parameter:", out_num)

# octal representation of 2nd array

# element  using padding parameter

out_num = geek.base_repr(in_arr[1], base = 8, padding = 4)

print ("Using padding parameter:", out_num)

print()

# hexa-decimal representation of 3rd array

# element without using padding parameter

out_num = geek.base_repr(in_arr[2], base = 16, padding = 0)

print("hexa-decimal representation of 21")

print ("Without using padding parameter:", out_num)

# hexa-decimal representation of 3rd array

# element  using padding parameter

out_num = geek.base_repr(in_arr[2], base = 16, padding = 3)

print ("Using padding parameter:", out_num)

输出:

Input array: [5, -8, 21]

binary representation of 5

Without using padding parameter: 101

Using padding parameter: 000101

octal representation of -8

Without using padding parameter: -10

Using padding parameter: -000010

hexa-decimal representation of 21

Without using padding parameter: 15

Using padding parameter: 00015

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值