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 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 # 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.
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
# 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
To learn more about Math Functions you can refer to this article on GeekforLazyroar
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 neveropen. You can refer to the article here.