PythonBasics: Difference between revisions
No edit summary |
|||
Line 32: | Line 32: | ||
=== Executing Scripts === | === Executing Scripts === | ||
The most basic way to execute a set of python commands (aside from just copying and pasting to the shell) is to use the '''execfile''' command. Calling '''execfile('myscript.py')''' from inside a python shell will execute 'myscript.py' one line at a time. You can use this to run a series of reduction commands or other simple scripts. | |||
''A simple example'' | |||
=== Functions === | === Functions === | ||
Python allows you to define functions either from the command line (or an execfile call) or as part of modules. | |||
=== Modules === | === Modules === |
Revision as of 01:34, 10 October 2011
Simple Variables
Basic Math
Ints and Floats
Booleans
Deleting Variables
Checking Whether a Variable Exists
Data Collections: Lists
Lists are one of the fundamental data collections in python (we will also discuss dictionaries and you may want to read about tuples and sets).
Data Collections: Dictionaries
Dictionaries are another basic python data collection. Dictionaries store data in pairs, with a set of unique keys each mapped to some value (the values do not have to be unique). Dictionaries are extremely useful in scripting. For example, imagine that we are reducing a complex set of observations and want to be able to look up a the calibrator associated with a given source.
Control Flow
If
While
For
Break/Continue
More Complex Programs
Executing Scripts
The most basic way to execute a set of python commands (aside from just copying and pasting to the shell) is to use the execfile command. Calling execfile('myscript.py') from inside a python shell will execute 'myscript.py' one line at a time. You can use this to run a series of reduction commands or other simple scripts.
A simple example
Functions
Python allows you to define functions either from the command line (or an execfile call) or as part of modules.