source: branches/alma/tutorials/python_introduction.rst@ 2377

Last change on this file since 2377 was 1757, checked in by Kana Sugimoto, 15 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
RevLine 
[1636]1====================
2Python 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
10This is a very quick introduction to the python programming language to get started with ASAP,
11which behaves just like any other python module you can find.
12It introduces basic programming concepts.
13
14
15Variables
16=========
17
18A variable is just an alias/handle to a value. The value can be anything understood by python
19
20Example::
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
33Syntax
34======
35
36Python uses **identation** to define blocks, where other proogramming language often use
37curly brackets, e.g.:
38
39in *c*:
40
41.. code-block:: c
42
43 while (i<10) {
44 j += il
45 }
46 // block ends
47
48in *python*:
49
50.. code-block:: python
51
52 while i<10:
53 j += 10
54 # block ends
55
56
57Functions
58=========
59
60When you need to repeat common behaviour you are better of defining a function, just like it would be
61in mathematics. A function can return something ot do no return anything but doing something implictly.
62
63Examples::
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
76Statements
77==========
78
79Often you find you will want to do something conditionally.
80For this you can use `if` statements.
81
82Example::
83
84 a = 1
85 if a == 1:
86 print 'Match'
87 else:
88
89 print 'No match'
90
91To apply a function to a range of values you can use `for` or `while`
92
93Example::
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
104Objects
105=======
106
107Objects are basically values with certain attributes, which are specific to that type of the object.
108For 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.