In computer science, type conversion, type casting, type coercion, and type juggling are different ways of changing an expression from one data type to another. An example would be the conversion of an integer value into a floating point value or its textual representation as a string, and vice versa.
Two important aspects of a type conversion are whether it happens implicitly (automatically) or explicitly, and whether the underlying data representation is converted from one representation into another, or a given representation is merely reinterpreted as the representation of another data type.
Python defines type conversion functions to directly convert one data type to another which is useful in day to day and competitive programming.
Python has two types of type conversion.
- Implicit Type Conversion
- Explicit Type Conversion
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 conversion of lower datatype (integer) to higher data type (float) to avoid data loss.
num_int = 123
num_flo = 1.23
num_new = num_int + num_flo
print("datatype of num_int:",type(num_int))
print("datatype of num_flo:",type(num_flo))
prInt("Value of num_new:",num_new)
print("datatype of num_new:",type(num_new))
In the above program,
- We add two variables num_int and num_flo, storing the value in num_new.
- We will look at the data type of all three objects respectively.
- In the output we can see the datatype of num_int is an
integer, datatype of num_flo is afloat. - Also, we can see the num_new has
floatdata type because Python always converts smaller data type to larger data type to avoid the loss of data.
Explicit Type Conversion
In Explicit Type Conversion, users convert the data type of an object to required data type. We use the predefined functions like
int(), float(), str(), etc to perform explicit type conversion.
This type conversion is also called typecasting because the user casts (change) the data type of the objects.
Example : Addition of string and integer using explicit conversion.
num_int = 123
num_str = "456"
print("Data type of num_int:",type(num_int))
print("Data type of num_str before Type Casting:",type(num_str))
num_str = int(num_str)
print("Data type of num_str after Type Casting:",type(num_str))
num_sum = num_int + num_str
print("Sum of num_int and num_str:",num_sum)
print("Data type of the sum:",type(num_sum))
In above program,
- We add num_str and num_int variable.
- We converted num_str from string(higher) to integer(lower) type using
int()function to perform the addition. - After converting num_str to a integer value Python is able to add these two variable.
- We got the num_sum value and data type to be integer.
This article is aimed at providing the information about certain conversion functions.
1. int(a,base) : This function converts any data type to integer. ‘Base’ specifies the base in which string is if data type is string.
2. float() : This function is used to convert any data type to a floating point number.
Python code to demonstrate Type conversion using int(), float()
# initializing string
s = "10010"
# printing string converting to int base 2
c = int(s,2)
print ("After converting to integer base 2 : ", end="")
print (c)
# printing string converting to float
e = float(s)
print ("After converting to float : ", end="")
print (e)
3. ord() : This function is used to convert a character to integer.
4. hex() : This function is to convert integer to hexadecimal string.
5. oct() : This function is to convert integer to octal string.
Python code to demonstrate Type conversion using ord(), hex(), oct() .
# initializing integer
s = '4'
# printing character converting to integer
c = ord(s)
print ("After converting character to integer : ",end="")
print (c)
# printing integer converting to hexadecimal string
c = hex(56)
print ("After converting 56 to hexadecimal string : ",end="")
print (c)
# printing integer converting to octal string
c = oct(56)
print ("After converting 56 to octal string : ",end="")
print (c)
6. tuple() : This function is used to convert to a tuple.
7. set() : This function returns the type after converting to set.
8. list() : This function is used to convert any data type to a list type.
Python code to demonstrate Type conversion using tuple(), set(), list()
# initializing string
s = 'PARITOSH'
# printing string converting to tuple
c = tuple(s)
print ("After converting string to tuple : ",end="")
print (c)
# printing string converting to set
c = set(s)
print ("After converting string to set : ",end="")
print (c)
# printing string converting to list
c = list(s)
print ("After converting string to list : ",end="")
print (c)
9. dict() : This function is used to convert a tuple of order (key,value) into a dictionary.
10. str() : Used to convert integer into a string.
11. complex(real,image) : : This function converts real numbers to complex(real,imag) number.
Python code to demonstrate Type conversion using dict(), complex(), str()
# initializing integers
a = 1
b = 2
# initializing tuple
tup = (('a', 1) ,('f', 2), ('g', 3))
# printing integer converting to complex number
c = complex(1,2)
print ("After converting integer to complex number : ",end="")
print (c)
# printing integer converting to string
c = str(a)
print ("After converting integer to string : ",end="")
print (c)
# printing tuple converting to expression dictionary
c = dict(tup)
print ("After converting tuple to dictionary : ",end="")
print (c)
Key Points to Remember:
- Type Conversion is the conversion of object from one data type to another data type.
- Implicit Type Conversion is automatically performed by the Python interpreter.
- Python avoids the loss of data in Implicit Type Conversion.
- Explicit Type Conversion is also called Type Casting, the data types of object are converted using predefined function by user.
- In Type Casting loss of data may occur as we enforce the object to specific data type.
What are boolean operators in Python?
The logical operators and, or and not are also referred to as boolean operators. While and as well as or operator needs two operands, which may evaluate to true or false, not operator needs one operand evaluating to true or false.
Boolean and operator returns true if both operands return true.
>>> a=50
>>> b=25
>>> a>40 and b>40
False
>>> a>100 and b<50
False
>>> a==0 and b==0
False
>>> a>0 and b>0
True
Boolean or operator returns true if any one operand is true
>>> a=50
>>> b=25
>>> a>40 or b>40
True
>>> a>100 or b<50
True
>>> a==0 or b==0
False
>>> a>0 or b>0
True
The not operator returns true if its operand is a false expression and returns false if it is true.
>>> a=10
>>> a>10
False
>>> not(a>10)
NOTE:-->
FOR MORE UNDERSTANDING OF TYPE CONVERSION REFER THE VIDEO IN THE LINK BY GIVING YOURSELF ONLY 3 MINUTES.
No comments:
Post a Comment
if you have any doubt please let me know.