Sitemap

What Is Python?

11 min readSep 25, 2023

--

Python is a general-purpose programming language. That means it was designed and
developed to write software for a wide variety of disciplines. Python has been used to
write applications to solve problems in biology, chemistry, financial analysis, numerical
analysis, robotics, and many other fields. It is also widely used as a scripting language for
use by computer administrators, who use it to capture and replay sequences of computer
commands. It is different from a language like HTML (HyperText Markup Language),
which was designed for the single purpose of allowing people to specify the layout of a
web page.
Once you learn the basic concepts of a programming language like Python, you
find that you can pick up a new computer languages very quickly. No matter what the
language (and there are many) the underlying concepts are very similar. The key things
that you learn about—variables, assignment statements, if statements, while loops,
function calls—are all concepts that are easily transferable to any other programming
language.

Installing Python

Python was created in the 1990s by Guido van Rossum. He is affectionately known as
Python’s Benevolent Dictator for Life. The language has two current versions: 2.7 and
3.6. Version 2.7 is still widely used, but its “end of life” has recently been announced.
Therefore, this version of the book will use the newer Python 3, as it is known. With
respect to the contents of this book, there are only a few differences between the versions
of the language. Where appropriate, I point out how something presented in Python 3
was handled in Python 2.
Python is maintained as an open source project by a group called the Python
Software Foundation. Because it is open source, Python is free. There is no single
company that owns and/or sells the software. You can get everything you need to write
and run all the Python programs in this book by simply downloading Python from the
Internet. I’ll explain how you can get it and install it.
The center of the Python universe is at www.python.org.
Bring up the browser of your choice and go to that address. The site changes over
time, but the essential functionality should remain the same. On the main page, there
should be a Downloads button or rollover. Once you’re in the Downloads area, you should be able to select Windows, Mac, or Other Platforms (which includes Linux).
After choosing your operating system, you should get an opportunity to choose between
versions 3.x.y (whatever is the current subversion of Python 3) and version 2.x.y
(whatever is the current subversion of Python 2). Choose version 3.x.y.
Clicking the button downloads an installer file. On a Mac, the downloaded file has
a name like python-3.6.4-macosx10.6pkg. On a Windows computer, the file has a
name like python-3.6.4-msi. On either platform, find the file that was downloaded and
double-click it. That should start the installation process, which should be very simple.

IDLE and the Python Shell

There are many different software development environments (applications) that you can use to write code in Python. It may seem odd that you use a program to write a program, but that’s what a software development environment is. Some of these environments are free; others can be costly. They differ in the tools they offer to help programmers be more
efficient.
The environment we will use in this book is called IDLE. You might think that IDLE is an acronym, maybe Interactive DeveLopment Environment. When the name was chosen, it didn’t mean anything. In fact, the name Python doesn’t refer to the snake.
Apparently, Guido van Rossum was a big fan of Monty Python’s Flying Circus, a TV series by a well-known comedy group from Britain, and he named the language after them. One of the founding members of Monty Python was Eric Idle. The name IDLE is a reference to him.
IDLE is free. When you download and run the Python installer, it installs IDLE on
your computer. Once installed, you can find IDLE on a Mac by opening the applications folder and locating the folder named Python 3.x. Once you open it, you should see the
IDLE application. To open IDLE, double-click the icon. On Windows, IDLE is installed
in the standard Program Files folder. If your version of Windows has a Start button, click the Start button and type IDLE in the type-in field. Otherwise, you might have to do a Control+R or Control+Q to bring up a dialog box where you can type IDLE. However you
open IDLE, you should see a window with contents that look something like this:
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>>
This window is called the Python Shell. In fact, the title of the window should be Python 3.x.y Shell.

Hello World

