Python programmers bring habits with them. Some of them help in Raku, and some get in the way. These articles start from what Python does.
False friends
- + adds, even when the numbers are stringsIn JavaScript,
"10" + 5is"105", and in Python it is an error. In Raku it is 15, because+always adds. - == does not compare stringsIn Python and JavaScript,
==compares strings too. In Raku,==compares numbers, and strings are compared witheq. - @a[-1] is not the last elementIn Python and Perl, index -1 counts from the end. In Raku it does not even compile.
- 0.1 + 0.2 is exactly 0.3In Python and JavaScript,
0.1 + 0.2is not equal to0.3. In Raku it is, because0.1is stored as a fraction. - There is no length
len,.lengthandlengthdo not exist in Raku. A string has.chars, and a list has.elems. - // is not integer divisionIn Python,
7 // 2is 3. In Raku,//means "or else, if there is no value", so7 // 2is 7. - Splitting on nothing does not give you the lettersIn JavaScript and Perl, splitting on an empty string gives the characters. In Raku you also get an empty string at each end. Use
.comb.
Accents
- Counting wordsThe three most frequent words in a sentence, written four times — with a Python, a Perl and a C accent, and as a native would write it.
- Totals per cityAdding up sales per city from a small table, written with a Python, a Perl and a C accent, and as a native would write it.
- Fibonacci numbers below 1000The Fibonacci numbers below 1000, written with a Python, a Perl and a C accent, and with a native Raku sequence.