Python Data Types

In Python,data types are the classification or categorization of data items. Every value in Python has a specific data type that defines what kind of data it is and what operations can be performed on it.

Python has several built-in data types that are categorized into different classes. These types define what kind of value a variable can hold and what operations can be performed on them.

Python is dynamically typed, meaning you don’t need to declare the data type — it is automatically determined at runtime.

Here are the most commonly used data types in Python:



1. Numeric Types :

age = 25
print(type(age))  # Output: 'int'
price = 49.99
print(type(price))  # Output: 'float'
z = 3 + 5j
print(type(z))  # Output: 'complex'


2. String Type :

name = "Alice"
print(type(name))  # Output: 'str'


3. Boolean Type :

is_active = True
print(type(is_active))  # Output: 'bool'


Sequence Types

List :

A list in Python is an ordered, mutable (changeable) collection of items. Lists can store elements of different data types, such as integers, strings, floats, or even other lists.

You can think of a list as a container that holds multiple values under a single variable name.



1. List Items :

A list in Python is a collection used to store multiple items in a single variable. Let’s break down the key features in easy-to-understand language.

numbers = [10, 20, 10, 30]


2. Ordered :

When we say lists are ordered, it means the items have a fixed position.
Each item has an index number, starting from 0.

fruits = ["apple", "banana", "cherry"]
# 'apple' is at index 0, 'banana' at 1, 'cherry' at 2

Even if you add new items later, they will be added at the end without changing the order of existing items.



3. Mutable(Changeable) :

Lists are mutable, meaning you can change their content without changing their identity.

Lists are changeable, which means you can:

fruits[1] = "grapes"  # Changes 'banana' to 'grapes'

You don’t have to create a new list every time — just modify the existing one!



4. Allows Duplicates :

Lists can contain multiple items with the same value. This means you can have duplicates.

numbers = [10, 20, 10, 30]
# No problem — '10' appears twice


5. Indexed Access :

You can access individual items in a list using their index number. Remember, Python uses zero-based indexing.

fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # Output: 'apple'
print(fruits[1])  # Output: 'banana'
print(fruits[2])  # Output: 'cherry'


Examples :

How to Create a List:

my_list = [10, 20, 30, 40]

You can also create an empty list:

empty_list = []

Lists can contain mixed data types:

mixed_list = [10, "apple", 3.14, True]

Accessing Elements:

Python lists use zero-based indexing.

fruits = ["apple", "banana", "cherry"]
print(fruits[0])  # Output: apple
print(fruits[1])  # Output: banana

You can also use negative indexing:

fruits = ["apple", "banana", "cherry"]
print(fruits[-1])  # Output: 'cherry'
print(fruits[-2])  # Output: 'banana'
print(fruits[-3])  # Output: 'apple'


Common List Methods :



1. append():

fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print("Updated fruits:", fruits)
# Output: Updated fruits: ['apple', 'banana', 'cherry', 'orange']


2. insert(index, item):

fruits = ["apple", "banana", "cherry"]
fruits.insert(1, "orange")
print("Updated fruits:", fruits)
# Output: Updated fruits: ['apple', 'orange', 'banana', 'cherry']


3. remove(item):

fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
print("Updated fruits:", fruits)
# Output: Updated fruits: ['apple', 'cherry']


4. pop(index):

fruits = ["apple", "banana", "cherry"]
removed_fruit = fruits.pop(1)
print("Removed fruit:", removed_fruit)
print("Updated fruits:", fruits)
# Output: Removed fruit: banana
# Output: Updated fruits: ['apple', 'cherry']


5. clear():

fruits = ["apple", "banana", "cherry"]
fruits.clear()
print("Updated fruits:", fruits)
# Output: Updated fruits: []


6. index(x):

fruits = ["apple", "banana", "cherry"]
index = fruits.index("banana")
print("Index of banana:", index)
# Output: Index of banana: 1


7. count(x):

fruits = ["apple", "banana", "cherry", "banana"]
count = fruits.count("banana")
print("Count of banana:", count)
# Output: Count of banana: 2


8. extend(iterable):

fruits = ["apple", "banana"]
more_fruits = ["cherry", "orange"]
fruits.extend(more_fruits)
print("Updated fruits:", fruits)
# Output: Updated fruits: ['apple', 'banana', 'cherry', 'orange']


9. reverse():

fruits = ["apple", "banana", "cherry"]
fruits.reverse()
print("Reversed fruits:", fruits)
# Output: Reversed fruits: ['cherry', 'banana', 'apple']


10. copy():

fruits = ["apple", "banana", "cherry"]
fruits_copy = fruits.copy()
print("Copied fruits:", fruits_copy)
# Output: Copied fruits: ['apple', 'banana', 'cherry']


11. When to Use Lists?

Use lists when:



Summary