Getting Started

Python - IntroductionPython - Hello World ProgramPython - SyntaxPython - Data TypesPython - Variables

Operators

Python - Arithmetic OperatorsPython - Relational OperatorsPython - Logical OperatorsPython - Assignment OperatorsPython - Bitwise OperatorsPython - Membership OperatorsPython - Identity OperatorsPython - Increment and Decrement Operators

Conditions

Python - If Else statement

Loop

Python - While LoopPython - For Loop

Numbers

Python - NumbersPython - Number Conversion

Strings

Python - StringsPython - String OperatorsPython - String FormattingPython - String MethodsPython - String Format Method

List

Python - ListPython - List Methods

Tuple

Python - Tuple

Set

Python - SetPython - Set Methods

Dictionary

Python - DictionaryPython - Dictionary Methods

Functions

Python - FunctionsPython - Functions - Variable length argumentsPython - Lambda Function

Scope of Variables

Python - Scope of Variables

Modules

Python - ModulesPython - Math ModulePython - JSON ModulePython - datetime ModulePython - time Module

I/O

Python - Read input from keyboard

File

Python - File Handling

Exception Handling

Python - Exception Handling

OOP

Python - Classes and ObjectsPython - Class Constructor __init__ methodPython - Class Destructor __del__ methodPython - Built-in Class AttributesPython - InheritancePython - Method OverridingPython - Method Overloading

Package Management

Python - PIP

Python - MySQL

Python - MySQL - Getting StartedPython - MySQL - Insert dataPython - MySQL - Select dataPython - MySQL - Update dataPython - MySQL - Delete data

Python - CSV

Python - Read data from CSV filePython - Write data in CSV file

Python - Number Conversion

Python

python logo

In this tutorial we will do some number conversion exercises in Python.

We have already covered Numbers in the previous tutorial so, feel free to check that out.

Type conversion

Python takes care of data type conversion for us to correctly evaluate an expression when mixed data types are present in it. This is also known as implicit type conversion.

But we will also have cases were we have to explicitly tell Python to convert a variable to a specific type. This is also known as explicit type conversion.

Following are the functions used to convert number data types.

The int() function

We use the int() function to explicitly convert a value to integer type.

Points to note!

  • If the value contains decimal point then the decimal part is truncated.
  • If we have a string value which is an integer (like "10") then it is converted to integer.

In the following example we are converting values to int.

# variables
x = "10"          # converting from string to int
y = 123.45        # converting from float to int

# convert
updated_x = int(x)
updated_y = int(y)

# output
print("x:", x, "is", type(x))
print("updated_x:", updated_x, "is", type(updated_x))
print("y:", y, "is", type(y))
print("updated_y:", updated_y, "is", type(updated_y))

We will get the following output.

x: 10 is <class 'str'>
updated_x: 10 is <class 'int'>
y: 123.45 is <class 'float'>
updated_y: 123 is <class 'int'>

The float() function

We use the float() function to explicitly convert a value to float type.

Points to note!

  • If the value is an integer then the decimal part is added.
  • If we have a string value which is a float (like "3.14") then it is converted to float.

In the following example we are converting values to float.

# variables
x = "3.14"     # converting from string to float
y = 123        # converting from int to float

# convert
updated_x = float(x)
updated_y = float(y)

# output
print("x:", x, "is", type(x))
print("updated_x:", updated_x, "is", type(updated_x))
print("y:", y, "is", type(y))
print("updated_y:", updated_y, "is", type(updated_y))

We will get the following output.

x: 3.14 is <class 'str'>
updated_x: 3.14 is <class 'float'>
y: 123 is <class 'int'>
updated_y: 123.0 is <class 'float'>

The complex() function

We use the complex() function to explicitly convert a value to complex type.

In the following example we are converting value x to complex form where x is the real part of the complex number.

# variable
x = 10        # real part of the complex number

# convert
result = complex(x)

# output
print("x:", x, "is", type(x))
print("result:", result, "is", type(result))

We will get the following output.

x: 10 is <class 'int'>
result: (10+0j) is <class 'complex'>

In the following example we are converting value x and y to complex form where x is the real part of the complex number and y is the imaginary part.

# variable
x = 10        # real part of the complex number
y = 20        # imaginary part of the complex number

# convert
result = complex(x, y)

# output
print("x:", x, "is", type(x))
print("y:", y, "is", type(y))
print("result:", result, "is", type(result))

We will get the following output.

x: 10 is <class 'int'>
y: 20 is <class 'int'>
result: (10+20j) is <class 'complex'>

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT