Articles

How To Convert Complex Numbers to Integer in Python

by Princeton IT Services Python Development

If you hire professional Python development services in new York, you are offered the flexibility of performing Python functions with ease.  Here are a few facts that might help you with the conversion. 

  • Numbers are used to store numeric values in the program. 

  • Python is designed to support three types of numbers - int, float and complex. 

  • Using  built-in functions to create numbers - int(), float() and complex(). 

  • Can create a number by directly assigning the value to a variable. 

  • In geometry we mostly used complex numbers, calculus and scientific calculations. 

  • _int_(), _float_() and_complex_() with implementation of this formula we can represent the numeric object.

How to create a number variable in Python

  1. x = 10

  2. y = 10.55

  3. z = 1 + 2j

Two parts of the complex number – real and imaginary.  The imaginary part is denoted by the “j” suffix.

How to find the type of a number? 

type() function using 3 types of number 

  1. print(type(x))

  2. print(type(y))

  3. print(type(z))

Integer 

Integers are whole numbers.  They can be positive or negative.  They must be without decimal values.  Use the int() function to get the integer representation of an object.  The object must implement the _int_() method that returns an integer. 

Let’s see some examples of how python creates integers.

  1. x = 10

  2. print(type(x))


  1. x = int("10")

  2. print(type(x))



  1. Class Data:

  2. id = 0


  1. def__init__(self, i):

  2. self.id = i


  1. def__int__(self):

  2. return self.id



  1. d = Data(10)


  1. x = int(d)

  2. print(x)

  3. print(type(x))

  4. Output:


  1. <class 'int'>

  2. <class 'int'>

  3. 10

  4. <class 'int'>

String class provides __int__() method, making it easy to convert a string to int using the int() method.


If the object doesn’t implement the __int__() method, the int() function throws a TypeError.



Sponsor Ads


About Princeton IT Services Freshman   Python Development

3 connections, 0 recommendations, 20 honor points.
Joined APSense since, August 5th, 2020, From Nwe York, United States.

Created on Sep 8th 2020 09:49. Viewed 135 times.

Comments

No comment, be the first to comment.
Please sign in before you comment.