Saturday, November 16, 2019

Type Conversion or Type Casting

Type Conversion or Type Casting 


Python defines type conversion functions to directly convert one data type to another which is useful in day to day and competitive programming, type name is used for type conversion.

Function and Description :

int(x[,base])
Converts x to an integer, base specifies the base if x is a string.

float(x)
Converts x to a floating-point number.

str(x)
Converts object x to a string representation. 

list(s)
Converts s to list.

tuple(s)
Convert s to a tuple.

set(s)
Convert s to a set.

dict(d)
Creates a dictionary. D must be a sequence of (key, value) tuple.

ord(x)
Converts a single character to its Integer value.

hex(x)
Coverts an integer to hexadecimal string.

oct(x)
Converts an integer to an octal string.

complex(real)
Creates a complex number.


int(a,base):  The function converts any data type to integer. ‘Base’ specifies the base in which string is if data type is string.

Example:

>>> print(int('2014'))
2014
>>> x='2014'
>>> print(type(x),x)
<class 'str'> 2014
>>> x=int(x)
>>> print(type(x),x)
<class 'int'> 2014
>>> print("1001",2)
1001 2
>>> print(int("1001",2))
9
>>> print(int(3.14055))
3


Type float(x) to convert x to a floating-point number.

Example:

>>> print(float('1.90'))

1.9
>>> print(float(5))

5.0

str() : Used to convert integer into a string.

Example :

>>> print(str(3.1455))

3.1455
>>> print(str([1,2,3,4]))

[1, 2, 3, 4]

list() : This function is used to convert any data type to a list type.

Example:
>>> print(list('Mary'))

['M', 'a', 'r', 'y']
>>> print(list((1,2,3,4)))

[1, 2, 3, 4]

>>> print(list((1,2,3,4)))

[1, 2, 3, 4]
>>> s="wercopypastedeveloper.blogspot.com"

>>> print(list(s))

['w', 'e', 'r', 'c', 'o', 'p', 'y', 'p', 'a', 's', 't', 'e', 'd', 'e', 'v', 'e', 'l', 'o', 'p', 'e', 'r', '.', 'b', 'l', 'o', 'g', 's', 'p', 'o', 't', '.', 'c', 'o', 'm']


tuple() : This function is used to convert to a tuple.

Example:
>>> print(tuple('Mary'))

('M', 'a', 'r', 'y')
>>> print(tuple([1,2,3,4]))

(1, 2, 3, 4)
>>> p="Python"

>>> print(tuple(p))

('P', 'y', 't', 'h', 'o', 'n')

set() : This function returns the type after converting to set.

Example:

>>> print(set('Mary'))

{'a', 'r', 'y', 'M'}
>>> print(set([1,2,3,4]))

{1, 2, 3, 4}
>>> s='Python'
>>> print(set(s))

{'n', 'P', 'y', 'o', 't', 'h'}


dict() : This function is used to convert a tuple of order (key, value) into a dictionary.

Example: 
>>> print(dict(tup))

{'a': 1, 'b': 2, 'c': 3}


ord() : This function is used to convert a character to integer.

Example:
>>> a='A'

>>> print(ord(a))

65
>>> print(ord('Z'))

90
>>> print(ord('a'))

97



hex(): This function is to convert integer to hexadecimal string.

Example:

>>> c=hex(19)

>>> print(c)

0x13
>>> print(hex(1))

0x1
>>> print(hex(9))

0x9


oct() : This function is to convert integer to octal string.

Example:
>>> print(oct(1))

0o1
>>> print(oct(9))

0o11
>>>


complex(real,imag) : This function converts real numbers to complex(real, imag) number.

Example:

>>> c=complex(1,2)

>>> print(c)

(1+2j)
>>> print(complex(5,10))

(5+10j)

1 comment:

  1. JackpotCity Casino: Online Casino Website for UK Players
    We have teamed up with JackpotCity Casino to provide our online casino experience to new and loyal players. Learn 바카라 more and try your luck 바카라사이트 at JackpotCity Casino. 카지노

    ReplyDelete