Python Numbers

Python Numbers

Namaste Coders!

In this article, we will discuss different numeric datatypes in Python.

Python supports integers, floating-point numbe,rs and complex numbers. They are defined as int, float, and complex classes in Python.

As python is dynamically typed, there is no need to specify explicitly, and long integer values can be stored in python very easily.

Integer values are recognized as numbers without a decimal point; if it contains a decimal point, it becomes a float type.

A complex number consists of an ordered pair of real floating-point numbers denoted by x+yj, where x and y are the real numbers and j is the imaginary unit.

We can check datatype using type() function .

Even in python, we do not need to take care of long numbers as there are taken care by the interpreter and there is no need to specify long int or long long as in other languages.

# Numeric Datatype in Python

a=275
print("type is : ",type(a))
print("value is : ",a)

b=27.75
print("type is : ",type(b))
print("value is : ",b)

c=25+5j
print("type is : ",type(c))
print("value is : ",c)

# Long digits can be easily stored
d=514548748645132131435484786746415313135
print("type is : ",type(d))
print("value is : ",d)

Output

image.png

Memory management in Numbers

a=2
b=a
print("value is: ",a)
print("id is: ",id(a))
print("value is: ",b)
print("id is: ",id(b))
b=23
print("-----After updation-----")
print("value is: ",a)
print("id is: ",id(a))
print("value is: ",b)
print("id is: ",id(b))

Output

image.png

In the above👆 case, we are assigning value of variable a to variable b. Thus, they will refer to same memory location. But when we will change the data value of b then, it will refer to different location in memory and value of a will not change.

image.png

Here is another scenario 👇

image.png

If we are changing the value of variable a, it will not reflect any change to variable b.

Number System

In computers, we have 4 different types of number systems. The one decimal system is with which we are familiar for long . Others are👇

  • Binary: Contains only two digits(0,1).It has base 2.
  • Octal: Contains only 8 digits(0,1,2,3,4,5,6,7).It has base 8.
  • Hexadecimal: Contains only 16 digits (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F).It has base 16.
# conversion from number sytem to another

# decimal to binary
a=10
change_a=(bin(a))
print("decimal value: ",a)
print("binary value: ",change_a)

# decimal to octal
b=10
change_b=(oct(b))
print("decimal value: ",b)
print("octal value: ",change_b)

# decimal to hexadecimal
c=10
change_c=(hex(c))
print("decimal value: ",c)
print("hexadecimal value: ",change_c)

Output

image.png

The End

I hope you enjoyed the article and had a good learning experience.

Follow for more articles and keep sharing👩

Keep coding

Python blogs

Linkedin

hashnode blogs

Did you find this article valuable?

Support akshita garg by becoming a sponsor. Any amount is appreciated!