• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Learning Python - Week 1
 

Learning Python - Week 1

on

  • 2,892 views

Based on Zed Shaw's "Learn Python the Hard Way," this is a review of Exercises 1 - 12 in that text. For non-computer-science students and learners. Updated with new slides Jan. 12, 2014. Introduces ...

Based on Zed Shaw's "Learn Python the Hard Way," this is a review of Exercises 1 - 12 in that text. For non-computer-science students and learners. Updated with new slides Jan. 12, 2014. Introduces math, print statement, variables, format strings, raw_input().

Statistics

Views

Total Views
2,892
Views on SlideShare
1,675
Embed Views
1,217

Actions

Likes
3
Downloads
24
Comments
0

8 Embeds 1,217

http://babydatajournalism.tumblr.com 1160
http://lifeandcode.tumblr.com 34
http://feedly.com 14
http://www.newsblur.com 4
https://twitter.com 2
http://translate.googleusercontent.com 1
http://inoreader.com 1
http://localhost 1
More...

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • SOURCE http://learnpythonthehardway.org/book/
  • ANSWER: b and d Quotation marks. NOTE: c is okay because it includes an escape character: \’ (It is DIFFERENT from b for that reason.)
  • ANSWER: b C has part of the line commented out. In b, the complete line is commented out.
  • Related to making comments. The second example will print two blank lines. The top example prints NO blank lines.
  • ANSWER: c Because addition would not be done first, otherwise. Only in c will the addition be done first.
  • PEMDAS is a mnemonic, but it doesn’t include modulo! (not explicitly)
  • See table at bottom - http://en.wikipedia.org/wiki/Order_of_operationsPython is NOT the only language that follows PEMDAS.
  • ANSWER: False (with a capital F)
  • ANSWER: 0 (see next slide)
  • ANSWER: 0.25 Floating point.
  • Floats vs. integers. Floats have decimals and integers do not.
  • CODE EXAMPLE. Floats and integers. TRY THIS YOURSELF.
  • ANSWER: 0, because there is no remainder for any of them Modulus.
  • “In computing, the modulo (sometimes called modulus) operation finds the remainder of division of one number by another.” http://en.wikipedia.org/wiki/Modulo_operation
  • ANSWER: 5 (the remainder) But see - http://forums.udacity.com/questions/1001665/python-help-with-modulus Example: - 7 % 5 -> 3 but 7 % 5 -> 2
  • (Exercise 4 LPTHW) Variables consist of two things, a name and a value.
  • I think of variables as containers. They can hold all kinds of values. You can name them almost anything.
  • The name is used in our code to represent the contents – but the contents (the value) can change.
  • CODE EXAMPLE. Changing the value of some variables.
  • Again: Variables consist of two things, a name and a value.
  • Thevalue. can change, while the name stays the same.
  • Thevalue. can change, while the name stays the same.
  • Thevalue. can change, while the name stays the same.
  • CODE EXAMPLE. Any questions? TRY THIS YOURSELF.
  • Also called formatters (“short abbreviated variable names”) – Zed introduces these in exercise 5.
  • CODE EXAMPLE. Each one has different abilities. Play with these yourself until you understand them.
  • CODE EXAMPLE. Best example I have seen of how %r produces a different result from %s or %d or %f.
  • http://docs.python.org/2/library/stdtypes.html#string-formattingIntegers are negative or positive numbers without decimals or fractions.
  • Playing with code at the command line – and writing lines in your program for which you don’t know how they will come out – this is a big part of how we learn.
  • CODE EXAMPLE. Playing with escape characters: \n and \t
  • Same thing, just with color added. Note that each of these four variables contains a string. Therefore, the format character is %s for each one.
  • Check out how the ”triple double-quotes” and the ”triple single-quotes” work. TRY THEM OUT.
  • CODE EXAMPLE. Notice that the \n added an extra line for each line – the way it was typed, each line would have broken at the end anyway.
  • LPTHW Exercise 10 – memorize\n and \\ and \’ -- it’s enough.
  • CODE EXAMPLE. Demonstrating \n and \\ and \"
  • Zed’s exercises 11 and 12 introduce this.
  • CODE EXAMPLE. LPTHW Exercises 11 and 12 – use of raw_input () to accept typing from the user while the program is running.
  • Mindy McAdams - CONTACT – http://mindymcadams.com/

