Python Programming Type Casting/Conversion, Frozen Set | Python Programming

Python Programming Type Casting/Conversion:

The process of converting the value of one data type such as integer, string, float, etc. to another data type is called type conversion/ typecasting. Python has two types of type conversion/typecasting.
  1. Implicit Type Conversion
  2. Explicit Type Casting

Implicit Type Conversion:

In Implicit type conversion, Python automatically converts one data type to another data type. This process doesn't need any user involvement.
Let's see an example where Python promotes the conversion of the lower data type (integer) to the higher data type (float) to avoid data loss.


Example 
Converting integer to float
n_int = 234
n_flo = 2.34
n_new = n_int + n_flo
print("datatype of n_int:",type(n_int))
print("datatype of num_flo:",type(n_flo))
print("Value of n_new:",n_new)
print("datatype of n_new:",type(n_new))

When we run the above program, the output will be
datatype of n_int:
datatype of n_flo:
Value of n_new: 234.25
datatype of n_new:
In the above program,
  • We add two variables n_int and n_flo, storing the value in n_new.
  • We will look at the data type of all three objects respectively.
  • In the output, we can see the datatype of n_int is an integer, the datatype of n_flo is float.
  • Also, we can see the n_new has float data type because Python always converts smaller data type to larger data type to avoid the loss of data.
Now, let us try adding a string and an integer, and see how Python treats it.

Explicit Type Casting:

In Explicit Type Conversion, users convert the data type of an object to required datatype. We use the predefined functions like int()float()str(), etc to perform explicit type conversion.
This type conversion is also called typecasting because of the user casts (change) the data type of the objects.
Syntax :
(required_datatype)(expression)
Typecasting can be done by assigning the required data type function to the expression.

Example
Addition of string and integer using explicit casting
n_int = 123
n_str = "456"
print("Data type of n_int:",type(n_int))
print("Data type of n_str before Type Casting:",type(n_str))
num_str = int(n_str)
print("Data type of n_str after Type Casting:",type(n_str))
num_sum = n_int + n_str
print("Sum of n_int and n_str:",n_sum)
print("Data type of the sum:",type(n_sum))
When we run the above program, the output will be
Data type of n_int:
Data type of n_str before Type Casting:
Data type of n_str after Type Casting:
Sum of n_int and n_str: 579
Data type of the sum:
In above program,
  • We add n_str and n_int variable.
  • We converted n_str from string(higher) to integer(lower) type using int() function to perform the addition.
  • After converting n_str to an integer value Python is able to add these two variable.
  • We got the n_sum value and data type to be an integer.

Key Points to Remember:
  1. Type Casting is the conversion of an object from one data type to another data type.
  2. Implicit Type Casting is automatically performed by the Python interpreter.
  3. Python avoids the loss of data in Implicit Type Conversion.
  4. Explicit Type Casting the data types of object are converted using predefined function by the user.
  5. In Type Casting loss of data may occur as we enforce the object to the specific data type.

Examples:-


def validSalary(sal):                                                           
    try:                                                                        
       sal = float(sal);                                                        
       return 4000 <= sal <= 7000;                                              
    except ValueError as ex:                                                    
        return False;                                                           
                                                                                
print "Salary '5000' is ", validSalary('5000');                                 
print "Salary '5000.0' is ", validSalary('5000.0');                             
print "Salary '' is ", validSalary(''); 

Output:

$ python type_conversion.py
Salary '5000' is  True
Salary '5000.0' is  True
Salary '' is  False

Frozen set

The frozen set is just an immutable version of a Python set object. Elements of a frozen set cannot be modified at any time, remains the same after creation but elements in set can be modified.
Returns an empty frozenset if no parameters in frozenset method.
#!/usr/bin/python                                                               
                                                                                
var_b = {1, 2, 3, 6, 7};                                                        
                                                                                
var_result = frozenset(var_b);                                                  
print("frozenset is: %s" % var_result);                                         
                                                                                
print("frozenset empty is: %s" % frozenset()); 
Output:
$ python typte_conversion.py
frozenset is: frozenset([1, 2, 3, 6, 7])
frozenset empty is: frozenset([])

Post a Comment

0 Comments