Sunday, June 13, 2010

Using stings And numbers in Python


Starting codeEditor
  Depending on your operating system, you will start codeEditor in different ways.
  Once it is installed on your system with Python, on Linux or Unix-based systems, you can just type python in a terminal or shell window and it will start.

Using string in python 

>>> “This text really won’t do anything”
“This text really won’t do anything”
>>> ‘This is also a string’
‘This is also a string’
>>> “””This is a third string that is some
   ...      how different”””
‘This is a third string that is some\n     how different’
>>> ‘And he said \’this string has escaped quotes\’’
“And he said ‘this string has escaped quotes’”

//Using + to Combine Strings
>>> “John” + “Q.” + “Public”
‘JohnQ.Public’
>>> “John” + “ “ + “Q.” + “ “ + “Public”
‘John Q. Public’
//Using a Format Specifier to Populate a String
//In the simplest case, you can do the same thing with your friend, John Q.:
>>> “John Q. %s” % (“Public”)
    ‘John Q. Public’
//More String Formatting
//You can do a couple of useful things when formatting a simple string:
>>> “%s %s %10s” % (“John”, “Q.”, “Public”)
   ‘John Q.        Public’
>>> “%-10s %s %10s” % (“John”, “Q.”, “Public”)
   ‘John         Q.       Public’

Numbers in Python
Python offers four different kinds of numbers with which you can work: integers, long numbers
 (or longs), floating-point numbers (or floats), and imaginary numbers.

>>> type(1)
<type ‘int’>
>>> type(2000)
<type ‘int’>
>>> type(999999999999)
<type ‘long’>
>>> type(1.0)
<type ‘float’>

Creating an Imaginary Number
The imaginary number behaves very much like a float, except that it cannot be mixed with a float. When
you see an imaginary number, it will have the letter j trailing it:
    >>> 12j
    12j

You can combine imaginary and nonimaginary numbers to create complex numbers:
   >>> 12j + 1
   (1+12j)
   >>> 12j + 1.01
   (1.01+12j)
   >>> type (12j + 1)
   <type ‘complex’>

Including Different Numbers in Strings
When you combined two strings in the first chapter by using a format specifier, you used the format
specifier %s, which means “a string.” Because numbers and strings have different types, you will use a
different specifier that will enable your numbers to be included in a string:
     >>> “Including an integer works with %%d like this: %d” % 10
     ‘Including an integer works with %d like this: 10’
     >>> “An integer converted to a float with %%f: %f” % 5
     ‘An integer converted to a float with %f: 5.000000’
     >>> “A normal float with %%f: %f” % 1.2345
     ‘A normal float with %f: 1.234500’
     >>> “A really large number with %%E: %E” % 6.789E10
     ‘A really large number with %E: 6.789000E+10’
     >>> “Controlling the number of decimal places shown: %.02f” % 25.101010101
     ‘Controlling the number of decimal places shown: 25.10’

  Escaping the % Sign in Strings
One other trick was shown before. In case you wanted to print the literal string %d in your program, you
achieve that in Python strings by using two % signs together. This is needed only when you also have
valid format specifiers that you want Python to substitute for you in the same string:
    print “The %% behaves differently when combined with other letters, like this: %%d
    %%s %%f %d” % 10
    The % behaves differently when combined with other letters, like this: %d %s %f 10

Doing Basic Math
You can enter basic arithmetic at the Python shell prompt and use it like a calculator. Like a calculator,
Python will accept a set of operations, and when you hit the Enter key, it will evaluate everything you’ve
typed and give you your answer:
    >>> 5 + 300
    305
    >>> 399 + 3020 + 1 + 3456
    6876
    >>> 300 - 59994 + 20
    -59674
    >>> 4023 - 22.46
    4000.54
>>> 2000403030 * 392381727
784921595607432810L
>>> 2000403030 * 3923817273929
7849215963933911604870L
>>> 2e304 * 3923817273929
inf
>>> 2e34 * 3923817273929
7.8476345478579995e+46
>>> 44 / 11
4
>>> 324 / 101
3
>>> 324.0 / 101.0
3.2079207920792081
>>> 324.0 / 101
3.2079207920792081
>>> 5 % 3
2

Printing the Results
Try actually printing the results, so that the preceding math with the unusual-looking results has its
results displayed to a user, as it would from inside of a program:
    >>> print “%f” % (4023 - 22.4)
    4000.600000
Using Math Operations
When you’re thinking about a particular set of mathematical operations, it can seem straightforward
when you write it down (or type it in). When you look at it later, however, it can become confusing. Try
these examples, and imagine them without the parentheses:
    >>> (24 * 8)
    192
    >>> (24 * (8 + 3))
    264
    >>> (24 * (8 + 3 + 7.0))
    432.0
   
Using Number Formats
Try this, for example. Here, you print a number as though you were printing a dollar amount:
    >>> print “$%.02f” % 30.0
    $30.00





No comments:

Post a Comment