Python Type Conversion and Type Casting
Video Lecture
As you know, every value in Python has a data type.Data type tells the compiler or the interpreter how you want to use the data.
Data type also describes that which operations that can be done on the data and the structure in which you want the data to be stored.
In program it often need to change the one type of value to another type.
Type Conversion:
The process of converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion.
Python has two types of type conversion.
- Implicit
- Explicit
Implicit Type Conversion
In Implicit type conversion, Python automatically converts one data type to another data type.
Example :
>>> a = 2 >>> b = 2.5 >>> c = a + b >>> print("Data type of a :",type(a)) Data type of a : <class 'int'> >>> print("Data type of b :",type(b)) Data type of b : <class 'float'> >>> print("Data type of c :",type(c)) Data type of c : <class 'float'>
In the above example,
We are adding two variables
a
andb
, storing the value inc
.As we can see, data type of
a
isinteger
, data type ofb
isfloat
.In a system operation cannot be performed until all the data types are not same in expression.
So, in above example when we are adding first,
a
gets type cast into float and then it add tob
.Type casting from int to float is happening automatically.
So that we get finally result in
.c
as float
Explicit Type Conversion :
In Explicit Type Conversion, programmers convert the data type of an object to required data type.
Explicit conversion also known as type casting.
For type casting, Python defines type conversion functions, to directly convert one data type to another.
Syntax :
(required_data_type)(expression)
1. int(x, base) : Converts x to an integer. base specifies the base if x is a string.
2. float(x) : Converts x to a floating-point number.
>>> s = "10010" >>> a = int(s,2) >>> print ("After converting to integer base 2 : ",a) After converting to integer base 2 : 18 b = int(s,8) >>> print ("After converting to integer base 8 : ",b) After converting to integer base 8 : 4104 c = int(s) >>> print ("After converting to integer base 10 : ",c) After converting to integer base 2 : 10010 >>> f = float(s) >>> print ("After converting to float : ",f) After converting to float : 10010.0
3. ord(x) : Converts a character to integer.
4. chr(x): Converts an integer to a character.
5. hex() : Converts integer to hexadecimal string.
6. oct() : Converts integer to octal string.
>>> s = 'A' >>>c = ord(s) >>> print(c) 65 OUTPUT >>> i = 65 >>> c = chr(i) >>> print(c) A OUTPUT >>> c= hex(65) >>> print(c) 0x41 OUTPUT >>> c= oct(65) >>> print(c) 0o101 OUTPUT
7. complex(real [,imag]) : Converts real numbers to complex(real,imag) number.
8. str(x): Converts object x to a string representation.
9. eval(str) : Evaluates a string and returns an object.
>>> c = complex(1,2) >>> print(c) (1+2j) OUTPUT >>> c= str(10) >>> print(c) 10 OUTPUT >>> c = eval('10') >>> print(c) 10 OUTPUT >>> c = eval('12.5') >>> print(c) 12.5 OUTPUT
10. tuple() : This function is used to convert to a tuple.
11. set() : This function returns the type after converting to set.
12. list() : This function is used to convert any data type to a list type.
13. dict() : This function is used to convert a tuple of order (key,value) into a dictionary.
>>> s = 'python' >>> c = tuple(s) >>> print(c) ('p', 'y', 't', 'h', 'o', 'n') >>> c = set(s) >>> print(c) {'p', 'h', 'n', 't', 'y', 'o'} >>> c = list(s) >>> print(c) ['p', 'y', 't', 'h', 'o', 'n'] >>> tup = (('a', 1) ,('b', 2), ('c', 3)) >>> c = dict(tup) >>> print(c) {'a': 1, 'b': 2, 'c': 3} OUTPUT
Online Live Training
We provide online live training on a wide range of technologies for working professionals from Corporate. We also provide training for students from all streams such as Computer Science, Information Technology, Electrical and Mechanical Engineering, MCA, BCA.
Courses Offered :
- C Programming
- C++ Programming
- Data Structure
- Core Java
- Python
- Java Script
- Advance Java (J2EE)
- Hibernate
- Spring
- Spring Boot
- Data Science
- JUnit
- TestNg
- Git
- Maven
- Automation Testing - Selenium
- API Testing
NOTE: The training is delivered in full during weekends and during the evenings during the week, depending on the schedule.
If you have any requirements, please send them to prowessapps.in@gmail.com or info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in