Difference between revisions of "Python1"
(→Lists) |
|||
Line 125: | Line 125: | ||
==Lists== | ==Lists== | ||
+ | |||
+ | An example of a list is: | ||
+ | |||
+ | <pre> | ||
+ | >>> shopping = ['bread', 'marmalade', 'milk', 'tea'] | ||
+ | </pre> | ||
+ | |||
+ | and we can enquire about the length of that using the same function as before: | ||
+ | |||
+ | <pre> | ||
+ | >>> len(shopping) | ||
+ | </pre> | ||
+ | |||
+ | We can also take '''slices''' of a list, as we did with a string: | ||
+ | |||
+ | <pre> | ||
+ | >>> shopping[0:2] | ||
+ | </pre> | ||
+ | |||
+ | and even reset a portion of the list that way: | ||
+ | |||
+ | <pre> | ||
+ | >>> shopping[0:2] = ['bagels', 'jam'] | ||
+ | >>> shopping | ||
+ | </pre> | ||
+ | |||
+ | A list object comes with a number of handy methods built in. For example we could type: | ||
+ | |||
+ | <pre> | ||
+ | >>> shopping.append("butter") | ||
+ | >>> print shopping | ||
+ | </pre> | ||
+ | |||
+ | and even: | ||
+ | |||
+ | <pre> | ||
+ | >>> shopping.reverse() | ||
+ | >>> print shopping | ||
+ | </pre> | ||
+ | |||
+ | or | ||
+ | |||
+ | <pre> | ||
+ | >>> shopping.sort() | ||
+ | >>> print shopping | ||
+ | </pre> | ||
+ | |||
==Dictionaries== | ==Dictionaries== | ||
Revision as of 11:54, 3 October 2012
Python for Scientists
Introduction
Getting Started on BlueCrystal Phase-2
After you have logged in, type the following at the command line:
module add languages/python-2.7.2.0 python
This should start up an interactive python session:
Python 2.7.2 (default, Aug 25 2011, 10:51:03) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
where we can type commands at the >>> prompt.
With thanks to Simon Metson and Mike Wallace for much of the following material.
Python as a Calculator
Let's try a few commands out. If you type:
>>> print "Hello!"
you'll get:
Hello!
If you try:
>>> print 5 + 9
you'll get:
14
And here is a copy of a session containing a few more commands:
>>> five = 5
>>> neuf = 9
>>> print five + neuf
14
>>> def say_hello():
... print "Hello, world!"
... # hit return here
>>> say_hello()
Hello, world!
You can exit an interactive session but typing Ctrl-D.
Getting Help
Python has lots of useful documentation. For example, take a look at: http://docs.python.org/.
Making a Script
An interactive session can be fun and useful for trying things out. However--to save our fingers--we will typically want to execute a series of commands as a script, created using your favourite text editor. Here is the contents of an example script:
#!/bin/env python
print "Hello, from a python script!"
Ensure that your script is executable:
chmod u+x myscript.py
and now you can run it:
[ggdagw@bigblue4 ~]$ ./myscript.py Hello, from a python script!
Python and Whitespace
Python incorporates whitespace in it's syntax, so spacing is very important. For example:
Nuts and Bolts
Conditionals
Strings
For example, the eagle-eyed will have spotted in the previous examples that we could ask the length a character string--straight off the bat. No need to write a counting routine ourselves:
>>> print len(message)
We also take slices of our character string. In my case
>>> print message[:5]
elicits:
hello
Lists
An example of a list is:
>>> shopping = ['bread', 'marmalade', 'milk', 'tea']
and we can enquire about the length of that using the same function as before:
>>> len(shopping)
We can also take slices of a list, as we did with a string:
>>> shopping[0:2]
and even reset a portion of the list that way:
>>> shopping[0:2] = ['bagels', 'jam'] >>> shopping
A list object comes with a number of handy methods built in. For example we could type:
>>> shopping.append("butter") >>> print shopping
and even:
>>> shopping.reverse() >>> print shopping
or
>>> shopping.sort() >>> print shopping