Learning Python - Week 1 Learning Python - Week 1 Presentation Transcript

  • Learn Python the Hard Way Exercises 1 – 12 http://learnpythonthehardway.org/
  • Exercise 1: Quotation marks Which of these will throw an error? a) b) c) d) print "They wouldn't do that." print 'They wouldn't do that.' print 'They wouldn't do that.' print "They wouldn't do that.'
  • Exercise 2: Comments Which line is commented out? a) print "Fourscore and seven years" b) # print "Fourscore" c) print "Eighty years" # this is fourscore years
  • Which one will print blank lines? 1) print "Fourscore and seven years ago" # # print "Our fathers brought forth" 2) print "Fourscore and seven years ago" print print print "Our fathers brought forth"
  • Exercise 3: Math Which one of these would give a different answer than the others? a) b) c) d) e) 4+6/3+5 4 + (6 / 3) + 5 (4 + 6) / 3 + 5 (4 + 6 / 3) + 5 4 + (6 / 3 + 5)
  • PEMDAS • • • • • • P E M D A S () exponents, e.g. 10**2 * / + – But …
  • PEMDAS is not the whole story •P () • E exponents, e.g. 10**2 • MD * / % • AS + – See http://en.wikipedia.org/wiki/Order_of_operations
  • Exercise 4: Math What is the answer Python will give to all of these? a) b) c) d) 5 > 10 10 < 2 1444 < 1443 1>2
  • Exercise 4: Math True or False: < > <= >= == != More about True and False to come, in Zed’s exercise 27. (Note: In Python, these values always start with an uppercase letter.)
  • Exercise 5: Integers and floats What is the answer Python will give to this? 1/4
  • Exercise 5: Integers and floats What is the answer Python will give to this? 1 / 4.0
  • Learn this! >>> 1 / 4 0 >>> >>> 1 / 4.0 0.25 >>> integer float
  • Exercise 6: Modulus What is the answer Python will give to all of these? a) b) c) d) 4%2 10 % 2 144 % 12 100 % 25
  • Modulo The modulus operand is commonly used to find out if a number is odd or even. % Don’t get confused: In Python, the percent sign (yes, the same character, but used differently) is also used for format strings, as seen first in Exercise 5. (You’ll be seeing even more of that!)
  • Modulo (also modulus) What will this return? (That is, what answer will Python give?) 115 % 11
  • Variables
  • Variables Name apple Value
  • Variables
  • Variables
  • Variables Name Value
  • Variables Name Value
  • Variables Name Value 57
  • Variables Name Value
  • Any questions?
  • “Format strings” • %s %d %r %f • Each one is slightly different • They are a kind of shorthand for working with variables in Python • NOTE! These are NOT variables! • Zed also calls these “format characters” • NOTE! This is NOT modulus!
  • %r %s %d Not all the same.
  • Notice how %r in this case returns something very different from %s
  • String formatting continued • • • • %s string: use this for text %d use this for integers (no decimal places) %f float: shows up to 6 decimal places %r representation: works for numbers and strings, but (usually) adds quotation marks * * Zed says, “The %r is best for debugging.” (But you don’t really know what debugging is.)
  • The value of “play” (an essential part of learning to code)
  • n newline (line break) t tab (indent) The backslash is “the escape character.”
  • Putting things together a = "Mary had a little lamb." b = "Its fleece was white as snow." c = "And everywhere that Mary went" d = "The lamb was sure to go." print "nn%snt%sn%snt%snn" % (a, b, c, d) What would happen if the order were changed to: (d, c, b, a)
  • Running that program …
  • Escapes Zed says memorize all of these. I don’t think that’s necessary. But do memorize this: The backslash is the escape character.
  • Escapes!
  • raw_input( ) Zed’s exercises 11 and 12 introduce this.
  • raw_input( )
  • Learn Python the Hard Way Exercises 1 – 12 (we’re just getting started)