pythonbasics

pythonbasics - CWCS Workshop May 2009 PYTHON Basics

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CWCS Workshop May 2009 PYTHON Basics http://hetland.org/writing/instant-hacking.html Python is an easy to learn, modern, interpreted, object-oriented programming language. It was designed to be as simple and accessible as possible with an emphasis on code readability. For chemist, it is one of the most widely used scripting languages. 1 Python interpreter You can start the Python interpreter from any terminal window. When you first start the interpreter program, you will see something like the following: $ python Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> The >>> is the Python prompt indicating it is waiting for a command. In programming languages, a complete command is called a statement . >>> print “Hello” Hello >>> 2+3 5 >>> print “2 + 3 =”,2+3 2 + 3 = 5 Note: the red text denotes python screen output. You can assign a value to a variable by putting the variable name on the left, followed by a single =, followed by what is to be stored. >>> x = 1.0+2**3 >>> x 9.0 To execute a sequence of statements, you can create a command called a function . For continuation lines, a secondary prompt (“…”) is displayed when you press the enter key. A blank line (or a secondary prompt on a line by itself) is used to end a multi-line command. As you enter the following statements, be sure to indent as shown. (You can use the tab key) >>> def hello(): ... print "Hello" ... print "Chemistry is fun!" ... >>> hello() Hello Chemistry is Fun >>> Typing an end-of-file character (Control-D) at the primary prompt causes the interpreter to exit the session. 1 http://baoilleach.blogspot.com/2008/03/python-scripting-language-of-chemistry.html
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Python modules You can also execute statements in a text file, i . e ., program, from a terminal window. Python source files have a .py suffix. For example, use the vi editor to create a file named “ circle.py ” containing the following statements. # This function asks the user for the radius
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

pythonbasics - CWCS Workshop May 2009 PYTHON Basics

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online