How to Think Like a Computer Scientist — How to Think Like a Computer Scientist: Learning with Python v2nd Edition documentation. Navigation How to Think Like a Computer Scientist¶ Learning with Python¶ 2nd Edition (Using Python 2.x) by Jeffrey Elkner, Allen B.
Downey, and Chris Meyers Last Updated: 21 April 2012 Copyright NoticeForewordPrefaceContributor ListChapter 1 The way of the programChapter 2 Variables, expressions, and statementsChapter 3 FunctionsChapter 4 ConditionalsChapter 5 Fruitful functionsChapter 6 IterationChapter 7 StringsChapter 8 Case Study: CatchChapter 9 ListsChapter 10 Modules and filesChapter 11 Recursion and exceptionsChapter 12 DictionariesChapter 13 Classes and objectsChapter 14 Classes and functionsChapter 15 Classes and methodsChapter 16 Sets of ObjectsChapter 17 InheritanceChapter 18 Linked ListsChapter 19 StacksChapter 20 QueuesChapter 21 TreesAppendix A DebuggingAppendix B GASPAppendix c Configuring Ubuntu for Python DevelopmentAppendix D Customizing and Contributing to the BookGNU Free Document License Search Page © Copyright 2010, Jeffrey Elkner, Allen B.
5. Built-in Types — Python v2.7.1 documentation. The following sections describe the standard types that are built into the interpreter.
Note Historically (until release 2.2), Python’s built-in types have differed from user-defined types because it was not possible to use the built-in types as the basis for object-oriented inheritance. This limitation no longer exists. The principal built-in types are numerics, sequences, mappings, files, classes, instances and exceptions. Some operations are supported by several object types; in particular, practically all objects can be compared, tested for truth value, and converted to a string (with the repr() function or the slightly different str() function). 5.1. Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. All other values are considered true — so objects of many types are always true. 5.2. These are the Boolean operations, ordered by ascending priority: Notes: 5.3. Comparison operations are supported by all objects. ! 4. More Control Flow Tools — Python v2.7.1 documentation. Besides the while statement just introduced, Python knows the usual control flow statements known from other languages, with some twists. 4.1. if Statements Perhaps the most well-known statement type is the if statement.
For example: >>> x = int(raw_input("Please enter an integer: "))Please enter an integer: 42>>> if x < 0:... x = 0... print 'Negative changed to zero'... elif x == 0:... print 'Zero'... elif x == 1:... print 'Single'... else:... print 'More'...More There can be zero or more elif parts, and the else part is optional. 4.2. for Statements The for statement in Python differs a bit from what you may be used to in C or Pascal. >>> # Measure some strings:... words = ['cat', 'window', 'defenestrate']>>> for w in words:... print w, len(w)...cat 3window 6defenestrate 12 If you need to modify the sequence you are iterating over while inside the loop (for example to duplicate selected items), it is recommended that you first make a copy. 4.3.
>>> range(10)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]