Contents
- ./function_example.py
- ./module_example_from_import.py
- ./module_example_import.py
- ./print_examples.py
- ./sum5.py
- ./sum_nums.py
./function_example.py 1/6
[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/6
[top][prev][next]
# Example of importing a module, using the from version of import
# by Sara Sprenkle
# With the from import, you don't need to prepend the names
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/6
[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 from math import * statement
# shouldbezero = e ** (i * 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/6
[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='') # make end the empty string
print("line")
# make end a space instead:
print("Put on same", end=' ')
print("line")
something = 7
print("The result is ", something, ".", sep = "")
./sum5.py 5/6
[top][prev][next]
# Adding up 5 numbers from the user
# By CSCI111
# defines a constant for the number of numbers to add up
NUMBER_OF_INPUTS=5
print("This program adds up", NUMBER_OF_INPUTS, "numbers given by you, the user!")
print()
# initialize the accumulator variable
total = 0
#how many times is that getting repeated? 5 times
for i in range(NUMBER_OF_INPUTS):
# what is getting repeated? That goes in the loop body
# get input from the user
userNumber = float(input("Enter your number: "))
# add the user's number to the accumulator
total = total + userNumber
# display the results
print("Your total is", total)
./sum_nums.py 6/6
[top][prev][next]
# Adding up numbers from the user, where
# users says how many numbers to add up
# 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.5.90.