Writing Python Programs

From Training Material
Revision as of 23:55, 7 February 2013 by Kristian Rother (talk | contribs) (Created page with "{{Cat|Python Reference}} {{Python Links}} == Essentials == * All program files should have the extension <source lang="python">.py </source> * When developing on Unix, the fi...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Essentials

  • All program files should have the extension
    .py
    
  • When developing on Unix, the first line in each Python program should be:
#!/usr/bin env python
  • Indentation is a central element of Python syntax, marking code blocks. Code blocks should be indented by four spaces/one tab.
  • Indentation must not be used for decorative purposes.

Only one command per line is allowed.

What Python does not have

  • Memory allocation.
  • Declaration of variables and types.
  • Strict object-orientation.
  • Line numbers.

Commenting code

  • In Python, single lines can be commented by the hash symbol:
# this is a comment.

print a + b # adding the variables.
  • Also, multi-line comments can be enclosed by triple quotes:
”””
This is some longer descriptions
that stretches over multiple lines.
”””
  • The triple quoted comments can be used to generate documentation automatically.