numpy.base_repr() in Python

numpy.base_repr(number, base=2, padding=0) function is used to return a string representation of a number in the given base system.

For example, decimal number 10 is represented as 1010 in binary whereas it is represented as 12 in octal.

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

Parameters :
number : Input number. Only an integer decimal number can be used as input.
base : [int, optional] Convert number to the base number system. The valid range is 2-36, the default value is 2.
padding : [int, optional] To add number of zeroes on the left. Default is 0.

Return : String representation of the input number in base system.



Code #1 : Working

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

Input  number :  10
binary representation of 10 :  1010

 
Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.