108HL10 - Sticky Points Lecture 10, Week 4 January 31, 2011...

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

View Full Document Right Arrow Icon
Sticky Points Lecture 10, Week 4 January 31, 2011 CSC108H1S Velian Pandeliev 1 Monday, January 31, 2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
Announcements 2 Solutions and rubric to quiz are posted on Blackboard. CodeLabs and labs happen every week! NEW: If you have to go to someone else's lab on any given week, give the TA leading the lab your reason. You no longer need to e-mail me. Monday, January 31, 2011
Background image of page 2
3 Recap of Lecture 9 Monday, January 31, 2011
Background image of page 3

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

View Full DocumentRight Arrow Icon
Namespaces 4 At the lowest level of every Python program there's a built-in namespace, which automatically contains the names of all available built-in functions. When the program starts, a global namespace is created to keep track of global variables. Finally, a new local namespace is created every time a function body is executed. It contains only variables local to that function (such as parameters). Local namespaces are destroyed when the function body exits. Since function bodies can contain other function de±nitions, namespaces can contain other nested local namespaces. Monday, January 31, 2011
Background image of page 4
Consequences 5 A few things to keep in mind: 1) If you want to hold on to a local variable's value, you have to make the function return it 2) A local namespace cannot change the values of more global variables 3) A variable in a local namespace will 'hide' variables of the same name in more global namespaces Monday, January 31, 2011
Background image of page 5

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

View Full DocumentRight Arrow Icon
String Formatting 6 Python has a way to specify where in a string you'd like a value to appear, and in what format. If x is an integer, instead of using: print "You have", x, "beers" We can use: print "You have %d beers" % x %d means "Insert the value of the variable I give you here, and format it as an integer" This is another use of the % operator. Monday, January 31, 2011
Background image of page 6
Format Placeholders 7 %d - displays the value as an integer %f - displays the value as a foating-point decimal %f.2 - displays the value as a foating-point decimal accurate (and padded) to 2 decimal places %s - displays the value as a string oF characters %2d - displays the value as an integer padded to 2 spaces (i.e. '_4' instead oF '4' ) Monday, January 31, 2011
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 To specify multiple variables with placeholders, you have to separate them with commas and enclose them in parentheses after the % : dollars = 4 cents = 35 print "You have %d dollars and %d cents" % (dollars, cents) The value of variable dollars will replace the Frst %d , while the value of cents will replace the second. Monday, January 31, 2011
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/25/2011 for the course CSC 108 taught by Professor Gries during the Spring '08 term at University of Toronto- Toronto.

Page1 / 30

108HL10 - Sticky Points Lecture 10, Week 4 January 31, 2011...

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

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