I am teaching a course "Introduction to Computer Programming" to the first year math students. One has to assume that this is the first exposure of students to computer programming. Here are the main goals of my teaching:
My method of teaching is to give for each newly introduced concept a series of problems and teasers that motivate students. For instance, when introducing strings and lists a natural question is the task of string or list reversal. If I ask students to write a code that will check whether a string is a palindrome then I better tell them how to reverse it.
For lists, a natural solution myString.reverse()
has at least two drawbacks:
The real question is: How should one introduce the problem of reversing a string in Python?
You could teach them about stride notation (::
) first and then slicing and apply both.
s = 'string'
s = s[::-1]
print s # gnirts
References and more information:
In response to your comment, you can supply either arguments.
>>> s[len(s):None:-1]
'gnirts'
>>> s[5:None:-1]
'gnirts'
>>> s[::-1] # and of course
'gnirts'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With