Python: Math

Variables and output were covered in the previous page. Here, we will add to this the ability to perform math.


Example: Simple Math in PHP (without user input)

As down in the example below, you can perform math on numbers of variables. You can print (output) the result of the calculation alone, or add text in the output. The last line of code shows how to format the numeric output to a specific number of digits to the right of the decimal place.


	a = 3
	b = 5
	c = 4.5
	d = 2
	total1 = a * b
	total2 = c + d + total1

	print(2 + 2)
	print(a + b)
	print("a + b =", a + b)
	print(total1)
	print("Total1 = ", total1)
	print("Total2 = ", total2)
	print("Total2 = %.2f" % total2)
	

Output:

  4
  8
  a + b = 8
  15
  Total1 = 15
  Total2 = 21.5
  Total2 = 21.50


Example: More Basic Math in Python

The example below demonstrates order of arithmetic, the modulus math operation (%) and symbol, as well as various other math operations in Python.


	a = 3
	b = 5
	c = 4.5
	d = 2

	print ("a=", a, "b=", b, "c=", c, "d=", d)
	print ("---------------------------")
	print ("a + b =", a + b)
	print ("a + b * d =", a + b * d)
	print ("(a + b) * d =", (a + b) * d)
	print ("b mod a =", b % a)
	print ("Average of a, b, and c=", (a+b+c) / 3)
	avg = (a+b+c) / 3
	print ("Average of a, b, and c= %.2f" % avg)
	print ("b to the power of d =", b ** d)
	

Output:

  a= 3 b= 5 c= 4.5 d= 2
  ---------------------------
  a + b = 8
  a + b * d = 13
  (a + b) * d = 16
  b mod a = 2
  Average of a, b, and c= 4.166666666666667
  Average of a, b, and c= 4.17
  b to the power of d = 25


Example: Multiplying Strings

If you want to repeat a string, you can multiple it by a number as shown below.


	print ('-' * 40)
	

Output:

  ----------------------------------------


Example: User Input and Calculation (Power)



Example: Basic Math Built-In Functions

Python includes a large set of built-in mathematical functions. Below is an example of three. The line "import math" brings in the Python math library where the three functions are available from. Thus, in the line of code "math.sqrt(36)", 36 is a parameter passed into the sqrt function that is inside the math built-in library.


	import math

	total1 = math.pow(5,2)
	total2 = math.sqrt(36)
	total3 = math.pi * 2.5

	print ("Total1 = ", total1)
	print ("Total2 = ", total2)
	print ("Total3 = ", total3)
	

Output:

  Total1 = 25.0
  Total2 = 6.0
  Total3 = 7.853981633974483


Example: Type Casting

The example below demonstrates how to type cast in Python. int(result1) changes (casts) the type of result2 to integer (it had been a float).


	x = 1.3
	y = 5
	result1 = x * y
	result2= int(result1)
	print("Result1 = ", result1)
	print("Result2 = ", result2)
	

Output:

  Result1 = 6.5
  Result2 = 6


Example: Math and Formatting

The example below demonstrates that you can display the result of a math formula either as a calculated variable (total) or place the formula inside the print statement without saving it in a seperate variable.


	x = 10
	y = 3
	total = x * y
	print("Total = %.2f" % total)
	print("Total = %.2f" % (x * y))
	

Output:

  Total = 30.00
  Total = 30.00


Example: Various Math Operators and Functions


	import math
	x = 100
	a = x / 7
	b = int(x / 7)
	c = math.ceil((x / 7))
	d = -(-x // 7)

	print(a)
	print(b)
	print(c)
	print(d)
	

Output:

14.285714285714286
14
15
15