Wikia

Peace Elements

Python

Talk2
185pages on
this wiki

Python2

It's . . .

Monty Pythons programming language . . .

Starring John Cleese as a constant
Michael Palin as a variable
Eric Idle for historical interest

The first programming language for the terminally insane (do not use near a naked flame)

Python is often suggested as suitable for:

  • Beginners (it is partly based on the very simple ABC teaching language)
  • Prototyping
  • People with a fear of hats

We will evaluate this whilst learning the language and drinking a nice cup of tea (no spam)


Why Python is suitable for the ASQ project Edit

[ahem - my theory which is mine, what my theory is . . .]
  • Python is open source. After experiences with unreliable licensing and other development issues with commercial software this becomes very central
  • Python is suitable for beginners. I have yet to come across any language apart from logo and perhaps panther that are genuinely simple. However in comparison to other languages Python may be considered simple.
  • Python is scripted Scripted languages are important because they allow real time programming. Python has both an interactive one line at a time approach and a whole sequence potential.
  • FREE book and other resources available
  • Python can produce java code with Jython
  • Python has a free IDE - Idle written in Python
  • Python is cross platform
  • An interactive high level scripted language
  • Python has various modules including one for XML
  • Nokia 60 mobile phone development is possible
  • Python has Unicode
Unicode is a standard way of writing international text for example Chinese - hear that ts?.

Remember to use Unicode strings when you are dealing with text files, especially when you know that the file will contain text written in languages other than English.

  • Serious enough for NASA and Google, it was named after Monty Python

What is programming for exactly? Edit

"SAY NA More! Govn' - bit of a goer your wife is she? SAY NA More!" (ooh suit you Sir)

It is a way of telling the computer what you want it to do
That might mean:

  1. Add numbers or do conversions between currencies
  2. Display options (as in a menu) and do different things on selection

HTML the language used for web pages - just allows you to modify what is displayed on a web page. A programming language like python allows you to call up the browser or even write a browser

Is programming like creating web pages (and stuff)? Edit

"He is not the Messiah - he is a very - naughty - boy!"

HTML the language used to create or code web pages is a simple form of programming. It only does a few things but people have added more complex capacities onto it.

A programming language like Java or C or Python is far more.

It allows:

  • Choices or branches
  • Repeats or loops
  • Changes or variables
  • Manipulating data

Web pages have a programming language built in (javascript) and also the more powerful java language enables one to run programs within web pages and also create standalone programs

Is programming hard? Edit

I want to be a woman.
You can't be a woman, you are a man
Don't you oppress me!

Yes I am sorry to say it is. It is one of the reasons we are working towards ASQ It is also time consuming and if you have never programmed before it seems that you are learning things that make little sense. There is a steep learning curve. However it is very empowering and can be very useful.

It may be that you have a very specific programming need eg

  • Ts programs graphics in POV-ray to create animations such as the bowing penguin on the Tmxxine Linux page
  • Know mystery creates shockwave/flash files which you may have seen on this site
  • Many of our regulars use HTML to create web pages or to supplemant their blogs
  • Some of us have been involved in programming the innovative CURL language
  • Many of us have added text and graphics to this wiki - this is the simplest sort of programming and will be a big part of ASQ when it is created

Can anyone learn to program? Edit

Lovely people the Romans!

Yes, children as young as two can program in logo type languages. It is very much a mind discipline. If you desire to train and discipline your mind, you can. I am not a very good programmer. I do not like the precise way of thinking it imposes. However that concentration I do find very useful.

Wot? Programming is hard and easy? Edit

. . . and now for something completely different

Yes. Hacking is easy and fun. Skilled artisans (programmers) have a mathematical aptitude and are experienced and dedicated to their craft. To become skilled is hard.

When you start (from scratch) you may have to get the program installed and running. Programmers (bless them) are like the cobblers son who goes barefoot. The programming interfaces (IDE or Integrated Development Environments) are often poor.

They prefer to cobble.

Python has a vast enthusiasm and community good will that will ensure its development and support. That is quite important, especially when you show off your first program. Only a programmer will remember the work involved to get through the initial phases.

Are you dedicated?

How does Python compare to other languages Edit

"I was blind and now I see." [falls into pit]

More Info coming soon . . .

  • It is more suited to large projects than Perl I started learning Perl and can not for the life of me remember doing anything useful with it. However that is because I quickly got bored with its obtruse nature.
  • It is not as fast as java or C (speed is a minor issue for most programming)
  • It is elegant compared to (spits in disgust) C

I hate C. It has probably kept programming in the dark ages more than any other language. Oh all right it got us out of the dark ages ;-)
C is portable, widely used and known. It is also complex and requires perseverance and experience. I have only written one program in C (it was a lapse of judgement gentle wikians)

  • Curl is a great language but the syntax (use of language) may be simpler in Python - will get back on that one
  • BASIC - pleeze that is not a language. Oh OK BASIC is good because people are familiar with its simple and effective style. However there are so many dialects of BASIC that it is not very compatible even between versions. Python should work on different operating systems - so there!
  • Logo - I like logo, in particular Netlogo which outputs java but it is very much tied into the turtle methodology and is not as flexible as Python
  • Autohotkey is a powerful scripting language for windows - you can get great results with it - very worthwhile - but it is not a full language. Well it is great for what it is

More opinionated info coming soon . . .

What Python is not Edit

We are the knights who say, "Ni"
  • Python is a high level language. Still not close enough to human language.
  • Python is not a fast running language.

In real development terms this is often not a problem. If speed is critical then faster modules can be glued in. Python can also be compiled


Programming for Virgins Edit

