Data-Types-in-Python-Mutability-Immutability-and-Operations
Data-Types-in-Python-Mutability-Immutability-and-Operations
by Farhaaann
Numeric Data Types: int, float, complex
Int Float Complex
The int data type represents integers, The float data type represents The complex data type represents
which are whole numbers without a floating-point numbers, which are complex numbers, which have a real
fractional component. Examples numbers with a decimal point. and an imaginary part. Examples
include 10, -5, and 0. Integers Examples include 3.14, -2.5, and 0.0. include 2 + 3j and -1 - 1j. Complex
support a wide range of arithmetic Floats support arithmetic operations numbers support arithmetic
operations such as addition, similar to integers, as well as operations. The real and imaginary
subtraction, multiplication, division, comparison operations. It's parts can be accessed using the .real
modulo, and exponentiation. important to note that floating-point and .imag attributes, respectively.
Additionally, bitwise operations like arithmetic may introduce small The cmath module provides
AND, OR, XOR, and shifts are also precision errors due to the way these functions for more advanced
supported. numbers are stored in memory. complex number operations.
\n: Newline
\t: Tab
\': Single quote
\": Double quote
\\: Backslash
The re module provides regular expression operations for advanced pattern matching and manipulation:
Tuple packing and unpacking allows for convenient creation and assignment of tuples:
Example:
# Packing
my_tuple = 1, 2, "hello"
# Unpacking
a, b, c = my_tuple
Set Data Type
A set is an unordered collection of unique items. Sets are defined using curly braces {} or the set() constructor. Sets are
mutable, allowing you to add or remove elements, but they cannot contain duplicate values.
Set operations:
get(): Returns the value associated with a key (returns None if the key is not found)
keys(): Returns a view object containing the keys in the dictionary
values(): Returns a view object containing the values in the dictionary
items(): Returns a view object containing the key-value pairs in the dictionary as tuples
pop(): Removes and returns the value associated with a key
popitem(): Removes and returns an arbitrary key-value pair
clear(): Removes all key-value pairs from the dictionary
copy(): Returns a shallow copy of the dictionary
update(): Updates the dictionary with key-value pairs from another dictionary or iterable
fromkeys(): Creates a new dictionary with the specified keys and a default value
setdefault(): Returns the value associated with a key (inserts the key with a default value if it is not found)
Example: {x: x**2 for x in range(5)} creates a dictionary where keys are numbers from 0 to 4 and values are their squares.
Mutable vs. Immutable Objects and Data Type
Conversion
Mutable objects can be changed after creation. Examples include lists, sets, and dictionaries. Mutability can lead to side
effects and unexpected behavior if not handled carefully. For example, modifying a list in place will affect all references to
that list.
Immutable objects cannot be changed after creation. Examples include integers, floats, strings, and tuples. Immutability
offers benefits such as predictable behavior and thread safety, as the value of an immutable object is guaranteed not to
change.
Implicit type conversion: Occurs automatically in certain situations (e.g., adding an integer to a float)
Explicit type conversion: Uses conversion functions to change the type of an object
Conversion functions: