{ "cells": [ { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ ".. meta::\n", " :description: Topic: Basic description of the Python programming language, Difficulty: Easy, Category: Background\n", " :keywords: python, install, basics, scripts, interpreter, foundations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Introducing the Python Programming Language\n", "\n", "In this section we will learn\n", "\n", " - What Python is.\n", " - What it means to \"install Python\" on your computer.\n", " - How one writes and executes Python code.\n", "\n", "## What is Python?\n", "**Python is a programming language**. That is, it provides us with a strict set of grammatical rules that correspond to well-defined instructions that a computer will obey. The tremendous value of this is that we can write text documents that are relatively simple and intuitive for humans to read, and yet can inform the computer to perform precise operations. Python **code** is simply text that conforms to the Python language.\n", "\n", "For example, the following text obeys the rules of the Python language:\n", "\n", "```python\n", "x = 2 + 3\n", "print('2 + 3 = {}'.format(x))\n", "```\n", "\n", "According to the Python language, it will instruct the computer to:\n", "\n", " - compute 2 + 3\n", " - assign the resulting value (5) to a variable `x`, in memory\n", " - access the value of `x`, and print to the computer's screen: '2 + 3 = 5'\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An example of code that **does not** obey Python's rules is:\n", "\n", "```python\n", "x = 2 ~ 3\n", "```\n", "because character `~` has no meaning in the Python language when it is placed between two integers.\n", "\n", "To \"learn Python\" thus entails learning the grammatical rules specified by the Python language, and the computer instuctions that they map to. Additionally, you will want to familiarize yourself with many of the convenient tools that are \"pre-written\" and come with the Python language; they are part of the so-called standard library.\n", "\n", "Given this basic understanding of what the Python programming language is, we now want to know how to use our Python code to send instructions to the computer. The most basic way of doing this is to:\n", "\n", " - Write a Python \"script\": a text file containing Python code.\n", " - Pass this text file to a **Python interpreter**, which will instruct the computer to carry out the operations described by the code.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python Scripts\n", "You can save the the valid code from the preceding section in a text file using a simple text editor, and *voilĂ * you have written a **Python script**: a text file containing Python code. It is standard to save this text file using the suffix `.py` (e.g. `my_code.py`), rather than the familiar `.txt` (e.g. `my_text.txt`). There is nothing special about the `.py` suffix; it simply helps differentiate files that contain Python code from run-of-the-mill text files, which contain plain English.\n", "\n", "Although you can use simple text editors to write Python scripts (e.g. notepad (Win), TextEdit (Mac), nano (Linux)), there are much more sophisticated editors that provide an \"integrated development environment\" (IDE) for writing code. An IDE, configured to support Python, will warn you if you have written code that violates Python's grammar rules, similar to the way word processing software warns you if you have made a spelling mistake. More on IDEs in a later section. \n", "\n", "