diff --git a/doc/Language/py-nutshell.rakudoc b/doc/Language/py-nutshell.rakudoc index f92d519f96..59f0e369be 100644 --- a/doc/Language/py-nutshell.rakudoc +++ b/doc/Language/py-nutshell.rakudoc @@ -17,24 +17,24 @@ Let's start with printing "Hello, world!". The L keyword in Raku is the equivalent of L in Python. Like Python 2, parentheses are optional. A newline is added to the end of the line. -I +I =for code :lang print "Hello, world!" -I +I =for code :lang print("Hello, world!") -Raku +I put "Hello, world!" There is also the L keyword, which behaves similarly, but will -call the L method of its argument. +call the L<.gist|/routine/gist> method of its argument instead of the C<.Str|/routine/Str> method. -Raku +I my $hello = "Hello, world!"; say $hello; # also prints "Hello, world!" @@ -46,7 +46,7 @@ quotes (C<">) signify that interpolation should be performed. For instance, variables that start with a C<$>, and expressions contained in curly braces are interpolated. -Raku +I my $planet = 'earth'; say "Hello, $planet"; # OUTPUT: «Hello, earth␤» @@ -64,10 +64,11 @@ matching curly braces are closed. In Raku, a semicolon signifies the end of a statement. The semicolon may be omitted if it is the last statement -of a block. The semicolon may also be omitted if there -is a closing curly brace followed by a newline. +of a block (i.e. of a list of statements enclosed in curly braces). +The semicolon may also be omitted after a closing curly brace +followed by a newline. -Python +I =for code :lang print 1 + 2 + \ @@ -75,19 +76,20 @@ print 1 + 2 + \ print ( 1 + 2 ) -Raku +I say 1 + 2 + 3 + 4; say 1 + 2; + if True { say 42 } =head2 Blocks In Python, indentation is used to indicate a block. Raku uses curly braces. -Python +I =for code :lang if 1 == 2: @@ -95,7 +97,7 @@ if 1 == 2: else: print("1 is not 2.") -Raku +I if 1 == 2 { say "Wait, what?" @@ -123,7 +125,7 @@ initialized or declared and initialized at once. $foo = 12; # initialize my $bar = 19; # both at once -Also, as you may have noticed, variables in Raku usually start with sigils -- +Also, as you may have noticed, variables in Raku usually start with sigils— symbols indicating the type of their container. Variables starting with a C<$> hold scalars. Variables starting with an C<@> hold arrays, and variables starting with a C<%> hold a hash (dict). Sigilless variables, declared with a @@ -136,7 +138,7 @@ correct, but in general we are going to use sigilless variables in places where their immutability (or independence of type, when they are used in signatures) is needed or needs to be highlighted. -Python +I =begin code :lang s = 10 @@ -149,7 +151,7 @@ print(d['a']) # 10, 3, 12 =end code -Raku +I my $s = 10; my @l = 1, 2, 3; @@ -172,7 +174,7 @@ Python 3, they do. In Raku, every block creates a lexical scope. -Python +I =for code :lang if True: @@ -180,7 +182,7 @@ if True: print(x) # x is now 10 -Raku +I =for code :skip-test if True { @@ -197,7 +199,7 @@ if True { say $x # ok, $x is 10 -Python +I =for code :lang x = 10 @@ -206,7 +208,7 @@ for x in 1, 2, 3: print(x) # x is 3 -Raku +I my \x = 10; for 1, 2, 3 -> \x { @@ -217,19 +219,19 @@ Raku Lambdas in Python can be written as blocks or pointy blocks in Raku. -Python +I =for code :lang l = lambda i: i + 12 -Raku +I =for code :preamble my $l = -> $i { $i + 12 } Another Raku idiom for constructing lambdas is the Whatever star, C<*>. -Raku +I my $l = * + 12 # same as above @@ -242,7 +244,7 @@ See the section below for more constructs regarding subroutines and blocks. Another example (from the Python L): -Python +I =for code :lang squares = [] @@ -252,7 +254,7 @@ print(squares[2]()) print(squares[4]()) # both 16 since there is only one x -Raku +I my \squares = []; for ^5 -> \x { @@ -279,7 +281,7 @@ or symbol that can be used in Raku has an ASCII equivalent. Python has C loops and C loops: -=for code :lang +=begin code :lang for i in 1, 2: print(i) j = 1 @@ -288,6 +290,7 @@ while j < 3: j += 1 # 1, 2, 1, 2 +=end code Raku also has C loops and C loops: @@ -307,7 +310,7 @@ C leaves a loop in Raku, and is analogous to C in Python. C in Python is C in Raku. -Python +I =for code :lang for i in range(10): @@ -317,7 +320,7 @@ for i in range(10): break print(i) -Raku +I for ^10 -> $i { next if $i == 3; @@ -332,7 +335,7 @@ The C statement within a C loop in Python, which produces a C, is like a C/C construct in Raku. These both print 1, 2, 3. -I +I =begin code :lang def count(): @@ -343,7 +346,7 @@ for c in count(): print(c) =end code -I +I sub count { gather { @@ -362,7 +365,7 @@ iterating lists or dict/maps can both be achieved using the same L method in Raku (because the "key" of a list is its array-like numeric index): -I +I =begin code :lang elems = ["neutronium", "hydrogen", "helium", "lithium"] @@ -382,7 +385,7 @@ for symbol, elem in elem4Symbol.items(): # Symbol 'n' stands for neutronium =end code -I +I my @elems = ; for @elems.kv -> $i, $e { @@ -398,15 +401,21 @@ I say "Symbol '$symbol' stands for $element"; } -=head2 Lambdas, functions and subroutines> +=head2 Functions, subroutines and lambdas Declaring a function (subroutine) with C in Python is accomplished with C in Raku. +I + =begin code :lang def add(a, b): return a + b +=end code + +I +=begin code sub add(\a, \b) { return a + b } @@ -431,10 +440,10 @@ sub add($a, $b) { Python 2 functions can be called with positional arguments or keyword arguments. These are determined by the caller. In Python 3, some arguments may be "keyword only". -In Raku, positional and named arguments are determined -by the signature of the routine. +In Raku, each argument is either positional or named, +as determined by the signature of the routine. -Python +I =begin code :lang def speak(word, times): @@ -444,7 +453,7 @@ speak('hi', 2) speak(word='hi', times=2) =end code -Raku +I Positional parameters: @@ -486,12 +495,12 @@ Named parameters can be sent using a variety of formats: Creating an anonymous function can be done with C, with a block or with a pointy block. -Python +I =for code :lang square = lambda x: x ** 2 -Raku +I my $square = sub ($x) { $x ** 2 }; # anonymous sub my $square = -> $x { $x ** 2 }; # pointy block @@ -509,12 +518,12 @@ parameters. Thus these are the same: Postfix statement modifiers and blocks can be combined to easily create list comprehensions in Raku. -Python +I =for code :lang print([ i * 2 for i in [3, 9]]) # OUTPUT: «[6, 18]␤» -Raku +I say ( $_ * 2 for 3, 9 ); # OUTPUT: «(6 18)␤» say ( { $^i * 2 } for 3, 9 ); # OUTPUT: «(6 18)␤» @@ -553,14 +562,14 @@ L =for code :lang class Dog: def __init__(self, name): self.name = name -Raku: +I class Dog { has $.name; @@ -569,7 +578,7 @@ Raku: For each created class, Raku provides the constructor method C by default which takes named arguments. -Python: +I =for code :lang d = Dog('Fido') @@ -577,7 +586,7 @@ e = Dog('Buddy') print(d.name) print(e.name) -Raku +I =for code :preamble my $d = Dog.new(:name); # or: Dog.new(name => 'Fido') @@ -588,7 +597,7 @@ say $e.name; Class attributes in Raku can be declared in a few ways. One way is to just declare a lexical variable and a method for accessing it. -Python: +I =for code :lang class Dog: @@ -602,7 +611,7 @@ print(e.kind) print(d.name) print(e.name) -Raku: +I class Dog { my $kind = 'canine'; # class attribute @@ -620,7 +629,7 @@ Raku: In order to mutate attributes in Raku, you must use the C trait on the attributes: -Python: +I =for code :lang class Dog: @@ -629,7 +638,7 @@ class Dog: d = Dog() d.name = 'rover' -Raku: +I class Dog { has $.name is rw; @@ -653,7 +662,7 @@ d = Dog() d.jump() =end code -Raku +I class Animal { method jump { @@ -677,7 +686,7 @@ Python class Dog(Animal, Friend, Pet): pass -Raku +I class Animal {}; class Friend {}; class Pet {}; ...; @@ -714,7 +723,7 @@ def world(): world(); =end code -Raku +I sub world { say 'world' @@ -806,7 +815,7 @@ in the C variable. This is similar to L in R Python tuples are immutable sequences. The sequence elements do not need to be of the same types. -Python +I =for code :lang tuple1 = (1, "two", 3, "hat") @@ -815,7 +824,7 @@ print(tuple1[1]) # OUTPUT: «two␤» tuple3 = tuple1 + tuple2 print(tuple3) # OUTPUT: «(1, 'two', 3, 'hat', 5, 6, 'seven')␤» -Raku +I Raku does not have a builtin Tuple type. You can get the same behavior from Raku using the List type, or from an external module. @@ -827,4 +836,23 @@ Raku using the List type, or from an external module. my $list4 = (|$list1, |$list2); # equivalent to previous line say $list3; # OUTPUT: «(1, two, 3, hat, 5, 6, seven)␤» +=head1 Ecosystem + +Where Python has pip, anaconda or uv, Raku has zef as its preferred tool for module management. + +Where Python has PyPi, Raku has L +as the central index of third-party L. +There are modules from three different ecosystems – +please stick to the I ecosystem. +(The others, I and I, are deprecated when it comes to Raku.) +See L for additional information, +including guidance on how to publish your own modules. + +=head2 Web development + +The Raku module L serves a similar job as Python's L. + +The Raku module L is an object–relational mapper, similar to Django's ORM capabilities or to SQLAlchemy. + =end pod +