Basic Data Types in Python – Numeric Types Numeric Types In Python, numeric types include integers, floating-point numbers, and complex numbers. 1. Integer: Whole numbers without a fractional part. 2. Float: Numbers with a decimal point. 3. Complex: Numbers with a real and imaginary part.

Basic Data Types in Python: Numeric Type

Learning Python

2024-10-16 00:51:23


In Python, numeric types are one of the most basic data types, dealing with various types of numbers that can perform mathematical operations. Numeric types include integers, floating-point numbers, and complex numbers. In this article, we will explain the main numeric data types in Python and their characteristics.

1. Integer Type (int)

**Integer (int)** refers to a number without a decimal point. In Python, very large integers can be used without limits, and unlike other programming languages, there is no overflow problem.

  • Example: In the code above, a and b are integer variables, and a simple addition operation can be performed.
  • a = 10 b = -5 print(a + b) # 5

2. Floating-Point Type (float)

**Floating-Point (float)** refers to a number with a decimal point. Floating-point numbers are stored in a floating-point format, and in Python, they are commonly used when dealing with numbers that include a decimal.

  • Example: In the code above, pi and radius are floating-point numbers and are used to calculate the area of a circle.
  • pi = 3.14 radius = 5.0 area = pi * (radius ** 2) print(area) # 78.5

3. Complex Type (complex)

**Complex (complex)** refers to a number composed of a real part and an imaginary part. Python supports complex numbers, with the imaginary part represented using j.

  • Example: In the code above, z is a complex number, and you can check the real and imaginary parts using the real and imag attributes.
  • z = 3 + 4j print(z.real) # 3.0 print(z.imag) # 4.0

4. Numeric Operations

In Python, you can perform operations on numeric data using various operators.

  • Arithmetic Operations: Addition (+), Subtraction (), Multiplication (*), Division (/)
  • Exponentiation: You can calculate powers using **.
  • print(2 ** 3) # 8
  • Modulus Operation: You can find the remainder using %.
  • print(10 % 3) # 1
  • Integer Division: You can find the quotient of division using //.
  • print(10 // 3) # 3

5. Type Conversion

In Python, type conversion between numeric types is possible. You can convert to different numeric types using the int(), float(), complex() functions.

  • Example:
  • a = 5 # Integer b = float(a) # Convert to float c = complex(b) # Convert to complex print(b) # 5.0 print(c) # (5+0j)

Conclusion

The numeric data types in Python are divided into integers, floating-point numbers, and complex numbers, each of which is used for various mathematical operations. Numeric types play a very important role in Python programming, allowing for complex calculations to be performed easily. By mastering the various features of numeric types and how to convert between them, you can solve mathematical problems more effectively.