Data Types
In Python, data types represent the type of value that a variable can hold. Python is a dynamically typed language, meaning you don't need to explicitly declare the data type of a variable; the interpreter infers it based on the assigned value. Here are some of the commonly used data types in Python:
1. Numeric Types:
int: Integer type represents whole numbers without any decimal point.
x = 5
float: Float type represents numbers with a decimal point.
y = 3.14
complex: Complex numbers have both a real and an imaginary part.
z = 2 + 3j
2. Text Type:
str: String type represents a sequence of characters and is enclosed in single ('
) or double ("
) quotes.
name = "John"
3. Boolean Type:
bool: Boolean type represents either True or False.
is_true = True
4. Sequence Types:
list: List is an ordered collection that can contain elements of different data types.
numbers = [1, 2, 3, 4]
tuple: Tuple is similar to a list but is immutable (cannot be modified after creation).
coordinates = (2, 3)
str (again): Strings can also be considered a sequence of characters.
greeting = "Hello"
5. Set Types:
set: Set is an unordered collection of unique elements.
unique_numbers = {1, 2, 3, 4}
6. Mapping Type:
dict: Dictionary is a collection of key-value pairs.
person = {"name": "John", "age": 30, "city": "New York"}
7. None Type:
NoneType: Represents the absence of a value or a null value.
result = None
These are some of the fundamental data types in Python. Understanding the types of data you're working with is essential for writing effective and readable code. Additionally, Python has various built-in functions and methods for working with different data types, allowing you to perform operations and manipulations as needed.