Hello Learners…
Welcome to the blog…
Table Of Contents
- Introduction
- Mojo Programming: Mojo Basics – 2
- Mojo Programming
- 1.2 Mojo Basic
- Summary
- References
Introduction
In this post, we are going to start with Mojo Programming Basics and try to implement and understand the concepts.
Mojo Programming: Mojo Basics – 2
More importantly, Mojo allows you to leverage the entire Python ecosystem so you can continue to use tools you are familiar with. Mojo is designed to become a superset of Python over time by preserving Pythonโs dynamic features while adding new primitives for systems programming
1.1 Mojo Installation And Setup
1.2 Mojo Basics
First, we have to check whether the mojo is already installed or not.
To check run the below command.
mojo --version
Hello World In Mojo
With the Mojo SDK, you can run a Mojo program from a terminal just like you can with Python. So if you have a file named hello.mojo
(or hello.๐ฅ
โyes, the file extension can be an emoji!), just type mojo hello.mojo
:
var a="hello mojo"
fn main():
print(a)
To run the code type the below command.
mojo hello.mojo
We can’t use the print() function at the file level which means we have to use it in function only when we write code into a .mojo file.
Variable declarations In Mojo With let
and var
Keywords
In Mojo, when you define a function, you can assign a value to a name, which creates a variable within the function’s scope, similar to Python. This approach is straightforward and flexible, but it presents two challenges:
- System programmers often prefer to declare that a value is immutable for the sake of type safety and performance.
- They may want to receive an error if they accidentally mistype a variable name during an assignment.
To address these concerns, Mojo introduces scoped runtime value declarations: “let” for immutable values and “var” for mutable values. These declarations utilize lexical scoping and allow for name shadowing
To define variables in Mojo we have to use one of these two keywords. otherwise, it returns an error.
Define Functions In Mojo
In mojo, we can use “fn” and “def” keywords to create functions.
var a = 10
fn main():
print("in main")
print(a)
main3()
fn main3():
print("in main3")
print(a)
And run the file.
But when we create a function using the “def” keyword and try to run that at that time we get an error.
def your_function(a, b):
let c = a
# Uncomment to see an error:
# c = b # error: c is immutable
if c != b:
let d = b
print(d)
your_function(2,3)
Summary
This is the basic introduction to Mojo programming and with time we go deep into this and explore the real capabilities of mojo programming language.
Happy learning And Keep Learning…
Thank you…