1 Starting with Python

Image

What you will learn

We’ll start with a discussion about Python, the programming language you’re learning. Then you’ll discover what kind of computer you need to write programs and how to find and install the tools you’ll use to build your code. You’ll also have your first of many interactions with the Python Shell and discover that programming-language designers do have a sense of humor.

What is Python?

Build a place to work with Python

Start Python

What you have learned

What is Python?

Before you start learning about Python, it’s worth considering just what we are learning about. Python is a programming language. In other words, it’s a language you use to write programs. A program is a set of instructions that tells a computer how to do something. We can’t use a “proper” language like English to do this because “proper English” is just too confusing for a computer to understand. As an example, consider a doctor’s instructions:

"Drink your medicine after a hot bath."

Well, we would probably take a hot bath and then drink our medicine. A computer, however, would probably drink the hot bath and then drink its medicine. You can interpret the above instructions either way, because the English language allows you to write ambiguous statements. Programming languages must be designed so that instructions written with them are not open to interpretation; they must tell the computer precisely and unambiguously what to do. This usually means breaking down actions into a sequence of simpler steps:

Step1: Take a hot bath
Step2: Drink your medicine

A programming language forces us to write instructions in this way. Python is one of many programming languages invented to provide humans with a way of telling the computer what to do.

In my programming career, I’ve learned many different languages over the years, and I confidently expect to need to learn even more in the future. None of them are perfect, and I see each of them as a tool that I would use in a particular situation, just as I would choose a different tool depending on whether I was making a hole in a brick wall, a pane of glass, or a piece of wood.

Some people get very excited when talking about the “best” programming language. I’m quite happy to discuss what makes the best programming languages, just as I’m happy to tell you all about my favorite type of car, but I don’t see this as something to get worked up about. I love Python for its power and expressiveness. I love the C# programming language for the way it pushes me to produce well-structured solutions. I love the C++ programming language for the way it gives me absolute control of hardware underneath my program. And so on. Python does have things about it that make me want to tear my hair out in frustration, but that’s true of the other languages, too. And all programming languages have aspects about them that I love. And I love using all of them.

Python origins

You might think that programming languages are a bit like space rockets in that they are designed by white-coated scientists with mega-brains who get everything right the first time and always produce perfect solutions. However, this is not the case. Programming languages have been developed over the years for all kinds of reasons, including reasons as simple as “It seemed like a good idea at the time.”

Guido van Rossum, the original developer of Python, had no idea just what he was starting when, in late 1989, he decided to spend a few weeks on a hobby programming project that he could work on while his office at work was closed during Christmas. He named this language “Python,” not because of a liking for snakes, but because he was an enthusiastic fan of the British TV comedy show Monty Python’s Flying Circus. However, the language was picked up by programmers the world over who loved its elegance and power. Python is now one of the most popular programming languages in use today.

Python versions

One of the first things you’ll discover about Python is that many versions of the language are available. From a programmer’s point of view, this is not terribly helpful. Programs you write for one version of Python are not guaranteed to work with another version. Python lets you use parts of other people’s programs in your programs, which is a great way to save time and effort. Of course, this only works properly if all the programs are written using the same Python version. Over the years, the different versions of the language have resolved into two distinct strands of development, which simplifies things somewhat.

Essentially the choice of which version of Python you’ll use boils down to a choice of version 2.7 or version 3.n (where n is a number that keeps increasing).

  • Version 2.7 (the old stalwart) is great because a lot of existing software uses version 2.7. If you’re looking for a library of Python code to perform a particular task, there’s a better chance of it being available in version 2.7.

  • Version 3.n (the latest one) is great because it eliminates some confusing features of the language that can trip you up.

This book focuses on Python version 3.n, but we’ll look at differences between the versions when they affect our programs. Also, we’ll consider how to use some libraries that are supported only in version 2.7.

The good news is that the fundamentals of program creation are the same for both versions of Python—and indeed for just about all programming languages. Once you’ve learned how to write Python programs, you’ll be able to transfer this skill into many other languages, including C++, C#, Visual Basic, and JavaScript.

Just as you can drive just about any vehicle once you learn the fundamentals of driving, you can transfer your Python programming skills to other programming languages. When driving a new car, you just need to locate the various switches and controls before setting off on your journey. The same holds true with programming.

Build a place to work with Python

If you were a truck driver who spends many hours in the cab hauling goods across the country, you’d want a truck with a comfortable seat, an unobstructed view of the road, and controls that are easy to find and use. It would also help if your truck had enough power to climb hills at a reasonable speed and was easy to handle on twisting mountainside roads.

In the same way, if you expect to spend any amount of time at a keyboard writing programs, you should have a decent place to work. Find somewhere to set up a PC, keyboard, and monitor. Pull up a chair that you don’t mind spending quite a few hours sitting in.

You don’t need a particularly fancy computer for writing programs, but your PC will need a reasonable amount of memory and processor performance to handle the tools we’ll use. I suggest using a device with at least an Intel i5 or equivalent processor, 4 GB of memory, and 256 GB of hard drive space. You can use smaller computers, but they can make the development process somewhat frustrating because they will take a while to update your program after you make changes to it.

The operating system you use is a matter of personal preference. I prefer Windows 10, but if you prefer using macOS or Linux, you can use those operating systems instead. The Python language and the development environment we’ll use are available for all three operating systems.

Get the tools

Before you can start sharing or selling your programs, you must download and install the tools that will make this possible. Installation will take a little while, depending the speed of your network connection. There will be a few occasions when you’ll just have to sit and wait while things are fetched from the Internet and installed. Note that it’s important to perform the actions in the order I provide. The good news is that you need to perform this installation only once for each computer you want to use.

All the tools we’ll use are free to download and install. I find it astonishing and wonderful that such powerful software is available for free for anyone to use. The Python distribution makes it easy for you to get started writing programs, and the Visual Studio Code editor is a great environment for creating large applications.

The tools and how you obtain them are slightly different for the different devices that you might be using. You can download the latest, up-to-date, instructions here:

www.begintocodewithpython.com/tools/

Python for Windows PC

If you have a Windows PC, you can download and install Python from the Python website. You’ll download the installer from the Python download site and then run it to install Python on your Windows PC. I used the Microsoft Edge browser. If you use a different browser, you’ll see slightly different screens, so my advice would be to perform the following steps using Edge.

Be sure that you are using the official download site (the one given below) and allow the installer to run when asked by Windows.

www.python.org/downloads/

Figure 1-1 shows the download page for Python.

Image

Figure 1-1 Python download page

The web page has determined that I am using a Windows PC and has offered me two versions to download. On the download page, you’ll want to download version 3.6.1 of Python, so click this button to start the download.

Image

The browser will ask what you want to do with the file. If you’re using Microsoft Edge, you’ll see the dialog box above. Click the Run button. When the Python installer has downloaded, it will start to run (Figure 1-2).

Image

Figure 1-2 Python installer

The Python installer program will load Python onto your computer and make it available for use. There are several settings that can change the way that Python is installed, but you don’t need to change any of these. The only change you should make is to select the Add Python 3.6 to PATH check box at the bottom of the installer dialog. Then click Install Now. You might be asked to confirm the changes being made to your system, so click OK to accept these changes.

At the end of the installation, you should see the window shown in Figure 1-3. Click Close to close the window.

Image

Figure 1-3 Successful setup

You might see the message “Disable path length limit” (Figure 1-4) at the end of the installation. This refers to the way that Windows manages references to files. If you see this message, click “Disable path length limit.” You’ll be asked to confirm the changes to your machine settings.

Image

Figure 1-4 Successful installation with path length limit

Once the installation has finished, click Close to close the installer program.

Image WHAT COULD GO WRONG

Python installer problems

The Python download web page can usually work out which operating system you’re using and will display buttons that automatically direct you to the correct files for your computer. However; sometimes this might not happen, in which case you won’t see any direct download buttons. If this happens to you; just select your operating system from the options displayed on the downloads page and then find the latest release of the language for your computer.

Python is upgraded quite regularly, so you might notice that the version offered is different from the one shown in Figure 1-1 (for example it might have reached version 3.6.3). This is not a problem. As long as the version number is 3.n you will be able to use the sample programs in this text.

Start Python

You’re about to start up an environment that supports the Python language and interact with it. This is a bit like opening the front door of a new apartment or house or getting into a shiny new car you just bought.

The tool you’ll use is called IDLE, which means “Integrated Development Learning Environment” (and might also be a reference to Eric Idle, one of the members of the Monty Python comedy team). IDLE provides two ways of interacting with Python: the “shell” where you can type Python commands that are executed instantly, and a text editor that allows you to create program code documents. IDLE is available on almost all operating systems these days.

Image MAKE SOMETHING HAPPEN

Open IDLE

First, open the IDLE environment. On Windows 10, click the Start button and then find IDLE in the Python group of programs (Figure 1-5).

Image

Figure 1.5 Start IDLE

It might be worth adding IDLE to the Start menu or pinning it to the taskbar. You can do this by right-clicking IDLE and selecting the appropriate option.

