• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Download and Install Python

Download and Install Python

Author: PFB Staff Writer
Last Updated: May 25, 2020

Overview

Python is a interpreted language which means that the code is translated
(interpreted) to binary code while the program runs.

That is different from compiled languages (C++ etc.) where the code is first
compiled to binary code.

To run Python code you need to have a Python interpreter. There are different
versions of Python, either Python 2 or Python 3. To see that the difference are
and to decide which one to use, please see this wiki page at python.org.

Installing Python

Python is available on most operating system, (Linux, Unix, Mac OS X and Windows)

Installing it on your computer is very easy, and on some systems it’s already
there. To see if it’s already installed, open up a terminal and run this command
below.

If you see a response from a Python interpreter it will include a version number
in its initial display.

>> python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)

If you don’t have Python installed, you can take a look at this link on how to
install it on the platform that you use. http://www.diveintopython.net/installing_python/index.html

How do I run my code?

There are two ways to run the programs in Python.

Either you type in the code directly in the Python shell. When doing that
you will see the result of every command that you type in.

This works best for very short programs or for testing purpose.

The other way to run the code is within a script.

Python Shell

When you are in the Python Shell, the python interpreter will translate all code for you.

To leave the help mode and return to the interpreter, we use the quit command.

The help command provides some help about Python

>>> help
Type help() for interactive help, or help(object) for help about object.
>>>

You can also use the Python Shell for doing math (please see my earlier post about using math in Python)

>>> 2 + 4
6
>>> 5 * 56
280
>>> 5 - 45
-40
>>> 

To exit the Python Shell, press Ctrl+d.

Python Script

To run the program as script, open an text editor (vi, pico, nano etc.)
and put in the following code:

#!/usr/bin/python  
print "hello world"

Save the file as hello.py and exit the editor.

# To execute the program, type python and the file name in the shell. 
$python hello.py

The output should be:
hello world

Python scripts can be made directly executable, like shell scripts, by putting the
shebang at the beginning of the script and give the file an executable mode.

The shebang is meant for the script to recognize the interpreter type when you
want to execute the script from the shell.

# The script can be given an executable mode, or permission, using the chmod command:
$ chmod +x hello.py

Now you can execute the script directly by its name.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us