Python Activity 4: Predefined Functions - IoCT

[Pages:4]Python Activity 4: Predefined Functions

"How can I use the built-in code that is already part of Python?"

Learning Objectives Students will be able to: Content: ? Explain the purpose of a predefined function ? Explain the functions: abs(), pow(), int() round(), random ? Explain the math library functions: floor() and ceil() ? Explain the use of the import statement ? Explain the purpose of a function argument Process: ? Write code that uses predefined functions Prior Knowledge ? Python concepts from Activities 1-3 ? Understanding of flowchart input symbols

Further Reading ?

Model 1: Predefined functions in Python print(), round(), abs(), pow(), int(), float(), etc. are known as predefined functions. Information that a function needs to do its work is sent to the function between the parentheses (). This information is known as an argument. To use a function, call the function. input("Enter your name") is a call to the input function sending the string "Enter your name" as an argument.

Critical Thinking Questions: 1. What are the predefined functions in the following program?

# python program with functions name = input("Enter your name: ") print("Name: ", name)

2. Using the shell window of your Thonny IDE, obtain the output for each of the following print

statements, and indicate briefly what the predefined function does:

Print statement

Output

Purpose of function

print(abs(-4.67))

print(pow(5,3))

print(pow(49,.5))

print(int(34.8))

print(int(34.2))

print(round(6.9))

print(round(6.2))

b. Is there a difference between the round() function and the int() function? If so, what is the difference?

4. What is the output for each line of code? Verify your answers by executing the code and explain the answer given by the interpreter. a. print(abs(4.5))

b. print(int("678"))

c. round(-5.6)

5. Write a definition of a predefined function.

6. Circle the argument(s) in the following predefined functions:

answer = abs(-5.2)

answer = pow(4,2)

number = 45.78

answer = round(number)

a. Can a function have more than 1 arguments?

b. Can arguments be stored in variables?

c. If a function contains more than one argument, do you think the order of the arguments makes a difference? Explain your answer.

Model 2: Modules in Python

A module is a file containing Python definitions and statements. Definitions from a module can be

imported into another python program. The math module gives access to functions for floating point

math. The random module provides tools for making random selections.

Python program 1

Python program 2

import random print(random.randint(1,100))

import math x = 4.7 y = 5.3 z = -4.8 a = -3.2 print(math.ceil(x)) print(math.ceil(y)) print(math.ceil(z)) print(math.ceil(a)) print(math.floor(x)) print(math.floor(y)) print(math.floor(z)) print(math.floor(a))

7. Consider program 1 a) Run the program multiple times, and write down the output below.

b) What is the purpose of "import random"? What happens if you omit that line of code?

c) How could you change the arguments of the randint function to choose a random number within the range of 4 to 10?

8. Run program 2 a. Explain what the ceil() function does.

b. Explain the purpose of the floor() function.

c. Why are the calls to the floor() and ceil() functions preceded by "math."?

Application Questions: Use the Python Interpreter to check your work 1. Write a line of code that prints the integer portion of the number 21.45.

2. Write code that prompts the user for a floating point number and prints the smallest integer that is larger than the number the user entered. (Hint: look at the program in question 10 in activity 3 for help with your input statement)

3. Write a line of code that prints a random number between one and 6.

4. Assume that a user enters any number and that the number is stored in the variable userNumber. Write a line of code that converts the input to a float. Then write a line of code that prints the positive value of the user's input.

5. Write a line of code that calculates the square root of 900 and stores the result in the variable answer.

Individual Homework Activity: Then Henderson-Hasselbalch equation is used to calculate the pH of a solution given the pKa and the concentration of acid and conjugate base. The equation is:

Write a program that asks for the pKa of an organic acid, and then randomly assign concentration values of the conjugate acids and bases between 0.000 and 1.000 M concentrations. Your program should then tell you what the pH of a randomly assigned solution is. Your output should report numbers to 3 decimal places. Sample output for this program:

Your program must contain documentation lines that include your name, the date, a line that states "PA4 Individual Homework" and a description line that indicates what the program is supposed to do. Take a screen shot of the program and the output and attach the files to homework python activity 4 in Moodle. You must also attach the file that ends in .py to the assignment. (10 points) Hints: you will have figure out (google) how to get random floating point numbers, and how to do a log base 10 mathematical function.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download