Objective
|
LessonGetting Python
Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit (In tel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> Here is another example on a 64-bit Windows 7. Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:16:31) [MSC v.1600 64 bit (AM D64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> If you need to install Python, you should download the most recent stable version. Python 2.7.9 release is the latest in the 2.X series, and Python 3.4.3 is the latest in the 3.X series. You'll find Python's major implementation, CPython, available on Python's website.
Online InterpreterIf you feel concerned about downloading Python onto your computer or you just can't do that, then you're in luck. The Python Software Foundation offers an interactive Python shell from PythonAnywhere on their website. It includes more implementations than just CPython, plus you don't need to install it to use it. On the downside, you cannot execute scripts and files from the shell, which will hinder more advanced Python topics that you'll see later on in the resource. The ShellThe shell, command prompt, prompt, command line, or whatever you like to personally call it, is one of the mediums of communication between you and the Python interpreter. Through out much of this resource, you'll see examples of Python code being used in a shell. This is what the shell may look like when you run interactive Python on your computer. Python 3.4.2 (v3.4.2:ab2c023a9432, Oct 6 2014, 22:15:05) [MSC v.1600 32 bit (In tel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> You may not get the first couple of lines exactly as shown above, since everyone has different versions, computers, and builds. As you've probably guessed, the interpreter tells us the Python version, date of build, the compiler used to build it, the computer's architecture, and the operating system. This information is important for debugging and is useful for checking Python's version. The most important thing that you'll need to know are those three greater-than signs ( >>> print("Hello, world!")
Hello, world!
>>> if True:
... print("Hello, world!")
...
Hello, world!
To exit interactive python: >>> quit()
Python ScriptYou will eventually find it tedious to retype programs into your shell. Like other interpreted programming languages, you can store and execute Python code from a file. There are two main types of Python files, plain text files and bytecode files. Plain text files are simple files that you can create and edit with just a simple plain text editor, like Notepad, Notepad++, or Python's IDE, IDLE. The other type of file, bytecode files, are compiled CPython files. Don't worry about this type of file, which will be discussed later on in this course.
print("Hello world, from a Python script!")
input("Press enter to continue . . . ")
IDLEIDLE, or Integrated Development Environment, is the default IDE that comes when you install CPython. On windows, it can be accessed by right clicking the Python script and then by clicking on Edit with IDLE. An alternative is to open IDLE via the shell using the command: When working with IDLE, you'll notice a couple things. First, you'll notice that your code is colored. By default, strings are green, comment red, built-in functions purple, etc. This allows you to see certain parts of your code. This gives you an advantage over monocolored text. Secondly, you'll notice that IDLE will make automatic indentations when your text is indented. IDLE will also convert presses of the tab key to four spaces. Although the default settings are great, not all people will like them. Therefore, IDLE is extremely customizable and can be changed as needed. For example, this gives a color-blind person the ability to change the colored text to something more friendly. You can change IDLE's default settings under
The Command Line
$ python3.6 -V Python 3.6.2 $ On Unix the $ man python3.6 PYTHON(1) PYTHON(1) NAME python - an interpreted, interactive, object-oriented programming language SYNOPSIS python [ -B ] [ -b ] [ -d ] [ -E ] [ -h ] [ -i ] [ -I ] [ -m module-name ] [ -q ] [ -O ] [ -OO ] [ -s ] [ -S ] [ -u ] [ -v ] [ -V ] [ -W argument ] [ -x ] [ [ -X option ] -? ] [ -c command | script | - ] [ arguments ] et cetera, et cetera.... Python commands may be executed on the command line with option $ python3.6 -c 'print("hello, world!")'
hello, world!
$
$ python3.6 -c 'import os ; print(os.getcwd())'
/Users/ThaniosAkro/SwareDev/python
$
$ python3.6 -c 'from decimal import *; getcontext().prec = 100; print (str(Decimal('1')/Decimal('3')))'
0.3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333
$
A python script may be invoked by python on the command line: $ python3.6 test.py >test.sout 2>test.serr When invoked as above the script
|
Assignments
|
|
Footnotes
- ↑ When you right-click your mouse on the desktop or in an explorer window, you'll be able to select a new file to create. If you have experience working with Windows registries you can add a Python file into New. I should warn you now, it can be dangerous editing your registries, so do so at your own risk. If you're still intent on doing this, run the program regedit.exe either from Run... or from the command prompt. When the program opens open My Computer, then open HKEY_CLASSES_ROOT. From here, scroll down until you find .py and open it. Then right-click .py, move the mouse over new and then select Key. Name this Key ShellNew. Now right-click ShellNew and add a New String this time. Name it NullFile and leave its value empty. There, you've done it! It probably won't work at first, so you'll need to restart the computer.
References
- ↑ http://wiki.python.org/moin/Python2orPython3
- ↑ http://wiki.python.org/moin/Python2orPython3
- ↑ On a pre Windows 8 machine, the shell is opened from the w:Start menu