Python Number

In this tutorial, you will learn about Python numbers. Python has a number datatype that has numeric values. Python supports various types of numbers:

1. Integers (int)

Python has an Integer data type, which has whole numbers, positive or negative numbers.

Note: The Number should not have any fractional part.

Example


x = 20  # Integer
y = -10  # Negative integer

2. Floating-point numbers (float)

Floating-point numbers mean numbers have decimal points or numbers have exponential notation.

Example:


x = 20.5  # Floating-point number
y = -5.7  # Negative float
z = 3.5e2  # 3.5 * 10^2 (Exponential notation, equivalent to 350.0)

3. Complex numbers (complex)

These numbers are written in the form a + bj, where a is the real part, and b is the imaginary part.

Example:


x = 3 + 4j  # Complex number
y = 2j  # Pure imaginary number

Type conversion between numbers

You can convert between different number types using the int(), float(), and complex() functions.

Example:


x = 5      # Integer
y = float(x)  # Converts x to 5.0 (float)

z = 3.6
w = int(z)  # Converts z to 3 (integer, truncating the decimal part)

c = complex(x, y)  # Converts to complex number (5+5.0j)

Python Number – Interview Questions

Q 1: What are numbers in Python?

Ans: Numbers are numeric data types used to store numerical values.

Q 2: Name numeric types in Python.

Ans: int, float, and complex.

Q 3: What is a complex number in Python?

Ans: A number with real and imaginary parts, written using j.

Q 4: How do you perform arithmetic operations?

Ans: Using operators like +, -, *, /.

Q 5: Are Python numbers immutable?

Ans: Yes, numeric values in Python cannot be changed after creation.

Python Number – Objective Questions (MCQs)

Q1. Which of the following is not a numeric data type in Python?






Q2. What will be the output of the following code?

x = 5
y = 2
print(x / y)






Q3. Which function is used to convert a float number into an integer in Python?






Q4. What will be the output of the following code?

x = 5 + 3j
print(type(x))






Q5. Which of the following statements about Python numbers is true?






Related Python Number Topics