Python Programming · Week 2

Numbers and Calculation:
Practice

Hyeongmin Lee · SeoulTech, Dept. of Electronic Engineering

Setup

Before we start

The same steps as in week 1.

  1. 1 VS Code → open your python_course folder
  2. 2 New Folder → week02
  3. 3 Terminal → python3 → the three arrows

This video is all typing. Pause whenever you need to catch up.

Drill

At the prompt: type these

One line, Enter, read the answer. Then the next one.

>>> 7 / 2

>>> 6 / 2

>>> 7 // 2

>>> 7 % 2

>>> 2 ** 10

>>> 2 + 3 * 4

>>> (2 + 3) * 4

then look back

Which answers have a decimal point?

and why: the three rules from the theory video

Drill

Break three things

On purpose.

>>> 1 / 0

ZeroDivisionError loud

>>> 2 +

SyntaxError loud

>>> 2 ^ 10

8 silent

two loud errors: good news · one silent wrong answer: the worst case · read an error from its last line

Files

Now a file: calc.py

In your week02 folder. Run it with the Run button.

# Week 2 practice
# a line that starts with # is a note for humans
# Python skips it

3 * 7
print(3 * 7)
print(258 // 60)
print(258 % 60)

21

4

18

the bare 3 * 7 line shows nothing · the three print lines do

# = a note for humans · Python skips the line · use it to label your work

Example · 1

How many seconds are in one day?

In a new file: examples.py

print(24 * 60 * 60)

86400

Example · 2

A song is 347 seconds long. Minutes and seconds?

Two answers, so two print lines.

print(347 // 60)
print(347 % 60)

5

47

Example · 3

What is the last digit of 2 to the power of 100?

A thinking one.

The last digit of any number is the remainder after dividing by 10.

print(2 ** 100 % 10)

6

Exercise 1

The problems

All in one file: week02_exercise.py, on e-Class.

  1. 1 How many seconds are in one year? (365 days)
  2. 2 500 minutes: how many hours, and how many minutes? Use // and %.
  3. 3 A video is 9271 seconds long. Print hours, minutes, and seconds.
  4. 4 Three resistors in parallel: 150 Ω, 220 Ω, 330 Ω. The formula is in the file.
  5. 5 The tens digit of 7482, using // and %.
Exercise 1

How to submit

  1. 1 download week02_exercise.py from e-Class
  2. 2 put it in python_course/week02 · open it in VS Code
  3. 3 fill in the print lines · save · run it once
  4. 4 upload the same file on e-Class · the deadline is in the post
wrong answers: no points lostlate: points lostsolutions: only in classAI: allowed, but try alone first

Next steps

Now: exercise 1.

Before class. Everything you need is on e-Class.

See you in class.

← All decks
01 / 00
Scroll · ↓ · Space