{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

module6-print - Module 6 Old wine in new wineskins...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
Note that "poodle" 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 ) 0 >> 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. We can also make functions that do not return values. 6.8 2
Image of page 2
Console input We also might want to have variables assigned values by the user typing on the keyboard. We never saw this in Scheme, but it’s easy in Python.
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern