![]() |
Prothon
Tutorial
|
![]() |
Prothon Home | Previous Page | Tutorial Outline | Next Page |
3.0 Data Types, Variables and Assignments 3.1 Objects, Types, & Containers You've already seen variables and assignment, but let's look at them more closely. In Prothon everything is an object. You will hear this over and over. Objects are actually quite simple but we will learn about them slowly as we go through this tutorial. For now we will learn that an object can hold data. Ordinary binary computer data of ones, zeros, bytes, characters, etc. Prothon makes it easy for you to use an object with simple data types like integers and strings. Unlike some languages these types are not special. You can create your own types like these. For now we will just use them and not worry about where they come from. Some of the most basic types to use are the Integer, Float, and String objects: >>> 5 + 4 # integer addition 9 >>> 1.5 * 4.0 # floating point multiplication 6 >>> "Hello " + "World" # string concatenation Hello World >>> You can also mix types sometimes. The rules for when types can be mixed is up to the definition of the type itself: >>> 1.3 * 2 2.6 >>> "hello " + 33 hello 33 >>> Prothon variables are all stored references to objects. You put an object reference into a variable using an assignment statement such as "x = 3". Then later you can use the variable in the place of the constant object 3. Note that you can have multiple items assigned at once, which is very useful for swapping items: >> x, y = 1, 2 1 >>> print x, y 1 2 >>> x, y = y, x 2 >>> print x, y 2 1 >>> You may also assign the same value to multiple destinations in one statement: >>> x = y = 99 99 >>> print x, y 99 99 >>> Prothon integers are 64-bit, signed numbers. Using 64-bit integers eliminates the need for having a special "long" type to have larger ints or having a unsigned type. Integer constants may be entered into Prothon code in several ways. The _ symbol in the number is just a comment to help read the number. The number starting with a zero is in octal and the numbers starting with 0x are hexadecimal. The last number is the largest positive number: >> print 1, -1, 1_000_000, 010, 0x1000, 0x7fff_ffff_ffff_ffff 1 -1 1000000 8 4096 9223372036854775807 >>> Floating point numbers are standard "C double" 64-bit IEEE floating point numbers. You can tell a floating point number from an integer by the presence of a decimal point, such as in "1.0" or the exponent symbol "e" as in "1e3". Most standard representations of floating point or scientific numbers will work in Prothon with one exception. That exception is that the number may not end in a period. When you type a number that ends in the decimal point add a zero, I.E. change "1." to "1.0". See here for the reason. Note that unlike many languages, when you divide two integers, you get a floating point number (unless you use a special // symbol which will be explained later). >>> print 1/3, 1.1, -1.1, 1e3, 1.1E22, 1e-1, -1e1, -1e-1 .333333 1.1 -1.1 1000 1.1e+22 .1 -10 -.1 >>> Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold any arbitrary data, even binary data such as photos and movies.They are of course also good for their traditional role of storing and manipulating text. The string can be entered in many forms. The two most common are as ascii characters surrounded by either single or double quotes. There is no difference if you use single or double quotes, except that using one makes it easier to include the other type inside the string: >>> "It's my turn" It's my turn >>> '"Hello World"' "Hello World" >>> If you need to use the same kind of quote inside the string you need to use an escape sequence. Escape sequences start with a backslash. If you wish to place a double quote inside a string made of double quotes then you insert a \" sequence. Similarly a \' will insert a single quote. Some other escape sequences are: \n new line \t tab \\ backslash character \nnn octal value char \xNN hex value char There are a number of special types of strings. If you prefix a string with the letter r as in r"xxx", then escape sequences are not converted. If you start it with the letter x, then it is a special string for inputting binary data in hex form where only hex chars, white space and newlines are allowed: >>> r' \n ' \n >>> x' 31 32 33 ' 123 >>> One last type of string is useful for entering multiple lines of text. You use three quotes instead of one and it will include line endings. x = """ line 1 line 2 """ Strings can be added together (concatenated) just like numbers. You can actually add a string on the left to anything on the right and Prothon will convert the item on the right to it's string value before the addition: >>> "abc" + "def" abcdef >>> "The number is " + 99 The number is 99 >>> You can access the individual characters in a string by the indexing operator "xxx"[n] where n specifies the char to be extracted. The chars are numbered with the first char as zero. So "abc"[1] evaluates to the string "b". You can also access slices of more than one char by using the form [m:n] where m specifies the index of the first char and n is one greater than the index of the last character. You can also add a third number to specify a step size to get non-contiguous characters and/or a negative number to access the chars in reverse: >>> x = "abcdefghijklm" abcdefghijklm >>> print x[2], x[1:3], x[0:10:2], x[12:6:-1] c bc acegi mlkjih If you use a negative number for an index, it is treated as an offset from the end of the string. The number -1 will index the last character in the string, -2 will access the next to last, etc. One way to think of this is that the index points to the left side of the char and zero points both to the left of the first char and the right of the last char. One last trick to use with indexes is to leave the number out and use the defaults. The first index will default to the first char if the third number is positive or missing and to the last position if the third is negative. The second will default to the opposite end of the string from the first: >>> x = "abcdef" abcdef >>> print x[-2], x[-3:-1], x[3:], x[:2], x[:2:-1], x[::-1], x[:] e de def ab fed fedcba abcdef There are many useful string functions, but we will point out just a few here: >>>Len("abc") # number of chars in string 3 >>>"abc".upper() # convert string to uppercase ABC >>>"ABC".lower() # converts string to lowercase abc >>>"abc def".capitalize() # capitalize string Abc def >>>"abc def".capWords() # capitalize words in string Abc Def >>>'<'+" x ".lStrip()+'>' # remove white space from left <x > >>>'<'+" x ".rStrip()+'>' # remove white space from right < x> >>>'<'+" x ".strip()+'>' # remove white space from sides <x> A symbol is a special type of object similar to a string that holds Prothon variable names. A symbol may only have the characters "a" to "z", "A" to "Z", the digits "0" to "9", the underbar "_", the exclamation mark "!", and the question mark "?". It may not begin with a digit and the exclamation mark and question mark may only appear at the end. You may put a symbol in the code as a constant by typing the variable name prefixed with a reverse quote symbol ( ` ). You may also convert a string to a symbol with "Symbol(str)" and do the reverse with "String(symbol)". >>> sym = `var_name `var_name >>> print String(sym) var_name >>> print Symbol("init_") `init_ >>> |
Prothon Home | Previous Page | Tutorial Outline | Next Page |