Chapter 1

1,561 views
1,485 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,561
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
73
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter 1

  1. 1. Guide to Programming with Python Chapter One Getting Started: The Game Over Program
  2. 2. Objectives <ul><li>Introduce Python </li></ul><ul><li>Demonstrate how to install Python </li></ul><ul><li>Explain how to print text to the screen </li></ul><ul><li>Describe comments and how to use them </li></ul><ul><li>Demonstrate Python’s development environment, IDLE, using it to write, edit, run, and save programs </li></ul>Guide to Programming with Python
  3. 3. Examining the Game Over Program <ul><li>Figure 1.1: Game Over Program Output </li></ul><ul><li>The all-too familiar words from a computer game </li></ul>Guide to Programming with Python
  4. 4. Examining the Game Over Program (continued) <ul><li>“ Hello World” program: By tradition, prints &quot;Hello, world!” </li></ul><ul><ul><li>Often used as first program </li></ul></ul><ul><li>Console window: Provides a text-based interface to Windows operating system </li></ul><ul><li>Terminal application : Provides a text-based interface to Mac OS X and Linux operating systems </li></ul>Guide to Programming with Python
  5. 5. Introducing Python <ul><li>Powerful yet easy to use programming language </li></ul><ul><li>Developed by Guido van Rossum </li></ul><ul><li>First released in 1991 </li></ul><ul><li>Named after comedy troupe Monty Python </li></ul><ul><li>An alarming number of references to spam, eggs, and the number 42 in documentation </li></ul>Guide to Programming with Python
  6. 6. Python Is Easy to Use <ul><li>High-level language : Separate from the low-level processor operations; closer to human language than machine language </li></ul><ul><li>&quot;Programming at the speed of thought&quot; </li></ul><ul><li>Increases productivity </li></ul><ul><ul><li>Python programs three to five times shorter than Java </li></ul></ul><ul><ul><li>Python programs five to ten times shorter than C++ </li></ul></ul>Guide to Programming with Python
  7. 7. Python Is Easy to Use (continued) <ul><li>Python Program </li></ul><ul><ul><li>print &quot;Game Over!&quot; </li></ul></ul><ul><li>C++ Program </li></ul><ul><ul><li>#include <iostream> </li></ul></ul><ul><ul><li>int main() </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>std::cout << &quot;Game Over!&quot; << std::endl; </li></ul></ul><ul><ul><li>return 0; </li></ul></ul><ul><ul><li>} </li></ul></ul>Guide to Programming with Python
  8. 8. Python Is Powerful <ul><li>Used by large organizations </li></ul><ul><ul><li>NASA </li></ul></ul><ul><ul><li>Google </li></ul></ul><ul><ul><li>Microsoft </li></ul></ul><ul><li>Used in published games </li></ul><ul><ul><li>Battlefield 2 </li></ul></ul><ul><ul><li>Civilization IV </li></ul></ul><ul><ul><li>Disney’s Toontown Online </li></ul></ul>Guide to Programming with Python
  9. 9. Python Is Object-Oriented <ul><li>Object-oriented programming (OOP): Methodology that defines problems in terms of objects that send messages to each other </li></ul><ul><ul><li>In a game, a Missile object could send a Ship object a message to Explode </li></ul></ul><ul><li>OOP not required, unlike Java and C# </li></ul>Guide to Programming with Python
  10. 10. Python Is a “Glue” Language <ul><li>Can be integrated with other languages </li></ul><ul><ul><li>C/C++ </li></ul></ul><ul><ul><li>Java </li></ul></ul><ul><li>Use existing code </li></ul><ul><li>Leverage strengths of other languages </li></ul><ul><ul><li>Extra speed that C or C++ offers </li></ul></ul>Guide to Programming with Python
  11. 11. Python Runs Everywhere <ul><li>Platform independent: Independent of the specific computer operating system </li></ul><ul><li>Python runs on </li></ul><ul><ul><li>Windows </li></ul></ul><ul><ul><li>DOS </li></ul></ul><ul><ul><li>Mac OS </li></ul></ul><ul><ul><li>Linux </li></ul></ul><ul><ul><li>Many more </li></ul></ul>Guide to Programming with Python
  12. 12. Python Has a Strong Community <ul><li>As an approachable language, has approachable community </li></ul><ul><li>Python Tutor mailing list </li></ul><ul><ul><li>http://mail.python.org/mailman/listinfo/tutor </li></ul></ul><ul><ul><li>Perfect for beginners </li></ul></ul><ul><ul><li>No actual &quot;tutors&quot; or &quot;students&quot; </li></ul></ul>Guide to Programming with Python
  13. 13. Python Is Free and Open Source <ul><li>Open source : Publicly available; open source software typically programmed by volunteers; anyone can use source code without fee </li></ul><ul><li>Can modify or even resell Python </li></ul><ul><li>Embracing open-source ideals is part of what makes Python successful </li></ul>Guide to Programming with Python
  14. 14. Setting up Python on Windows <ul><li>The book suggests using the CD-ROM it came with, but that version of Python (2.3.5) is old now </li></ul><ul><li>Instead, follow the instructions on the class web page: </li></ul><ul><ul><li>Go to http://www.python.org/download/ </li></ul></ul><ul><ul><li>Download the latest “standard” (aka “production”) release installer (.msi file) </li></ul></ul><ul><ul><li>Double-click the installer program and follow its instructions to install Python on your boot (C:) drive </li></ul></ul>Guide to Programming with Python
  15. 15. Setting up Python on Windows Guide to Programming with Python Figure 1.2: Python 2.3.5 Installation Dialogue under Windows Your computer is soon to be home to Python.
  16. 16. Setting up Python on Other Operating Systems <ul><li>Linux </li></ul><ul><ul><li>Python probably already installed </li></ul></ul><ul><ul><li>Test: try running python at command prompt </li></ul></ul><ul><ul><li>If not installed, go to http://www.python.org/download/ (you will probably need to build from source) </li></ul></ul><ul><li>Mac OS 10.5.x </li></ul><ul><ul><li>Leopard (10.5.x) already has Python 2.5.1 installed, but you need to install IDLE.app following instructions at http://wiki.python.org/moin/MacPython/Leopard </li></ul></ul><ul><li>Earlier Mac OS X and other systems </li></ul><ul><ul><li>If necessary, download appropriate version from Python web site at http://www.python.org/download/ </li></ul></ul>Guide to Programming with Python
  17. 17. Introducing IDLE <ul><li>Integrated Development Environment (IDE) : Application that helps software developers write programs </li></ul><ul><ul><li>Like a word processor for your code </li></ul></ul><ul><li>IDE that ships with Python </li></ul><ul><li>Has two “modes”: Interactive and Script </li></ul>Guide to Programming with Python
  18. 18. Programming in Interactive Mode Guide to Programming with Python Figure 1.4: Python in interactive mode Python awaits your command.
  19. 19. Programming in Interactive Mode (continued) <ul><li>Great for immediate feedback </li></ul><ul><ul><li>Test a simple idea </li></ul></ul><ul><ul><li>Remember how something works </li></ul></ul><ul><li>Open Python in interactive mode </li></ul><ul><ul><li>In Windows, from the Start menu, choose Programs, Python <version>, IDLE (Python GUI) </li></ul></ul><ul><li>On STC Lab machines </li></ul><ul><ul><li>Windows: Will be in Start menu > All Programs > Departmentally Sponsored > Informatics </li></ul></ul><ul><ul><li>Mac: Type python in /Applications/Utilities/Terminal.app or run IDLE.app from the Developer Tools folder in the Dock </li></ul></ul>Guide to Programming with Python
  20. 20. Programming in Interactive Mode (continued) <ul><li>At command prompt ( >>> ), type: print &quot;Game Over&quot; </li></ul><ul><li>Python responds with: Game Over </li></ul>Guide to Programming with Python
  21. 21. Programming in Interactive Mode (continued) <ul><li>print Statement can display a string (actually, any expression ) </li></ul><ul><li>String : Sequence of characters </li></ul><ul><li>Statement : Single unit in programming language that performs some action </li></ul><ul><ul><li>print &quot;Game Over&quot; </li></ul></ul><ul><li>Expression : Something which has a value or that can be evaluated to a single value </li></ul><ul><ul><li>&quot;Game Over&quot; </li></ul></ul><ul><ul><li>7 + 2 </li></ul></ul><ul><li>Code : Sequence of programming statements </li></ul>Guide to Programming with Python
  22. 22. Programming in Interactive Mode (continued) <ul><li>Syntax highlighting : Displaying programming code in different colors or fonts, according to the category of each item </li></ul><ul><li>Errors </li></ul><ul><ul><li>Computers take everything literally </li></ul></ul><ul><ul><li>pri m t &quot;Game Over&quot; produces an Error Message: SyntaxError: invalid syntax </li></ul></ul><ul><ul><li>Syntax error : Error in the rules of usage; often a typo </li></ul></ul><ul><ul><li>Bug : Error in programming code </li></ul></ul>Guide to Programming with Python
  23. 23. Guide to Programming with Python
  24. 24. Programming in Script Mode Guide to Programming with Python Figure 1.5: Python in script mode Your blank canvas awaits.
  25. 25. Programming in Script Mode (continued) <ul><li>Great for programs you want to run later </li></ul><ul><ul><li>Write, edit, save, and load programs </li></ul></ul><ul><ul><li>Like word processor for your programs </li></ul></ul><ul><ul><ul><li>Find and replace </li></ul></ul></ul><ul><ul><ul><li>Cut and paste </li></ul></ul></ul><ul><li>Open a script window </li></ul><ul><ul><li>In interactive window, select File menu, New Window </li></ul></ul>Guide to Programming with Python
  26. 26. Programming in Script Mode (continued) <ul><li>Write program </li></ul><ul><ul><li>In script window, type print &quot;Game Over&quot; </li></ul></ul><ul><li>Save program </li></ul><ul><ul><li>Select File, Save As, name game_over.py </li></ul></ul><ul><ul><li>Always save before running </li></ul></ul><ul><li>Run Program </li></ul><ul><ul><li>Select Run, Run Module </li></ul></ul><ul><ul><li>Results displayed in interactive window </li></ul></ul>Guide to Programming with Python
  27. 27. Programming in Script Mode (continued) <ul><li>Figure 1.6: Python after a script has been run </li></ul><ul><li>The results of running the Game Over program </li></ul>Guide to Programming with Python
  28. 28. The Game Over Program <ul><li># Game Over </li></ul><ul><li># Demonstrates the print command </li></ul><ul><li>print &quot;Game Over&quot; </li></ul><ul><li>raw_input(&quot; Press the enter key to exit.&quot;) </li></ul>Guide to Programming with Python
  29. 29. The Game Over Program (continued) <ul><li>Comment : Note in source code meant only for programmers; ignored by computer </li></ul><ul><ul><li>Start comment with # </li></ul></ul><ul><ul><li>Use opening block of comments </li></ul></ul><ul><li>Blank Lines </li></ul><ul><ul><li>Also (generally) ignored by computer </li></ul></ul><ul><ul><li>Use for readability; keep related code together </li></ul></ul><ul><li>Console Window </li></ul><ul><ul><li>Final line keeps console window open </li></ul></ul>Guide to Programming with Python
  30. 30. Summary <ul><li>Python is a high-level, object-oriented programming language that’s powerful yet easy to use </li></ul><ul><li>Python can interface with other programming languages </li></ul><ul><li>IDLE is Python’s standard IDE </li></ul><ul><li>IDLE has an interactive mode that offers immediate response to Python code </li></ul><ul><li>IDLE has a script mode that allows programmers to write, edit, load, save, and run their programs </li></ul>Guide to Programming with Python
  31. 31. Summary (continued) <ul><li>A string is a sequence of characters </li></ul><ul><li>A statement is a single unit of programming that performs some action </li></ul><ul><li>The print statement displays strings on the screen </li></ul><ul><li>An expression is something which has a value or that can be evaluated to a single value </li></ul><ul><li>Syntax highlighting is displaying programming code in different colors or fonts, according to the category of each item </li></ul>Guide to Programming with Python
  32. 32. Summary (continued) <ul><li>A syntax error is a violation of the grammar of a programming language; often caused by a typo  </li></ul><ul><li>A bug is an error in programming code </li></ul><ul><li>A comment is a note in source code meant only for programmers; ignored by computer  </li></ul><ul><li>Comments start with # </li></ul><ul><li>You should use an opening block of comments in your programs to identify the programmer, the creation date, and the program’s purpose  </li></ul>Guide to Programming with Python
  33. 33. Guide to Programming with Python Chapter Two Types, Variables, and Simple I/O: The Useless Trivia Program
  34. 34. Objectives <ul><li>Use triple-quoted strings and escape sequences </li></ul><ul><li>Make programs do math </li></ul><ul><li>Store data in the computer’s memory </li></ul><ul><li>Use variables to access and manipulate that data </li></ul><ul><li>Get input from users to create interactive programs </li></ul>Guide to Programming with Python
  35. 35. The Useless Trivia Program <ul><li>Figure 2.1: Sample run of the Useless Trivia program </li></ul><ul><li>Whoa! Steve might think about a diet before he visits the sun. </li></ul>Guide to Programming with Python
  36. 36. Using Quotes with Strings <ul><li>Can create a single string that's paragraphs long </li></ul><ul><li>Can format text of string in a specific manner </li></ul><ul><li>Can use quotes to create long string or to format </li></ul>Guide to Programming with Python
  37. 37. The Game Over 2.0 Program <ul><li>Figure 2.2: Sample run of the Game Over 2.0 program </li></ul><ul><li>Ah, the game is really over. </li></ul>Guide to Programming with Python
  38. 38. Using Quotes <ul><li>Using quotes inside strings </li></ul><ul><ul><li>Define with either single ( ' ) or double quotes ( &quot; ) </li></ul></ul><ul><ul><ul><li>'Game Over' or &quot;Game Over&quot; </li></ul></ul></ul><ul><ul><li>Define with one type, use other type in string </li></ul></ul><ul><ul><ul><li>&quot;Program 'Game Over' 2.0&quot; </li></ul></ul></ul><ul><li>Triple-quoted strings can span multiple lines </li></ul><ul><ul><li>&quot;&quot;&quot; </li></ul></ul><ul><ul><li>I am a </li></ul></ul><ul><ul><li>triple-quoted string </li></ul></ul><ul><ul><li>&quot;&quot;&quot; </li></ul></ul><ul><li>Line-continuation character </li></ul>Guide to Programming with Python
  39. 39. Using Escape Sequences with Strings <ul><li>Escape sequence : Set of characters that allow you to insert special characters into a string </li></ul><ul><ul><li>Backslash followed by another character </li></ul></ul><ul><ul><li>e.g. </li></ul></ul><ul><ul><li>Simple to use </li></ul></ul>Guide to Programming with Python
  40. 40. The Fancy Credits Program <ul><li>Figure 2.3: Sample run of the Fancy Credits program </li></ul><ul><li>So many people to thank, so many escape sequences </li></ul>Guide to Programming with Python
  41. 41. Escape Sequences <ul><li>System bell </li></ul><ul><ul><li>print &quot;a&quot; </li></ul></ul><ul><li>Tab </li></ul><ul><ul><li>print &quot; Fancy Credits&quot; </li></ul></ul><ul><li>Backslash </li></ul><ul><ul><li>print &quot; amp;quot; </li></ul></ul><ul><li>Newline </li></ul><ul><ul><li>print &quot; Special thanks goes out to:&quot; </li></ul></ul><ul><li>Quote </li></ul><ul><ul><li>print &quot;My hair stylist, Henry 'The Great', who never says &quot;can't&quot;.&quot; </li></ul></ul>Guide to Programming with Python
  42. 42. Escape Sequences (continued) <ul><li>Table 2.1: Selected escape sequences </li></ul>Guide to Programming with Python
  43. 43. Concatenating and Repeating Strings <ul><li>Can combine two separate strings into larger one </li></ul><ul><li>Can repeat a single string multiple times </li></ul>Guide to Programming with Python
  44. 44. The Silly Strings Program <ul><li>Figure 2.4: Sample run of the Silly Strings program </li></ul><ul><li>Strings appear differently than in the program code. </li></ul>Guide to Programming with Python
  45. 45. Concatenating Strings <ul><li>String concatenation : Joining together of two strings to form a new string </li></ul><ul><li>When used with string operands , + is the string concatenation operator </li></ul><ul><ul><li>&quot;concat&quot; + &quot;enate&quot; </li></ul></ul><ul><li>Suppressing a Newline </li></ul><ul><ul><li>When used at the end of print statement, comma suppresses newline </li></ul></ul><ul><ul><li>print &quot;No newline after this string&quot;, </li></ul></ul>Guide to Programming with Python
  46. 46. Repeating String <ul><li>Multiple concatenations </li></ul><ul><ul><li>When used with strings, * creates a new string by concatenating a string a specified number of times </li></ul></ul><ul><ul><li>Like “multiplying” a string </li></ul></ul><ul><ul><li>&quot;Pie&quot; * 10 creates new string &quot;PiePiePiePiePiePiePiePiePiePie&quot; </li></ul></ul>Guide to Programming with Python
  47. 47. Working with Numbers <ul><li>Can work with numbers as easily as with strings </li></ul><ul><li>Need to represent numbers in programs </li></ul><ul><ul><li>Score in space shooter game </li></ul></ul><ul><ul><li>Account balance in personal finance program </li></ul></ul><ul><li>Python can represent different types of numbers </li></ul>Guide to Programming with Python
  48. 48. The Word Problems Program <ul><li>Figure 2.5: Sample run of the Word Problems program </li></ul><ul><li>With Python, you can keep track of a pregnant hippo’s weight. </li></ul>Guide to Programming with Python
  49. 49. Numeric Types <ul><li>Type : Represents the kind of value; determines how the value can be used </li></ul><ul><li>Two common numeric types </li></ul><ul><ul><li>Integers : Numbers without a fractional part </li></ul></ul><ul><ul><li>1, 0, 27, -100 </li></ul></ul><ul><ul><li>Floating-Point Numbers (or Floats ): Numbers with a fractional part </li></ul></ul><ul><ul><li>2.376, -99.1, 1.0 </li></ul></ul>Guide to Programming with Python
  50. 50. Mathematical Operators <ul><li>Addition and Subtraction </li></ul><ul><ul><li>print 2000 - 100 + 50 displays 1950 </li></ul></ul><ul><li>Integer Division </li></ul><ul><ul><li>print 24 / 6 displays 4 </li></ul></ul><ul><ul><li>But print 19 / 4 displays 4 as well </li></ul></ul><ul><ul><li>Result of integer division always integer </li></ul></ul><ul><li>Floating-Point Division </li></ul><ul><ul><li>print 19.0 / 4 displays 4.75 </li></ul></ul><ul><ul><li>When at least one number is a float, result is a float </li></ul></ul><ul><li>Modulus (remainder of integer division) </li></ul><ul><ul><li>print 107 % 4 displays 3 </li></ul></ul>Guide to Programming with Python
  51. 51. Mathematical Operators (continued) <ul><li>Table 2.2: Mathematical operators with integers </li></ul>Guide to Programming with Python
  52. 52. Mathematical Operators (continued) <ul><li>Table 2.3: Mathematical operators with floats </li></ul>Guide to Programming with Python
  53. 53. Understanding Variables <ul><li>Variable : Represents a value; provides way to get at information in computer memory </li></ul><ul><li>Variables allow you to store and manipulate information </li></ul><ul><li>You can create variables to organize and access this information </li></ul>Guide to Programming with Python
  54. 54. The Greeter Program <ul><li>Figure 2.6: Sample run of the Greeter program </li></ul><ul><li>Using a variable for the name </li></ul>Guide to Programming with Python
  55. 55. Creating Variables <ul><li>Assignment statement : Assigns a value to a variable; creates variable if necessary </li></ul><ul><li>name = &quot;Larry&quot; </li></ul><ul><ul><li>Stores string &quot;Larry&quot; in computer memory </li></ul></ul><ul><ul><li>Creates variable name </li></ul></ul><ul><ul><li>Assigns value so that name refers to &quot;Larry&quot; </li></ul></ul>Guide to Programming with Python
  56. 56. Using Variables <ul><li>Use variable where you want value it represents </li></ul><ul><li>print name or print &quot;Larry&quot; </li></ul><ul><li>Both display Larry </li></ul><ul><li>print &quot;Hi, &quot; + name or print &quot;Hi, Larry&quot; </li></ul><ul><li>Both display Hi, Larry </li></ul>Guide to Programming with Python
  57. 57. Naming Variables <ul><li>Rules for legal variable names </li></ul><ul><ul><li>Can contain only numbers, letters, and underscores </li></ul></ul><ul><ul><li>Can’t start with a number </li></ul></ul><ul><ul><li>Can’t be a keyword </li></ul></ul><ul><li>Keyword : Built-in word with special meaning </li></ul><ul><li>Legal Names </li></ul><ul><ul><li>velocity , player2 , max_health </li></ul></ul><ul><li>Illegal Names </li></ul><ul><ul><li>?again , 2nd_player , print </li></ul></ul>Guide to Programming with Python
  58. 58. Naming Variables (continued) <ul><li>Guidelines for good variable names </li></ul><ul><ul><li>Choose descriptive names; score instead of s </li></ul></ul><ul><ul><li>Be consistent; high_score or highScore </li></ul></ul><ul><ul><li>Follow traditions; Names that begin with underscore have special meaning </li></ul></ul><ul><ul><li>Keep the length in check personal_checking_account_balance - too long? </li></ul></ul><ul><ul><li>Self-documenting code : Code written so that it’s easy to understand, independent of any comments </li></ul></ul>Guide to Programming with Python
  59. 59. Getting User Input <ul><li>Variables important for </li></ul><ul><ul><li>Getting user input </li></ul></ul><ul><ul><li>Storing user input </li></ul></ul><ul><ul><li>Manipulating user input </li></ul></ul>Guide to Programming with Python
  60. 60. The Personal Greeter Program <ul><li>Figure 2.7: Sample run of the Personal Greeter program </li></ul><ul><li>name is assigned a value based on what the user enters. </li></ul>Guide to Programming with Python
  61. 61. Getting User Input <ul><li>Function : A named collection of programming code that can receive values, do some work, and return values </li></ul><ul><li>Argument : Value passed to a function </li></ul><ul><li>Return value : Value returned from a function upon completion </li></ul><ul><li>Function is like a pizzeria </li></ul><ul><ul><li>Make a call </li></ul></ul><ul><ul><li>Provide information (like pepperoni) </li></ul></ul><ul><ul><li>Get something back (like a hot pepperoni pizza) </li></ul></ul>Guide to Programming with Python
  62. 62. Getting User Input (continued) <ul><li>raw_input() function </li></ul><ul><ul><li>Prompts the user for text input </li></ul></ul><ul><ul><li>Returns what the user entered as a string </li></ul></ul><ul><li>name = raw_input(&quot;Hi. What's your name? &quot;) </li></ul><ul><ul><li>Uses argument &quot;Hi. What's your name? &quot; to prompt user </li></ul></ul><ul><ul><li>Returns what user entered as a string </li></ul></ul><ul><ul><li>In assignment statement, name gets returned string </li></ul></ul>Guide to Programming with Python
  63. 63. Using String Methods <ul><li>String methods allow you to do many things, including: </li></ul><ul><ul><li>Create new strings from old ones </li></ul></ul><ul><ul><li>Create string that’s all-capital-letters version of original </li></ul></ul><ul><ul><li>Create new string from original, based on letter substitutions </li></ul></ul>Guide to Programming with Python
  64. 64. The Quotation Manipulation Program <ul><li>Figure 2.8: Sample run of the Quotation Manipulation program </li></ul><ul><li>The slightly low guess is printed several times with string methods. </li></ul>Guide to Programming with Python
  65. 65. String Methods <ul><li>Method : A function that an object has </li></ul><ul><li>Use dot notation to call (or invoke ) a method </li></ul><ul><ul><li>Use variable name for object, followed by dot, followed by method name and parentheses </li></ul></ul><ul><ul><li>an_object.a_method() </li></ul></ul><ul><li>Strings have methods that can return new strings </li></ul>Guide to Programming with Python
  66. 66. String Methods (continued) <ul><li>quote = &quot;I think there is a world market for maybe five computers.&quot; </li></ul><ul><ul><li>print quote.upper() </li></ul></ul><ul><ul><li>I THINK THERE IS A WORLD MARKET FOR MAYBE FIVE COMPUTERS. </li></ul></ul><ul><ul><li>print quote.lower() </li></ul></ul><ul><ul><li>i think there is a world market for maybe five computers. </li></ul></ul><ul><ul><li>print quote.title() </li></ul></ul><ul><ul><li>I Think There Is A World Market For Maybe Five Computers. </li></ul></ul><ul><ul><li>print quote.replace(&quot;five&quot;, &quot;millions of&quot;) </li></ul></ul><ul><ul><li>I think there is a world market for millions of computers. </li></ul></ul><ul><li>Original string unchanged </li></ul><ul><ul><li>print quote </li></ul></ul><ul><ul><li>I think there is a world market for maybe five computers. </li></ul></ul>Guide to Programming with Python
  67. 67. String Methods (continued) <ul><li>Table 2.4: Useful string methods </li></ul>Guide to Programming with Python
  68. 68. Using the Right Types <ul><li>Important to know which data types are available </li></ul><ul><li>Equally important to know how to work with them </li></ul><ul><li>If not, might end up with program that produces unintended results </li></ul>Guide to Programming with Python
  69. 69. The Trust Fund Buddy–Bad Program <ul><li>Figure 2.9: Sample run of the Trust Fund Buddy-Bad program </li></ul><ul><li>The monthly total should be high, but not that high. </li></ul>Guide to Programming with Python
  70. 70. Logical Errors <ul><li>Logical Error : An error that doesn’t cause a program to crash, but instead produces unintended results </li></ul><ul><li>Program output that looks like very large number: 200001000017000500075001200068001000 </li></ul><ul><li>Remember, raw_input() returns a string, so program is not adding numbers, but concatenating strings </li></ul>Guide to Programming with Python
  71. 71. Logical Errors (continued) <ul><li>car = raw_input(&quot;Lamborghini Tune-Ups: &quot;) </li></ul><ul><li>rent = raw_input(&quot;Manhattan Apartment: &quot;) </li></ul><ul><li>jet = raw_input(&quot;Private Jet Rental: &quot;) </li></ul><ul><li>gifts = raw_input(&quot;Gifts: &quot;) </li></ul><ul><li>food = raw_input(&quot;Dining Out: &quot;) </li></ul><ul><li>staff = raw_input(&quot;Staff (butlers, chef, driver, assistant): &quot;) </li></ul><ul><li>guru = raw_input(&quot;Personal Guru and Coach: &quot;) </li></ul><ul><li>games = raw_input(&quot;Computer Games: &quot;) </li></ul><ul><li>total = car + rent + jet + gifts + food + staff + guru + games </li></ul><ul><li>car, rent, jet, gifts, food, staff, guru, games are strings </li></ul><ul><li>total is concatenation of all strings </li></ul>Guide to Programming with Python
  72. 72. Converting Values <ul><li>Can convert one type of value to another </li></ul><ul><li>Use built-in functions </li></ul><ul><li>Solution to Trust Fund Buddy–Bad program </li></ul>Guide to Programming with Python
  73. 73. The Trust Fund Buddy–Good Program <ul><li>Figure 2.10: Sample run of the Trust Fund Buddy-Good program </li></ul><ul><li>Now the total is right. </li></ul>Guide to Programming with Python
  74. 74. Converting Types <ul><li>int() function converts a value to an integer </li></ul><ul><ul><li>car = raw_input(&quot;Lamborghini Tune-Ups: &quot;) </li></ul></ul><ul><ul><li>car = int(car) </li></ul></ul><ul><li>Can nest multiple function calls </li></ul><ul><ul><li>rent = int(raw_input(&quot;Manhattan Apartment: &quot;)) </li></ul></ul>Guide to Programming with Python
  75. 75. Converting Types (continued) <ul><li>Table 2.5: Selected type conversion functions </li></ul>Guide to Programming with Python
  76. 76. Augmented Assignment Operators <ul><li>Common to assign a value to a variable based on its original value </li></ul><ul><ul><li>For example, increment value of variable </li></ul></ul><ul><li>Augmented assignment operators provide condensed syntax </li></ul><ul><ul><li>Original: score = score + 1 </li></ul></ul><ul><ul><li>Augmented: score += 1 </li></ul></ul>Guide to Programming with Python
  77. 77. Augmented Assignment Operators (continued) <ul><li>Table 2.6: Useful augmented assignment operators </li></ul>Guide to Programming with Python
  78. 78. Printing Multiple Values <ul><li>To print multiple values in single print statement, separate values by commas </li></ul><ul><ul><li>print &quot; Grand Total: &quot;, total </li></ul></ul>Guide to Programming with Python
  79. 79. Summary <ul><li>String can be defined with either single or double quotes </li></ul><ul><li>Tripled-quoted strings, defined by three opening and closing quotes, can span multiple lines </li></ul><ul><li>An escape sequence is a set of characters that allow you to insert special characters into a string </li></ul><ul><li>String concatenation is the joining together of two strings to form a new string </li></ul><ul><li>Integers, whole numbers with no decimal part, and floats, numbers with a decimal part, are two numeric types </li></ul>Guide to Programming with Python
  80. 80. Summary (continued) <ul><li>Result of integer division is always an integer while result of floating-point division is always a float </li></ul><ul><li>A variable represents a value and provides way to get at information in computer memory </li></ul><ul><li>An assignment statement assigns a value to a variable and creates variable if necessary </li></ul><ul><li>A function is a named collection of programming code that can receive values, do some work, and return values </li></ul><ul><li>The raw_input() function prompts the user for input and returns what the user entered as a string </li></ul>Guide to Programming with Python
  81. 81. Summary (continued) <ul><li>A method is a function that an object has </li></ul><ul><li>Strings have methods that can return new strings </li></ul><ul><li>A logical error produces unintended results </li></ul><ul><li>Python has functions for converting values to an integer, a float, or a string </li></ul><ul><li>Augmented assignment operators provide condensed syntax for changing the value of a variable based on its original value </li></ul>Guide to Programming with Python

×