Contents
- function_example.py
- input_demo.py
- module_example.py
function_example.py 1/3
[top][prev][next]
# Examples using built-in functions
# Sara Sprenkle, 09.18.2007
x = 3.14159
print "We start with x having value", x
roundx = round(x)
print "We round x to the nearest int", roundx
roundx = round(x,1)
print "We round x to the nearest tenth", roundx
roundx = round(x,3)
print "We round x to the nearest thousandth", roundx
print ""
print "x is of", type(x)
input_demo.py 2/3
[top][prev][next]
# Demonstrate numeric and string input
# by Sara Sprenkle for CS111 on 9/12/07
#
color = raw_input("What is your favorite color? ")
print "Cool! My favorite color is _light_", color, "!"
scale = input("On a scale of 1 to 10, how much do you like Matt Damon? ")
print "Cool! I like him", scale*1.8, "much!" # always likes him more
x = input()
module_example.py 3/3
[top][prev][next]
# Example of importing a module
# by Sara Sprenkle, 09.17.07
#
import math
i = 1j
# The equation e^(i pi) + 1 = 0
shouldbezero = math.e ** (i * math.pi) + 1
print "e^(i pi) + 1 equals", shouldbezero
# practice using functions from modules
print "100^(1/2) =", math.sqrt(100)
Generated by GNU enscript 1.6.4.