lecture02

lecture02 - Lecture 2: September 29, 2010 String Theory...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Caltech CS 1: Fall 2010 Lecture 2 : September 29, 2010 String Theory
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 Overview of course Introduction to Python running programs types, expressions, operators variables and assignment functions using and defining with def
Background image of page 2
Caltech CS 1: Fall 2010 Strings a fundamental data type an example of an "object" very widely used in practice
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 In programming, many familiar words are used with unfamiliar meanings Example: string Doesn't mean this:
Background image of page 4
Caltech CS 1: Fall 2010 In programming, many familiar words are used with unfamiliar meanings Example: string Instead, it means a sequence of characters Examples: 'this is a string' "so is this"
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 Strings are one of the most-used data types Examples: DNA sequences: "ACCTGGAACT" Web pages Documents in word processors Computer source code! User interaction (string input/output) etc. Lots of useful functions predefined for strings
Background image of page 6
Caltech CS 1: Fall 2010 In Python, strings are just one example of sequences Other kinds of sequences exist e.g. lists , tuples Significance: the same functions and operators can usually be used with different sequences, and they mean the same kind of thing In other words: "learn one sequence, learn them all" (almost)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 In Python, there is no special data type for characters (letters, digits, etc.) unlike many other computer languages A character can only be represented as a string of length 1: 'a' # the character a (letter) '1' # the character 1 (digit) '_' # the underscore character '?' # question mark (symbol)
Background image of page 8
Caltech CS 1: Fall 2010 Python allows you to use either single or double quotes to represent a string: ' I am a string ' " So am I " You have to be consistent! " This is no good ' ' This is also no good " If you start the string with a single quote, you must end it with a single quote and similarly for double quotes
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 If you leave out the quotation marks, it isn't a string: >>> 'foobar' 'foobar' >>> foobar NameError: name 'foobar' is not defined Python interprets foobar as a variable name which you haven't defined, so error
Background image of page 10
Caltech CS 1: Fall 2010 One kind of quote can be inside another: " I can have ' single quotes ' inside " ' I can have " double quotes " inside '
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1: Fall 2010 However, you can't put the same kind of quote inside itself: s = 'this isn't going to work' Why not? s = 'this isn' t going to work' Type this into Python and you get: s = 'this isn't going to work' ^ Syntax error: invalid syntax
Background image of page 12
Caltech CS 1: Fall 2010 A "syntax error" means that you broke the rules of how the language is written In this case: s = 'this isn't going to work' is interpreted as: s = 'this isn' t going to work' The t going to work' part doesn't make sense to Python so it aborts with an error
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/22/2011 for the course CS 1 taught by Professor Pinkston,d during the Fall '08 term at Caltech.

Page1 / 78

lecture02 - Lecture 2: September 29, 2010 String Theory...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online