MrSchmid.com

Types and Variables, Input and Output

Posted On: Tue, 2007-03-27 12:46 by mrschmid

carpet python Types and Variables, Input and Output

  • Strings – Remember, Strings are nuggets of text that can be displayed or manipulated
  • Triple Quotes – “”” triple quotes allow use to continue a string that extends for more than one line.
  • Quotes inside strings - to use a quote inside of a string with out ending the string we use an escape string that differentiates the quote from the ending quotes for the string. - \’ or \”
  • the \ to continue on new line allows us to continue one command onto the next line. Makes the code easier to read for humans, but makes no difference for the computer
  • Ascii Art - http://www.network-science.de/ascii/

    Hello World 2.0

  • Click Read More to see the rest.
    • Escape Strings

Sequence

Description

\\

Backslash. Prints one backslash.

\'

Single quote. Prints a single quote.

\"

Double quote. Prints a double quote.

\a

Bell. Sounds the system bell.

\b

Backspace. Moves cursor back one space.

\n

Newline. Moves cursor to beginning of next line.

\t

Horizontal tab. Moves cursor forward one tab stop.

      • Some escape strings, such as the system bell, must run through the operating system, not through IDLE. Find your program and double click on the .py file to run it through your operating system.

  • Examples
    Strings
    • Fancy_Credits
      • system bell ="\a"
      • tab stops="\t"
      • backslash ="\\"
      • newline ="\n"
      • quotes ="\'" for single and "\"" for double (without the outside quotes, of course)
    • Numbers
      • Word Problems Program
      • Integers vs. floating-point numbers
      • Integers are whole numbers while floating-point numbers are decimals. Example: 4 (integer) vs. 4.12359 (floating point)
      • To return a floating-point answer, use a floating point number. Example 12/7.0=1.714 vs. 12/7=1
      • Mathematical operations: Addition +, Subtraction -, Multiplication *, Division /, Modulus (what’s the remainder) %,

Variables

- Variable definition – Variables let us store a value as a term. I can assign a variable to a value, or allow the user to do so through raw_input()

- Variable naming rules

- - Be consistent (MyVariable vs. my_variable)

- - Be descriptive

- - Watch the length

- Raw_input() allows the user to supply a variable

Functions and Strings: Functions are built in features that allow you to accomplish a task. Raw_input is a function that displays the string in the parenthesis and stores the user’s response as a variable. Upper(), lower(), title(), and replace() are all functions that you will be using in the ….

Quotation Manipulation Program

 

EXERCISES:

1. Write a Tipper program where the user enters a restaurant bill total. The program should then display two amounts: a 15 percent tip and a 20 percent tip. Use helpful comments (At the top and throughout the program)

2. Write a Car Salesman program where the user enters the base price of a car. The program should add on a bunch of extra fees such as tax, license, dealer prep, and destination charge. Make tax and license a percent of the base price. The other fees should be set values. Display the actual price of the car once all the extras are applied. Use helpful comments (At the top and throughout the program)

3. Modify the quotation manipulation program to use a quote of your choice. Experiment and use one of the string methods documented here: http://docs.python.org/lib/string-methods.html Describe what the function does in your comments in the program. Use helpful comments (At the top and throughout the program)

Types and Variables Assignment list:

Practices: Fancy Credits, Hello World 2.0, Word Problems, and Quotation Manipulation Program.

Exercises: Tipper, Car Salesman, Modified Quotation Manipulation Program

AttachmentSize
escape-credits.gif7.61 KB
helloworld2.0.gif6.79 KB
word-problems.gif16.3 KB
ManipulatingStrings.gif6.83 KB
( categories: | )

Poll