There is a tradition that when programmers learn a new computer language, they try
writing what is called the Hello World program. That is, just to make sure they can get something to work, they write a simple program that writes out “Hello World!”
Let’s do that now with Python. The Python Shell (commonly just called the Shell) gives you a prompt that looks like three greater-than signs. This is called the chevron prompt or simply the prompt. When you see the prompt, it means the Shell is ready for you to type something. Throughout this book, I strongly encourage you to use the IDLE
environment by trying out code as I explain it. At the prompt, enter the following:
>>> print(’Hello World!’)
Then press the Return key or Enter key. When you do, you should see this:
>>> print(’Hello World!’)
Hello World!
>>>

Get Sam’s stories in your inbox

Join Medium for free to get updates from this writer.

Congratulations! You have just written your first computer program. You told the computer to do something, and it did exactly what you told it to do. My work is done here. You’re not quite ready to add Python programmer to your résuméand get a job as a professional computer programmer, but you are off to a good start!

Note - If you don’t like the font and/or size of the text used in the Shell, you can
choose IDLE ➤ Preferences (Mac) or Configure IDLE (Windows) and easily change
either or both.

One of the key advantages of the Python language is how readable it is. The program
you just wrote is simply the word print, an open parenthesis, whatever you want to be printed (inside quotes), and a closing parenthesis. Anyone can understand the Hello
World program written in Python. But to make this point very clear, let’s see what you
have to do to write the Hello World program in some other popular languages.
You’ve probably heard of the language called C, perhaps the most widely used programming language in the world. Here is what you have to write in C to get the same
results:
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}
Notice all the brackets, parentheses, braces, and semicolons you need to have, along with how many lines you have to write?
There is another language called C++, which is a modification of the original C language to give it more power. Here’s what the Hello World program looks like in C++:
#include "std_lib_facilities.h"
int main()
{
cout << "Hello World!\n";
return 0;
}
Not surprisingly, it also has many brackets, parentheses, braces, and semicolons.
Finally, here is the same Hello World program written in Java, yet another popular
computer language:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Again, there are many brackets, parentheses, and semicolons, and many words with meanings that are not immediately obvious.
By comparison, notice how English-like, simple, and readable the Python version
is. This readability and simplicity are big reasons why Python is growing in popularity,
especially as a language used to teach programming to beginners.

Creating, Saving, and Running a Python File

So far, you have only seen a single line of Python code:
>>> print(’Hello World!’)
You typed it into the Shell and pressed Enter or Return to make it run. Typing one line at a time into the Shell is a great way to learn Python, and it is very handy for trying out things quickly. But soon I’ll have you writing programs with tens, hundreds, and maybe thousands of lines of code. The Shell is not an appropriate place for writing large programs. Python, like every other computer language, allows you to put the code you
write into a file and save it. Programs saved this way can be opened at any time and run
without having to retype them. I’ll explain how we do this in Python.
Just like any standard word processor or spreadsheet program, to create a new file in
IDLE, you go to the File menu and select New File (denoted from here on as File ➤ New
File). You can also use the keyboard shortcuts Control+N (Windows) or Command+N
(Mac).
This opens a new, blank editing window, waiting for you to enter Python code. It behaves just like any text editing program you have ever used. You enter your Python code, line by line, similar to the way that you did it in the Shell. However, when you
press Return or Enter at the end of a line, the line does not run—it does not produce immediate results as it did in the Shell. Instead, the cursor just moves down to allow you to enter another line. You can use all the standard text-editing features that you are used to: Cut, Copy, Paste, Find, Replace, and so on. You can move around the
lines of code using the arrow keys or by clicking the mouse. When a program gets long enough, scrolling becomes enabled. You can select multiple lines using the standard click-and-drag or click to create a starting point and Shift-click to mark an ending point.
Let’s build a simple program containing three print statements. Open a new file.
Notice that when you open the file, it is named Untitled in the window title. Enter the
following:
print(’I am now entering Python code into a Python file.’)
print(’When I press Return or Enter here, nothing happens.’)
print(’This is the last line.’)
When you type the word print, IDLE colorizes it (both here in the editing window
and when you type it in the Shell). This is IDLE letting you know that this is a word that
it recognizes. IDLE also turns all the words enclosed in quotes to green. This also is an acknowledgement from IDLE that it has an understanding of what you are trying to say.
Notice that when you started typing, the window title changed to *Untitled*. The
asterisks around the name are there to show that the contents of the file have been changed, but the file has not been saved. That is, IDLE knows about the new content, but the content has not yet been written to the hard disk. To save the file, press the standard
Control+S (Windows) or Command+S (Mac). Alternatively, you can click File ➤ Save.
Because this is the first time the file is being saved, you see the standard Save dialog box.
Feel free to navigate to a folder where you are able to find your Python files(s), or click the New Folder button to create a new folder. In the top of the box, where it says “Save As”, enter a name for this file. Because we are just testing things out, you can name the file Test. However, Python filenames should always end with a .py extension. Therefore,
you should enter the name Test.py in the Save As box.

