DEV Community

Murali
Murali

Posted on

Basic Data Types in Python

1. int (Integer)
Definition:
An integer is a whole number (positive, negative, or zero) without a decimal point.

Examples:

a = 10
b = -25
c = 0
Enter fullscreen mode Exit fullscreen mode

Properties:

  • No limit on the size of an integer (Python handles big integers automatically).
  • Supports arithmetic operations: +, -, , /, //, %, *.

Example:

x = 5
y = 2
print(x + y)   # 7
print(x // y)  # 2  (integer division)
print(x ** y)  # 25 (power)

Enter fullscreen mode Exit fullscreen mode

2. float (Floating Point)
Definition:
A float represents real numbers (numbers with decimals).

Examples:

pi = 3.14159
temperature = -5.4
height = 10.0
Enter fullscreen mode Exit fullscreen mode

Properties:

  • Used for precise decimal calculations.
  • Supports all arithmetic operations.
  • You can use scientific notation: e = 1.23e4 # 1.23 × 10⁴ → 12300.0

Example:

a = 2.5
b = 4.0
print(a * b)    # 10.0
Enter fullscreen mode Exit fullscreen mode

3. complex (Complex Number)
Definition:
A complex number has a real and an imaginary part, written as a + bj,
where a is real and b is imaginary.

Examples:

z1 = 2 + 3j
z2 = 1 - 4j
Enter fullscreen mode Exit fullscreen mode

Properties:

  • z.real gives the real part.
  • z.imag gives the imaginary part.
  • Supports arithmetic operations.

Example:

z = 2 + 3j
print(z.real)  # 2.0
print(z.imag)  # 3.0
print(z * 2)   # (4+6j)
Enter fullscreen mode Exit fullscreen mode

4. bool (Boolean)
Definition:
Boolean type has only two values: True and False.
Used in conditional logic and comparisons.

Examples:

is_active = True
is_admin = False
Enter fullscreen mode Exit fullscreen mode

Properties:

  • bool is actually a subclass of int:
  • True → 1
  • False → 0

Example:

print(True + True)   # 2
print(False + True)  # 1
#Used in comparisons:
x = 10
y = 5
print(x > y)  # True
print(x == y) # False
Enter fullscreen mode Exit fullscreen mode

5. str (String)
Definition:
A string is a sequence of characters enclosed in single, double, or triple quotes.

Examples:

name = "Ravi"
greeting = 'Hello'
paragraph = """This is
a multi-line string."""
Enter fullscreen mode Exit fullscreen mode

Properties:

  • Strings are immutable (cannot be changed after creation).
  • You can concatenate (+), repeat (*), slice, and iterate.

Example:

s = "Python"
print(s[0])      # P
print(s[-1])     # n
print(s[0:3])    # Pyt
print(s + "3")   # Python3
print(s * 2)     # PythonPython
Common string methods:
text = "hello world"
print(text.upper())    # HELLO WORLD
print(text.capitalize()) # Hello world
print(text.replace("world", "Python"))  # hello Python
print(text.split())    # ['hello', 'world']
Enter fullscreen mode Exit fullscreen mode

Top comments (0)