Sunday 19 February 2023

Type conversion in Python

Type conversion 

We cannot perform operations of two different data types so that we need to convert and one of data into other to perform operation. For example we have a string ''12'' and integer 20 and we cannot perform addition operation on this so we need to convert string to integer data type.

The functions for Type conversion are

  •  int(a)
  • float(a)
  • str(a)
  • tuple(a)
  • list(a)
  • dict(a)
  • set(a)
  • ord(a)
  • oct(a)
  • hex(a)
  • chr(a)

int(a)

Int(a)  converts the input a into integer datatype.


#a is a string
b=int(a)
#now b is an integer
print(type(b))
Output
<class 'int'>

float(a)

float(a) converts the given input into floating point value. For example if a is an integer of 120 if we apply type conversion function float(120) then 120 will be converted to 120.0


i=120
print(type(i))
f=float(i)
print(type(f))
Output
<class 'int'>
<class 'float'>

str(a)

str(a) converts any other data type into string data type. For example if we want to convert a number into a string we can convert it by passing it as a parameter to str(a) it the place of a we can change any value we can use 123, 125.664, etc.,


a=126.5674
print(type(a))
st=str(a)
print(type(st))
Output
<class 'float'>
<class 'str'>

tuple(a)

Tuple is a sequential data structure in which the data is separated by comma and enclosed in between the parenthesis. We can convert a list of integers into a tuple by passing a list to the function tuple(). This converts a list data type into a tuple.


l=[1,2,6,4,9,3,6]
print(type(l))
tu=tuple(l)
print(type(tu))
Output
<class 'list'>
<class 'tuple'>

list(a)

 List is a sequential data structure where the data is separated by commas and enclosed in between the square brackets. We can convert a string into list of characters using the list function.


king='James'
print(type(king))
l=list(king)
print(l)
print(type(l))
Output
<class 'str'>
['J', 'a', 'm', 'e', 's']
<class 'list'>

dict(a)

Dictionary is a Mapping data structure where the values are arranged in the format of key value pair and the key value is unique every time and the value may be duplicate. We can convert a list  which contains the name of the persons to a dictionary.

print(names)
dic=dict(names)
print(dic)
print(type(dic))
Output
[('mark', 0), ('ashwin', 1), ('amulya', 2), ('jaya', 3)]
{'mark': 0, 'ashwin': 1, 'amulya': 2, 'jaya': 3}
<class 'dict'>

set(a)

Set is data structure which allows only unique values to be present in it. We can convert a list of integers into set by passing the set as a parameter in the set function.


ord(a)

ord function converts the given character to the integer. For example if we give the character 'h' to ord function it will return the ASCII value of h as 104.
print(ord('a'))
Output
97


chr(a)

chr converts the given integer to the respective character. For example if we want to convert the integer 97 it returns the value of 'a'.

print(chr(97))
Output
a

oct(a)

oct converts the given integer value to the respective octal value. For example if we want to convert 87 to the octal value we use oct function and oct() will return '0o127'.


a=105
oc=oct(105)
print(oc)
Output
0o151

hex(a)

hex converts the given integer value to the respective hexadecimal value. For example if we want to convert 15 to hexadecimal value and hex() will return '0xf'.


ax=15
hex_value=hex(ax)
print(hex_value)
Output
0xf

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top