Docsity

comp science worksheet, Assignments of Computer science

Toggle sidebar
bg1
Math Expressions — Practice & Program
AP CSP · Unit 2 (Programming) · arithmetic, integer division, and MOD — no if statements or loops needed
Name: Date: Period:
What this is. PartA you do by hand — no computer. Predict what Python would give, and watch the traps: division that turns into a
decimal, the difference between / and // , and order of operations. PartB is a short program you’ll write and run.
Part A — Evaluate by hand (no computer)
A1 · Division and remainder
Write the exact value Python would give. Careful: some of these are decimals.
1. 9 / 4
2. 9 // 4
3. 9 % 4
4. 20 // 6
5. 20 % 6
6. 8 / 2 (look twice)
A2 · Order of operations
Same rules as math class: parentheses, then ** , then * / // % , then + - .
7. 3 + 4 * 2
8. (3 + 4) * 2
9. 10 - 2 * 3
10. 2 * 3 ** 2
11. 12 / 2 + 4 (what type?)
Trap to remember. A single slash / always gives a decimal (a float), even when it divides evenly: 8 / 2 is 4.0 , not 4. Use
// when you want a whole number.
pf3

Partial preview of the text

Download comp science worksheet and more Assignments Computer science in PDF only on Docsity!

Math Expressions — Practice & Program

AP CSP · Unit 2 (Programming) · arithmetic, integer division, and MOD — no if statements or loops needed Name: Date: Period:

What this is. Part A you do by hand — no computer. Predict what Python would give, and watch the traps: division that turns into a decimal, the difference between / and // , and order of operations. Part B is a short program you’ll write and run.

Part A — Evaluate by hand (no computer)

A1 · Division and remainder Write the exact value Python would give. Careful: some of these are decimals.

  1. 9 / 4
  2. 9 // 4
  3. 9 % 4
  4. 20 // 6
  5. 20 % 6
  6. 8 / 2 (look twice)

A2 · Order of operations Same rules as math class: parentheses, then ** , then * / // % , then + -.

  1. 3 + 4 * 2
  2. (3 + 4) * 2
  3. 10 - 2 * 3
  4. 2 * 3 ** 2
  5. 12 / 2 + 4 (what type?)

Trap to remember. A single slash / always gives a decimal (a float ), even when it divides evenly: 8 / 2 is 4.0 , not 4. Use // when you want a whole number.

Part A — continued

A3 · Types and conversion Answer in words where asked.

  1. What is the value of 7 / 1 , and is it an int or a float?
  2. What does int(6.8) give — does it round or chop?
  3. What does "12" + "3" give?
  4. What does int("12") + int("3") give?
  5. Will int("3.5") work? If not, why?

A4 · Put MOD to work

  1. Is 47 even or odd — and which expression proves it?
  2. What is the last digit of 8302 , using one MOD expression?

Why this matters (the reading). Every one of these shows up as a “what does this print?” question. The two rules that catch the most people: (1) / makes a float, // keeps it whole; (2) anything typed with input() is text until you convert it with int() or float().

Part B — Program: Seconds → H : M : S

Goal. Ask the user for a whole number of seconds, then print how many hours , minutes , and seconds that is. This is the change- maker idea with time: // pulls out the whole groups, % keeps what’s left over.

The math you need

Step Expression Why Whole hours total // 3600 3600 seconds in an hour Seconds left after hours total % 3600 the remainder Whole minutes remaining // 60 60 seconds in a minute Seconds left over remaining % 60 the final remainder

Requirements ☐ Start the file with a purpose sentence comment: # This program ___ so that ___ ☐ Use input() to ask for the seconds, and int() to convert it to a number ☐ Use // and % to find hours, minutes, and seconds ☐ Store each result in a clearly named variable ☐ print() a clean, readable result (a sentence, not just three bare numbers) ☐ No if statements and no loops — you don’t need them