13.numbers in Python
13.numbers in Python
Home /
Tutorial /
Numbers in Python
Numbers in Python
By Manoj 4.4 K Views 20 min read Updated on March 9, 2022
In this module of the Python tutorial, we will learn in detail about one of the data types in Python, which is the number data type.
We will further learn about different categories of the number data type such as integer, long integer, octal, hexadecimal, etc.
This module also explains the type conversion of the number data type.
Python Tutorial
Comments in Python
Python Variables -
Constant, Global & Static
Variables
Numbers in Python
String in Python
Python Lists
Tuple in Python
Python Sets
Python Dictionary
Python Operators
Type conversion in
Python
Python If Else
Statements
Python Functions -
Define & Call a Functions
in Python
Lambda Function in
Python
Python Built in
Functions with Examples
Functions with Examples
Python Arrays
Python Modules
Python Dates
Python JSON
Python RegEx
PIP Python
Exception Handling in
Python Numbers
In Python, the number data type is used to store numeric values. Numbers in Python are an immutable data type. Being an
immutable data type means that if we change the value of an already allocated number data type, then that would result in
a newly allocated object.
In this module, we will delve deeper into the number data type. Following is the list of the topics that will be covered in this
module:
Integers in Python
Long Integers in Python
Octal and Hexadecimal in Python
Floating-point in Python
Complex Numbers in Python
Check out this YouTube video specially designed for Python beginners.
The number data type is divided into the following five data types in Python:
Integer
Long Integer
Octal and Hexadecimal
Floating-point Numbers
Complex Numbers
We will now understand each of these categories of the number data type, separately.
Learn more about Python from this Python Data Science Course to get ahead in your career!
Integers in Python
Python integers are nothing but whole numbers, whose range dependents on the hardware on which Python is run.
Integers can be of different types such as positive, negative, zero, and long.
Example:
K = 0 #Zero Integer
I = 99999999999L
To represent the octal number which has base 8 in Python, add a preceding 0 (zero) so that the Python interpreter can
recognize that we want the value to be in base 8 and not in base 10.
Example:
I = 11
I = 011
print (I)
Output:
To represent the hexadecimal number (base 16) in Python, add a preceding 0x so that the interpreter can recognize that we
want the value to be in base 16 and not in base 10.
Example:
I = 0x11
print (I)
Output:
17
Submit
Floating-point numbers may also come with scientific notation with E or e, indicating the power of 10.
Example:
I = 2.5
J = 7.5e4
Example:
2.5 + 2j
We can use the following built-in functions to convert one number type into another:
Example:
a = 3.5
b = 2
c = -3.5
a = int(a)
print (a)
b = float(b)
print (b)
c = int(c)
print (c)
Output:
2.0
-3
When converting a float data type into an integer data type, the value gets converted into an integer value closest to zero.
import random
print(random.randrange(1, 10))
sum = 0
temp = num
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
else:
Input: 153
Career Transition
import math
def factorial(a):
return(math.factorial(a))
num = 6
reversed_n = 0
while n != 0:
digit = n % 10
n //= 10
temp = num
rvrs = 0
while(num>0):
dig = num%10
rvrs = rvrs*10+dig
num = num//10
if(temp == rev):
else:
Input: 121
Input: 567
if(num2==0):
return num1
else:
return hcfnaive(num2,num1%num2)
num1 = 60
num2 = 48
print (hcfnaive(60,48))
if a > b:
greater = a
else:
greater = b
while(True):
lcm = greater
break
greater += 1
return lcm
num1 = 54
num2 = 24
sum = 0
if(num % i == 0):
sum = sum + i
if (sum == num):
Else:
Input: 6
n = 7
if n>1:
if(n%i)==0:
break
else:
else:
7 is a prime number
This brings us to the end of this module in Python Tutorial. Now, if you are interested in knowing why python is the most
preferred language for data science, you can go through this Python Data Science tutorial.
Further, once done with all Python topics, get Python Certification from Intellipaat to ensure career stability. Also, take
advantage of our Python interview questions listed by the experts.
Previous Next