Data Types in Python
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.
Integer (int)
An integer is a whole number (positive, negative or zero). It does not have a decimal point.
age = 25
temperature = -5
count = 0
big_number = 1000000
print(age)
print(temperature)
print(type(age))
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.
Float
A float represents a number with a decimal point (floating-point number).
price = 99.99
height = 5.9
pi = 3.14159
temperature = -2.5
print(price)
print(type(price))
print(height)
99.99 <class 'float'> 5.9
Scientific Notation
x = 2.5e3 # 2.5 × 10³ = 2500.0
y = 1.2e-4 # 1.2 × 10⁻⁴ = 0.00012
print(x)
print(y)
2500.0 0.00012
Complex Numbers
A complex number has a real part and an imaginary part. It is written as a + bj.
num = 3 + 4j
print(num)
print(type(num))
print("Real part:", num.real)
print("Imaginary part:", num.imag)
(3+4j) <class 'complex'> Real part: 3.0 Imaginary part: 4.0
Complex numbers are mainly used in scientific and engineering applications.
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 ''' ''' / """ """.
name = "Kundan"
message = 'Hello Python'
paragraph = """This is
a multi-line
string"""
print(name)
print(message)
print(paragraph)
print(type(name))
Kundan Hello Python This is a multi-line string <class 'str'>
String Indexing (Preview)
text = "Python"
print(text[0]) # First character
print(text[-1]) # Last character
P n
Boolean (bool)
A boolean data type has only two possible values: True and False.
It is mainly used in conditions and logical operations.
is_active = True
has_passed = False
print(is_active)
print(has_passed)
print(type(is_active))
True False <class 'bool'>
Boolean from Comparison
age = 20
print(age > 18)
print(age < 18)
print(age == 20)
True False True
Type Checking
You can check the data type of any value or variable using the built-in type() function.
print(type(25))
print(type(3.14))
print(type("Hello"))
print(type(True))
print(type(3 + 4j))
<class 'int'> <class 'float'> <class 'str'> <class 'bool'> <class 'complex'>
Using type() with variables
name = "Python"
version = 3.12
is_fun = True
print(type(name))
print(type(version))
print(type(is_fun))
<class 'str'> <class 'float'> <class 'bool'>
Type Conversion (Casting)
You can convert one data type into another using built-in functions.
Common Conversion Functions
int()→ converts to integerfloat()→ converts to floatstr()→ converts to stringbool()→ converts to boolean
Examples
# 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)))
9 5.0 100 <class 'str'>
String to Number
num_str = "50"
print(int(num_str) + 20)
price_str = "99.5"
print(float(price_str) + 0.5)
70 100.0
Boolean Conversion
print(bool(1))
print(bool(0))
print(bool("Hello"))
print(bool(""))
True False True False
Empty string, 0, and None are considered False. All other values are usually True.
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
Data Types in Python
Understand int, float, complex, str, bool and type conversion.
Select any topic from the left sidebar to begin.
Start Learning →
