×
   ❮   
PYTHON FOR DJANGO DJANGO FOR BEGINNERS DJANGO SPECIFICS PAYMENT INTEGRATION Roadmap
     ❯   

VARIABLES & TYPES IN PYTHON

Numeric type

Understanding Python Numeric Types

Introduction

Python provides several built-in numeric types to handle different kinds of numbers. These numeric types are essential in performing calculations, data analysis, and various mathematical operations. The three main numeric types in Python are int (integer), float (floating point), and complex (complex number). Each type serves a distinct purpose and understanding how they work is crucial for effective programming.

Numeric Types in Python

1. int (Integer)

The int type in Python represents whole numbers without a decimal point. These numbers can be positive, negative, or zero. Integers are commonly used when you need to count items, perform indexing, or work with data that doesn't require fractional values.

Example:

a = 10
b = -5
c = 0

In this example, a, b, and c are all integers. The variable a holds a positive integer, b holds a negative integer, and c holds zero.

2. float (Floating Point)

The float type is used to represent numbers that have a decimal point. These numbers are essential when working with measurements, calculations that require precision, or when dealing with continuous data.

Example:

pi = 3.14
gravity = 9.81
temperature = -5.2

Here, pi, gravity, and temperature are all floating-point numbers. Each of these variables holds a number that includes a fractional part.

3. complex (Complex Number)

The complex type in Python is used to represent numbers that have both a real part and an imaginary part. Complex numbers are particularly useful in scientific computing, signal processing, and certain areas of mathematics.

Example:

z1 = 3 + 4j
z2 = 1 - 2j

In this example, z1 and z2 are complex numbers. The variable z1 represents the complex number 3 + 4j, where 3 is the real part and 4j is the imaginary part. Similarly, z2 represents 1 - 2j.

Common Operations on Numeric Types

Python supports a variety of operations that can be performed on numeric types. These include arithmetic operations and type conversions.

1. Arithmetic Operations

Python allows you to perform standard arithmetic operations on numeric types. These operations include addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (**).

Example:

# Addition
sum_result = 10 + 5  # 15

# Subtraction
diff_result = 10 - 5  # 5

# Multiplication
product_result = 10 * 5  # 50

# Division
quotient_result = 10 / 2  # 5.0

# Exponentiation
power_result = 2 ** 3  # 8

In this example, various arithmetic operations are performed on integers. The results of these operations are stored in variables such as sum_result, diff_result, product_result, quotient_result, and power_result.

2. Converting Between Numeric Types

Python provides built-in functions to convert between different numeric types. This is useful when you need to change a number from one type to another, such as converting an integer to a float or a float to a complex number.

Conversion Functions:

  • int(): Converts a number to an integer by removing the decimal part.
  • float(): Converts a number to a floating-point number.
  • complex(): Converts a number to a complex number, with an optional imaginary part.

Example:

# Converting float to int
a = 3.14
b = int(a)  # 3

# Converting int to float
c = 10
d = float(c)  # 10.0

# Converting int to complex
e = 5
f = complex(e)  # 5+0j

# Converting float to complex
g = 2.71
h = complex(g)  # 2.71+0j

In this example, the number a is a float that is converted to an integer b, resulting in 3. Similarly, the integer c is converted to a float d, and both integers e and floats g are converted to complex numbers f and h, respectively.


Django-tutorial.dev is dedicated to providing beginner-friendly tutorials on Django development. Examples are simplified to enhance readability and ease of learning. Tutorials, references, and examples are continuously reviewed to ensure accuracy, but we cannot guarantee complete correctness of all content. By using Django-tutorial.dev, you agree to have read and accepted our terms of use , cookie policy and privacy policy.

© 2024 Nischal Lamichhane. All Rights Reserved.
Django-tutorial.dev is styled using Bootstrap 5.
And W3.CSS.