Run Python Code from the Web

From OpenCircuits
(Difference between revisions)
Jump to: navigation, search
m (1 revision)

Revision as of 09:28, 6 January 2017

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 alway 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.

Personal tools