Contents
- function_example.py
- module_example.py
- pick4.py
- print_examples.py
- random_test.py
- sum5_constant.py
- sum5_no_loop.py
- sum5.py
function_example.py 1/8
[top][prev][next]
# Examples using built-in functions
# Sara Sprenkle
x = 6.817454321
#x = 5.6512542
print("We start with x having value", x)
# Call the function round with input x
# Then, save 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 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))
print("-"*40)
print("x is of", type(x))
module_example.py 2/8
[top][prev][next]
# Example of importing a module
# by Sara Sprenkle
# Alternative: could use import math
# Would then need to prepend all constants and functions with math.
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))
pick4.py 3/8
[top][prev][next]
# Display the numbers that are selected by the magic
# ping-pong ball machine for Pick4 VA Lottery, all on one line
# By CS111
print_examples.py 4/8
[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/8
[top][prev][next]
# Demonstrating random module
# by Sara Sprenkle
import random
NUM_RANDOM = 10
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_constant.py 6/8
[top][prev][next]
# This program adds up fixed number of inputs from the user.
# By CS111
NUM_INPUTS = 3
print("This program will add up", NUM_INPUTS, "numbers given by the user.")
print()
sum = 0
for i in range(NUM_INPUTS):
number = eval(input("Enter your number: "))
sum += number
# same as saying: sum = sum + number
print()
print("The sum of these", NUM_INPUTS, "numbers is", sum)
sum5_no_loop.py 7/8
[top][prev][next]
# Representative of the way to implement sum5.py before today's lecture.
# What happens if I asked you to add up 15 numbers...
# Or 100 numbers intead? What would writing that code be like?
print("This program will total 5 numbers entered by you!")
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
num3 = input("Enter the third number: ")
num4 = input("Enter the fourth number: ")
num5 = input("Enter the fifth number: ")
total = num1 + num2 + num3 + num4 + num5
print("The total of these numbers is", total)
sum5.py 8/8
[top][prev][next]
# This program adds up 5 numbers from the user.
# By CS111
print("This program will add up 5 numbers given by the user.")
print()
sum = 0
for i in range(5):
number = eval(input("Enter your number: "))
sum += number
print()
print("The sum of these 5 numbers is", sum)
Generated by GNU Enscript 1.6.6.