HomePython Page 4 - Python 3.1: Programming Basics and Strings
Using the Python Shell - Python
Ready to take your first steps in Python? This three-part article series walks you through the basics, introducing important concepts such as strings. In this first part, you'll learn how programming is different from using a computer, how to install Python, and how to start using the Python shell. It is excerpted from the book Beginning Python: Using Python 2.6 and Python 3.1,, written by James Payne, Developer Shed Editor-in-Chief (Wrox, 2010; ISBN: 0470414634).
Before starting to write programs, you’ll need to learn how to experiment with the Python shell. For now, you can think of the Python shell as a way to peer within running Python code. It places you inside of a running instance of Python, into which you can feed programming code; at the same time, Python will do what you have asked it to do and will show you a little bit about how it responds to its environment. Because running programs often have a context — things that you as the programmer have tailored to your needs — it is an advantage to have the shell because it lets you experiment with the context you have created.
Now that you have installed Python version 3.1, you can begin to experiment with the shell’s basic behavior. For starters, type in some text:
> > > ”Hello World. You will never see this.”
Note that typing the previous sentence into the shell didn’t actually do anything; nothing was changed in the Python environment. Instead, the sentence was evaluated by Python, to determine what, if anything, you wanted Python to do. In this case, you merely wanted it to read the text.
Although Python didn’t technically do anything with your words, it did give some indication that it read them. Python indicated this by displaying the text you entered (known as a string ) in quotes. A string is a data type, and each data type is displayed differently by Python. As you progress through this book, you will see the different ways Python displays each one.
Beginning to Use Python — Strings
At this point, you should feel free to experiment with using the shell’s basic behavior. Type some text, in quotes; for starters, you could type the following:
> > > “This text really won’t do anything”
“This text really won’t do anything”
You should notice one thing immediately: After you entered a quote ( " ), the Python shell changed the color of everything up to the quote that completed the sentence. Of course, the preceding text is absolutely true. It did nothing: It didn’t change your Python environment; it was merely evaluated by the running Python instance, in case it did determine that in fact you’d told it to do something. In this case, you’ve asked it only to read the text you wrote, but doing this doesn’t constitute a change to the environment.
However, you can see that Python indicated that it saw what you entered. It showed you the text you entered, and it displayed it in the manner it will always display a string — in quotes. As you learn about other data types, you’ll find that Python has a way of displaying each one differently.
Please check back tomorrow for the second part of this series.