Contents

  1. function_example.py
  2. module_example_from_import.py
  3. module_example_import.py
  4. print_examples.py
  5. random_test.py
  6. sum5.py
  7. sum_nums.py

function_example.py 1/7

[
top][prev][next]
# Examples using built-in functions
# Sara Sprenkle

#x = 6.817454321
x = 5.60123

print("We start with x having value", x)

# Call the function round with input x
# and save the output of function call in variable roundedXInt
roundedXInt = round(x)
print("x rounded to the nearest int:", roundedXInt)

roundedXTenth = round(x, 1)
print("x rounded to the nearest tenth:", roundedXTenth)

a = round(x, 2)
print("x rounded to the nearest hundredth:", a)
# demonstrating that the variable name doesn't matter,
# but good names make the code easier to understand

roundx = round(x, 3)
print("x rounded to the nearest thousandth:", roundx)

print(round(x, 4)) # not saving returned value in a variable

print("-"*40)
print("x is of", type(x))

module_example_from_import.py 2/7

[
top][prev][next]
# Example of importing a module, using the from version of import
# by Sara Sprenkle

from math import *

i = 1j

# The equation e^(i pi) + 1 = 0

# with import math statement
# shouldbezero =  math.e ** (i * math.pi) + 1

shouldbezero =  e ** (i * pi) + 1

print("e^(i pi) + 1 equals", shouldbezero)

# practice using functions from modules
print("100^(1/2) =", sqrt(100))

module_example_import.py 3/7

[
top][prev][next]
# Example of importing a module
# by Sara Sprenkle

# Need to prepend all constants and functions with math.
import math

i = 1j

# The equation e^(i pi) + 1 = 0

# with import math statement
# shouldbezero =  math.e ** (i * math.pi) + 1

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))

print_examples.py 4/7

[
top][prev][next]
# Examples calling the print function
# Sara Sprenkle for CSCI111

print("Hi", "there", "class", sep='; ')
# By default end is "\n" --> called "the new line character"
# means, put the next displayed text on the next line.
print("Put on same", end='')
print("line")

# make end=" " (a space) instead:
print("Put on same", end=' ')
print("line")

something = 7

print("The result is ", something, ".", sep = "")

random_test.py 5/7

[
top][prev][next]
# Demonstrating random module
# by Sara Sprenkle

import random

NUM_RANDOM = 8
print("This program generates", NUM_RANDOM, "random numbers.")

# Demonstrates that it's a pseudo-random number generator
# If using the same seed, the program generates the same list of
# "random" numbers.

# The following function call sets the seed.
random.seed(1)

for x in range(NUM_RANDOM):
    print(random.random())
    #print(random.randint(0, 10))


sum5.py 6/7

[
top][prev][next]
# Adding up 5 numbers from the user
# By CSCI111

#initialize the total
total = 0

# repeat 5 times
for num in range(5):
    # asking for input
    n = int(input("Enter a number: "))
    # adding the user's input to our sum
    total = total + n

print("Your total is", total)

sum_nums.py 7/7

[
top][prev][next]
# Adding up numbers from the user
# By CSCI111

# initialize the accumulator variable
total = 0

# request that user says how many numbers they want to add up.
# (originally, we just had the user enter 5 numbers.)
numNums = int(input("How many numbers do you want to add up? "))


# loop until we're done
for i in range(numNums):
	number = int(input("What number do you want to add? "))
	# update the accumulator
	total = total + number
	# not needed but helpful in watching our program run
	print("Total so far is...",i,  total)
	
# display the results
print("Your total is", total)

Generated by GNU Enscript 1.6.6.