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:
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
var1 = 3
var2 = 3.14
var3 = 3j
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
var1 = 3.14
var2 = int (var1)
print ( "After converting float to integer : " , var2)
print ( "type : " , type (var2))
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 a number
You can add, subtract, multiply, and divide numbers using various methods.
operator
|
description
|
example
|
+ Addition |
Add values on either side of the operator. |
2 + 3 = 5 |
– Subtraction |
Subtracts right-hand value from left hand value. |
3 – 2 = 1 |
* Multiplication |
Multiply values on either side of the operator |
2 * 3 = 6 |
/ Division |
Divides two operands |
3 / 2 = 1.5 |
% Modulus |
Divides two operands and returns remainder |
3 / 2 =1 |
** Exponent |
Perform power operation |
3 ** 2 = 9 |
// |
Floor Division |
3 // 2 = 1 |
Example:
Python3
a = 50
b = 30
add = a + b
sub = a - b
mul = a * b
div1 = a / b
div2 = a / / b
mod = a % b
p = a * * b
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.
Method
|
Description
|
math.sqrt() |
Returns the square root of a number |
math.pow() |
Returns the value of x to the power of y |
math.perm() |
Returns the number of ways to choose k items from n items with order and without repetition |
math.gcd() |
Returns the greatest common divisor of two integers |
math.floor() |
Rounds a number down to the nearest integer |
math.ceil() |
Rounds a number up to the nearest integer |
math.factorial() |
Returns the factorial of a number |
Example 1:
Python3
import math
a = 2.3
print ( "The ceil of 2.3 is : " , end = "")
print (math.ceil(a))
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
import math
a = - 10
b = 5.5
c = 15
d = 5
print ( "The copysigned value of -10 and 5.5 is : " , end = "")
print (math.copysign( 5.5 , - 10 ))
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
To learn more about Math Functions you can refer to this article on Geekforgeeks
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
import random
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.
Share your thoughts in the comments
Please Login to comment...