DATA HANDLING
Data Types in Python
1. Integer (int): Represents whole numbers.
o Example: x = 10
2. Floating Point (float): Represents real numbers with decimals.
o Example: y = 3.14
3. Complex (complex): Represents complex numbers with real and imaginary parts.
o Example: z = 2 + 3j
4. Boolean (bool): Represents truth values, True or False.
o Example: flag = True
5. String (str): Represents a sequence of characters.
o Example: name = "Alice"
6. List (list): Ordered, mutable collection of items.
o Example: lst = [1, 2, 3]
7. Tuple (tuple): Ordered, immutable collection of items.
o Example: t = (1, 2, 3)
8. Dictionary (dict): Unordered collection of key-value pairs.
o Example: d = {"name": "Alice", "age": 25}
9. Set (set): Unordered collection of unique items.
o Example: s = {1, 2, 3}
10. None (NoneType): Represents the absence of a value or a null value.
o Example: x = None
Mutable vs. Immutable:
o Mutable: If the data changed in the variable its object ID should not be changed
then it is called a mutable datatype. EX list, dictionary etc
o Immutable: If the data is changed in the variable and its object ID is also changed
then it is an immutable datatype (e.g., integers, strings, tuples).
Type casting: -
Type conversion is the process of changing the data type of a value in programming.
There are two types of type conversion: implicit and explicit:
Implicit conversion
The compiler automatically performs this conversion when a value of one type is assigned to a
variable of another type. This happens when the value can fit into the variable without being
rounded or truncated.
Explicit conversion
The programmer manually performs this conversion, also known as type casting, when the compiler
requires it to avoid losing information. For example, in C, the programmer can place the type in
parentheses in front of the value.
We have different types of functions for type casting.
for example –
for integer – int()
for float – float()
for string – str()
for list -list()
for tuple – tuple()