Hey guys, well here are some more of my questions (sorry!! i know, im a pain..) Ive finished "most" of everything in this tutorial. I missed some out because they were too much for
me to understand at the time. will go back to them and come back with more questions:D .

Anyways, could you explaint to me how some of these things work?

#'Inheritance'
class SchoolMember:
    '''Represents any school member.'''
    def __init__(self, name, age):
        self.name = name
        self.age = age
        print('(Initialized SchoolMember: {0})'.format(self.name))
 
    def tell(self):
        '''Tell my details.'''
        print('Name:"{0}" Age:"{1}"'.format(self.name, self.age), end=" ")
 
class Teacher(SchoolMember):
    '''Represents a teacher.'''
    def __init__(self, name, age, salary):
        SchoolMember.__init__(self, name, age)
        self.salary = salary
        print('(Initialized Teacher: {0})'.format(self.name))
 
    def tell(self):
        SchoolMember.tell(self)
        print('Salary: "{0:d}"'.format(self.salary))
 
class Student(SchoolMember):
    '''Represents a student.'''
    def __init__(self, name, age, marks):
        SchoolMember.__init__(self, name, age)
        self.marks = marks
        print('(Initialized Student: {0})'.format(self.name))
 
    def tell(self):
        SchoolMember.tell(self)
        print('Marks: "{0:d}"'.format(self.marks))
 
t = Teacher('Mr. P', 26, 80000)
s = Student('rs', 17, 99)
 
print() # prints a blank line
 
members = [t, s]
for member in members:
    member.tell() # works for both Teachers and Students

Q1. The last 3 lines on here i do not get.

def reverse(text):
    return text[::-1]
 
def is_palindrome(text):
    return text == reverse(text)
 
something = input('Enter text: ')
if (is_palindrome(something)):
    print("Yes, it is a palindrome")
else:
    print("No, it is not a palindrome")

Q2. How does "return text[::-1]" here work?

print(line, end='')

Q3. Ive seen (end=''' ) in many places but still do not know what it does

#'pickle'
 
import pickle
 shoplistfile = 'shoplist.data'

shoplist = ['apple', 'mango', 'carrot']
f = open(shoplistfile, 'wb')
pickle.dump(shoplist, f) 
f.close()
 
del shoplist 
 

f = open(shoplistfile, 'rb')
storedlist = pickle.load(f) 
print(storedlist)

Q4. Why isn't this working?

Q1: On those last three lines you're setting up a list of stuff (line 40). Then you're going into a loop that will perform the same action on each item in your list. The list that you created has a teacher and a student in it, so your for loop will perform the same action on your teacher and your student. On the last line, you're calling the tell function for the teacher and then the tell function for your student. You could have simply written t.tell() and then on the next line written s.tell(). However, what if you had a list of 1000 teachers and students? The way it is done on those last three lines is more scalable.

I'll take a little piece of it too:

Q3: 'end' is an argument of the Python2.6/3.0 print function. It tells the function what character to append to the output. I believe that by default it appends a newline character ('\n'), so end='' just tells it not to append anything.

Q2 is a slice construct I had not seen before. Apparently the full slice spec is [start : end : step] so [::-1] says I want the whole string stepping backwards.

You can slice any list or string...play with it, it is a fun syntax.

It can also be used to make a true copy of a list. Most list assignments do not make a copy, they copy a reference.

x = range(5) 
# x contains 0,1,2,3,4
y = x
y.append(7) 
# y and x both contain 0,1,2,3,4,7
# because they both refer to the same list
z = y[:]
z.append(10)
# y and x are unchanged because z doesn't refer to the same list
# z contains 0,1,2,3,4,7,10

Q2 for an expression like text[start: stop: step], if text is a string, it will return a string whith the characters text[start], text[start+step], text[start+2*step], etc, as long as start + k*step < stop if step is > 0 and start + k*step > stop if step < 0. When start is missing it defaults to 0 if step > 0 and len(text)-1 if step < 0, similarly, when stop is missing, it defaults to len(text) if step > 0 and -1 if step < 0.
In your case, both start and stop are missing, and step is < 0, so it will start from the end of the string and take all chars to the beginning of the string.
Someone please correct me if I made a mistake ;)

Someone please correct me if I made a mistake ;)

That hardly ever happens I'm sure. :)

And your description matches my empirical testing.

Thanks a lot guys, that tutorial site doesn't have proper explanations...:). One last question under this thread:D, where can i find a tutorial on 'slicing'? there seems to be a lot you can do with it apart from the basic.

-thanks again

This article has been dead for over six months. Start a new discussion instead.