Scientific Computing
Download demo_functions.py and open it in Thonny. Save it to your cs100/ch1
folder. Run the program by clicking the green ‘play’ button. You should see the following as a result in the shell area:
Hello students
3566.5
Remember that if you want to use comments to silence out parts of Python instructions, use #
. For example,
#print("This is commented out by the # symbol")
print("This is not commented out")
print("This also is not commented out") # but this part is a comment
return
and a function that does not have return
.Experiment calling hello()
function in several ways.
hello()
print(hello())
None
coming from? Why would we ONLY want to use the call hello()
, and not print(hello())
?Now, experiment calling avg()
function in several ways.
avg(4,8)
print( avg(4,8) )
rez = avg(4, 8)
print(rez)
avg(4,8)
?Add code to compute the average of 50 and 75 and print the result.
long_hello
shown below. Test the function to see that it works, i.e., call/invoke long_hello()
after it is defined.
def long_hello():
print("Hello students")
print("Oh hi")
print("How are you?")
print("I'm good thanks.")
Define a function prod(num1, num2)
that takes two numbers as input and returns their product. Call/invoke your function to test it.
Define a function add(x, y)
that adds its two parameter and prints the result. Call/invoke your function to test it.
Define a function subtract(x, y)
that subracts y
from x
and prints the result. Call/invoke your function to test it.
hello(name)
that has a single parameter that is a string and prints out a few welcoming messages. For example, if “Heather” is passed as an argument via the function call hello("Heather")
, then the function should print out:
Hello, Heather
Welcome to CS 100
Have an awesome day!
def mystery():
print("A")
print("B")
return "123"
print("C")
print("D")
myResult = mystery()
print(myResult)
weirdSum
that takes four numbers as input and returns the sum of the first two multiplied by the sum of the last two.secondsToday
with no parameters that prints out the number of seconds in a day (note: there are 24 hours in a day; 60 minutes in an hour; and 60 seconds in a minute). When called, your function should produce the following output:
IN A DAY THERE ARE: 86400 seconds.
a
, b
, and c
are passed as parameters. Note: to compute square root of 7 you may use:
import math
result = math.sqrt(7)
Make sure you saved your work and that it runs without errors. Submit your demo-functions.py
file on Moodle.