Tuesday, November 20, 2012

Part 3 - Basic Python

In Part 3 we will attempt to cover the basics of python programming in laymen's term.


Learn basic Python programming

What is Python and IronPython?

Python is an open-source dynamic programming language that is fast and simple and runs on many different platforms. IronPython is an open-source implementation of the Python programming language which is tightly integrated with the .NET Framework. IronPython makes all .NET libraries easily available to Python programmers, while maintaining compatibility with existing Python code.

Where to find help with Python?

In the command prompt area, type in help(X) to get the built-in help system to print the help page of that object X in the Console. Type in keyword dir(X) with the object X enclosed in the parenthesis, and you will get a listing of all the names (variables, modules, functions) that this object defines. Autocomplete only works in the command prompt area, and you can invoke it by typing the dot(.) after an object and hold down the CTRL and Spacebar keys together.
  • help(object)
  • dir(object) or dir() for currently defined objects
  • Activate Autocomplete* : after the dot(.) press CTRL + Spacebar
* For Autocomplete to work, make sure the IronPython search path was added to the Configuration file.

Python Programming Syntax


  • Case sensitive (Ex: count is not the same as COUNT
  • No more if …end if, or curly brackets { …}
  • Use indentation for program blocking
  • Use blank spaces or tabs for indentation
  • >>> primary prompt
  •      secondary prompt
  • Use # for single line comment
  • Use triple quotes (‘’’ or “””) for multiline comments
  • Use CamelCase for classes
  • Use lower_case_with_underscores  for functions and methods.

*See files/Scripts/1_PythonBasic.py for examples.

Python Reserved Keywords

Python Operators


Python Variable Types


Python offers dynamic variable typing. Unlike other structured programming languages, in python, you don’t need to explicitly declare variable types. Python figures out what variable type it should be by the type of objects that you assign it to. This is often referred to as “duck typing”, that “If it quacks like a duck, it must be a duck”. The principal types that are built-in are numerics, sequences, mappings, files, classes, instances and exceptions.

  • Numerics : Integer, float, long and complex types.
  • Sequences: str, list, tuple, xrange, bytearray, buffer, unicode
  • Boolean: True, False

To convert between types, use type name as function

          int(x)    converts x to an integer
          float(x) converts x to a float
          str(x)    converts x to a string
          list(x)   converts x to a list

To declare variable type

          myList = [ ]
          myRA = ReferenceArray()

No comments:

Post a Comment