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.
<class 'int'> <class 'float'> <class 'complex'>
Converting the value of one data type (integer, string, float, etc.) to another data type is called type conversion.
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
80 20 1500 1.6666666666666667 1 20 931322574615478515625000000000000000000000000000000
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
The ceil of 2.3 is : 3 The floor of 2.3 is : 2
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
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:
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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.