Python Foundation

Data Types in Python

01 · What are Data Types? Data Types · 1 of 9

What are Data Types?

A data type tells Python what kind of value a variable is holding.

Different types of data need different types of memory and operations. That’s why data types are important.

Main Built-in Data Types in Python

  • Numeric → int, float, complex
  • Text → str
  • Boolean → bool
  • Sequence → list, tuple, range
  • Set → set
  • Mapping → dict

In this page we will focus on the basic data types: int, float, complex, str and bool.

02 · Integer (int) Data Types · 2 of 9

Integer (int)

An integer is a whole number (positive, negative or zero). It does not have a decimal point.

Python
age = 25
temperature = -5
count = 0
big_number = 1000000

print(age)
print(temperature)
print(type(age))
Output
25
-5
<class 'int'>

Key Points

  • No limit on the size of an integer in Python (only limited by memory)
  • Can be positive or negative
  • Used for counting, indexing, ages, quantities etc.
03 · Float Data Types · 3 of 9

Float

A float represents a number with a decimal point (floating-point number).

Python
price = 99.99
height = 5.9
pi = 3.14159
temperature = -2.5

print(price)
print(type(price))
print(height)
Output
99.99
<class 'float'>
5.9

Scientific Notation

Python
x = 2.5e3      # 2.5 × 10³ = 2500.0
y = 1.2e-4     # 1.2 × 10⁻⁴ = 0.00012

print(x)
print(y)
Output
2500.0
0.00012
04 · Complex Data Types · 4 of 9

Complex Numbers

A complex number has a real part and an imaginary part. It is written as a + bj.

Python
num = 3 + 4j
print(num)
print(type(num))

print("Real part:", num.real)
print("Imaginary part:", num.imag)
Output
(3+4j)
<class 'complex'>
Real part: 3.0
Imaginary part: 4.0

Complex numbers are mainly used in scientific and engineering applications.

05 · String (str) Data Types · 5 of 9

String (str)

A string is a sequence of characters. It is used to store text.

Strings can be created using single quotes ' ', double quotes " ", or triple quotes ''' ''' / """ """.

Python
name = "Kundan"
message = 'Hello Python'
paragraph = """This is
a multi-line
string"""

print(name)
print(message)
print(paragraph)
print(type(name))
Output
Kundan
Hello Python
This is
a multi-line
string
<class 'str'>

String Indexing (Preview)

Python
text = "Python"
print(text[0])      # First character
print(text[-1])     # Last character
Output
P
n
06 · Boolean (bool) Data Types · 6 of 9

Boolean (bool)

A boolean data type has only two possible values: True and False.

It is mainly used in conditions and logical operations.

Python
is_active = True
has_passed = False

print(is_active)
print(has_passed)
print(type(is_active))
Output
True
False
<class 'bool'>

Boolean from Comparison

Python
age = 20
print(age > 18)
print(age < 18)
print(age == 20)
Output
True
False
True
07 · Type Checking Data Types · 7 of 9

Type Checking

You can check the data type of any value or variable using the built-in type() function.

Python
print(type(25))
print(type(3.14))
print(type("Hello"))
print(type(True))
print(type(3 + 4j))
Output
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'complex'>

Using type() with variables

Python
name = "Python"
version = 3.12
is_fun = True

print(type(name))
print(type(version))
print(type(is_fun))
Output
<class 'str'>
<class 'float'>
<class 'bool'>
08 · Type Conversion Data Types · 8 of 9

Type Conversion (Casting)

You can convert one data type into another using built-in functions.

Common Conversion Functions

  • int() → converts to integer
  • float() → converts to float
  • str() → converts to string
  • bool() → converts to boolean

Examples

Python
# float to int
x = 9.8
print(int(x))

# int to float
y = 5
print(float(y))

# number to string
z = 100
print(str(z))
print(type(str(z)))
Output
9
5.0
100
<class 'str'>

String to Number

Python
num_str = "50"
print(int(num_str) + 20)

price_str = "99.5"
print(float(price_str) + 0.5)
Output
70
100.0

Boolean Conversion

Python
print(bool(1))
print(bool(0))
print(bool("Hello"))
print(bool(""))
Output
True
False
True
False

Empty string, 0, and None are considered False. All other values are usually True.

09 · Summary Data Types · 9 of 9

Summary – Data Types

  • int → Whole numbers (10, -5, 0)
  • float → Decimal numbers (3.14, -0.5)
  • complex → Complex numbers (3+4j)
  • str → Text data ("Hello")
  • bool → True or False
  • Use type() to check the data type
  • Use int(), float(), str(), bool() for type conversion

Next Topic → Operators in Python

Home Data Types in Python

Data Types in Python

Understand int, float, complex, str, bool and type conversion.

Select any topic from the left sidebar to begin.


Start Learning →