What are Data Types?
In Python, data types define the kind of value that a variable can hold. A data type determines what kind of operations you can perform on that variable. Python has several built-in data types, such as integers, floating-point numbers, strings, booleans, and more.
1. Numeric Types
Python has three numeric data types:
- Integer (
int
): Whole numbers, both positive and negative. - Floating-Point (
float
): Numbers with decimal points. - Complex (
complex
): Numbers with a real and an imaginary part (less commonly used in basic programming).
Examples:
# Integer num1 = 10 # An integer value print(type(num1)) # Output: <class 'int'> # Float num2 = 3.14 # A floating-point value print(type(num2)) # Output: <class 'float'> # Complex num3 = 2 + 3j # A complex number (2 + 3i) print(type(num3)) # Output: <class 'complex'>
2. String Type (str
)
A string is a sequence of characters enclosed in quotes. You can use either single ('
) or double ("
) quotes to define strings. Strings are used to represent textual data.
Examples:
# String with single quotes name = 'John' print(type(name)) # Output: <class 'str'> # String with double quotes greeting = "Hello, world!" print(type(greeting)) # Output: <class 'str'>
Strings are very versatile in Python. You can concatenate them, find their length, and manipulate them in many ways.
String Operations:
first_name = "John" last_name = "Doe" full_name = first_name + " " + last_name # Concatenate strings print(full_name) # Output: John Doe
3. Boolean Type (bool
)
A Boolean represents one of two possible values: True
or False
. It is commonly used for conditional statements and comparisons.
Examples:
is_active = True is_valid = False print(type(is_active)) # Output: <class 'bool'>
You can perform logical operations using Boolean values.
Example:
x = 5 y = 10 is_equal = (x == y) # Checks if x is equal to y print(is_equal) # Output: False
4. List Type (list
)
A list is an ordered collection of items that can be of different data types. Lists are mutable, meaning you can change their elements after creation.
Examples:
# List with mixed data types my_list = [1, "apple", 3.14, True] print(type(my_list)) # Output: <class 'list'>
You can access elements in a list by their index (starting from 0) and modify them.
Example:
my_list[0] = 42 # Change the first element print(my_list) # Output: [42, 'apple', 3.14, True]
5. Tuple Type (tuple
)
A tuple is similar to a list, but it is immutable, meaning that once created, its elements cannot be changed. Tuples are useful when you want to store a collection of values that shouldn’t be altered.
Examples:
# Tuple with mixed data types my_tuple = (1, "banana", 2.5) print(type(my_tuple)) # Output: <class 'tuple'>
You can access elements of a tuple just like a list, but you cannot modify them.
Example:
# Accessing an element from a tuple print(my_tuple[1]) # Output: banana
6. Set Type (set
)
A set is an unordered collection of unique items. Sets do not allow duplicates and do not preserve the order of the items. They are useful for checking membership and eliminating duplicates from a collection.
Examples:
# Set with unique elements my_set = {1, 2, 3, 4, 5} print(type(my_set)) # Output: <class 'set'>
Set Operations:
# Adding an element to a set my_set.add(6) print(my_set) # Output: {1, 2, 3, 4, 5, 6} # Checking if an element is in a set print(3 in my_set) # Output: True
7. Dictionary Type (dict
)
A dictionary is an unordered collection of key-value pairs. The keys are unique, and each key maps to a value. Dictionaries are widely used for storing data that is associated with a particular key.
Examples:
# Dictionary with keys and values my_dict = {"name": "Alice", "age": 25, "city": "New York"} print(type(my_dict)) # Output: <class 'dict'>
You can access values using keys and modify the dictionary.
Example:
# Accessing a value from a dictionary print(my_dict["name"]) # Output: Alice # Modifying a value my_dict["age"] = 26 print(my_dict) # Output: {'name': 'Alice', 'age': 26, 'city': 'New York'}
8. None Type (None
)
The None
type is a special data type in Python that represents the absence of a value. It is often used to indicate that a variable does not have a valid or meaningful value.
Example:
result = None print(type(result)) # Output: <class 'NoneType'>
You can use None
as a placeholder, or to indicate that a variable or function has no value.