Python - Data Types

9.
What is the purpose of the bool data type in Python?

The bool data type in Python is used to represent Boolean values, which can be either True or False. Booleans are essential for logical operations, conditional statements, and comparisons in Python.

Example: Demonstrating the purpose of the bool data type.

# Boolean variables
is_python_fun = True
is_learning = False

# Logical operations
result_and = is_python_fun and is_learning
result_or = is_python_fun or is_learning
result_not = not is_python_fun

print("Is Python fun?", is_python_fun)
print("Is learning?", is_learning)
print("Result of AND operation:", result_and)
print("Result of OR operation:", result_or)
print("Result of NOT operation:", result_not)
Is Python fun? True
Is learning? False
Result of AND operation: False
Result of OR operation: True
Result of NOT operation: False

In this example, boolean variables are created and used in logical operations. Booleans are crucial for decision-making and control flow in Python programs. They are often employed in conditions to determine the flow of the program based on the truth value of expressions.


10.
Explain the concept of type casting in Python.

Type casting in Python refers to the process of converting a variable from one data type to another. This is useful when you want to perform operations or comparisons involving variables of different data types.

Example: Demonstrating type casting in Python.

# Integer to float
int_variable = 42
float_variable = float(int_variable)

# Float to integer
float_number = 3.14
int_number = int(float_number)

# Integer to string
number = 123
string_number = str(number)

print("Original integer:", int_variable)
print("Converted to float:", float_variable)

print("Original float:", float_number)
print("Converted to integer:", int_number)

print("Original integer:", number)
print("Converted to string:", string_number)
Original integer: 42
Converted to float: 42.0

Original float: 3.14
Converted to integer: 3

Original integer: 123
Converted to string: 123

In this example, type casting is demonstrated for converting an integer to a float, a float to an integer, and an integer to a string. Python provides built-in functions (float(), int(), str(), etc.) for performing type casting.


11.
How do you create an empty dictionary in Python?

In Python, an empty dictionary can be created using curly braces ({}) or by using the dict() constructor without any arguments.

Example: Creating an empty dictionary in Python.

# Using curly braces
empty_dict1 = {}
print("Empty dictionary 1:", empty_dict1)

# Using dict() constructor
empty_dict2 = dict()
print("Empty dictionary 2:", empty_dict2)
Empty dictionary 1: {}
Empty dictionary 2: {}

In this example, two different ways of creating an empty dictionary are shown. Both methods result in an empty dictionary, which can later be populated with key-value pairs.


12.
What is the difference between a shallow copy and a deep copy in Python dictionaries?

In Python, copying a dictionary involves creating a new dictionary with the same key-value pairs. Shallow copy and deep copy are two approaches to achieve this, and they differ in how they handle nested structures within the dictionary.

Shallow Copy: A shallow copy creates a new dictionary but does not create new objects for the nested structures. Changes in nested structures are reflected in both the original and the copied dictionary.

import copy

# Original dictionary with a nested list
original_dict = {'key': [1, 2, 3]}

# Shallow copy
shallow_copy_dict = copy.copy(original_dict)

# Modifying the nested list in the shallow copy
shallow_copy_dict['key'][0] = 99

print("Original dictionary:", original_dict)
print("Shallow copy dictionary:", shallow_copy_dict)
Original dictionary: {'key': [99, 2, 3]}
Shallow copy dictionary: {'key': [99, 2, 3]}

Deep Copy: A deep copy creates a new dictionary along with new objects for all nested structures. Changes in nested structures are independent of each other.

# Deep copy
deep_copy_dict = copy.deepcopy(original_dict)

# Modifying the nested list in the deep copy
deep_copy_dict['key'][0] = 88

print("Original dictionary:", original_dict)
print("Deep copy dictionary:", deep_copy_dict)
Original dictionary: {'key': [99, 2, 3]}
Deep copy dictionary: {'key': [88, 2, 3]}

In these examples, a dictionary with a nested list is created, and both shallow and deep copies are made. Modifying the nested list in the shallow copy affects the original, while modifying the nested list in the deep copy does not affect the original dictionary.