Both of these are introductory texts in computer science:
Introduction to Computing: Explorations in Language, Logic, and Machines, by David Evans, associate professor of computer science, University of Virginia (this book uses Python only in two chapters at the end)
How to Think Like a Computer Scientist, by Jeffrey Elkner, Allen B. Downey, and Chris Meyers (this book uses Python throughout; it’s online only, no PDFs)
Allen [Downey] had already written a first-year computer science textbook, How to Think Like a Computer Scientist. When I read this book, I knew immediately that I wanted to use it in my class. It was the clearest and most helpful computer science text I had seen. It emphasized the processes of thought involved in programming rather than the features of a particular language. Reading it immediately made me a better teacher.
How to Think Like a Computer Scientist was not just an excellent book, but it had been released under the GNU public license, which meant it could be used freely and modified to meet the needs of its user. Once I decided to use Python, it occurred to me that I could translate Allen’s original Java version of the book into the new language.
— Jeffrey Elkner, from the Preface, How to Think Like a Computer Scientist
Elkner is a high school math and computer science teacher in the Arlington County, Virginia, public schools.
Here’s what I learned today:
>>> def checkpage(a): ... if a: ... print 'ok' ... >>> page = 'tulip' >>> checkpage(page) ok >>> page = True >>> checkpage(page) ok >>> print page True >>> page = False >>> checkpage(page) >>> page = None >>> checkpage(page) >>> page = True >>> checkpage(page) ok >>> page = '' >>> checkpage(page) >>>
So, in addition to False, the value None and the empty string ( ” ) also evaluate as false. A while loop, therefore, can begin with
See: Built-in Types at python.org.
— David Evans, instructor
Udacity CS101 course, Unit 2, segment 21
(And that’s what makes programming so cool !)
A good reference: Python slice notation
Interesting: All of these return your full, complete string (s):
s[:3] + s[3:] s[:1] + s[1:] s[:2] + s[2:]
It’s a kind of a mirror effect, asking for everything up until x — s[:x] — and then concatenating with everything after x — s[x:]. This works even if the length of s is less than 3.
Unnecessary, but it shows a little more how s[:] (slicing a string) works in Python.