Bin () function in Python
In Python programming, the bin () function converts and returns the binary string of the given integer. So what is the syntax of bin () function, parameters and how to use bin ()? Let's find out together.
Python () function syntax (Python):
bin(num)
Parameters of bin () function:
The bin () function has only one parameter, num, ie the integer that takes the binary string. If the parameter is not an integer, it must execute the __index __ () method to return an integer.
What value does bin () return?
The bin () function returns a corresponding binary string of a given integer. Without specifying an integer, TypeError will be generated, highlighting the data type that is not understood as an integer.
Example of bin function ()
# Ví dụ 1: Lấy chuỗi nhị phân của số nguyên
so = 10
print('Chuỗi nhị phân của ', so, 'là: ', bin(so))
# Ví dụ 2: Dùng __index__() để chuyển đối tượng So_luong sang nhị phân
class So_luong:
hoa_hong = 5
hoa_dao = 10
hoa_sen = 15
def __index__(sl):
return sl.hoa_hong + sl.hoa_dao + sl.hoa_sen
print('Chuỗi nhị phân của So_luong là: ', bin(So_luong()))
After running the above program, we get the output as:
Chuỗi nhị phân của 10 là: 0b1010
Chuỗi nhị phân của So_luong là: 0b11110
From now on, whenever you need to get the binary value of a number or object, you just need to use the bin () function to finish.
See more:
- Built-in Python functions
- More than 100 Python exercises have solutions (sample code)
- The loop technique in Python
You should read it
May be interested
- The function set () in Pythonin this article, tipsmake.com will learn with you about set (), syntax, parameters and specific examples. invites you to read the track.
- Help () function in Pythonthe built-in help () function in python is used to display documents and invoke the help system of modules, functions, classes, keywords ...
- Sum () function in Pythonthe built-in function sum () in python returns the sum of all numbers in iterable.
- The slice () function in Pythonthe slice () function in python returns a slice object that helps you determine how to cut an existing string.
- The chr () function in Pythonin python, the chr () function returns a character (a string) from an integer that represents the unicode of the returned character.
- The iter () function in Pythoncontinuing with the topic of python's built-in functions, the article will introduce you to the iter () function with the syntax, usage as well as specific examples. invites you to read the track.
- The pow () function in Pythonthe pow () function built into python returns the value of x with the power of y (xy). if there is a third parameter, the function returns x to the power y, the module z.
- The reversed () function in Pythonthe reversed () function is one of the built-in functions in python, used to reverse the original string returning the iterator.
- Exec () function in Pythonthe exec () function used to execute python programs dynamically can be string or object code. how does exec () function syntax, what parameters do it have, and how is it used? invites you to read the track.
- Built-in Python functionsin the previous article, you learned that python has a basic function type, one is a built-in function, two are user-defined functions. today, we will learn about a list of python's built-in functions.