Learning Python
Introduction
I present here what I think is the simplest start with Python on Windows for non-programmers.
Use English everywhere: for the names of the folders, for the names of your programs, for comments in your programs. English is the language of programming.
Do not use spaces in the names of the folders and in the names of your programs. Everything is easier when there are no spaces in the names, e.g. when you later use Unix tools like "cygwin" on your PC. Consequently do not install Python in the standard Windows directories (i.e. folders) "C:\Program Files" or "C:\Program Files (x86)" because the names contain spaces.
A possible directory structure could be:
- "C:\Programs\Python" for the installation of Python
- "D:\Devl\Python" for your Python programs. (If you do not have the D-drive, then use "C:\Devl\Python".) In any case, it is useful to separate the installation folder from the folder with your programs.
I will refer to this structure in the description below.
Installation on Windows
Download the latest Python version from python.org. This is currently the version 3.9.7. Use "Customize Installation" and proceed according to the following snapshopts.
Press "Install" and agree to the Installation. After a short time you get:
The window shows the links:
Python Shell
The directory "C:\Programs\Python\Python39" was filled with the installation of Python. Open it in the Windows Explorer and double click on "python.exe". The Python shell will open where you can type Python statements that will be immediately executed:
We will not be using this window presently so close it.
It is useful for later convenience to create a shortcut for "C:\Programs\Python\Python39\python.exe" say "PY".
IDLE Shell
In the Windows Explorer navigate to "C:\Programs\Python\Python39\Lib\idlelib" and double click on "idle.py". Two windows will appear on the screen:
The IDLE Shell can be used as a Python "command interpreter". An interpreter can useful for simple calculations but not for programs because everything will be lost after we close the window. We will write Python programs and save them for future usage. To this purpose click on "File/New File":
A new window "untitled" appears.
The First Python Program
Into the "untitled" window type:
# My first Python program
print("Hello World")
The first line starting with "#" is a "comment", that is not executed by Python. The second line is a command to print the "string" (a message) inside the quotes. Use the menu command "File/Save As" to save the window content in the file "HelloWorld.py" in "D:\Devl\Python". The name of the program window changes to "HelloWorld.py". Python programs (also called "scripts") should have the extension ".py".
Select the menu item "Run/Run Module" (or press F5) to execute the program. The string "HelloWorld" is written onto the Shell window.
Congratulations - you developed your first Python program!
When you remain with IDLE, then create a shortcut (say IDLE.py) to "C:\Programs\Python\Python39\Lib\idlelib\idle.py" and put it on your desktop.
IDLE is then started by a double click on the icon.
Notepad++ and a Windows Shell
Probably all developers on Windows have the excellent, free editor "Notepad++" and I recommend to download it. Use the "Installer" on the download page and install the latest available version (currently 8.1.4 - 32-bit) in "C:\Programs\Notepad++". You can write your Python programs with this editor and save them in your development folder ("D:\Devl\Python").
For easy access create a shortcut for "C:\Programs\Notepad++\notepad++.exe", name it "Notepad++" and put it on the desktop. Double click this shortcut and in the Notepad++ open your "HelloWorld.py" program.
Modify your program as you like and save it again. Execute this program using "python.exe" (or using the shortcut "PY" if you have defined it) in a Windows command shell. A simple method is to start the "Windows Power Shell". Right click on the Windows Start Menu on the left bottom of the screen and select "Windows Power Shell". Then navigate to the directory with your Python programm. Execute the program as "PY HelloWorld.py" (or "python.exe HelloWorld.py").
If you do not have the "Windows Power Shell" then use the standard "cmd" window. Right click on the Windows Start Menu and select "Run", then enter "cmd" into the Open field and press OK.
In the command shell navigate to the directory with your Python programm. Execute the program as "python.exe HelloWorld.py".
Further Steps
- Google for "Learning Python" on the Internet (bur do not loose too much time with it - there are tons of resources).
- Download the free book Think Python by Allen Downey and study it.
- If you get more familiar with Python and want to develop larger projects consider using an IDE (Integrated Development Environment), e.g.
Links
- Getting Started (python.org)
- Online Python
- Python for non programmers
- Python Tutorial (https://ir.cwi.nl/pub/5007/05007D.pdf) by Guido van Rossum, the creator of Python. It covers the older version Python 2 (which differs slightly from Python 3) but is still an excellent introduction to Python.
New Comment