Contents | Next (1.2 A First Program)
Python is an interpreted, high-level, dynamically and strongly typed, garbage-collected, multi-paradigm, general-purpose, object-oriented, platform-independent (cross-platform), "batteries-included" language. It is often classified as a "scripting language" and is considered similar to languages such as Perl, Tcl, or Ruby. The syntax of Python is loosely inspired by elements of C programming.
Python was created by Guido van Rossum around 1990 who named it in honor of Monty Python.
This course uses Python 3.10, installed and managed with uv. See Course Setup for installation instructions. Python 3.10 is used in the notes and solutions.
In the words of Python's creator:
My original motivation for creating Python was the perceived need for a higher level language in the Amoeba [Operating Systems] project. I realized that the development of system administration utilities in C was taking too long. Moreover, doing these things in the Bourne shell wouldn't work for a variety of reasons. ... So, there was a need for a language that would bridge the gap between C and the shell.
- Guido van Rossum
Although there are many environments in which you might run Python, Python is typically installed on your machine as a program that runs from the terminal or command shell. From the terminal, you should be able to start an interactive session like this (we use ptpython, a friendlier shell, launched through uv):
bash $ uv run --with ptpython ptpython
>>> print("hello world")
hello world
>>>
If you are new to using the shell or a terminal, you should probably stop, finish a short tutorial on that first, and then return here.
Although there are many non-shell environments where you can code Python, you will be a stronger Python programmer if you are able to run, debug, and interact with Python at the terminal. This is Python's native environment. If you are able to use Python here, you will be able to use it everywhere else.
On your machine, start Python (launch the interactive shell with
uv run --with ptpython ptpython) and use it as a calculator to solve the
following problem.
Lucky Larry bought 75 shares of Google stock at a price of $235.14 per share. Today, shares of Google are priced at $711.25. Using Python’s interactive mode as a calculator, figure out how much profit Larry would make if he sold all of his shares.
>>> (711.25 - 235.14) * 75
35708.25
>>>Pro-tip: Use the underscore (_) variable to use the result of the last calculation. For example, how much profit does Larry make after his evil broker takes their 20% cut?
>>> _ * 0.80
28566.600000000002
>>>Use the help() command to get help on the abs() function. Then use
help() to get help on the round() function. Type help() just by
itself with no value to enter the interactive help viewer.
One caution with help() is that it doesn’t work for basic Python
statements such as for, if, while, and so forth (i.e., if you type
help(for) you’ll get a syntax error). You can try putting the help
topic in quotes such as help("for") instead. If that doesn’t work,
you’ll have to turn to an internet search.
Followup: Go to http://docs.python.org and find the documentation for
the abs() function (hint: it’s found under the library reference
related to built-in functions).
This course is structured as a series of traditional web pages where you are encouraged to try interactive Python code samples by typing them out by hand. If you are learning Python for the first time, this "slow approach" is encouraged. You will get a better feel for the language by slowing down, typing things in, and thinking about what you are doing.
If you must "cut and paste" code samples, select code
starting after the >>> prompt and going up to, but not any further
than the first blank line or the next >>> prompt (whichever appears
first). Select "copy" from the browser, go to the Python window, and
select "paste" to copy it into the Python shell. To get the code to
run, you may have to hit "Return" once after you’ve pasted it in.
Use cut-and-paste to execute the Python statements in this session:
>>> 12 + 20
32
>>> (3 + 4
+ 5 + 6)
18
>>> for i in range(5):
print(i)
0
1
2
3
4
>>>Warning: It is never possible to paste more than one Python command
(statements that appear after >>>) to the basic Python shell at a
time. You have to paste each command one at a time.
Now that you've done this, just remember that you will get more out of the class by typing in code slowly and thinking about it--not cut and pasting.
Try something more advanced and type these statements to find out when the next buses are due to arrive at Plateia Aristotelous (Aristotelous Square), one of the busiest stops in central Thessaloniki. The data comes straight from the city's live bus-tracking service:
>>> import json
>>> import urllib.parse
>>> import urllib.request
>>> from datetime import datetime
>>> stop = '1045' # Plateia Aristotelous, Thessaloniki
>>> date = urllib.parse.quote(datetime.now().strftime('%d/%m/%Y %H:%M:%S'))
>>> url = f'https://oseth.com.gr/el/telematics-api/stop/{stop}/timetable?date={date}'
>>> req = urllib.request.Request(url, headers={'Referer': 'https://oseth.com.gr/el'})
>>> data = json.load(urllib.request.urlopen(req))
>>> for trip in data['data']['trips'][:5]:
print(trip['route']['shortName'], trip['arrivalTime'], trip['headsign'])
83B 12:56:56 ΘΕΣ/ΝΙΚΗ - ΛΑΓΚΑΔΑΣ
02K 12:58:00 Α.Σ.ΙΚΕΑ-Κ.Τ.Ε.Λ ΜΑΚΕΔΟΝΙΑ
27 13:00:36 ΠΑΝΕΠΙΣΤΗΜΙΟ - ΣΤΑΥΡΟΥΠΟΛΗ
39 13:00:44 ΚΗΦΙΣΙΑ - ΔΙΚΑΣΤΗΡΙΑ
50 13:02:11 ΠΟΛΙΤΙΣΤΙΚΗ ΓΡΑΜΜΗ
>>>Yes, you just downloaded a web page, parsed a JSON document, and extracted some useful information in about a half-dozen lines of code. The data you accessed is the same feed that powers the live map at https://oseth.com.gr. Try it again in a few minutes and watch the arrival times change.
Note: This is live data, so the buses you see depend on the time of day. Late at night you may see only a few upcoming arrivals, or none at all.
If you can't make this work, don't worry about it. The rest of this course has nothing to do with downloading data from the web.