Numbers

In Python, there are different types of numbers.

Let’s say,

num = 45

Here, 45 is a number of type integer which means it has no decimal part. This type of number is known as int in Python. In Python, int represents whole numbers of unlimited length, positive or negative.

On the other hand,

price  = 25.99

Here, 25.99 is a floating point  number which can also be called a decimal number.

It contains an integer part as well as a decimal part separated by a decimal. This type of number is known as float in Python. In Python, float represents floating-point numbers with limited precision (around 15 decimal places)

Python also has support for complex numbers. Complex numbers are those numbers which can be represented in the form of x+yj, where j is the imaginary unit and x and y are real numbers. In Python, the imaginary unit can be represented by J or j.

comp = 5+4j

numval = 2+5J

Both these are valid complex numbers in Python.

Interview Questions

Q. What is the difference between an integer and a floating-point number in Python?

Ans.  In Python, an integer (int) is a whole number with no decimal part (e.g., 45). A floating-point number (float) is a number with both an integer and decimal part (e.g., 25.99). Integers have unlimited length, while floats have limited precision (about 15 decimal places).

Q.  What is a complex number in Python, and how is it represented?

Ans. A complex number in Python has both a real and imaginary part, taking the form x + yj, where x and y are real numbers, and j is the imaginary unit (√-1). Complex numbers are represented using either ‘j’ or ‘J’ after the imaginary part (e.g., 5 + 4j or 2 + 5J).

Q. If you wanted to store the number -123456789012345, which data type would you choose and why?

Ans. You would choose the int data type. While this is a large number, Python integers can be of any length, positive or negative, making it suitable for storing large whole numbers.