On macOS or Linux, open a terminal, type idle, and press Enter. It doesn’t matter which operating system you use; once IDLE is running, you should see the IDLE command shell appear on the screen (Figure 1-6).

Image

Figure 1.6 IDLE shell

A shell encloses or “wraps around” something. The IDLE program is enclosing the Python engine. The Python engine is what runs Python programs. The IDLE Python Shell takes Python commands that you type in, feeds them into the Python engine, and then displays the results.

Think of the IDLE shell as a waiter in a restaurant. You tell a waiter what you want to eat, he goes off to the kitchen and asks the chef to make the food, and then brings the food to your table. The waiter serves as a “shell” around the kitchen.

You can use IDLE to say hello to Python (Figure 1-7). Type hello and press the Enter key:

Image

Figure 1.7 A failed hello

Hmmmm…this did not go well. As a rule of thumb, when the computer prints a message in red, this is usually bad news. In this case, the rather long-winded message in red is telling us that Python does not recognize the word “hello.” Whenever you type a command, the program behind the Python Shell will look through the list of words it understands and try to find one that matches. The word “hello” is not defined on this list, so Python issues this error message.

This is a bit like asking a waiter for a dish that the chef doesn’t know how to cook.

I suppose that the Python Shell could have printed “I don’t know what ‘hello’ means,” but that would make it too easy. As you will find out (or may already know), computer error messages have a way of making simple mistakes sound really complicated.

In the next chapter, we’ll explore in more detail how to use the Python Shell to discover what computers actually do when they run programs. However, for now, we can end our session with the shell by typing import this (Figure 1-8), which activates an Easter Egg.

Image

Figure 1.8 The Zen of Python

If you’re looking for a “Philosophy of Python,” then this is a very good one. Perhaps that is why this Easter Egg has survived as part of the language for so long. If some of the statements sound a bit profound, don’t worry, we’ll touch on what they mean as we go through the book.

What you have learned

In this first chapter, you built yourself a place to work by installing the Python language and the IDLE development environment.

You discovered that Python is a programming language, and that a programming language is a simplified version of English that allows programmers to tell a computer how to do things.

You had a brief conversation with the Python language and discovered that it is not terribly helpful if you just try to be friendly with it. Instead, you must give it exactly the right instructions. It can then be surprisingly chatty.

To reinforce your understanding of this chapter, you might want to consider the following “profound questions” about computers, programs, and programming.

What is the difference between a program and an application?

When people talk about software, you will find the words program and application used interchangeably. When I talk about a program, I am describing some code that instructs the computer what to do. I regard an application as something larger and more developed. An application brings together program code and assets, such as images and sounds, to make a complete experience for a user. A program can be as simple as a few lines of Python code.

Will “artificial intelligence” (AI) mean that one day we won’t have to write programs?

This is a very deep question. To me, artificial intelligence is a field in which lots of people are working very hard to make a computer very good at guessing. It turns out that by giving computers lots of information—and telling them how the information is related—a program can then use all this stuff to make a good guess as to the context of a statement.

I suppose that humans “guess” at the meaning of things. Maybe one day a doctor really will want me to drink a hot bath before I take my medicine (per the instructions at the beginning of this chapter), in which case I’ll do the wrong thing. However, humans have a much greater capacity to store and link experiences, which puts the computer at a distinct disadvantage when it comes to showing intelligence. Maybe in time, this will change. We are already seeing that in specific fields of expertise—for example, finance, medical diagnosis, and artificial intelligence—can do very well.

However, when it comes to telling computers exactly what we want them to do, we’ll need programmers for quite a long time…certainly long enough for you to pay off your mortgage.

Is IDLE the only tool for writing programs?

No. There are a great many tools that you can use to create programs. Some are tied to one particular programming language, and others are more general purpose. My personal favorite is a tool called Visual Studio. It can be used with many programming languages, including Python. However, Visual Studio is probably a bit complex for people getting started in programming; it’s like learning to drive in a race car. We’ll look at Visual Studio and its cousin, Visual Studio Code, in Part 3 of this book.

What do I do if I break the program?

Some people worry that things they do with a program on the computer might “break” it in some way. I used to worry about this too, but I’ve conquered this fear by making sure that whenever I do something I always have a way back. You are currently in that happy position. You now know how to get Python on your computer, and it’s unlikely that you’ll damage your Python installation simply by running programs in it. Even if everything goes horribly wrong, and you end up breaking a program such that it won’t work again, you can simply install a clean copy of the program and start again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.193.148