lecture03

lecture03 - Caltech CS 1 Fall 2010 Lecture 3 October 4 2010...

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Caltech CS 1: Fall 2010 Lecture 3 : October 4, 2010 Objects and Modules (Libraries) Caltech CS 1: Fall 2010 Strings String operators String formatting Caltech CS 1: Fall 2010 User input • The raw_input() function Objects • Method syntax (dot syntax) • String methods Modules • Importing modules • The help() function • Defining modules • Some useful modules Caltech CS 1: Fall 2010 raw_input() returns a single line read from the keyboard: >>> line = raw_input() I'm a lumberjack and I'm OK. >>> line "I'm a lumberjack and I'm OK." Question: Why does Python print this with double quotes? raw_input() also removes the end-of-line ( \n ) character from the line read in • which you had to type to finish the line [you type this] Caltech CS 1: Fall 2010 raw_input() returns a single line read from the keyboard: >>> line = raw_input() I'm a lumberjack and I'm OK. >>> line "I'm a lumberjack and I'm OK." Question: Why does Python print this with double quotes? raw_input() also removes the end-of-line ( \n ) character from the line read in • which you had to type to finish the line [you type this] Caltech CS 1: Fall 2010 raw_input can take a string argument, which is used as a prompt: >>> SAT = raw_input("Enter your SAT score: ") Enter your SAT score: 2500 >>> SAT '2500' Note that SAT is still a string! raw_ in raw_input means that the input value is just represented as a string [you type this] Caltech CS 1: Fall 2010 To convert to an int , use the int conversion function: >>> SATs = raw_input("Enter your SAT score: ") Enter your SAT score: 2500 >>> SAT = int (SATs) >>> SAT 2500 Usually, this would be written more simply as: >>> SAT = int (raw_input("Enter your SAT score: ")) Other conversion functions exist: float , str , etc. • convert from some data item to a particular type • an error occurs if conversion can't be done Caltech CS 1: Fall 2010 Python is what's known as an "object-oriented" programming language • What that means will take many lectures to completely describe One aspect is that most nontrivial data types are represented as "objects" An "object" is some data + some associated functions that work on that data Python strings are an example of a Python object Caltech CS 1: Fall 2010 Functions that are associated with an object are referred to as methods • they're like functions, but the syntax is different (and there are other differences which we'll learn later) Example method: the upper method returns an upper-cased version of a string >>> 'spam'.upper() 'SPAM' Caltech CS 1: Fall 2010 Functions that are associated with an object are referred to as methods • they're like functions, but the syntax is different (and some other differences which we'll learn later) Example method: the upper method converts a particular string to upper case >>> 'spam'.upper() 'SPAM' Caltech CS 1: Fall 2010 'spam' . upper() Caltech CS 1: Fall 2010 'spam' .upper() the object (being acted upon) Caltech CS 1: Fall 2010 'spam'. upper () name of the method Caltech CS 1: Fall 2010...
View Full Document

{[ snackBarMessage ]}

Page1 / 76

lecture03 - Caltech CS 1 Fall 2010 Lecture 3 October 4 2010...

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

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