Python Tutorial

Thursday, August 19, 2010

python:Number

import math

'''
   This code use python math class syntax
'''
n=2.5
'''
   return the smallest integer not less than n
'''
print "math.ceil(2.5) ",math.ceil(n)
'''
   return the largest integer less than x
'''
print "math.floor(2.5) ",math.floor(n)
'''
   return value of n^m 
'''
n=2
m=3
print "math.pow(2,3) ",math.pow(n,m)
'''
   compare two number
      return 0 in n==m
      return 1 if n>m
      return -1 if n0 
'''
n=100
print "math.log10(100) ",math.log10(n)
'''
   return natural logarithm of n, n>0
'''
print "math.log(100) ",math.log(n)
'''
   return absulate value of floating number
'''
print "math.fabs(-2.5) ",math.fabs(-2.5)
'''
   get maximum and minimum number from a array
'''
myArray=[]
myArray.append(5)
myArray.append(1)
myArray.append(6)
myArray.append(2)
print "max(myArray) ",max(myArray)
print "min(myArray) ",min(myArray)
'''
   return exponential of n(e^n)
'''
print "math.exp(5) ",math.exp(5)
'''
   round n to m digit from decimal point 
'''
n=math.pi
m=5
print "round(3.14159265359,5) ",round(n,m)
'''
   python random number
'''
import random
print "random.random() ",random.random()
'''
   random number from a array
'''
print "random.choice(myArray) ",random.choice(myArray)
'''
   random number in a range
'''
print "random.randint(1,100) ",random.randint(1,100)
'''
   python trigonometric function
'''
n=30
'''
   return sin of n in radian
'''
print "math.sin(30) ",math.sin(n)
'''
   return cos of n in radian
'''
print "math.cos(30) ",math.cos(n)
'''
   return tan of n in radian
'''
print "math.tan(30) ",math.tan(n)
n=1
'''
   return arc sin of n in radian
'''
print "math.asin(1) ",math.asin(n)
'''
   return arc cos of n in radian
'''
print "math.acos(1) ",math.acos(n)
'''
   return arc tan of n in radian
'''
print "math.atan(1) ",math.atan(n)
'''
   conver n radian to degree
'''
n=5
print "math.degrees(5) ",math.degrees(n)
'''
   conver n degree to radian
'''
print "math.radians(5) ",math.radians(n)



Output:
math.ceil(2.5)  3.0
math.floor(2.5)  2.0
math.pow(2,3)  8.0
cmp(2,3)  -1
math.sqrt(25)  5.0
math.pi  3.14159265359
math.log10(100)  2.0
math.log(100)  4.60517018599
math.fabs(-2.5)  2.5
max(myArray)  6
min(myArray)  1
math.exp(5)  148.413159103
round(3.14159265359,5)  3.14159
random.random()  0.32172452922
random.choice(myArray)  1
random.randint(1,100)  82
math.sin(30)  -0.988031624093
math.cos(30)  0.154251449888
math.tan(30)  -6.40533119665
math.asin(1)  1.57079632679
math.acos(1)  0.0
math.atan(1)  0.785398163397
math.degrees(5)  286.478897565
math.radians(5)  0.0872664625997

0 comments:

Post a Comment