Python - Numbers


Numbers are an important part of any programming language.
Numbers are immutable.
Numbers have been catagorized based on their representation on Number line.
    eg. positive, negative, boolean, complex, whole , even, odd,real etc.
Python supports 4 different numeric types. int, long, float, complex.
    Let's see the use of these numbers in Python.



  """
      created_at       : 17 Feb 2017.
      updated_at       : 17 Feb 2017.
      aim of script    : To understand the concept of numbers and use these in Python.
      coded by         : Rishikesh Agrawani.
      Python version   : 2.7.12
  """
  

 #Interactive mode programming - #1

>>> a = 128 #integer >>> b = 256L #long >>> c = 8l #long >>> >>> d = 3.14 #float >>> e = 27.567 #float >>> >>> f = 4+5j #complex >>> i = 3-6j #complex >>> >>> type(a) #type <type 'int'> >>> type(b) <type 'long'> >>> type(c) <type 'long'> >>> >>> type(d) #type <type 'float'> >>> type(e) <type 'float'> >>> >>> type(f) #type <type 'complex'> >>> type(i) <type 'complex'> >>>

 #Interactive mode programming - #2 (Arithmetic)

>>> a = 4 #integer operations >>> b = 5 >>> a+b 9 >>> a-b -1 >>> a*b 20 >>> a/b 0 >>> a//b 0 >>> a**3 64 >>> b**2 25 >>> 10%3 1 >>> 23%3 2 >>> >>> l = 34L #operatins on long integers and integers >>> n = 2 >>> l+n 36L >>> l-n 32L >>> l*n 68L >>> l//n 17L >>> l%n 0L >>> l%3 1L >>> l**2 1156L >>> >>> f = 23.56 #floating point operations >>> f2 = 87.56 >>> >>> f+f2 111.12 >>> >>> f-f2 -64.0 >>> >>> f2-f 64.0 >>> >>> f2*f 2062.9136 >>> >>> f2/f 3.716468590831919 >>> >>> 16**0.5 4.0 >>> 16.0**0.5 4.0 >>> 16.0**0.25 2.0 >>> 62.5/2.5 25.0 >>> 62.5%2.5 0.0 >>> 62.7%2.5 0.20000000000000284 >>> >>> x = 3+2j #operations on complex numbers >>> y = 4-1j >>> >>> z = x+y >>> >>> print z (7+1j) >>> >>> print x-y (-1+3j) >>> >>> print x*y (14+5j) >>> >>> print x/y (0.588235294118+0.647058823529j) >>> >>> print x%y (3+2j) >>> >>> print y%x (4-1j) >>> >>> print y**x (-57.5885872812+98.8580342721j) >>> >>> print x**y (146.20922993+266.834170637j) >>>

 #Interactive mode programming - #3 (Number type conversion)

>>> num1 = 2 >>> num2 = 3L >>> num3 = 4.5 >>> >>> print num1 2 >>> print num2 3 >>> print num3 4.5 >>> >>> print int(num3) #conversion into int 4 >>> print long(num3) #conversion into long 4 >>> l=long(num3) >>> l 4L >>> print float(num1) #conversion into float 2.0 >>> >>> print complex(4) #conversion into complex (4+0j) >>> >>> print complex(4,5) (4+5j) >>> >>> print complex(num1,num3) (2+4.5j) >>>