Python 3.1: Programming Basics and Strings

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).

If you like what you’re reading here, you can find it at Wrox or purchase the entire book at Amazon.

How Programming is Different from Using a Computer

The first thing you need to understand about computers when you’re programming is that you control the computer. Sometimes the computer doesn’t do what you expect, but even when it doesn’t do what you want the first time, it should do the same thing the second and third time — until you take charge and change the program. The trend in personal computers has been away from reliability and toward software being built on top of other, unreliable, software. The results that you live with might have you believing that computers are malicious and arbitrary beasts, existing to taunt you with unbearable amounts of extra work and various harassments while you’re already trying to accomplish something. However, after you’ve learned how to program, you gain an understanding of how this situation has come to pass, and perhaps you’ll find that you can do better than some of the programmers whose software you’ve used.

Note that programming in a language like Python, an interpreted language, means that you are not going to need to know a whole lot about computer hardware, memory, or long sequences of 0s and 1s. You are going to write in text form like you are used to reading and writing but in a different and simpler language. Python is the language, and like English or any other language(s) you speak, it makes sense to other people who already speak the language. Learning a programming language can be even easier, however, because programming languages aren’t intended for discussions, debates, phone calls, plays, movies, or any kind of casual interaction. They’re intended for giving instructions and ensuring that those instructions are followed. Computers have been fashioned into incredibly flexible tools that have found a use in almost every business and task that people have found themselves doing, but they are still built from fundamentally understandable and controllable pieces.

Programming is Consistency

In spite of the complexity involved in covering all of the disciplines into which computers have crept, the basic computer is still relatively simple in principle. The internal mechanisms that define how a computer works haven’t changed a lot since the 1950s when transistors were first used in computers. In all that time, this core simplicity has meant that computers can, and should, be held to a high standard of consistency. What this means to you, as the programmer, is that anytime you tell a computer to metaphorically jump, you must tell it how high and where to land, and it will perform that jump — over and over again for as long as you specify. The program should not arbitrarily stop working or change how it works without you facilitating the change.

{mospagebreak title=Programming is Control}

Programming a computer is very different from creating a program, as the word applies to people in real life. In real life, you ask people to do things, and sometimes you have to struggle mightily to ensure that your wishes are carried out — for example, if you plan a party for 30 people and assign two of them to bring the chips and dip and they bring the drinks instead, it is out of your control.

With computers that problem doesn’t exist. The computer does exactly what you tell it to do. As you can imagine, this means that you must pay some attention to detail to ensure that the computer does just what you want it to do.

One of the goals of Python is to program in blocks that enable you to think about larger and larger

projects by building each project as pieces that behave in well – understood ways. This is a key goal of a programming style known as object – oriented programming . The guiding principle of this style is that you can create reliable pieces that still work when you piece them together, that are understandable, and that are useful. This gives you, the programmer, control over how the parts of your programs run, while enabling you to extend your program as the problems you’re solving evolve.

Programming Copes with Change

Programs are run on computers that handle real – world problems; and in the real world, plans and circumstances frequently change. Because of these shifting circumstances, programmers rarely get the opportunity to create perfectly crafted, useful, and flexible programs. Usually, you can achieve only two of these goals. The changes that you will have to deal with should give you some perspective and lead you to program cautiously. With sufficient caution, you can create programs that know when they’re being asked to exceed their capabilities, and they can fail gracefully by notifying their users that they’ve stopped. In the best cases, you can create programs that explain what failed and why. Python offers especially useful features that enable you to describe what conditions may have occurred that prevented your program from working.  

What All That Means Together

Taken together, these beginning principles mean that you’re going to be introduced to programming as a way of telling a computer what tasks you want it to do, in an environment where you are in control. You will be aware that sometimes accidents can happen and that these mistakes can be accommodated through mechanisms that offer you some discretion regarding how these conditions will be handled, including recovering from problems and continuing to work. 

{mospagebreak title=The First Steps}

The absolute first step you need to take before you can begin programming in Python is to download and install Python version 3.1. Navigate to www.python.org/download and choose the newest version of Python. You will be taken to a page with instructions on how to download the appropriate version for your computer. For instance, if you are running Windows, it may say Windows x86 MSI Installer (3.0). Programs are written in a form called source code. Source code contains the instructions that the language follows, and when the source code is read and processed, the instructions that you’ve put in there become the actions that the computer takes.

Just as authors and editors have specialized tools for writing for magazines, books, or online publications, programmers also need specialized tools. As a starting Python programmer, the right tool for the job is the Python IDLE GUI (graphical user interface).

Once the download is finished, double – click it to run the program. Your best bet is to accept the default prompts Python offers you. This process may take a few minutes, depending on your system. After setup is complete, you will want to test to make sure it is installed properly. Click the Windows Start menu and go to All Programs. You will see Python 3.0 in the menu. Choose IDLE (Python GUI) and wait for the program to load.

Once IDLE launches, type in “ Test, test, testing ” and press the Enter key. If Python is running correctly, it should return the value ‘Test, test, testing’ in blue letters and with single quotes (I’ll get more into this soon). Congratulations — you have successfully installed Python and are well on your way to becoming a programming guru.

Installing Python 3.1 on Non – Windows Systems

If you are the proud owner of a Mac and are running Mac OS X, you are in luck; it comes with Python installed. Unfortunately, it may not be the most up – to – date version. For security and compatibility purposes, I would suggest logging on to www.python.org/download/mac . Check to see that your Mac OS X version is the right version for the Python you are installing.

If you have a Linux computer, you may also already have Python installed, but again, it may be an earlier version. I would once more suggest you go to the Python website to find the latest version (and of course, the one appropriate to your system). The website www.python.org/download should have instructions on how to download the right version for your computer.

{mospagebreak title=Using the Python Shell}

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.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye