PYTHON MADE SIMPLE (2015)
Chapter 1: Setting Up Python on Your computer
To be able to use python, you need to ensure that your computer has all the requirements needs to run python. This chapter offers you all the instructions you need to follow to get started. There are different settings for python depending with the computer you want to use.
If you are using Mac OS X computers: You will not need to install Python program since you already have a Python 2. Neither will you need a any other version such as Python 3.
You many need other programs such as PowerShell if you are going to use windows, Terminal when using OS X and bash if you prefer using Linux. But if this is your first time using this program, then you will need to learn how they can be used first.
However, the most basic requirements include having Python program and everything starts here:
How do I install python?
This is a very simple process
· First, download the latest version of python and for compatibility with other programs that you may need, use the product version 2.7.1 of python. Find it in the following link: Python Download.
· Do you know how to install a program on a computer? Yes, just double click the icon downloaded from your download folder and wait for further instructions.
· But, ensure that you accept all the default settings of the program till you are asked to press the finish button.
· The program will then inform you if the installation is completed.
Getting ready to use Python
· Check to see that all programs are in the right locations: start from My Computer, locate the directory C: \ python 27. You should be able to locate all the python program files here.
· Next, copy all the details of the directory beginning from C: to 27 and exit from the window.
· Then, go for the start button, and right click on My Computer.
· Select properties.
· Select; Advanced System Settings.
· Click on Environment Variables.
· In that folder, search for the folder variable path.
· Go for the file called Path and right click on it, select edit.
· Search for the field called Variable Value using the right arrow.
· Introduce a semi colon at the end of the word and past the directory path of the python folder you had copied previously.
· Then press on the okay button.
How to create a first Python Program
· Go to start, and then my computer
· Select drive C and click on it.
· Click on the Python 27 folder.
· Next, right click on the space and create a new folder.
· Name the new folder Python Programs
· Save all the new programs you create in this folder.
· Then go to Start and type Run in the Search box at the bottom and press ENTER.
· Next, type notepad in the new window field referred to as Open.
· Type the following words in Notepad just as it is written here:
# File: Hello.py
print "Hello World!"
· Go for the file in the control panel and select Save As.
· In the section Save in browse for the C: drive and then select the folder PythonPrograms.
· For the field File name remove everything that is there and type in Hello.py.
· In the field Save as type select All Files
· Click on Save. You have just created your first Python program.
Run the program you have just created
· Click on the Start button and click on Run.
· Input cmd in typing field followed by OK.
· A new dark small window will pop up.
· In the new window, type cd C:\ and then press the key Enter.
· However, in case you type dir. You will get a list of all the directory folders in the drive C:\.
· Search for the folder labeled PythonPrograms created by you in the previous step.
· Next, type cd PythonPrograms and press the Enter button. This is a direct way of accessing the python programs folder.
· Just by typing dir and you are supposed to see the file named Hello.py.
· Try running the program by typing python Hello.py followed by Enter.
· The output should be a single line reading Hello World!
· This is your first program, Bravo!! You have just been able to create and run your first Python program.
Python for Mac Users
When using Mac, Python comes already installed with your Mac OS X. The versions however may vary. You can download a new version of python when the version you have is a very old. This can be accessed by following the hyperlink here Download. This is one of the latest binary versions of Python. It is able to runs on both Power PC and Intel systems. After the download is complete, install it on your computer.
You are now ready to create your first program!
How to start IDLE on Mac
1. While in windows we talk of the start button
2. In Mac, it is initiated from the Terminal window, and then typing python. This will start the python shell automatically.
3. Use the >>> to prompt for Python shell.
4. Then, you can type in the python shell prompt import idlelib.idle
5. This will activate the IDLE IDE
Python IDLE on Windows and Mac
· Go to my programs
· Locate python on the list of programs
· Select IDLE to start IDLE
· Then go to File, and select New Window
· Type your program in “Hello World”
· Next, locate the File menu and click on Save. Type in Helloworld.py
· This allows it to be saved as a plain text file. The extension of the file name (.py)helps to enable the python text reader to be able to open it and read it. It also enables other programs which are text editors such as Notepad, Notepad+++ or TextEdit to read the program.
· After saving the program, go to Run and select Run Module or use the F5 key on your computer to run your program.
· The output should read: Hello world!
If using Mac OS X
The following will need to be done:
1. Using your browser, download the TextWrangler to be used as a text editor and install it on your computer. This can be acceded on http://www.barebones.com/products/textwrangler/
2. The editor (TextWrangler) to be placed in the docks where it can be accessed very easily.
3. Go to the terminal program; search for the TextWrangler and you should be able to find it with ease.
4. Next, place you terminal in your docks too.
5. Then, try to run your Terminal program.
6. In the terminal program, run python. This is performed by typing the things you need done and hitting return. So, type Python and hit RETURN.
7. Next, try to exit Python by Typing quit(), Enter. This will exit you from Python.
8. This will Return you to a similar prompt that was there in the program even before you entered Python. If not there, seek to find out why this is not there.
9. You may also need to learn how one can make a directory in the Terminal.
10. Moreover, find out how the directory can be changed from the Terminal.
11. A file can also be created using your editor in the directory. Follow the file, then, Save or use Save As and then locate the directory you have created for saving your Programs.
12. Try also, getting back to the terminal using your keyboard by switching windows.
13. Get back to the Terminal and try to locate your newly created file.
1. You many also need to have notepad++. This can be accessed from http://notepad-plus-plus.org/ on your browser. This is an alternative text Editor. Install it on your computer. This does not require you to be the administrator.
2. Place it on the desktop or start button where it can be easily accessed or in Quick Launch. Select the most preferable options during installation.
3. Similarly, run PowerShell from the Start menu. Search for it and you can just press Enter to run it.
4. It is easier to access the program by using a short cut on the desktop and using a quick launch for convenience.
5. In addition, use the PowerShell program which is also the Terminal program.
6. In the PowerShell program, initiate python. Just like in the other terminal programs, you run programs by typing the name and then press Enter.
1) In case the python installed is not responsive, download it again and install it afresh from the following link http://python.org/download.
2) The best version is Python 2.7.5. The latest Version many not be compatible with some of the programs you will need to use here, therefore, don’t go for Python 3 and any other.
3) In case you do not have Administrative rights, you will be better off using the ActiveState Python.
4) Check to ensure that the Python program just installed is in good working condition. If not, use PowerShell to try if it can be activated by typing the following:
5) Then close PowerShell and restart the Program to confirm whether the Python now runs. In case it is not working, you will need to Restart your computer to integrate all the programs.
7. Next, exit PowerShell by typing quit() followed by Enter to move out of python.
8. Check if the initial prompt has been retained even before you typed python in the program. In case it is not there, seek to solve the bug in the program.
9. In the same way like TextWrangler which is also a text Editor like PowerShell, find out how you can make a directory directly in the PowerShell (Terminal).
10. Similarly, try to change into a directory in the PowerShell (Terminal).
11. Moreover, find out if you can be able to create a file in this directory using file, Save or Save As and locate the directory to which it will be stored.
12. Also, while in the PowerShell (Terminal), try switching windows using your keyboard. Search it up if you find a problem figuring it out.
13. In the PowerShell (Terminal), try to list the directory just to check if the newly created file is in the directory.
The word terminal as used in this content refers to either the “Shell” or “PowerShell” in this book.
Most of the time, installing Python in Windows may face a few challenges. It may fail to get configured to the right path. This can be solved by typing the following text in the python PowerShell
Then, either Restart the PowerShell or the whole computer so as it gets fixed.
Windows: What You Should See
Just by opening Python
This is what you should see
To find help, Type help ()followed by enter. This is what you will see!
For the previous versions of Python; this is what you will see as the output:
It is still correct if you see different information than mine, but yours should be similar.
Python on Linux
Linux is a unique operating system that comes with a very different ways of installing programs on it. But, installing any software on any computer requires your ability to follow instructions as directed by the software during installation. Default settings are always recommended and vary with the OS to be used.
My assumption is that if have Linux, then, you probably understand how to install a package. This is how this can be done:
1. The best text editor in Linux is the Gedit text editor.
2. It is installed using the Linus package manager
3. Place the program gedit on the desktop as a short cut to the program or pin it on the start button for easy access.
1) Run gedit to see whether the program is fully configured or it will need some quick fixes. Ensure the program has been set to the default mode.
2) Next, get to Preferences and select the Editor tab.
3) Then, edit the tab width to 4.
4) Check on the box representing the part Insert spaces instead of tabs.
5) Ensure that Automatic Indentation is turned on.
6) Similarly, go to open in the file control menu, select View tab, then, check “Display line numbers" to turn it on.
4. Locate your terminal program.
5. It may be referred to as GNOME Terminal, Konsole, or xterm.
6. Transfer the Terminal to your docks.
7. Try a run of your Terminal program.
8. Run python in the terminal program by typing required command and pressing Enter.
1) If your python is not responsive or missing in your Terminal. Download it address and install it. It is recommended that you try getting python version 2 and not 3.
9. Then, exit python by typing quit() and press Enter to exit from python.
10. This should return you to the python prompt before you typed Python. Incase this does not take place, try to fix the bug.
11. Try making a directory from the Terminal.
12. In addition, try changing into a directory in Terminal.
13. Create a file using your editor and save it in the directory created. This can be done by using the file and then Save or Save As. to the directory required.
14. Try shifting between windows to get to the directory.
15. Confirm in the directory whether the program created is listed.
How to find solutions in the internet
The internet search engine is the best place for you to find solutions on Python programming. Most of the challenges may have already been done and solved by people and posted online. Hence, in some parts in this book; I will from time to time provide direction on how best to find solutions on the search engines.
This is the best way by which you can become an independent learner in programming. The ability to trouble shoot and find solutions shows the potential of being a good programmer in the near future. This is the main goal of this book. Allowing you to exploit your potential and become the best in Python programming.
Google is the market place for all this information. Thanks to the World Wide Web developers. You can find a response to everything you need.
For instance, if you want to download Python software:
1. Go to http://google.com/
2. Type: python download
3. Read the websites listed to find the best answer.
A sample screenshot of this search on Google is attached below: