This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Module 6 Old wine in new wineskins Elementary programming in Python CS 116: Introduction to Computer Science 2 Daniel G. Brown/Troy Vasiga, University of Waterloo 6.1 Purpose of Module 6 • Input/output in Python • Types of data in Python • Conditional statements and functions in Python • Readings: PfSD 3, 5, 6 6.2 Getting up to speed in a new language We will mostly talk about how to do a lot of the same things we’ve already done in Scheme, but in Python instead. • This is an important skill to have: you may have to program in a language you hadn’t officially been taught, sometime in the future. • Knowing how a couple of languages differ and are the same is very helpful for this. • Python is also much more like Scheme than most imperative languages, and should be easier to learn for you than they would. 6.3 1 Python output and input Output to the screen One feature we’ve not seen in Scheme: how to print text on the screen. • Traditionally the first thing people learn in a new language. • We’re never going to see it in Scheme. In Python, it is very simple: >> X = 324 >> print X 324 6.4 Printing is very easy This can apply to a bunch of other types of objects: >> X = "poodle" >> print X poodle 1 Note that "foo" is a string, and that when we print it, we don’t see the quotation marks. Also, note that we just mutated the value of X. It used to be 324, but now is a string. (There is nothing special about this in Python.) >> print "X" X Here, we printed a one-letter string; this is not the same as the variable X . 6.5 A couple of basic string facts We’ll use strings a lot in Python, so now is a good time to mention two things about them. • The empty string is the string with no characters. In Python, you can write it as "" . • The function len computes the length of a string. (Actually, we’ll see that you can use len on all kinds of things, like lists, as well.) >> X = "" >> len ( X ) >> len ( "Python" ) 6 Also, note that print is not a function returning a value when it displays text. The displayed text is a side-effect of calling the keyword print . 6.6 Printing other kinds of values >> X = 32.4 >> print X 32.4 We can print multiple values at once, by separating them with a comma. >> Y = 3 >> print X , Y 32.4 3 And we can print values of expressions: >> print X * Y , Y 97.2 3 6.7 Not all statements compute values One big difference between Python and Scheme: • Not every statement evaluates to a value! This is typical for imperative languages. • Some statements make a value, and others don’t. We can’t, for example, write: >> X = print X or >> print ( X = 4) Neither of those work. • Neither assignment nor print makes a value....
View Full Document
This note was uploaded on 01/06/2011 for the course CS 116 taught by Professor T.vasiga during the Winter '09 term at Waterloo.
- Winter '09
- Computer Science