Module 3 ICT
Module 3 ICT
Ashton Bron
Schermaine Joy D. Presno Mikylla Anog
Name: Partner:
“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
✓ Transitioning from Visual Logic to Python: Chapter 4
Let’s go Pythons!
Go to windows search and type Python. Open IDLE and refer to the image below.
1
Let’s organize what’s in your mind
1. Circle the predefined functions in the following program and explain why.
2
3. Enter and execute the Python program on the previous page.
4. What is the output for each line of code? Verify your answers by
executing the code and explain theanswer given by the interpreter.
a. abs(4.5) a. abs(4.5) returns the absolute value of 4.5,
b. int(“678”) which is 4.5.
b. int("678") converts the string "678" to the
c. round(-5.6) integer 678.
c. round(-5.6) rounds the number -5.6 to the
d. import random nearest integer, which is -6.
random.randint(4,10) d. random.randint(4,10) generates and returns
a random integer between 4 and 10, inclusive.
9. If a function contains more than one argument, do you think the order
of the arguments makes adifference? Explain your answer.
Yes, the order of arguments can make a significant difference in the behavior of a
function. Arguments are passed to a function in the order they are defined in the function
signature. If the order of the arguments is changed when calling the function, the
behavior of the function will change as well because that is how you code it.
3
4. Execute the following code:
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))
c. Why are the calls to the math() and ceil() functions preceded by
“math.”?
The calls to the math() and ceil() functions are preceded by "math." because these
functions are defined in the math module of the Python standard library.
Application Questions: Use the Python interpreter to design and check your work
1. Write a line of code that prints the integer portion of the number 21.45.
X=int(21.45)
print(x)
Answer= 21
2. Write code that prompts the user for a floating-point number and prints
the smallest integer that islarger than the number the user entered.
import math
num =float(input(“Enter a floating-point number:”))
next_int=math.ceil(num)
print(f”The smallest integer that is larger than {num} is {next_int}.”)
3. Write a line of code that prints a random number between one and 6.
import random
print(random.randint(1, 6))
4
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.
userNumber = input("Enter a number: ")
userNumber = float(userNumber)
print(abs(userNumber))
5. Write a line of code that calculates the square root of 900 and stores the
result in the variable answer.
import math
answer = math.sqrt(900)
print(answer)
5
What’s More
Learning Objectives
6
1. Each of the flowchart symbols in the chart above represents lines or
segments of code. After examining the chart, write a description of:
a. sequence structure
The sequence structure is represented by the rectangular symbol in the flowchart. It represents a
series of instructions or code that are executed in a sequential order, one after the other, without
any branching or repetition.
b. decision or branching structure
The decision or branching structure is represented by the diamond-shaped symbol in the
flowchart. It is used to make decisions based on a certain condition. If the condition is true, the
program will follow one path of execution, and if it is false, it will follow another path of execution.
c. looping structure
The looping structure is represented by the oval-shaped symbol in the flowchart. It is used to execute a series
of instructions or code repeatedly, until a certain condition is met. The loop may be executed a fixed number of
times or until a certain condition is satisfied, such as when a particular value is reached.
2. Which structure best describes the types of Python programs you have
written so far? Do you think this structure is the best structure to use
for all programs? Why or why not?
The structure that best describes the types of Python programs I have written so far is the
sequence structure. However, depending on the task at hand, I have also used decision
structures and looping structures. I don't think any one structure is the best to use for all
programs, as the best structure to use will depend on the specific task at hand and the
complexity of the program.
3. Which structure allows the programmer to create code that decides what
code is executed?
The decision or branching structure allows the programmer to create code that decides
what code is executed based on a certain condition.
a. <
a. < : less than, it returns true if the value on the
b. > left is less than the value on the right.
c. <= b. > : greater than, it returns true if the value on
d. >= the left is greater than the value on the right.
e. != c. <= : less than or equal to, it returns true if the
f. == value on the left is less than or equal to the value
on the right.
d. >= : greater than or equal to, it returns true if
the value on the left is greater than or equal to
the value on the right.
e. != : not equal to, it returns true if the value on
the left is7not equal to the value on the right.
f. == : equal to, it returns true if the value on the
left is equal to the value on the right.
5. What is the result of each of the following expressions?
Assume x = 4, y = 5, z = 4
a. False
a. x > y
b. True
b. x < y c. False
c. x == y d. True
d. x != y e. True
e. x >= z f. True
f. x <= z g. True
g. x + y > 2 * x h. False
h. y * x – z != 4 % 4 + 16 i. True
i. pow(x,2) == abs(-16)
7. Explain how the conditional operators are used when the operands are
strings.
When the operands of the conditional operators are strings, the operators compare the values of
the characters in the strings based on their ASCII values. For example, "apple" < "banana" because
the ASCII value of "a" is less than the ASCII value of "b". If the two strings are of different lengths,
the comparison is made character by character until a difference is found, or until the end of one of
the strings is reached.
8. There are only two possible answers to the expressions in questions 5
and 6. What are they?
The two possible answers to the expressions in questions 5 and 6 are True and
False.
8
10. Enter and execute the following code. Use various values for the
original cost and the sale price.
11. Explain what the following lines of code do. Each line appears in the
program above.
a. originalPrice = int(originalPriceString)
This line converts the string value of originalPriceString to an integer and
assigns it to the variable originalPrice.
Flowchart
14. Compare the flowchart above with the corresponding Python Program.
Carefully compare if/elsestatement in the two programs. Enter and
execute the Python program above.
a. Test the program at least three times. List the three-test data you
used and the corresponding output. Explain why they were the
best data to use as a test for the program.
Test 1: temperature = 200 => Output: "The water is not boiling."
Test 2: temperature = 212 => Output: "Water is boiling."
Test 3: temperature = 250 => Output: "Water is boiling."
print("Done!")
10
15. Sometimes you want to test more than one condition to determine
which code segment should be executed? You can use the following
logical operators to create compound conditions. Examineeach
operator and a sample of its use. Provide an explanation of how each
operator works.
16. Assume the value of the variable numBooks is 40. State the
values of each of the Booleanexpression.
Expression Value
(numBooks > 5) and (numBooks < 100) true
if numCredits
Missing >=expression
Boolean 120 and majorGPA >= 2.0 and overallGPA >= 2.0:
18. Which Boolean expression would be the correct test for Python code?
a. numCredits >= 120 or majorGPA >= 2.0 or overallGPA >= 2.0
b. numCredits > 120 and majorGPA > 2.0 or overallGPA > 2.0
c. numCredits > 119 and majorGPA >= 2.0 and overallGPA >= 2.0
d. numCredits >= 120 and majorGPA >= 2.0 and overallGPA >= 2.0
11
19. Enter and execute the program in #16. Include your choice for the
correct Boolean expression. Create several data sets to test all
possibilities for the Boolean expression. List the data you usedto test
all possibilities for the expression.
Data
numCredits majorGPA overallGPA Expression Result (True or False)
Set
1 119 1.5 1.8 False
2 120 1.5 1.8 False
3 119 2.0 1.8 False
4 119 1.5 2.0 False
5 120 2.0 1.8 False
6 120 1.5 2.0 False
7 119 2.0 2.0 False
8 120 2.0 2.0 False
9
10
Application Questions: Use the Python interpreter to design and check your
work
1. Write a Boolean expression that tests if the value stored in the variable
num1 is equal to the value stored in the variable num2.
num1 == num2
2. Write a Boolean expression that tests if the value stored in the variable
time is less than the valuestored in the variable maxTime or if the
value stored in the variable cost is less than the value stored in the
variable maxCost
time < maxTime or cost < maxCost
3. Write the code for an if statement that adds 5 to the variable num1 if
the value stored in the variable testA equals 25. Otherwise subtract 5
from num1.
if testA == 25:
num1 += 5
else:
num1 -= 5
4. Write the code for an if statement that prints a random number
between one and 6 if the value stored in the variable isValid equals the
Boolean value true.
if isValid == True:
import random
print(random.randint(1, 6))
12
# prompt user for cost of two items
item1_cost = float(input("Enter the cost of item 1: "))
item2_cost = float(input("Enter the cost of item 2: "))
5. Write a Python program that prompts# the user for the cost of two items
calculate total cost of items
to be purchased. Then prompt the user for= their
total_cost item1_costpayment.
+ item2_cost If they enter
an amount that is less than the total cost of the
# prompt user for payment two items, print a
message that tells them how much they still owe. Otherwise, print a
payment = float(input("Enter your payment: "))
6. Write a Python program that prompts the user for a word. If the word
comes between the word’s apple and pear alphabetically, print a
message
# prompt userthat tells the user that the word is valid, otherwise,tell the user
for a word
the
wordword is out
= input("Enter a word:or
") range.
References
https://www.python.org/
https://www.python.org/downloads/
https://wiki.python.org/moin/PythonBooks
https://www.programiz.com/python-programming
https://www.programiz.com/python-programming/online-compiler/
13