Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. Built-In Data Types
  5. Numeric Types in Python

Numeric Types in Python

Numeric data types are used to store numbers.

Python supports three types of numeric types:

  • Integer (int)
  • Floating Point (float)
  • Complex Number (complex)

An integer is a whole number that does not have a fractional or decimal part. It can be positive, negative, or zero. In Python, integers are created by assigning a whole number value to a variable.

Example:

a = 10
print(type(a))  # Output: <class 'int'>

A float represents a real number that contains a decimal point. It is used to represent numbers that require precision, such as scientific measurements or calculations involving fractions.

Example:

b = 3.14
print(type(b))  # Output: <class 'float'>

A complex number is a number that has a real part and an imaginary part. In Python, complex numbers are written in the form a + bj, where a is the real part and b is the imaginary part.

Example:

c = 3 + 4j
print(type(c))  # Output: <class 'complex'>

In Python, the Boolean data type represents one of two possible values: True or False. It is used to perform logical operations, make decisions in control structures (like if statements), and evaluate conditions.

Booleans are a subclass of integers, with:

  • True having an integer value of 1
  • False having an integer value of 0

Examples:

x = 5 > 3
print(x)      # Output: True
age = 18
if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

#output: You are an adult.
is_valid = True
print(is_valid)   # Output: True

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *