Skip to content

Variables

Jean Dubois edited this page Mar 10, 2024 · 21 revisions

Definition

  • var identifier = expr: identifier is now expr
  • var identifier = var foo = expr: identifier and foo are now expr
  • Reserved variable names: there is no reserved variable name.
  • A variable definition return the expr given so it can be used in mathematicals operations: 1 + (var foo = 2) retuns 3

Multiple definitions

  • You can do var a, b = 1, 2
  • You can do var a, b = b, a to swipe values of a and b.

Edition

You can edit variables by multiple ways:

  • var a = value
  • var a += value
  • var a ++: same as var a += 1
  • var a -= value
  • var a --: same as var a -= 1
  • var a *= value
  • var a /= value
  • var a ^= value
  • var a //= value
  • var a %= value
  • var a ||= value: same as var a = a or value
  • var a &&= value: same as var a = a and value
  • var a ^^^= value: same as var a = a xor value
  • var a |= value: same as var a = a | value
  • var a &= value: same as var a = a & value
  • var a ^^= value: same as var a = a ^^ value
  • var a === value: same as var a = a == value
  • var a <== value: same as var a = a <= value
  • var a <<= value: same as var a = a < value
  • var a >== value: same as var a = a >= value
  • var a >>= value: same as var a = a > value

Multiple edits

You can do the same thing with multiple variables:

  • var a, b += 1, 2: same as var a += 1 ; var b += 2
  • var a, b ++: same as var a, b += 1, 1
  • var a, b //= b, a: same as var c = a ; var a //= b ; var b //= c ; del c

Access

To access to a variable, you can:

  • give the identifier: foo (returns the value of foo)
  • use the special syntax: foo ? bar ? a ? b (returns the value of foo if it is defined, else the value of bar... You can put as much ? identifier as you want.) Then, you can put an expression at the end, such as 2. In this case, the value returned will be 2 if none of the identifiers are given.

Deletion

If you don't need anymore a variable, you can delete it with the del keyword.

Types

Nougaro is a dynamic-typed language. Examples of types:

  • int (integers)
  • float (floats)
  • str (strings)
  • list (lists)
  • NoneType (None)
  • function
  • module
  • object

Examples

  • var foo = var bar = 12
  • while var foo = bar - 1 then var bar -= 1

Example for deletion

Create a variable a, the delete it:

nougaro> var a = 1
1
nougaro> del a

But be careful! It can return errors:

nougaro> var a = 1
1
nougaro> del a
nougaro> del a
Traceback (more recent call last):
 In file <stdin>, line 1, in <program>:

  del a
      ^
 NotDefinedError: a is not defined.
Clone this wiki locally