position: fixed; top: auto !important; margin-left: 112px;

Python Numbers

Python basically supports three types of numbers which includes

  • int
  • float
  • complex

Integer

Integers can be represented using following syntax. You can also print which type of variable is defined in python code using type() function

i = 5 print(type(i)) # Check the datatype of variable i print(i) #Print the value of i

You can also apply various operations on integer numbers as shown in given example

i = 5 print('Number is %d' % i) #print value of i print('Addition is %d' %(i + 1)) # Addition print('Addition is %d' %(i - 1)) # Subtraction print('Multiplication is %d' %(i * 2)) # Multiplication print('Exponential is %d' %(i ** 3)) # Exponential print('floating Division is %f' %(i / 2)) # Floating Division print('Integer Division is %d' %(i // 2)) # Integer Division i += 1 #Increment the value of i by 1 print(i) i -= 1 #Decrement the value of i by 1 print(i) i *= 1 #Multiply the value of i by 1 print(i) i **= 2 #i raise to power 2 which is 5 square print(i) i /= 4 #Floating division prints value in floating format print(i) i //= 2 #Integer divison prints value in integer format print(i)

Float

Float numbers can be represented using following syntax. You can also print which type of variable is defined in python code using type() function

i = 5.0 print(type(i)) # Check the datatype of variable i print(i)
i = 5 print(float(i))

You can also apply various operations on float numbers as shown in given example

print('\n') f = 11.7 print(type(f)) # Check the datatype of variable i print(f) # Print the value of variable i print('Number is %f' % f) # printf() type syntax print('Number is %.3f' % f) # printf() style formatting print('Addition is %f' %(f + 1)) # Addition print('Substraction is %f' %(f - 1)) # Subtraction print('Multiplications is %f' %(f * 2)) # Multiplication print('Exponential is %f' %(f ** 3)) # Exponential print('Floating division is %f' %(f / 2)) # Floating Division print('Integer Divison is %f' %(f // 2)) # Integer Division f += 1 #Increment the value of f by 1 print(f) f -= 1 #Decrement the value of f by 1 print(f) f *= 1 #Multiply the value of f by 1 print(f) f **= 2 #f raise to power 2 print(f) f /= 4 #Floating division prints value in floating format print(f) f //= 2 #Integer division prints value in Integer format print(f)

Complex

Complex numbers are written with a "j" as the imaginary part:

x = 7+8j y = 2j z = -3j print(type(x)) print(type(y)) print(z)

Scroll Indicator


Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.