Basic python type casting

Akhila
2 min readNov 18, 2020

How to convert one data type to another data type.

***********************

Converting one datatype to another datatype is known as “Data type convertion” or “Type casting”

Note: Follow “Pro tip” and “Must try” given in the documentation.

In this example we come accross some Data types convertion like.

  1. Integer
  2. Float
  3. Boolean
  4. String

Converting tips

Syntax: variable_name = key(variable_name)

Ex:

  1. b = int(a)
  2. b = float(a)
  3. b = bool(a)
  4. b = str(a)

keywords like int(), float(), bool(), str() are used to convert the value in the variable to required type.

=======================================

Converting “INTEGER” to other types.

=======================================

  1. Converting “integer” to “float”

a = 10

b = float(a)

print(b) #output: 10.0

print(type(b)) #output: <class ‘float’>

2. Converting “integer” to “string”

a = 10

b = str(a)

print(b) #output: 10

print(type(b)) #output: <class ‘str’>

3. Converting “integer” to “boolean”

a =10

b = bool(a)

print(b) #output: True

print(type(b)) #output: <class ‘bool’>

*****Pro tip: The number other than ‘0’ always return true, number can be either negative or positive.

*****Must try: a = 0, a = -1

=======================================

Converting “FLOAT” to other types.

=======================================

  1. Converting “float” to int

a = 10.56

b = int(a)

print(b) #output:10

print(type(b)) #output: <class ‘int’>

2. Converting “float” to str

a = 10.56

b = str(a)

print(b) #output: 10.56

print(type(b)) #output: <class ‘str’>

3. Converting “float” to boolean

a = 10.56

b = bool(a)

print(b) #output:True

print(type(b)) #output:<class ‘bool’>

***** Pro tip: Number other than ‘0’ are always true

****Must try: a = 0.0

=======================================

Converting “BOOLEAN” to other types.

=======================================

  1. Converting boolean to integer

a = True

b = int(a)

print(b) #output: 1

print(type(b)) #output: <class ‘int’>

2. Converting boolean to string

a = False
b = str(a)
print(b) #output:False
print(type(b)) #output:<class ‘str’>

3. Converting boolean to float
a = True
b = float(a)
print(b) #output:1.0
print(type(b)) #output:<class ‘float’>

=======================================

Convert “STRING” to other types

=======================================

  1. Converting string to integer
    a = ‘10a’
    b = int(a)
    print(b) # output: ValueError: invalid literal for int() with base 10: ‘10a’

*****Pro tip: While converting string to int, the value should always be in digits.

*****Must try: a = ‘10’, this should give you output without error.

2. Convert string to float
a = “hello”
b = float(a)
print(b) # output:ValueError: could not convert string to float: ‘hello’

*****Pro tip: While converting string to float, the value should always be in digits.

*****Must try: a= ‘10.56’, this should give you output without error.

3. Converting string to boolean
a = ‘0’
name = bool(a)
print(name) #output: True
print(type(name)) #output:<class ‘bool’>

*****Pro tip: Any value in single quotes gives True.

*****Must try: a = ‘’, This should give you False.

*********************** Thank you ***********************

--

--