Min menu

Pages

Conversion functions of number types in Python

Conversion functions of number types in Python 

  Functionint(x)

its definition

This function is already available in Python, and it converts the value we pass into the place of the parameter xto an integer( int)And you return it.


built

                  class int(x=0)
	  

parameters

xIt can be an object of any type provided that it represents a numeric value.
Note: A value can be xa number or text representing an integer provided that it does not contain any symbol or a blank space, for example: '20'.


Return value

The value we pass returns the place of the parameter xas an integer( int).


Example

Test.py
                    x = 5.5 # ( float ) its value is a decimal number x Here we have defined a variable named
		  y = int(x) # y Then we put it in a new variable named ( int ie ) to an integer value x here we converted the value of the variable
	  
		  print('x =', x) # x Here we have shown the value of the variable
		  print('y =', y) # y Here we have shown the value of the variable
	

We will get the following result when running.

x = 5.5
y = 5

  conversion function int(x, base)

its definition

This function is already available in Python, and it converts the string value we pass into the place of the parameter xto an integer( int)Based on the value of the variable baseand returns it.


Technical information

By default, any number we define is based on .Base 10Even if we don't define it ourselves.
This is why you notice that the parameter value baseis 10 by default unless you specify it.


built

class int(x, base=10)
	  

parameters

  • xA string representing the integer whose value will be converted and returned.

  • baseAn integer representing the type of unit that will be used when converting the value of the variable x.

    So the number that we put in the place of the basedetermines the type of value of the variable x, which can bebinary-octal-decimal-hexadecimal.
    The basecan be 2 - 8 - 10 - 16 in the order as above.


Return value

The value we pass returns the place of the parameter xas an integer( int).


Example

Test.py
x = '101' # ( string ) its value is text x Here we have defined a variable named
		  y = int(x, 2) # y is based on unit 2. Then we put it in a new variable named x and we specify that the (int ie) is an integer value x here we convert the value of the variable
	  
		  print('x =', x) # x Here we have shown the value of the variable
		  print('y =', y) # y Here we have shown the value of the variable
    

We will get the following result when running.

x = 101
y = 5

  conversion function float(x)

its definition

This function is already available in Python, and it converts the value we pass into the place of the parameter xto a decimal number( float)And you return it.


built

class float([x])
	  

parameters

xIt can be an object of any type provided that it represents a numeric value.
Note: A value can be xa number or a text representing a number provided that it does not contain any symbol or a blank space, for example: '5.2'.


Return value

Returns the value we pass in the place of the parameter as a xdecimal( float).


Example

Test.py
x = '10.5' # ( float ) is a float ( string ) its value is text x here we have defined a variable named
		  y = float(x) # y then we put it in a new variable named ( float ie ) to a decimal value x here we converted the value of the variable
	  
		  print('x =', x) # x Here we have shown the value of the variable
		  print('y =', y) # y Here we have shown the value of the variable
    

We will get the following result when running.

x = 10.5
y = 10.5

  conversion function complex(real, imag)

its definition

This function is already available in Python, and it returns the value of the variable realand the variable imagas a complex number(Complex Number).
A complex number by its nature contains two values:
The first is the value of the variable as the realreal value(Real Part).
The second is the value of the variable imagas an imaginary value(Imaginary Part).

Note: If you pass a single value to it, here you are actually entering the real value of the complex number, and the dummy value is 0 .


built

                  class complex([real[, imag]])
	  

parameters

  • realA number representing an integer or a decimal point.

  • imagA number representing an integer or a decimal point.

Note: Instead of setting two separate values, you can pass text representing the value of the variable realand the variable imagprovided that it does not contain any symbol or empty space.
Example: complex('1+2j').


Return value

Returns the value of the variable realand the variable imagas a complex number(Complex Number).


Example

Test.py
                    x = complex(1, 2) # x and we put the complex number that you returned based on these two numbers in the variable imag and 2 in the place of the real parameter and give it 1 in the place of the complex parameter here we called the function
	  
		  print('x =', x) # which represents a complex number of x here we have shown the value of the variable
	

We will get the following result when running.

x = (1+2j)