TranslateProject/sources/tech/20190814 How to install Python on Windows.md
DarkSun 23fc3d17dd 选题: 20190814 How to install Python on Windows
sources/tech/20190814 How to install Python on Windows.md
2019-08-15 00:57:57 +08:00

9.5 KiB

How to install Python on Windows

Install Python, run an IDE, and start coding right from your Microsoft Windows desktop. Hands programming

So you want to learn to program? One of the most common languages to start with is Python, popular for its unique blend of object-oriented structure and simple syntax. Python is also an interpreted language, meaning you don't need to learn how to compile code into machine language: Python does that for you, allowing you to test your programs sometimes instantly and, in a way, while you write your code.

Just because Python is easy to learn doesn't mean you should underestimate its potential power. Python is used by movie studios, financial institutions, IT houses, video game studios, makers, hobbyists, artists, teachers, and many others.

On the other hand, Python is also a serious programming language, and learning it takes dedication and practice. Then again, you don't have to commit to anything just yet. You can install and try Python on nearly any computing platform, so if you're on Windows, this article is for you.

If you want to try Python on a completely open source operating system, you can install Linux and then try Python.

Get Python

Python is available from its website, Python.org. Once there, hover your mouse over the Downloads menu, then over the Windows option, and then click the button to download the latest release.

Downloading Python on Windows

Alternatively, you can click the Downloads menu button and select a specific version from the downloads page.

Install Python

Once the package is downloaded, open it to start the installer.

It is safe to accept the default install location, and it's vital to add Python to PATH. If you don't add Python to your PATH, then Python applications won't know where to find Python (which they require in order to run). This is not selected by default, so activate it at the bottom of the install window before continuing!

Select "Add Python 3 to PATH"

Before Windows allows you to install an application from a publisher other than Microsoft, you must give your approval. Click the Yes button when prompted by the User Account Control system.

Windows UAC

Wait patiently for Windows to distribute the files from the Python package into the appropriate locations, and when it's finished, you're done installing Python.

Time to play.

Install an IDE

To write programs in Python, all you really need is a text editor, but it's convenient to have an integrated development environment (IDE). An IDE integrates a text editor with some friendly and helpful Python features. IDLE 3 and NINJA-IDE are two options to consider.

IDLE 3

Python comes with an IDE called IDLE. You can write code in any text editor, but using an IDE provides you with keyword highlighting to help detect typos, a Run button to test code quickly and easily, and other code-specific features that a plain text editor like Notepad++ normally doesn't have.

To start IDLE, click the Start (or Window) menu and type python for matches. You may find a few matches, since Python provides more than one interface, so make sure you launch IDLE.

IDLE 3 IDE

If you don't see Python in the Start menu, launch the Windows command prompt by typing cmd in the Start menu, then type:

`C:\Windows\py.exe`

If that doesn't work, try reinstalling Python. Be sure to select Add Python to PATH in the install wizard. Refer to the Python docs for detailed instructions.

Ninja-IDE

If you already have some coding experience and IDLE seems too simple for you, try Ninja-IDE. Ninja-IDE is an excellent Python IDE. It has keyword highlighting to help detect typos, quotation and parenthesis completion to avoid syntax errors, line numbers (helpful when debugging), indentation markers, and a Run button to test code quickly and easily.

Ninja-IDE

To install it, visit the Ninja-IDE website and download the Windows installer. The process is the same as with Python: start the installer, allow Windows to install a non-Microsoft application, and wait for the installer to finish.

Once Ninja-IDE is installed, double-click the Ninja-IDE icon on your desktop or select it from the Start menu.

Tell Python what to do

Keywords tell Python what you want it to do. In either IDLE or Ninja-IDE, go to the File menu and create a new file.

Ninja users: Do not create a new project, just a new file.

In your new, empty file, type this into IDLE or Ninja-IDE:

`print("Hello world.")`
  • If you are using IDLE, go to the Run menu and select the Run Module option.
  • If you are using Ninja, click the Run File button in the left button bar.

Running code in Ninja-IDE

Any time you run code, your IDE prompts you to save the file you're working on. Do that before continuing.

The keyword print tells Python to print out whatever text you give it in parentheses and quotes.

That's not very exciting, though. At its core, Python has access to only basic keywords like print and help, basic math functions, and so on.

Use the import keyword to load more keywords. Start a new file in IDLE or Ninja and name it pen.py.

Warning: Do not call your file turtle.py, because turtle.py is the name of the file that contains the turtle program you are controlling. Naming your file turtle.py confuses Python because it thinks you want to import your own file.

Type this code into your file and run it:

`import turtle`

Turtle is a fun module to use. Add this code to your file:

turtle.begin_fill()
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.end_fill()

See what shapes you can draw with the turtle module.

To clear your turtle drawing area, use the turtle.clear() keyword. What do you think the keyword turtle.color("blue") does?

Try more complex code:

import turtle as t
import time

t.color("blue")
t.begin_fill()

counter = 0

while counter < 4:
    t.forward(100)
    t.left(90)
    counter = counter+1

t.end_fill()
time.sleep(2)

As a challenge, try changing your script to get this result:

Example Python turtle output

Once you complete that script, you're ready to move on to more exciting modules. A good place to start is this introductory dice game.

Stay Pythonic

Python is a fun language with modules for practically anything you can think to do with it. As you can see, it's easy to get started with Python, and as long as you're patient with yourself, you may find yourself understanding and writing Python code with the same fluidity as you write your native language. Work through some Python articles here on Opensource.com, try scripting some small tasks for yourself, and see where Python takes you. To really integrate Python with your daily workflow, you might even try Linux, which is natively scriptable in ways no other operating system is. You might find yourself, given enough time, using the applications you create!

Good luck, and stay Pythonic.


via: https://opensource.com/article/19/8/how-install-python-windows

作者:Seth Kenlon 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出