Putting it together
- Python course
- Intermediate
- Lesson 116/116
- 6 min
Combine all layers into a working app.
Putting it together: solve with a class
In the final lesson you will combine everything into a Sayac class. This class holds word frequencies:
class Sayac:
def __init__(self):
self.veri = {}
def ekle(self, k):
self.veri[k] = self.veri.get(k, 0) + 1
s = Sayac()
s.ekle("elma")
print(s.veri)The starting code includes all previous solutions. You will define and use the Sayac class. This makes the code modular.
The project is complete!
Practice Define and use the Sayac class
Define class Sayac:. In __init__ write self.veri = {}. In def ekle(self, k): write self.veri[k] = self.veri.get(k, 0) + 1. Then write s = Sayac(), s.ekle("elma"), print(s.veri).
Starter code
# Word processing - word list
words = ["elma", "armut", "elma"]
print(len(words))
def say(words):
return len(set(words))
print(say(words))
def uret(words):
for w in words:
yield w
for w in uret(words):
print(w)
from io import StringIO
with StringIO("kiraz") as f:
print(f.read())
# define and use the Sayac classSample solution
words = ["elma", "armut", "elma"]
print(len(words))
def say(words):
return len(set(words))
print(say(words))
def uret(words):
for w in words:
yield w
for w in uret(words):
print(w)
from io import StringIO
with StringIO("kiraz") as f:
print(f.read())
class Sayac:
def __init__(self):
self.veri = {}
def ekle(self, k):
self.veri[k] = self.veri.get(k, 0) + 1
s = Sayac()
s.ekle("elma")
print(s.veri)Expected output
3
2
elma
armut
elma
kiraz
{'elma': 1}
Project complete
Congratulations! You completed the five-lesson intermediate final. You combined:
- List for storing data
- function for counting unique items
- generator for memory-friendly production
- with for safe file reading
- class for modular code
These five layers summarise intermediate Python. In the next level async, FastAPI and packaging will follow.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match the project assembly
| Term | Meaning |
|---|---|
class Sayac |
Class |
__init__ |
Constructor |
self.veri |
Dict |
ekle |
Method |
2 Complete the ekle method
def ekle(self, k):
self.veri[k] = self.veri.____(k, 0) + 1
def ekle(self, k):
self.veri[k] = self.veri.get(k, 0) + 1
Explanation: get(k, 0) returns 0 if the key is missing.
3 If s.ekle("elma") is called twice, what is s.veri["elma"]?
- 2 Correct answer
- 1
- 0
- Error
Explanation: Each ekle call increments the count; two calls give 2.