""" This is a doc string for the whole module. """ class Person """ This is a class declaration. """ var _name as String # declare an object variable. every instance of Person will have a name var _age as int cue init(name as String, age as int) base.init _name = name _age = age def sayHello # This is a method # In strings, anything in brackets ([]) is evaluated as an expression, # converted to a string and substituted into the string: print 'Hello. My name is [_name] and I am [_age].' def add(i as int, j as int) as int """ Adds the two arguments and returns their sum. """ return i + j class Program def main # Create an instance p = Person('Bob', 30) # Invoke a method p.sayHello # Invoke a method with arguments print p.add(2, 2) # Assert the truth of something assert p.add(2, 2)==4 # If statement a = 2 b = 1 if a > b print 'a is greater' print 'b is smaller' # If-else statement # See also: "Make An If Else Ladder" How To if a > b print 'a is greater' else print 'a is not greater' # Can put target on same line if the target is just one statement if a > b, print 'a is greater' else, print 'b is greater or equal' # While loop while a > b a -= 1 # augmented assignment # While loop in one line while a > b, a -= 1 # Parallel assignment a, b = 1, 2 # Lists -- see the "Use Lists" How To for more info t = ['a', 'b', 'c'] for letter in t, print letter # Dictionaries d = {'a': 1, 'b': 2} assert d['a'] == 1 and d['b'] == 2 for key, value in d print '[key] = [value]' # string interpolation # Sets -- like lists, but no repetition letters = {'a', 'b', 'c'} assert 'a' in letters for letter in letters, print letter # Line continuation is implicit with parenthesized arguments p = Person('Bob', 30) # Line continuation can be done explicitly with an underscore # The next line is indented the same or more b = a * _ 10 |
How To Write Basic Syntax