How do programming languages good for AI look like?
Tags: programming, python, cpp, machinelearning
Published: 2021-08-14 20:26:09

## First - what do I mean by "good for AI"? 

Let's begin by stating the obvious - terms like **Artificial Intelligence** and **Machine Learning** are here to stay, and investing your time to learn more about it seems like a solid, safe choice. 

**Machine Learning** is used by giants like Google and by small companies alike. It is the perfect technology for solutions like speech and image recognition, and it's a perfect fit for working with [Big Data](https://pl.wikipedia.org/wiki/Big_data). 

**Artificial Intelligence**, on the other hand, allows us to take a step into the future with AI-based solutions straight from movies, like e.g. [Deep-Image](https://deep-image.ai/), which puts all the CSI memes to shame, by actually upscaling and enhancing images. 

![Zoom in, enhance](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5aoc8xjlr9bfmezk3qp.png)

Languages that are considered to be the best for playing around with Artificial Intelligence and Machine Learning include C++, Python, Java, and a few niche (or new) languages. 

## Secondly - what do I mean by "look like"?

There are tons of articles telling you which programming language to choose. Authors of these texts tell you in detail why you should pick this and this programming language, when it is wise to go with Python, when it is wise to go with Java, etc. 

They focus on your needs and capabilities of each language, and obviously it's a very good strategy to take that into consideration :) **However**, I would like to point out something less critical, but also important - the code itself, as in - its looks. 

I believe you should know how the syntax looks like, how easy it is to take a glance at code and understand it, and how hard it can be to create it. 

In the case of human-to-human relationships, it may be a bad idea to pick a life partner based solely on the looks, but it's hard to say that visual attractiveness doesn't matter. It is very similar in case of programming languages. 

After all - you are going to spend a lot of time actually creating it, editing, analyzing, gazing with confusion, and many others. If you do not want to be stressed and annoyed every day, you should have some kind of satisfaction, just by looking at the code.  

## How does Python look like?

Python deserves to be at the top of the list, as far as aesthetics are concerned. It's easy to learn, it's pleasant to read, it's popular, and it's a powerful language for machine learning.

Take a look at this sample code for a Magic 8-ball from [Python For Beginners](https://www.pythonforbeginners.com/code-snippets-source-code/magic-8-ball-written-in-python):

```
# Import the modules
import sys
import random

ans = True

while ans:
    question = raw_input("Ask the magic 8 ball a question: (press enter to quit) ")
    
    answers = random.randint(1,8)
    
    if question == "":
        sys.exit()
    
    elif answers == 1:
        print "It is certain"
    
    elif answers == 2:
        print "Outlook good"
    
    elif answers == 3:
        print "You may rely on it"
    
    elif answers == 4:
        print "Ask again later"
    
    elif answers == 5:
        print "Concentrate and ask again"
    
    elif answers == 6:
        print "Reply hazy, try again"
    
    elif answers == 7:
        print "My reply is no"
    
    elif answers == 8:
        print "My sources say no"
```

## How does Java look like?

The core strength of Java is that it's available for a number of different platforms, it's popular, and it's powerful for creating mobile apps. 

Below, you can find a simple source code of a word-count tool. 

```
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class SimpleWordCounter {
    
    public static void main(String[] args) {
        try {
            File f = new File("ciaFactBook2008.txt");
            Scanner sc;
            sc = new Scanner(f);
            // sc.useDelimiter("[^a-zA-Z']+");
            Map<String, Integer> wordCount = new TreeMap<String, Integer>();
            while(sc.hasNext()) {
                String word = sc.next();
                if(!wordCount.containsKey(word))
                    wordCount.put(word, 1);
                else
                    wordCount.put(word, wordCount.get(word) + 1);
            }
            
            // show results
            for(String word : wordCount.keySet())
                System.out.println(word + " " + wordCount.get(word));
            System.out.println(wordCount.size());
        }
        catch(IOException e) {
            System.out.println("Unable to read from file.");
        }
    }
}
```

## How does C++ look like?

Super popular language, with possibilities as big as its popularity. You can take a look at many samples on [Programiz](https://www.programiz.com/cpp-programming/examples), with Fibonacci Series up to n number of terms (shown below) being one of them:

```
#include <iostream>
using namespace std;

int main() {
    int n, t1 = 0, t2 = 1, nextTerm = 0;

    cout << "Enter the number of terms: ";
    cin >> n;

    cout << "Fibonacci Series: ";

    for (int i = 1; i <= n; ++i) {
        // Prints the first two terms.
        if(i == 1) {
            cout << t1 << ", ";
            continue;
        }
        if(i == 2) {
            cout << t2 << ", ";
            continue;
        }
        nextTerm = t1 + t2;
        t1 = t2;
        t2 = nextTerm;
        
        cout << nextTerm << ", ";
    }
    return 0;
}
```

# How does R language look like? 

R may not be the most popular language, but it is suitable for both Machine Learning and Artificial Intelligence. 

You can find some cool examples on [Datamentor](https://www.datamentor.io/r-programming/examples/), below you can check a simple calculator:

```
# Program make a simple calculator that can add, subtract, multiply and divide using functions
add <- function(x, y) {
return(x + y)
}
subtract <- function(x, y) {
return(x - y)
}
multiply <- function(x, y) {
return(x * y)
}
divide <- function(x, y) {
return(x / y)
}
# take input from the user
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
choice = as.integer(readline(prompt="Enter choice[1/2/3/4]: "))
num1 = as.integer(readline(prompt="Enter first number: "))
num2 = as.integer(readline(prompt="Enter second number: "))
operator <- switch(choice,"+","-","*","/")
result <- switch(choice, add(num1, num2), subtract(num1, num2), multiply(num1, num2), divide(num1, num2))
print(paste(num1, operator, num2, "=", result))
```

## Any suggestions? 

Do you think there are some other examples of languages that deserve to be mentioned on this list? Leave a comment below, and I will add it to this post :)