Python Data Types
$count++; if($count == 1) { include "../mobilemenu.php"; } ?> if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
Data types in Python define the nature of the data stored in variables. Understanding the various data types is essential for manipulating and working with data in Python effectively. Python has several built-in data types, and each data type supports specific operations.
1. Text Type: String (`str`)
Strings in Python are sequences of characters enclosed in single (`'`) or double (`"`) quotes. Strings are immutable, meaning they cannot be changed after they are created.# Example of a string
greeting = "Hello, Python!"
print(greeting)
# Accessing characters
print("First character:", greeting[0])
# Slicing a string
print("Substring:", greeting[0:5])
# String concatenation
name = "Alice"
message = greeting + " " + name
print("Concatenated message:", message)
Output:
Hello, Python!
First character: H
Substring: Hello
Concatenated message: Hello, Python! Alice
2. Numeric Types: Integer (`int`), Float (`float`), Complex (`complex`)
Integer (`int`)
Integers represent whole numbers, positive or negative, without a decimal point.age = 30
print("Age:", age)
Output:
Age: 30
Float (`float`)
Floats are numbers with a decimal point. They are useful for representing real numbers.price = 19.99
print("Price:", price)
Output:
Price: 19.99
Complex (`complex`)
Complex numbers consist of a real part and an imaginary part and are represented as `a + bj`, where `j` is the imaginary unit.complex_number = 3 + 4j
print("Complex Number:", complex_number)
print("Real Part:", complex_number.real)
print("Imaginary Part:", complex_number.imag)
Output:
Complex Number: (3+4j)
Real Part: 3.0
Imaginary Part: 4.0
3. Sequence Types: List, Tuple, Range
List (`list`)
Lists are ordered collections that are mutable, meaning you can change, add, or remove elements. Lists are created using square brackets `[]`.fruits = ["apple", "banana", "cherry"]
print("Fruits:", fruits)
# Accessing elements
print("First fruit:", fruits[0])
# Modifying an element
fruits[1] = "blueberry"
print("Modified Fruits:", fruits)
Output:
Fruits: ['apple', 'banana', 'cherry']
First fruit: apple
Modified Fruits: ['apple', 'blueberry', 'cherry']
Tuple (`tuple`)
Tuples are similar to lists but are immutable, meaning their values cannot be changed after they are set. They are created using parentheses `()`.coordinates = (10, 20)
print("Coordinates:", coordinates)
# Accessing elements
print("First coordinate:", coordinates[0])
Output:
Coordinates: (10, 20)
First coordinate: 10
Range (`range`)
Ranges represent sequences of numbers, often used in looping.range_example = range(5)
print("Range Example:", list(range_example))
Output:
Range Example: [0, 1, 2, 3, 4]
4. Mapping Type: Dictionary (`dict`)
Dictionaries are collections of key-value pairs, where each key is unique. Dictionaries are mutable and are created using curly braces `{}`.person = {
"name": "Alice",
"age": 30,
"city": "New York"
}
print("Person:", person)
# Accessing a value by key
print("Name:", person["name"])
# Modifying a value
person["age"] = 31
print("Updated Person:", person)
Output:
Person: {'name': 'Alice', 'age': 30, 'city': 'New York'}
Name: Alice
Updated Person: {'name': 'Alice', 'age': 31, 'city': 'New York'}
5. Set Types: Set (`set`), Frozenset (`frozenset`)
Set (`set`)
Sets are unordered collections of unique items, defined using curly braces `{}`. Sets are mutable and do not allow duplicate values.unique_numbers = {1, 2, 3, 3, 4}
print("Unique Numbers:", unique_numbers)
# Adding an element
unique_numbers.add(5)
print("Updated Set:", unique_numbers)
Output:
Unique Numbers: {1, 2, 3, 4}
Updated Set: {1, 2, 3, 4, 5}
Frozenset (`frozenset`)
Frozenset is an immutable version of a set. Once created, you cannot modify it.
immutable_set = frozenset([1, 2, 3, 4])
print("Immutable Set:", immutable_set)
Output:
Immutable Set: frozenset({1, 2, 3, 4})
6. Boolean Type (`bool`)
Boolean data type has only two possible values: `True` and `False`. They are commonly used for conditional testing.is_active = True
print("Is Active:", is_active)
# Boolean result from a comparison
is_equal = (10 == 10)
print("Is Equal:", is_equal)
Output:
Is Active: True
Is Equal: True
7. None Type (`NoneType`)
`None` is a special constant in Python that represents the absence of a value or a null value.result = None
print("Result:", result)
# Checking if a variable is None
if result is None:
print("The result is None.")
Output:
Result: None
The result is None.
Summary
Python’s data types provide a variety of ways to store, manipulate, and work with different kinds of data. Understanding these types and their unique characteristics is essential for effective programming in Python.