Friday, December 18, 2009
Wednesday, November 25, 2009
A sequence type is one that supports the membership operator (in), the size function (len()), slices ([]), and is iterable.
Python provides five built-in sequence types: bytearray, bytes, list, str, and tuple.
Some other sequence types are provided in the standard library, most notably, collections.namedtuple.
Saturday, October 31, 2009
Wednesday, September 30, 2009
Monday, August 31, 2009
Thursday, July 23, 2009
Tuesday, June 30, 2009
Monday, May 25, 2009
Concatenating Strings :
Simply written two strings, one after the other, and Python automatically concate-nates them (makes them into one string).
This mechanism isn’t used very often, but it can be useful at times.
However, it works only when you actually write both strings at the same time, directly following one another:
>>> y = "world!"
>>> x y
SyntaxError: invalid syntax
It is just a special way of writing strings, not a general method of concat-enating them.
How, then, do you concatenate strings? Just like you add numbers:
>>> "Hello, " + "world!"
'Hello, world!'
>>> x = "Hello, "
>>> y = "world!"
>>> x + y
'Hello, world!'
Single-Quoted Strings and Escaping Quotes :
Strings are values, just as numbers are:
'Hello, world!'
There is one thing that may be a bit surprising about this example, though:
When Python printed out our string, it used single quotes, whereas we used double quotes.
What’s the difference? Actually, there is no difference:
'Hello, world!'
Here, we use single quotes, and the result is the same. So why allow both?
Because in some cases it may be useful:
"Let's go!"
>>> '"Hello, world!" she said'
Python Strings :
The raw_input and "Hello, " + name + "!"
Let’s go for the "Hello" part first and leave raw_input for later.
The first program in this chapter was simply print "Hello, world!"
But what is "Hello, world!"? It’s called a string (as in “a string of characters”).
Strings are found in almost every useful, real-world Python program and have many uses.
Their main use is to represent bits of text, such as the exclamation “Hello, world!”
Wednesday, April 22, 2009
Running Python Scripts from a Command Prompt :
Actually, there are several ways to run your programs. First, let’s assume that you have a DOS
window or a UNIX shell prompt before you, and that the directory containing the Python exe-
cutable (called python.exe in Windows, and python in UNIX) or the directory containing the
executable (in Windows) has been put in your PATH environment variable.
Also, let’s assume that your script from the previous section (hello.py) is in the current directory. Then you can execute your script with the following command in
Windows: C:\>python hello.py
OR
UNIX: $ python hello.py
As you can see, the command is the same. Only the system prompt changes.
Note : If you don’t want to mess with environment variables, you can simply specify the full path of the Python interpreter.
In Windows, you might do something like this: C:\>C:\Python25\python hello.py
Saving and Executing Your Programs :
First of all, you need a text editor, preferably one intended for programming. (If you use
something like Microsoft Word, which I don’t really recommend, be sure to save your code as
plain text.)
With IDLE, you can simply create a new editor window with File >> New Window.
Another window appears, without an interactive prompt.
Whew! Start by entering the following:
print "Hello, world!"
Now select File >> Save to save your program (which is, in fact, a plain text file).
Be sure to put it somewhere where you can find it later. You might want to create a directory where you put all your Python projects, such as C:\python in Windows.
In a UNIX environment, you might use a directory like ~/python. Give your file any reasonable name, such as hello.py. The .py ending is important.
Tuesday, March 31, 2009
Python Lexical Structure :
Following is a simple Python program. It shows the basic structure of many Python scripts follows:
2. Do some processing (lines 4 – 5).
3. Make decisions and perform actions based on those decisions (lines 6 – 10).
name = “Jim”
age = 42
highschoolGPA = 3.89
print “\n\n”
if name == “Jim”:
print “Your age is “, age
print “You had a”, highschoolGPA, “GPA in high school”
if (highschoolGPA > 3):
print “You had better than a 3.0 GPA...good job!”
Tuesday, January 13, 2009
Python Number and Expressions :
The interactive Python interpreter can be used as a powerful calculator.
Try the following : >>> 2 + 2
This should give you the answer 4. That wasn’t too hard.
Well, what about this : >>> 53672 + 235253 = 288925
Still not impressed? Admittedly, this is pretty standard stuff. (I’ll assume that you’ve used
a calculator enough to know the difference between 1+2*3 and (1+2)*3.)
All the usual arith- metic operators work as expected—almost. There is one potential trap here, and that is integer division (in Python versions prior to 3.0) : >>> 1/2 = 0
Python Interactive Interpreter :
When you start up Python, you get a prompt similar to the following:
Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
Type "help", "copyright", "credits" or "license" for more information. >>>
■Note: The exact appearance of the interpreter and its error messages will depend on which version you are using.
It’s an interactive Python interpreter.
Just try the following :
>>> print "Hello, world!"
When you press the Enter key, the following output appears : Hello, world! >>>
Data Science - 103 (Kapil Sharma)
Cloud Computing Basic:- In this the PC is on service provider data center and secuirty maintenance and upgrades are done by the service pro...
-
Getting Started : Of course, the first thing you need to do is install Python, if you don ’ t already have it. Installers are available for ...
-
Why to use Ajax ? Mainly to build a fast, dynamic website, but also to save resources.For improving sharing of resources, it is better to us...
-
Python Width and Precision: A conversion specifier may include a field width and a precision. The width is the minimum number of characters...