Skip to content

Commit

Permalink
Added links to API references and fixed whitespace in code samples
Browse files Browse the repository at this point in the history
MODULAR_ORIG_COMMIT_REV_ID: 08f97ad1651fd8940c9d1e8bc6681ffb1460e007
  • Loading branch information
KenJones-Modular authored and modularbot committed Jan 14, 2025
1 parent df5423c commit c4261ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
10 changes: 6 additions & 4 deletions docs/manual/get-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def main():
## 5. Create and use a function to print the grid

Now let's create a function to generate a string representation of the game grid
that we can print it to the terminal.
that we can print to the terminal.

There are actually two different keywords that we can use to define functions in
Mojo: `def` and `fn`. Using `fn` gives us finer level control over the function
Expand All @@ -362,7 +362,7 @@ def grid_str(rows: Int, cols: Int, grid: List[List[Int]]) -> String:
# Iterate through rows 0 through rows-1
for row in range(rows):
# Iterate through columns 0 through cols-1
# Iterate through columns 0 through cols-1
for col in range(cols):
if grid[row][col] == 1:
str += "*" # If cell is populated, append an asterisk
Expand Down Expand Up @@ -540,7 +540,7 @@ def grid_str(grid: Grid) -> String:
# Iterate through rows 0 through rows-1
for row in range(grid.rows):
# Iterate through columns 0 through cols-1
# Iterate through columns 0 through cols-1
for col in range(grid.cols):
if grid.data[row][col] == 1:
str += "*" # If cell is populated, append an asterisk
Expand Down Expand Up @@ -876,7 +876,9 @@ respectively. This function actually returns a value of type `Int64`, which is a
signed 64-bit integer value. As described in [Numeric
types](/mojo/manual/types#numeric-types), this is *not* the same as the `Int`
type whose precision is dependent on the native word size of the system.
Therefore we're passing this value to the built-in
Therefore we're passing this value to the
[`Int()`](/mojo/stdlib/builtin/int/Int/#__init__) constructor, which explicitly
converts a numeric value to an `Int`.

The return type of the method is `Self`, which is an alias for the type of the
struct. This is a convenient shortcut if the actual name of the struct is long
Expand Down
30 changes: 16 additions & 14 deletions docs/manual/python/types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,26 @@ Some Mojo APIs handle `PythonObject` just fine, but sometimes you'll need to
explicitly convert a Python value into a native Mojo value.

Currently `PythonObject` conforms to the
[`Intable`](/mojo/stdlib/builtin/int/Intable),
[`Stringable`](/mojo/stdlib/builtin/str/Stringable), and
[`Boolable`](/mojo/stdlib/builtin/bool/Boolable) traits, which means you can
convert Python values to Mojo `String`, and `Bool` types using the
[`str()`](/mojo/stdlib/builtin/str/str) and
[`bool()`](/mojo/stdlib/builtin/bool/bool-function) functions, construct an
`Int` using `Int(python_object)`, and print Python values using the built-in
[`print()`](/mojo/stdlib/builtin/io/print) function.

`PythonObject` also provides the
[`to_float64()`](/mojo/stdlib/python/python_object/PythonObject#to_float64) for
converting to a Mojo floating point value.
[`Stringable`](/mojo/stdlib/builtin/str/Stringable),
[`Boolable`](/mojo/stdlib/builtin/bool/Boolable),
[`Floatable`](/mojo/stdlib/builtin/floatable/Floatable/), and
[`Intable`](/mojo/stdlib/builtin/int/Intable) traits, which means you can
convert Python values to Mojo `String`, `Bool`, and `Float64` types using the
[`str()`](/mojo/stdlib/builtin/str/str),
[`bool()`](/mojo/stdlib/builtin/bool/bool-function) and
[`float()`](/mojo/stdlib/builtin/floatable/float/) built-in functions, and
construct an `Int` using the [`Int()`](/mojo/stdlib/builtin/int/Int/#__init__)
constructor. `PythonObject` also conforms to the
[`Writable`](/mojo/stdlib/utils/write/Writable) trait so that you can print
Python values using the built-in [`print()`](/mojo/stdlib/builtin/io/print)
function.

```mojo
var i: Int = Int(py_int)
var s: String = str(py_string)
var b: Bool = bool(py_bool)
var f: Float64 = py_float.to_float64()
var f: Float64 = float(py_float)
var i: Int = Int(py_int)
print(py_obj)
```

### Comparing Python types in Mojo
Expand Down

0 comments on commit c4261ca

Please sign in to comment.