«

»

How to Use Python

Python is a popular programming language known for being simultaneously powerful and easy to learn and use. Several projects on this site use Python. This page will show how to set up and use a Python environment on your computer, not how to use the language itself. If you don’t know how to code in Python and want to learn, take a look at any of the many Python tutorials available for free online, like this one.

Set Up Python

Windows

  1. Download the installer from the official Python website. The exact version number may vary from what is shown here, but it should look like 3.X.X
  2. Run the downloaded file, following the instructions onscreen. Make sure that you check the box to add it to PATH. This is essential to running Python easily from a command prompt.
  3. Open a command prompt. This can be done by searching “cmd” in the Start menu, or pressing Windows+R and typing “cmd”.
  4. Type python --version at the command prompt to verify that Python was installed. You should get a version number 3.X.X. If you instead get an error, either Python was not installed correctly or it was not added to PATH.

Install Modules

Modules are sets of code pre-written for doing common tasks, like creating graphs, loading images, or doing certain kinds of math. Some modules come pre-installed with Python, but some Python scripts require you to install certain modules from the internet before they can run. Luckily, installing modules is very easy.

  1. Make sure your computer is connected to the internet.
  2. Open a command prompt and type python -m pip install NameOfTheModule. Wait until the installation process returns you to the command prompt.
  3. You’re done! You only have to do this once for any module required by a script.

Create, Edit, and Run Python Scripts

IDLE

The Python installer, in addition to installing Python, installed a program called IDLE. IDLE is an Integrated Development Environment, meaning that it gives you a set of tools to create, edit and run Python scripts.

  1. Open IDLE.
  2. This window is called the “shell.” You can execute single lines of Python code at a time by typing them into the prompt and hitting Enter/Return. Start out by trying simple commands like print("hello") or sum([1, 4, 6, 7]) to get the hang of it.
  3. While typing directly into the shell is good for doing relatively simple tasks, you will quickly find that doing anything very complicated or repeating previous commands takes a lot of retyping and/or copy-pasting. Luckily for you, there is a way to save many lines of code at once and have them executed in order on command without having to retype them.
  4. In the shell menu, go to File -> New File to create a new Python code (.py) file. A blank editor window should come up. Notice that your shell window is still open – don’t close it; you’ll need it!
  5. Now, type in some Python commands, one on each line. You can use as many lines as you want; the commands will be executed in order, starting with the first line and ending with the last.
  6. To run your code, go the the menu option Run -> Run Module. Name and save your file when it prompts you to do so. Notice that the file extension it is saved as is .py, signifying that it is a Python program. You can open, edit and run any .py file with IDLE
  7. When you run your file, your shell window will be brought to the front, a == RESTART == line will appear, and the output of your code will be displayed in blue. When your program finishes, the prompt >>> will appear again and you can type in the shell again like normal.
  8. Tip: if you ever want to stop your running program and get back to the shell prompt immediately, hit Ctrl-C, Command-C, or Shift-F6.

Command Prompt

While IDLE will run any Python file, sometimes you may want to run a .py file from the command line. Perhaps you are working on someone else’s computer and IDLE isn’t installed, or you already have a command prompt open, or you just like working from a command prompt better. In any case, it’s a good skill to know. Assuming that you have already installed Python on your computer:

  1. Open a command prompt.
  2. Find the file path to the .py file you want to run. It should look something like C:/This/Is/A/Windows/File/Path/ or /This/Is/An/Apple/File/Path/
  3. In the command prompt, type cd "/Your/File/Path/", including the quotes. This cd command tells the prompt where to look for your Python file.
  4. Finally, type python YourFileName.py. The output of that file will show in the command prompt, like it did in the IDLE shell.
  5. Alternately, instead of using the cd command and then the python -m command in steps (3) and (4), you could type python "/Your/File/Path/YourFileName.py". Either way works.

1 ping

  1. Maze Traversal – The Code Fair

    […] How to Use Python » […]

Comments have been disabled.