So, you've never programmed before. As we go through this tutorial I will attempt to teach you how to program. There really is only one way to learn to program. You must read code and write code (as computer programs are often called). I'm going to show you lots of code. You should type in code that I show you to see what happens. Play around with it and make changes. The worst that can happen is that it won't work. When I type in code it will be formatted like this:

##Python is easy to learn
print "Hello, World!"

That's so it is easy to distinguish from the other text. If the computer prints something out it will be formatted like this:

Hello, World!

And there will often be a mixture of the text you type (which is shown in bold) and the text the program prints to the screen (Note that some of the tutorial has not been converted to this format. Since this is a wiki, you can convert it when you find it.), which would look like this:

Halt!
Who Goes there? Josh
You may pass, Josh

I will also introduce you to the terminology of programming - for example, that programming is often referred to as coding. This will not only help you understand what programmers are talking about, but also helps the learning process.

Installing Python Edit

setting it all up

This is sometimes a stumbling block and programmers would do well to ease this process and make it as neat and clean as possible

Now, on to more important things. In order to program in Python you need the Python software. If you don't already have the Python software go to http://www.python.org/download/ and get the proper version for your platform. Download it, read the instructions and get it installed.

On Windows the download is a self extracting exe of 9 mb (quite large for modem users) and it comes with the language, documentation and the idle editor which is to be recommended as it is written in python and is therefore available for Linux and Mac.

One very strange occurence is how a firewall informs you that idle is trying to access the internet when this is not the case (the program mentions this - so it is a known bug) No such connection is made.

First you need to download the appropriate file for your computer from http://www.python.org/download. Go to the 2.0 link (or newer) and then get the windows installer if you use Windows or the rpm or source if you use Unix.

The Windows installer will download to file. The file can then be run by double clicking on the icon that is downloaded. The installation will then proceed.

If you get the Unix source make sure you compile in the tk extension if you want to use IDLE.

What is IDLE Edit

IDLE is the environment (where we do programming) It is is named after Monty Python actor Eric Idle and the IDE (integrated Development Environment) Check out this tutorial on Using Idle (nudge - nudge wink wink)

Tutorials Edit

I wish to report a dead parrot

Help Edit

  • press f1 in idle
  • type help()' at the >>> prompt

Interactive Mode Edit

Go into IDLE (also called the Python GUI). You should see a window that has some text like this:

Python 2.0 (#4, Dec 12 2000, 19:19:57) 
[GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
Type "copyright", "credits" or "license" for more information.
IDLE 0.6 -- press F1 for help
>>> 

The >>> is Python way of telling you that you are in interactive mode. In interactive mode what you type is immediately run. Try typing 1+1 in. Python will respond with 2. Interactive mode allows you to test out and see what Python will do. If you ever feel you need to play with new Python statements go into interactive mode and try them out.

Creating and Running Programs Edit

Go into IDLE if you are not already. Go to File then New Window. In this window type the following:

print "Hello, World!"

First save the program. Go to File then Save. Save it as hello.py. (If you want you can save it to some other directory than the default.) Now that it is saved it can be run.

Next run the program by going to Run then Run Module (or if you have a older version of IDLE use Edit then Run script). This will output Hello, World! on the *Python Shell* window.

Confused still? Try this tutorial for IDLE at http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html

Using Python from the command line Edit

If you don't want to use Python from the command line, you don't have too, just use IDLE. To get into interactive mode just type python with out any arguments from the shell. To run a program create it with a text editor (Emacs has a good python mode) and then run it with python program name.

Windows users can select
Programs/Python/Idle
with the mouse

First Script Edit

# The First and most Holy Script of Rev Crusty S Lobster (deceased)
# in the language of Python to the Glory of Cecil
print 'You are about to activate the cleansing Mantra Ray'
print
resp = raw_input("What's your name? ")
print "Hi, %s, nice to meet you" % resp 
for i in range (109):
    print "Our Friend, %s, has cyber chanted" % resp , i, "Om Ya Ha Hum"




# internationalization tried by L18L
import gettext
gettext.bindtextdomain('lob', '/usr/share/locale')
gettext.textdomain('lob')
_ = gettext.gettext
# that s it # 
# create lob.pot by: xgettext lob --language=python -o lob.pot
# localization HOWTO. for example: spanish
# create es.po by: msginit --locale=es
# edit es.po and save it.
# create mo file from edited po file: msgfmt es.po -o /usr/share/locale/es/LC_MESSAGES/lob.mo
# run it Spanish: LANGUAGE=es ./python lob
#
print
print _('You are about to activate the cleansing Mantra Ray')
print
print _("What's your name? "),
resp = raw_input("")

print _( "Hi, %s, nice to meet you") % resp

for i in range (0,9):
   #print "Our Friend, %s, has cyber chanted" % resp , i, "Om Ya Ha Hum"
    print gettext.ngettext ( "Our Friend %s has cyber chanted one 'Om Ya Ha Hum'" % resp,
                             "Our Friend has cyber chanted %d 'Om Ya Ha Hum's"  % i ,
                             i)
#                             
# 
# internationalization no go
# ngettext did not translate
# _( "Hi, %s, nice to meet you") % resp 
#              cannot be replaced by better
# _( "Hi, %(resp)s, nice to meet you")
# no more than 1 variable possible
#
# this version FAILED

Second Script Edit

# Wikipedia search engine single word) 
# Monday Feb 14
import webbrowser                
sought_word = raw_input("What is your wikipedia search word? ")
goto_url_location = "http://en.wikipedia.org/wiki/" +  sought_word
webbrowser.open(goto_url_location)

Links Edit

Graphics with Python

Python has twenty-nine keywords:

and def exec if not return assert del finally import or try break elif for in pass while class else from is print yield continue except global lambda raise

Around Wikia's network

Random Wiki