Page 236 - Ai_V1.0_Class9
P. 236
• Dictionary: In Python, a dictionary is a data type that stores a collection of Key : Value pairs. Each key is unique
within the dictionary, and it maps to a corresponding value. Dictionary is represented by curly braces {} with
each pair separated by a colon (:). Dictionaries are mutable, meaning they can be modified after creation.
For example, person = {'name': 'Yash', 'Department': 'Sales', 'city': 'Delhi'}
Data Type Conversion
The process of converting value of one data type to another is called type conversion or type casting. This is
useful when you need to perform operations involving mixed types.
Python supports two types of type conversion:
1. Implicit Type Conversion
2. Explicit Type Conversion
1. Implicit Type Conversion: In implicit type conversion, when Python encounters mixed data types, it
automatically converts one data type to another data type to avoid data loss and ensure compatibility. This
process doesn't require any user involvement.
# Example of implicit type conversion
a = 5 # Integer
b = 2.0 # Float
# Adding an integer and a float
result = a + b
print(result) # Output: 7.0
print(type(result)) # Output: <class 'float'>
2. Explicit Type Conversion: Explicit type conversion, also known as type casting, is performed by the
programmer using predefined functions to convert one data type to another. This is necessary when you
need to ensure that a specific type is used for an operation or when Python does not automatically convert
the types.
Common Functions for Explicit Type Conversion
• int(): Converts a value to an integer.
• float(): Converts a value to a float.
• str(): Converts a value to a string.
• bool(): Converts a value to a boolean.
• list(), tuple(), set(), etc.: Convert to respective data structures.
Examples:
>>> float(12)
12.0
>>> int(15.5)
15
>>> str(12)
'12'
>>> int(True)
1
>>> bool(0)
False
234 Artificial Intelligence Play (Ver 1.0)-IX

