The material from the exercise session: You can contact me via my email address: siavashs at csc dot kth.se, you can see my callendar in my homepage.
We discussed the phonebook program. You can get my code from here.
You can download a very short sample file of your phonebook here.
You can download a very short sample file of your friend's phonebook here.
Your questions and comments are very welcome, always.
We discussed the class definition and usage in python. I upload some programs here which are taken from some other courses I teach. I hope they are useful. Comments and feedbacks are very welcome and can be sent to may email: siavashs at csc dot kth.se
Temperature can be set at different scales. Define the class temp with the methods Setka, setC, setF and getK, getca, getF. Then use the class in a program that reads the outside temperature (Celsius) and prints out the temperature so that an American understands (Fahrenheit).
The file temp.py:
# -*- Coding: Latin-1 -*-
"" "Abstract data type for temperature" ""
nollC = 273.15
nollF = 255.3666666 # F-zero in the Kelvin
class Temp:
def __init__ (self):
self.K = 0 # Temperature in Kelvin
def Setka (self, C):
self.K = K
def setC (self, C):
self.K = nollC + C
def setF (self, F):
self.K = nollF +5 * F / 9
def getK (self):
Return self.K
def getca(self):
Return self.K-nollC
def getF (self):
Return (self.K-nollF) * 9 / 5
# usage
D31_temp = Temp()
D31_temp.setC(23)
print(D31_temp.getca())
D41_temp = Temp()
D41_temp.setC(21)
print(D41_temp.getca())
You can find some nice examples here
I got a question today during the class but I forgot to answer it. The question was about "object" which comes in parentheses in front of the class definition. Object is the base class in python (just like Java). The name that comes in parentheses in front of the class definition (in this particular case "object") is the name of the class which the definition inherits from. If you dont understand this now, dont worry inheritance is not part of this course, just skip this remark.