Python 🆚 Algorand Python ⚔️

Python 🆚 Algorand Python ⚔️

Python is the most popular programming language used by developers worldwide. Its simplicity and developer-friendly environment always attract programmers to write their code in Python. Imagine writing smart contracts, or code on the blockchain, using Python! There's no need to learn a new programming language like Solidity or complex ones like Rust.

Algorand, a leading LAYER-1 blockchain, allows you to write your smart contract code in Python. Let's explore how you can use and understand writing smart contracts on Algorand with Python.

Why Algorand Uses Python 🐍 ?

According to StackOverflow’s annual survey for 2024, Python is the third most used programming language by developers worldwide. Python is naturally easy to understand and allows developers to build logic and complex code without worrying too much about syntax.

In the current blockchain space, developers with a Web2 background often need to learn a new programming language to build their applications on the blockchain. This makes onboarding developers a bit challenging.

Algorand is addressing this issue by allowing developers to build smart contracts on Algorand using the language they already know with the highest security and speed. So you don’t need to worry about scalibilty and security. Python is the most suitable language for this purpose.

Algorand Python 📘 : BUILD YOUR DAPP WITH PYTHON

Now you know why Algorand uses Python for smart contract development. Although Python is used on Algorand for this purpose, there are some differences when writing smart contracts with Python on Algorand. This article will give you an overview of how to write your first smart contract and how it differs from the regular Python you are familiar with.

Algorand Python is the subset of Python 🐍 programming language, which means it only supports some specific features of Python programming as of now, it’s due to AVM ( Algorand Virtual Machine ) which runs your smart contract on the Algorand Blockchain.

Algokit 🛠️ : One-stop tool for the developers to build on Algorand

Before learning the differences in Algorand Python, it's important to understand how to run your Python smart contract on the Algorand Blockchain. Algokit is the tool provided by Algorand to simplify your setup for building applications on Algorand. It includes various utilities and templates to kickstart your development.

To run and deploy your smart contract on Algorand, you need to install Algokit. I'll provide a link to the installation guide where you can download it. Algokit is a large topic that we will cover in a separate blog. For this tutorial, you can use or review the smart contract template provided in Algokit with a simple hello_world contract.

→ For Installation

Algkit Installation Guide

In summary, you can follow this guide to install Algokit. You require

  • VS Code

  • Python 3.12 + version

  • Docker

  • Algokit

Docker is used for running local Algorand Blockchain for your testing purposes.

algokit localnet start

This command will start your local environment of Algorand Blockchain.

algokit init

Then run this command and follow the steps. You can choose "1. Smart contract" and then select Python as your smart contract language. Use a starter template to generate an Algorand Python project where you can modify the smart contract file. It will provide you with a default smart contract called hello_world. We will explore how it differs from regular Python.

How does your Algorand Python code work on AVM ⚙️ ?

As we know, Python uses an interpreter to convert high-level code into machine code, processing it line by line. This is different from a compiler, which translates the entire source code into machine code before the program runs and creates an executable file that the computer can execute.

However, in Algorand, Python designed for the AVM uses a compiler to generate AVM bytecode, which the Algorand Virtual Machine can run. This means, unlike regular Python where you get errors at runtime, you can catch errors at compile time. Let's understand this with a simple flow diagram.

You can see how different is Algorand python in execution. While Python is a dynamically interpreted language where you can catch errors at run time, Algorand Python is a statically typed compiled language where you’re able to catch errors at compile time.

Now you might have a question why we made Algorand python statically typed compiled language because our smart contract is going to run on Algorand virtual machine which is a statically typed stack-based virtual machine that requires whole executable bytecode at once.

Key Differences between Python Vs Algorand Python ⚖️

Algorand Python is a subset of Python, so it only supports certain features and doesn't have the full capabilities of regular Python. However, it's easy to use because the coding style feels like writing normal Python code, as it fully follows the syntax and semantics of standard Python.

PythonAlgorand Python
Dynamically Interpreted Language.Statically typed compiled language.
You can run the code using Python IDE.You need Algokit setup to deploy the written Python smart contract on Algorand Blockchain / Algorand Virtual Machine.
Python is a syntax semanticAlgorand Python also has syntax semantic capability just like Python
You’re able to catch errors at run-timeYou can catch errors at compile-time
Uses Python InterpreterUses PUYA Compiler
It supports multiple data types ( Int, float, string, unit, etc )Algorand Python supports specific data types due to AVM supports only UInt64 & byte[] array so there are limited data types in Algorand Python.
data type is limited only by the memory.There is a maximum length/size of memory you can use in Algorand Python.
Module-level values can be variableModule-level values must be constant, not variable!
You can import any in-built/ other Python librariesYou can’t use other Python libraries inside of Algorand Python. It can only include - Algopy Contract, Contract methods, Subroutines, and Constant Variables.
It supports all functions and syntax.It does not support - raise, try/except, finally, with, async, nested functions, lambda functions, and global keywords.

Understanding your first Algorand Python Smart Contract 🔄

In Algorand Python, you can define a smart contract using a Python class. A Python class represents an Algorand Smart Contract. Each Algorand Smart Contract consists of an Approval program and a Clear program. To make it easier for you to write smart contracts and focus on your code, Algorand introduces Algorand Requests for Comments (ARC standards). It’s recommended to use the ARC4 standard to write your smart contract. Let’s see what our very first smart contract looks like.

from algopy import ARC4Contract, String
from algopy.arc4 import abimethod


class HelloWorld(ARC4Contract):
    @abimethod()
    def hello(self, name: String) -> String:
        return "Hello, " + name
  • Here, we have imported the ARC4Contract standard and the String variable from the algopy library. ARC4Contract gives us a standard template, allowing us to focus on writing our smart contract without worrying about the approval and clear programs.

  • We have defined the class HelloWorld, which represents our smart contract. We can define multiple classes in one file, and each acts as a separate smart contract.

  • The @abimethod() decorator makes our function callable from external sources, like the frontend. It helps the AVM understand and generate an ABI or app client file for interacting with the function.

  • In Algorand Python, we must strictly define the argument type and return type in functions. Even if there is no return, you still need to specify none.

Hurray! Now you understand how to write a basic smart contract on the Algorand blockchain using Python. There are more concepts you can explore to develop complex smart contracts and build your decentralized applications on Algorand, whether it's NFTs, DeFi, or anything else. You can do it on Algorand using Python. Oops, we forgot to deploy our contract...

In your project environment created by Algokit, you just need to run two commands:

algokit project run build

algokit project deploy localnet

You have successfully deployed your first smart contract written in Python on the Algorand blockchain on your localnet!

In future articles, we will explore data types, method references (like @abimethod(), @subroutines(), etc.), transactions, app-account structure, and more complex contracts. In the meantime, you can start following the Algorand resources mentioned in this article.

#algorandcan

Resources 📚