**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:

Without any further ado, let’s get started.

*Check out this YouTube video specially designed for Python beginners.*

**Categories of Number Data Type**

The number data type is further categorized based on the type of numeric value that can be stored in it. If a variable in Python contains a numeric value, the data type of that variable will be under one of the categories of the number data type based on the type of value assigned to that variable.

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:

I = 123 #Positive Integer J = -20 #Negative Integer K = 0 #Zero Integer

**Long Integers in Python**

**L** suffix is used for the representation of long integers in Python. Long integers are used to store large numbers without losing precision.

I = 99999999999L

**Octal and Hexadecimal in Python**

In Python, we also have another number data type called octal and hexadecimal numbers.

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 #Then in Octal we will write – I = 011 print (I) Output: 9

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

*Interested in learning Python? Enroll in our Python Course in London now!*

Get 100% Hike!

Master Most in Demand Skills Now !

**Floating-point numbers in Python**

Floating-point numbers symbolize the real numbers that are written with a decimal point dividing the integer and fractional parts.

Floating-point numbers may also come with scientific notation with E or e, indicating the power of 10.

Example:

5.6e2 that means 5.6 * 102. I = 2.5 J = 7.5e4

**Complex Numbers in Python**

Complex numbers are of the form, ‘a + bj’, where *a* is real part floating value and *b* is the imaginary part floating value, and *j* represents the square root of −1.

Example:

2.5 + 2j

**Be a Python Expert. Enroll in Python training in Bangalore by Intellipaat****Number Type Conversion in Python**

There are a few built-in Python functions that let us convert numbers explicitly from one type to another. This process is called coercion. The type conversion in python is one type of number to another becomes essential when performing certain operations that require parameters of the same type. For example, programmers may need to perform mathematical operations like addition and subtraction between values of different number types such as integer and float.

We can use the following built-in functions to convert one number type into another:

- int(x), to convert
*x*into an integer value - long(x), to convert
*x*into a long integer value - float(x), to convert
*x*into a floating-point number - complex(x), to convert
*x*into a complex number where the imaginary part stays 0 and*x*becomes the real part - complex(x,y), to convert
*x*and*y*to a complex number where*x*becomes the real part and*y*becomes the imaginary part

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: 3 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.

**Python Programs on Numbers**

Let’s go through some common programs in Python.

**Random Number Generator in Python**

Python doesn’t have a function to make a random number, but it does have a built-in module called random that can be used to generate random numbers. Here’s how to do it

import random print(random.randrange(1, 10))

**Program to add two numbers in Python**

a = input('Enter first number: ') b = input('Enter second number: ') sum = float(a) + float(b) print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

**Program to Check Armstrong Number in Python**

An Armstrong number is a number in which the sum of the cubes of its digits is equal to the number itself. It is also called a narcissistic number.

num = int(input(“Enter a number”) sum = 0 temp = num while temp > 0: digit = temp % 10 sum += digit ** 3 temp //= 10 if num == sum: print(num, “ is an Armstrong Number”) else: print(num, “ is not an Armstrong Number”) Input: 153 Output: 153 is an Armstrong Number

Career Transition

**Program for Factorial of a number in Python**

Factorial of any non-negative integer is equal to the product of all integers smaller than or equal to it. There is a built-in factorial() function in Python.

For example factorial of 6 is 6*5*4*3*2*1 which is 720.

import math def factorial(a): return(math.factorial(a)) num = 6 print(“Factorial of ”, num, “ is”, factorial(num)) The output will be 720

**Program to Reverse a number in Python**

n = 1234 reversed_n = 0 while n != 0: digit = n % 10 reversed_n = reversed_n * 10 + digit n //= 10 print("Reversed Number: " + str(reversed_n)) The output will be 4321

**Program for Palindrome number in Python**

A palindrome is a number or a string which when reversed, remains unaltered.

num = int(input("Enter a number")) temp = num rvrs = 0 while(num>0): dig = num%10 rvrs = rvrs*10+dig num = num//10 if(temp == rev): print("The number is a palindrome") else: print("The number is not a palindrome!") Input: 121 Output: The number is a palindrome Input: 567 Output: The number is not a palindrome

**Program to calculate GCD of two numbers in Python**

def hcfnaive(num1,num2): if(num2==0): return num1 else: return hcfnaive(num2,num1%num2) num1 = 60 num2 = 48 print ("The gcd of 60 and 48 is ",end="") print (hcfnaive(60,48)) The output will be The gcd of 60 and 48 is 12

**Program to calculate LCM of two numbers in Python**

def cal_lcm(a,b): if a > b: greater = a else: greater = b while(True): if((greater % a == 0) and (greater % b == 0)): lcm = greater break greater += 1 return lcm num1 = 54 num2 = 24 print("The L.C.M. is", cal_lcm(num1, num2)) The output will be The L.C.M. is 216

**Program Perfect number in Python**

A perfect number is one that is equal to the sum of its proper divisors, that is, sum of its positive divisors excluding the number itself.

num = int(input("Enter any number: ")) sum = 0 for i in range(1, num): if(num % i == 0): sum = sum + i if (sum == num): print("The number is a Perfect number") Else: print("The number is not a Perfect number") Input: 6 Output: The number is a Perfect number!

**Program to check prime number in Python**

A prime number is a whole number greater than 1 that has no positive divisors other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, ….}.

n = 7 if n>1: for i in range(2, int(n/2)=1): if(n%i)==0: print(n,“ is not a prime number”) break else: print(n,“ is a prime number”) else: print(n,“ is not a prime number”) The output will be 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.

i’m really gratefull for sharing many details about yours rich content, thanks