Run Python Code from the Web
The web is a great resource for learning Python. There is also a lot of example code. In this course, in the wiki, there is quite a bit of example code. You should run this code, and perhaps twiddle around with it and run it again. But how?
Code from this Wiki
When I put code in wiki pages I put it in a "unformatted block that looks like this:
# this example show the classic hello world using # two words and addition # this is not supposed to be a good way to do this task, # but just an example of coding first = "Hello" second = "Universe" print first + " " + second # notice we also add in a space
There are two easy way to run it.
- Copy the code and paste it into a file, this is often called a scratch file. You can erase the code later, comment it out, or save it in some way. It is up to you.
- Copy the code and past it into the console. It will all be run but one line at a time. I do not like this way for myself, but you may.
For my example code:
- Please run it. And run the code above to make sure you know how.
- I have tested it, unless I have a type it should run without error. If it does not run let me know.
- I have tried to format the code to make it as easy to read as possible. My style is not quite standard, if you find it hard to read let me know how you think I might improve it.
- When moved to, try to vary the code to see what happens.
Code from Web and Books
You can try to run code from the web or books in the same way as above, but it may not work because:
- Some code just has mistakes, bad proof reading generaly.
- Not all Python 3 code works in Python 2.7.
- You may be missing some packages required for the example.
- Sample code sometimes forgets required "imports".
- The code is sometimes mixed with stuff that is not code, line numbers, comments without the # or particularly with pdf's characters that are not what they look like or invisible characters. This can be frustrating.
Still try it, just know it does not always work.
Downloads are usually quite reliable, but may suffer from some as copying from the web or books. I have a special directory, or project for my downloads.
ways of running Python code you write
On computers that do not yet have Python installed properly, perhaps the quickest way to run a little snippet of Python code is in an online Python interpreter.
- Python fiddle
- Python Tutor
- online Python3 at repl.it
- online Python interpreter at Holy Cross, which uses Skulpt, an entirely in-browser implementation of Python.
- other online Python interpreters
Online Python interpreters have the advantage of working from practically any machine that runs a web browser. However, they have many limitations Ward Cunningham warned us about in his "Program Computers You Can Unplug" essay, so we recommend installing Python directly on your computer if at all possible: Python Installation On Your System.