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 :
- int : Integer numbers (e.g., 5, -10, 100)
age = 25
print(type(age)) # Output: 'int'
- float : Decimal numbers (e.g., 3.14, -0.99)
price = 49.99
print(type(price)) # Output: 'float'
- complex : Complex numbers (e.g., 3+5j)
z = 3 + 5j
print(type(z)) # Output: 'complex'
2. String Type :
- str: A sequence of characters enclosed in quotes (e.g., "Hello", 'Python')
name = "Alice"
print(type(name)) # Output: 'str'
3. Boolean Type :
- bool: Represents either True or False, used in logical operations.
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:
- Update items
- Remove items
- Add new items
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:
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 :
- append(): Adds an item to the end of the list.
- insert(index, item): Inserts an item at a specified index.
- remove(item): Removes the first occurrence of an item.
- pop(index): Removes and returns the item at the specified index (default is the last item).
- sort(): Sorts the items in ascending order.
- reverse(): Reverses the order of items in the list.
- clear(): Removes all items from the list.
- index(x): Returns the index of the first occurrence of an item.
- count(x): Returns the number of occurrences of an item.
- extend(iterable) : Adds elements from another iterable (e.g., list, tuple) to the end of the list.
- reverse() : Reverses the order of items in the list.
- copy() : Returns a shallow copy of the list.