Lesson 11 of 70 – Python Data Types
16%

Python Data Types

In Python, a data type tells us what kind of value a variable contains. Python has several built-in data types such as numbers, strings, lists, tuples, sets, dictionaries and Boolean values.

Note: Python is dynamically typed, so you do not need to specify the data type when creating a variable. Python determines it automatically from the assigned value.
What is a Data Type?

A data type defines the kind of data stored in a variable. For example:

name = "Rahul"
age = 20
price = 99.50
is_student = True

Here, each variable contains a different type of data.

Python Built-in Data Types

Python provides several built-in data types. The commonly used types are:

  • str – String
  • int – Integer
  • float – Floating-point number
  • complex – Complex number
  • bool – Boolean
  • list – List
  • tuple – Tuple
  • set – Set
  • dict – Dictionary
  • NoneType – None value
String Data Type

The str data type is used to store text. Strings can be written using single or double quotation marks.

name = "Rahul"
city = 'Patna'

print(name)
print(city)
Output:
Rahul
Patna
Integer Data Type

The int data type is used to store whole numbers without decimal points.

age = 25
marks = 90
number = -10

print(age)
print(marks)
print(number)
Output:
25
90
-10
Float Data Type

The float data type is used to store numbers containing decimal values.

price = 99.50
height = 5.8
temperature = -2.5

print(price)
print(height)
print(temperature)
Output:
99.5
5.8
-2.5
Complex Data Type

The complex data type is used to represent complex numbers. A complex number contains a real part and an imaginary part.

number = 3 + 4j

print(number)
Output:
(3+4j)

In Python, j represents the imaginary part of a complex number.

Boolean Data Type

The bool data type represents one of two values: True or False.

is_student = True
is_teacher = False

print(is_student)
print(is_teacher)
Output:
True
False
List Data Type

A list is used to store multiple values in a single variable. Lists are written using square brackets [ ].

fruits = ["Apple", "Banana", "Mango"]

print(fruits)
Output:
['Apple', 'Banana', 'Mango']

Lists can contain different types of values.

data = ["Rahul", 20, 85.5, True]

print(data)
Tuple Data Type

A tuple is similar to a list, but tuples are immutable. Tuples are usually written using parentheses ( ).

colors = ("Red", "Green", "Blue")

print(colors)
Output:
('Red', 'Green', 'Blue')
Set Data Type

A set is an unordered collection of unique values. Sets are written using curly brackets { }.

numbers = {10, 20, 30, 40}

print(numbers)

A set automatically removes duplicate values.

numbers = {10, 20, 20, 30}

print(numbers)
Output:
{10, 20, 30}
Dictionary Data Type

A dictionary stores data in key-value pairs. Dictionaries are written using curly brackets.

student = {
    "name": "Rahul",
    "age": 20,
    "marks": 85
}

print(student)
Output:
{'name': 'Rahul', 'age': 20, 'marks': 85}
NoneType

The None value represents the absence of a value. Its data type is called NoneType.

result = None

print(result)
print(type(result))
Output:
None
<class 'NoneType'>
Checking Data Type with type()

The type() function is used to determine the data type of a value or variable.

name = "Rahul"
age = 20
price = 99.50

print(type(name))
print(type(age))
print(type(price))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
Checking Multiple Data Types
name = "Amit"
age = 21
height = 5.7
student = True
marks = [80, 85, 90]

print(type(name))
print(type(age))
print(type(height))
print(type(student))
print(type(marks))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
<class 'list'>
Changing Data Types

A variable can be assigned a value of a different data type later.

x = 10

print(type(x))

x = "Hello"

print(type(x))
Output:
<class 'int'>
<class 'str'>

This is possible because Python uses dynamic typing.

Sequence Data Types

Some Python data types are used to store collections of values. Common sequence types include:

  • String – str
  • List – list
  • Tuple – tuple
name = "Python"

numbers = [10, 20, 30]

values = (1, 2, 3)
Mutable and Immutable Data

Python data types can also be understood as mutable or immutable.

Mutable objects can be changed after they are created. For example, lists are mutable.

numbers = [10, 20, 30]

numbers[0] = 100

print(numbers)
Output:
[100, 20, 30]

Tuples and strings, on the other hand, are immutable.

Data Type Example
name = "Rahul"
age = 22
salary = 25000.50
is_employee = True
skills = ["Python", "SQL", "HTML"]
address = ("Patna", "Bihar")

print("Name:", name)
print("Age:", age)
print("Salary:", salary)
print("Employee:", is_employee)
print("Skills:", skills)
print("Address:", address)
Output:
Name: Rahul
Age: 22
Salary: 25000.5
Employee: True
Skills: ['Python', 'SQL', 'HTML']
Address: ('Patna', 'Bihar')
Key Points
  • Data types define the kind of value stored in Python.
  • str is used for text.
  • int is used for whole numbers.
  • float is used for decimal numbers.
  • complex is used for complex numbers.
  • bool stores True or False.
  • list stores a collection of values.
  • tuple stores an immutable collection.
  • set stores unique values.
  • dict stores key-value pairs.
  • NoneType represents None.
  • The type() function is used to check the data type.

🧠 Quick Quiz

Question: Which data type is used to store a whole number in Python?