Python Programming · Week 7
Hyeongmin Lee · SeoulTech, Dept. of Electronic Engineering
A new folder, and the prompt for a drill.
On paper: what comes out? Then type each line at the prompt.
a string
>>>word = "voltage"
>>>word[1]
>>>word[-1]
>>>word[:4]
>>>word[4:]
>>>word[::-1]
a list
>>>nums = [3, 1, 4, 1, 5, 9]
>>>nums[2] + nums[-2]
>>>nums[1:4]
>>>nums[::2]
>>>nums[len(nums)]
digits in a string
>>>year = "2026"
>>>year[0] + year[-1]
>>>int(year[0]) + int(year[-1])
>>>"0" in year
In your week07 folder: the program from the theory video.
n = int(input("How many scores? "))
scores = []
for i in range(1, n + 1):
score = int(input(f"Score {i}: "))
scores.append(score)
average = sum(scores) / len(scores)
above = 0
for score in scores:
if score > average:
above += 1
print(f"Scores: {sorted(scores)}")
print(f"Average: {average}")
print(f"Highest: {max(scores)}, lowest: {min(scores)}")
print(f"Above the average: {above}") How many scores? 4
Score 1: 85
Score 2: 72
Score 3: 93
Score 4: 60
Scores: [60, 72, 85, 93]
Average: 77.5
Highest: 93, lowest: 60
Above the average: 2
1n = int(input("How many scores? "))
2scores = []
3for i in range(1, n + 1):
4 score = int(input(f"Score {i}: "))
5 scores.append(score)
6
7average = sum(scores) / len(scores)
⋮
scores in VARIABLES
one append at a time
[]
[85]
[85, 72]
[85, 72, 93]
[85, 72, 93, 60]
opened with ▸
In stats.py, on purpose. After each one, undo it: ⌘Z (Windows: Ctrl+Z).
the append line
delete it
ZeroDivisionError: division by zero
the list stays empty: len(scores) is 0
the append line
store what it returns: scores = scores.append(score)
AttributeError: 'NoneType' object has no attribute 'append'
append returns None, so now scores is None
the Scores line
change sorted(scores) to scores.sort()
Scores: None
Average: 77.5
Highest: 93, lowest: 60
Above the average: 2
.sort() sorts the list and returns None
append, .sort(): they change the list · and return None
Ask for a word. Print how many vowels it has: a, e, i, o, u.
for ch in word: one character per iteration · ch in "aeiou": True or False
on the screen
Word: banana
Vowels: 3
Word: Education
Vowels: 5
the code
word = input("Word: ").lower()
count = 0
for ch in word:
if ch in "aeiou":
count += 1
print(f"Vowels: {count}") Ask for an integer, 0 or more. Print the sum of its digits.
week 5 used % 10 and // 10 · now: the typed string, one character at a time
on the screen
Number: 2026
Digit sum: 10
Number: 99999
Digit sum: 45
the code
number = input("Number: ")
total = 0
for digit in number:
total += int(digit)
print(f"Digit sum: {total}") week 5
n = int(input("Number: "))
total = 0
while n > 0:
total += n % 10
n = n // 10
print(f"Digit sum: {total}")
Ask for a sentence. Print the average length of its words, then each word longer than the average, with its position.
on the screen · one run
Sentence: Python is easy to learn
Average length: 3.8
Word 1: Python
Word 3: easy
Word 5: learn
the sentence after the prompt is typed · the prompt word is up to you
steps, in words
examples · typed values in order
typed
prints
Python is easy to learn
Average length: 3.8, then words 1, 3, 5
Ohm's law is simple
Average length: 4.0, then words 1, 4
Hello world
Average length: 5.0 only
under the comments: one block per step
# Write your program here.
sentence = input("Sentence: ")
words = sentence.split()
total = 0
for word in words:
total += len(word)
average = total / len(words)
print(f"Average length: {average}")
for i, word in enumerate(words, start=1):
if len(word) > average:
print(f"Word {i}: {word}")
on the screen · one run
Sentence: Python is easy to learn
Average length: 3.8
Word 1: Python
Word 3: easy
Word 5: learn
A palindrome reads the same forwards and backwards, such as level. Ask for a word in lowercase letters. If it is a palindrome, print Palindrome. If not, print Not a palindrome.
on the screen · one run
Word: level
Palindrome.
the value after the prompt is typed · the prompt word is up to you
examples · typed values in order
typed
prints
level
Palindrome.
python
Not a palindrome.
racecar
Palindrome.
noun
Not a palindrome.
a
Palindrome.
Ask for a binary number (only 0s and 1s), and print its value as a decimal number. For example, 1011 is 8 + 0 + 2 + 1 = 11.
on the screen · one run
Binary: 1011
Decimal: 11
the value after the prompt is typed · the prompt word is up to you
Hint: go through the digits from the right (a slice can reverse a string); their place values are 1, 2, 4, 8, …
examples · typed values in order
typed
prints
1011
Decimal: 11
11111111
Decimal: 255
100000
Decimal: 32
0
Decimal: 0
1
Decimal: 1
A sensor is read several times. Ask how many readings there are, then each reading. Print the average, then each reading that is more than 10% off, with its position: more than 10% off means its difference from the average is more than 10% of the average.
on the screen · one run
How many readings? 4
Reading 1: 5.1
Reading 2: 6.0
Reading 3: 4.9
Reading 4: 4.0
Average: 5.0
Reading 2: 6.0 is more than 10% off.
Reading 4: 4.0 is more than 10% off.
the values after the prompts are typed · the prompt words are up to you
steps, in words
examples · typed values in order
typed
prints
4, 5.1, 6.0, 4.9, 4.0
Average: 5.0, then readings 2 and 4
3, 4.8, 5.0, 5.2
Average: 5.0 only
1, 7.5
Average: 7.5 only
5, 12.0, 9.8, 10.2, 10.1, 7.9
Average: 10.0, then readings 1 and 5
Next steps
Before class. Everything you need is on e-Class.
See you in class.