Introducing the Python Programming Language
Introducing the Python Programming Language
Image of ENIAC,
ENIAC,
ENIAC,the
the
thefirst
first
firstgeneral
general
generalpurpose
purpose
purposecomputer
computer
computer.
computer The original uploader was TexasDex at English
Wikipedia. - Transferred from en.wikipedia to Commons by Andrei Stroe using CommonsHelper, CC BY-SA
3.0, https://commons.wikimedia.org/w/index.php?curid=6557095
https://commons.wikimedia.org/w/index.php?curid=6557095
https://commons.wikimedia.org/w/index.php?curid=6557095.)
https://commons.wikimedia.org/w/index.php?curid=6557095
Yet for all their computational power, computers have one fundamental limitation: they have no
agency, no inherent ability to make decisions about what to do. All they can do is take a set of
(possibly very complex!) instructions, what we call a computer program, and execute those
instructions—no more, and no less. And so if we, as computer scientists, want to harness the
awesome power of computers, we need to learn how give these instructions in a way that a
computer understands.
We need to learn how to speak to a computer.
A program is simply the text of the instructions we wish to tell the computer to execute. We call
this kind of text program source code, or code for short, to differentiate it from other forms of
text. To write programs in a particular programming language, we need to understand two key
properties of the language. The first is the syntax of the language, which is the name we give to
the rules governing what constitutes a valid program in the language. 1 Before a computer can
1 Programming language syntax is analogous to the rules of grammar governing human languages, which tell us how
words are meant to be put together to form sentences.
execute a program, it must read the instructions for the program; the syntax of the programming
language specifies the expected format of these instructions.
The second concept is the semantics of the programming language, which refers to the rules
governing the meaning of different instructions in the language. 2 Once the computer has read the
2 This is analogous to the meanings of words in a human language.
instructions in a program, it begins executing them. The language semantics specifies what the
computer should do for each instruction.
Now, neither our computer hardware nor operating system understand the Python programming
language directly. Instead, the creators of Python wrote a program called the Python interpreter,
whose job is to take programs written in the Python programming language and execute the
instructions. 3 You can think of the Python interpreter as a mediator between you the programmer,
3 So when you “download Python” onto your computer, what you’re actually downloading and installing is this
Python interpreter program.
writing instructions in Python code, and the computer hardware that actually executes
instructions.
There are two ways of writing code in the Python language and giving that code to the Python
interpreter to execute it. The first way is to write full Python programs and save them as text
files, 4 and then tell the Python interpreter to run that file. This is the standard way of creating
4 Python programs use the .py file extension to distinguish them from other kinds of text files, like .txt files that
you might have seen before.
Python programs: write the instructions, and then run them with the interpreter.
The second way is to run the Python interpreter in an interactive mode, which we call the Python
console or Python shell. In this mode, we can write small fragments of Python code and ask the
Python interpreter to execute each fragment one at a time. 5 The Python console is useful for
5
We’ll make the meaning of the word “fragment” more precise in the next section!
experimenting and exploring the language, as you get quick feedback after every single instruction
you write. The drawback is that interactions with the interpreter in the Python console are lost
every time you restart the console. So we’ll use the following approach through the course: use the
Python console to learn about and experiment with the Python language, and write full programs in .py
files so that you can save and edit them later.
A demo of the Python console inside the PyCharm application. While there are many ways of using the Python
interpreter, we’ll be using PyCharm as the standard way of creating and running Python programs throughout
this course.
CSC110/111
CSC110/111Course
CSC110/111 CourseNotes
Course NotesHome
Notes Home
Home