Practical Coding in Python

Learn to write and validate your own code

Darren Kessner, PhD

(revised September 1, 2025)

Previous: Hello strings

Hello math

#
# hello_math.py
#

# Importing the math module gives you access to mathematical
# constants and functions.

import math         

print("math.pi:", math.pi)
print("math.cos(math.pi):", math.cos(math.pi))

# You can import all the names (constants and functions) from a
# module for convenience.  In larger projects it is not generally
# recommended to do this due to the potential for name conflicts
# between functions in different modules.

from math import *

print("pi:", pi)
print("e:", e)

print("cos(0):", cos(0))
print("sin(0):", sin(0))
print("cos(pi):", cos(pi))
print("sin(pi):", sin(pi))

result = sin(pi)

if abs(result) < 1e6:
    print("Yay!")
else:
    print("Boo!")

Output:

math.pi: 3.141592653589793
math.cos(math.pi): -1.0
pi: 3.141592653589793
e: 2.718281828459045
cos(0): 1.0
sin(0): 0.0
cos(pi): -1.0
sin(pi): 1.2246467991473532e-16
Yay!

Next: