 Open in App
Not now

# Python Numbers, Type Conversion and Mathematics

• Last Updated : 17 Dec, 2020

Prerequisite: Python Language Introduction

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum. It is an open-source programming language.

### Types of numbers in Python

There are three numeric types in Python:

• int
• float
• complex

As Python is a Loosely typed language, we need not define the variable. Variables of numeric types are created when you assign a value to them.

Example :

## Python3

 `# int``var1 ``=` `3`` ` `# float``var2 ``=` `3.14`` ` `# complex``var3 ``=` `3j`` ` `# type() method return the ``# data type of the variable``print``(``type``(var1))``print``(``type``(var2))``print``(``type``(var3))`

Output:

```<class 'int'>
<class 'float'>
<class 'complex'>```

### Type conversion

Converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion.

Example:

## Python3

 `# Python code to demonstrate Type conversion ``var1 ``=` `3.14`` ` `# type conversion of float to int .``var2 ``=` `int``(var1) `` ` `print` `(``"After converting float to integer : "``, var2) ``print` `(``"type : "``,``type``(var2))`` ` `# type conversion of string to integer``var3 ``=` `"323"``var4 ``=` `int``(var3) `` ` `print` `(``"After converting string to integer : "``, var4) ``print` `(``"type : "``,``type``(var4))`

Output:

```After converting float to integer :  3
type :  <class 'int'>
After converting string to integer :  323
type :  <class 'int'>```

### Arithmetic operations on anumber

You can add, subtract, multiply, and divide numbers using various methods.

Example:

## Python3

 `a ``=` `50``b ``=` `30`` ` `# Addition of numbers ``add ``=` `a ``+` `b `` ` `# Subtraction of numbers ``sub ``=` `a ``-` `b `` ` `# Multiplication of number ``mul ``=` `a ``*` `b `` ` `# Division of number ``div1 ``=` `a ``/` `b `` ` `# Division of number ``div2 ``=` `a ``/``/` `b `` ` `# Modulo of both number ``mod ``=` `a ``%` `b `` ` `# Power ``p ``=` `a ``*``*` `b `` ` `# print results ``print``(add) ``print``(sub) ``print``(mul) ``print``(div1) ``print``(div2) ``print``(mod) ``print``(p) `

Output:

```80
20
1500
1.6666666666666667
1
20
931322574615478515625000000000000000000000000000000```

### Math Functions:

The math module has a set of mathematical functions, Some of them are discussed below.

Example 1:

## Python3

 `# Python code to demonstrate the working of ``# ceil() and floor() `` ` `# importing "math" for mathematical operations ``import` `math `` ` `a ``=` `2.3`` ` `# returning the ceil of 2.3 ``print` `(``"The ceil of 2.3 is : "``, end``=``"") ``print` `(math.ceil(a)) `` ` `# returning the floor of 2.3 ``print` `(``"The floor of 2.3 is : "``, end``=``"") ``print` `(math.floor(a)) `

Output:

```The ceil of 2.3 is : 3
The floor of 2.3 is : 2```

Example 2:

## Python3

 `# importing "math" for mathematical operations ``import` `math ``   ` `a ``=` `-``10``b ``=` `5.5``c ``=` `15``d ``=` `5``   ` `# returning the copysigned value. ``print` `(``"The copysigned value of -10 and 5.5 is : "``, end``=``"") ``print` `(math.copysign(``5.5``, ``-``10``)) ``   ` `# returning the gcd of 15 and 5 ``print` `(``"The gcd of 5 and 15 is : "``, end``=``"") ``print` `(math.gcd(``5``,``15``)) `

Output:

```The copysigned value of -10 and 5.5 is : -5.5
The gcd of 5 and 15 is : 5```

Random numbers:

In Python, we have a set of functions that are used to generate random numbers. These functions are used in games and lottery applications.

Methods in the random library:

• Choice()
• randrange()
• random
• seed()

Example:

## Python3

 `# importing "random" for random operations ``import` `random ``   ` `# using random() to generate a random number ``# between 0 and 1 ``print` `(``"A random number between 0 and 1 is : "``, end``=``"") ``print` `(random.random()) `

Output:

```A random number between 0 and 1 is : 0.8548698466875713
```

There is a detailed article on random numbers in geeksforgeeks.  You can refer to the article here.

My Personal Notes arrow_drop_up