Note - If you save your Python file without a .py extension, IDLE will not recognize it as a Python file. If Python does not know that your file is a Python file, it will not colorize your code. This may not seem important now, but it will turn out to be very helpful when you start writing larger programs. So make it a habit right from the
start to always ensure that your Python file names end with the.py extension.

Now that we have a saved Python file, we want to run, or execute, the statements in
the file. To do that, click Run ➤ Run Module or press the F5 shortcut key. If everything went well, the program should print the following in the Shell:
I am now entering Python code into a Python file.
When I press Return or Enter here, nothing happens.
This is the last line.
Now let’s quit IDLE by pressing Control+Q (Windows) or Command+Q (Mac) keys.
Alternatively, you can click IDLE ➤ Exit (Windows) or IDLE ➤ Quit IDLE (Mac).
When you are ready to open IDLE again, you have choices. You can open IDLE by typing IDLE into the Start menu (Windows) or by double-clicking the IDLE icon (Mac).
If you then want to open a previously saved Python file, you can click File ➤ Open and
navigate to the file you want to open.

However, if you want to open IDLE and open a previously saved Python file, you can
navigate to the saved Python file (for example, find the Test.py file that you just saved)
and open IDLE by opening the file. On Windows, if you double-click the icon, a window
typically opens and closes very fast. This runs the Python program, but does not keep
the window open. Instead, to open the file and IDLE, right-click the file icon. From the context menu that appears, select the second item, Edit with IDLE.
On a Mac, you can simply double-click the file icon. If double-clicking the Python
file opens a program other than IDLE, you can fix that with a one-time change. Quit whatever program opened. Select the Python file. Press Command+I (or click File ➤ Get Info), which opens a long dialog box. In the section labeled “Open with”, select the IDLE application (IDLE.app). Finally, click the Change All button. Once you do that, you should be able to double-click any file whose name ends in .py, and it should
open with IDLE.
Programming typically involves iterations of edits to one or more Python files. Each time you make changes and you want to test the new code, you must save the file and then run it. If you don’t save the file before you try to run it, IDLE will prompt
you by asking you to save the file. You’ll quickly become familiar with the typical
development cycle of edit your code, save the file (Command+S or Control+S), and run the program (F5).

IDLE on Multiple Platforms

One other very nice feature of Python and IDLE is that the environment is almost
completely platform independent. That is, the IDLE environment looks almost identical on a Windows computer, Mac, or Linux system. The only differences are those associated with the particular operating system (such as the look of the window’s title bar, the location of the menus, the look of the dialog boxes, and so on). These are very minor details. Overall, the platform you run on does not matter. Perhaps even more importantly, the code you write is platform independent. If
you create a Python file on one platform, you can move that file to another platform and it will open and run just fine. Many programmers use multiple systems to develop Python code. In fact, even though I typically develop most of my Python code on a Mac, I often bring these same files into classrooms, open them, teach with them on Windows systems.

Summary

In this chapter, you got up and running with Python. You should now have Python installed on your computer and have a good understanding of what the IDLE environment is. You built the standard Hello World program in the Shell, and then used the editor window to build, save, and run a simple multiline Python program (whose name ends in .py) made up of print statements. Finally, you learned that Python and
the IDLE environment are platform independent.

--

--

Sam
Sam

Written by Sam

0 followers

Writer of unknown things.