Skip to main content
Chemistry LibreTexts

2.4: Predefined Functions

  • Page ID
    206262
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    hypothes.is tag: s20iostpy04ualr
    Download Assignment: S2020py04

     

    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 Acitivities 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 functionsInformation 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:

    Hint: Use the up arrow and down arrow in the shell to allow you to traverse prior commands, so you only need to paste the first print statement, and can the modify it.

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

     

     

    print(round(16.5))    
    print(round(15.5))    

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

           i)   do even numbers that truncate a 5 round up or down?

           ii)  do odd numbers that truncate a 5 round up or down? 

     

    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)

    1. Can a function have more than 1 arguments?
    1. Can arguments be stored in variables?
    1. 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

    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

    1. Run the program multiple times, and write down the output below.
    2. What is the purpose of "import random"? What happens if you omit that line of code?
    3. 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

    1. Explain what the ceil() function does.
    2. Explain the purpose of the floor() function.
    3. 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.    

    Assignment

    Individual Homework Activity:

    Download Assignment: S2020py04

    1.  Then Henderson-Hasselbalch equation is used to calculate the pH of a solution given the pKa and the concentration of acid and conjugate base. (15 pts)

    The equation is:

    \[pH=pK_a + log\frac{\text{[conjugate base}]}{[\text{acid}]}\]

    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:

    clipboard_e520ee0e36ee0ef74c7bb7f6a6c10aa46.png

    Your program must contain documentation lines that include your name, the date, a line that states "Py04 Individual Homework" and a description line that indicates what the program is supposed to do. 

     Hints: you will have figure out (google) how to get random floating point numbers, and how to do a log base 10 mathematical function.

     

    2.  Hypothes.is assignment, make two annotations on external content related to predefined functions in python and tag them s20iostpy04ualr (5 pts)

    Upload to Google Drive:

    • This word document with your code and name on it, labeled [your last name]_py04_HW
    • Your python program labeled [your last name]_py04 Program

     

     

    Copyright Statement

    cc4.0
    This work  is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, this material is a modification by Ehren Bucholtz and Robert Belford of CS-POGIL content, with the original material developed by Lisa Olivieri, which is available here.

     


    This page titled 2.4: Predefined Functions is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?