A short tutorial - Part 2: Variables and TypesMay 22. 2012
Variables and TypesLike in most other languages - variable names may contain letters, digits and underscores. Lower case letters are usual but upper case letters are allowed: my_value = 18 MyTemperature = 36.8 text_21 = "Hirn" Variables have types, even if you do never declare them. my_value in the upper example is of type Integer, because an integer value is assigned to it. The following would be an error: ERROR! my_value = 18 # my_value is now int my_value = 18.5 # ERROR -> my_value cannot be float at the same time! The following datatypes are builtin into Wirbel:
Type conversionThere is no automatic type conversion in Wirbel. But many operators and functions do automatically behave correctly with mixed types. Take the following example. The variable a will always be of type Integer: a = 11 a += 2.3 # a is now 13 a *= 2.2 # a is now 28 print(a) But the following would be an error: ERROR! a = 11 a = a + 2.3 # int + float gives float. Error! Why? Because integer + float gives float. So a + 2.3 is an expression with type float. And that cannot be assigned to an integer variable. In such a case you have to explicitely convert the result to integer: This way it works a = 11 a = int(a + 2.3) |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||