Last change
on this file since 2726 was 1757, checked in by Kana Sugimoto, 14 years ago |
New Development: Yes
JIRA Issue: Yes (CAS-2211)
Ready for Test: Yes
Interface Changes: Yes
What Interface Changed: ASAP 3.0.0 interface changes
Test Programs:
Put in Release Notes: Yes
Module(s): all the CASA sd tools and tasks are affected.
Description: Merged ATNF-ASAP 3.0.0 developments to CASA (alma) branch.
Note you also need to update casa/code/atnf.
|
File size:
1.9 KB
|
Rev | Line | |
---|
[1636] | 1 | ====================
|
---|
| 2 | Python in 20 minutes
|
---|
| 3 | ====================
|
---|
| 4 |
|
---|
| 5 | .. sectionauthor:: Malte Marquarding
|
---|
| 6 |
|
---|
| 7 | **Main goal:** To get a basic understanding of the python programming language
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | This is a very quick introduction to the python programming language to get started with ASAP,
|
---|
| 11 | which behaves just like any other python module you can find.
|
---|
| 12 | It introduces basic programming concepts.
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | Variables
|
---|
| 16 | =========
|
---|
| 17 |
|
---|
| 18 | A variable is just an alias/handle to a value. The value can be anything understood by python
|
---|
| 19 |
|
---|
| 20 | Example::
|
---|
| 21 |
|
---|
| 22 | # an integer
|
---|
| 23 | x = 1
|
---|
| 24 | # a string
|
---|
| 25 | y = 'Hello World!'
|
---|
| 26 | # a boolean
|
---|
| 27 | z = True
|
---|
| 28 | # list of ...
|
---|
| 29 | a = [0, 1, 2]
|
---|
| 30 | b = ['a', 'b']
|
---|
| 31 |
|
---|
| 32 |
|
---|
| 33 | Syntax
|
---|
| 34 | ======
|
---|
| 35 |
|
---|
| 36 | Python uses **identation** to define blocks, where other proogramming language often use
|
---|
| 37 | curly brackets, e.g.:
|
---|
| 38 |
|
---|
| 39 | in *c*:
|
---|
| 40 |
|
---|
| 41 | .. code-block:: c
|
---|
| 42 |
|
---|
| 43 | while (i<10) {
|
---|
| 44 | j += il
|
---|
| 45 | }
|
---|
| 46 | // block ends
|
---|
| 47 |
|
---|
| 48 | in *python*:
|
---|
| 49 |
|
---|
| 50 | .. code-block:: python
|
---|
| 51 |
|
---|
| 52 | while i<10:
|
---|
| 53 | j += 10
|
---|
| 54 | # block ends
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | Functions
|
---|
| 58 | =========
|
---|
| 59 |
|
---|
| 60 | When you need to repeat common behaviour you are better of defining a function, just like it would be
|
---|
| 61 | in mathematics. A function can return something ot do no return anything but doing something implictly.
|
---|
| 62 |
|
---|
| 63 | Examples::
|
---|
| 64 |
|
---|
| 65 | def squared(x):
|
---|
| 66 | return x*x
|
---|
| 67 |
|
---|
| 68 | result = squared(2)
|
---|
| 69 | print result
|
---|
| 70 |
|
---|
| 71 | def prefix_print(value):
|
---|
| 72 | print 'My value:', value
|
---|
| 73 |
|
---|
| 74 | prefix_print('Hello')
|
---|
| 75 |
|
---|
| 76 | Statements
|
---|
| 77 | ==========
|
---|
| 78 |
|
---|
| 79 | Often you find you will want to do something conditionally.
|
---|
| 80 | For this you can use `if` statements.
|
---|
| 81 |
|
---|
| 82 | Example::
|
---|
| 83 |
|
---|
| 84 | a = 1
|
---|
| 85 | if a == 1:
|
---|
| 86 | print 'Match'
|
---|
| 87 | else:
|
---|
| 88 |
|
---|
| 89 | print 'No match'
|
---|
| 90 |
|
---|
| 91 | To apply a function to a range of values you can use `for` or `while`
|
---|
| 92 |
|
---|
| 93 | Example::
|
---|
| 94 |
|
---|
| 95 | i = 0
|
---|
| 96 | while i < 10:
|
---|
| 97 | print i
|
---|
| 98 | i +=1
|
---|
| 99 |
|
---|
| 100 | for i in [0, 1, 2]:
|
---|
| 101 | print i
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | Objects
|
---|
| 105 | =======
|
---|
| 106 |
|
---|
| 107 | Objects are basically values with certain attributes, which are specific to that type of the object.
|
---|
| 108 | For example strings in python have attribute functions which can perform operations on the string::
|
---|
| 109 |
|
---|
| 110 | x = 'Test me'
|
---|
| 111 | print x.upper()
|
---|
Note:
See
TracBrowser
for help on using the repository browser.