Writing Python Programs

From Training Material
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.